-
Notifications
You must be signed in to change notification settings - Fork 25
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Cgsmiles 1 #377
Open
fgrunewald
wants to merge
3
commits into
master
Choose a base branch
from
cgsmiles_1
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+110
−32
Open
Cgsmiles 1 #377
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,13 +13,14 @@ | |
# limitations under the License. | ||
from collections import (namedtuple, OrderedDict) | ||
import networkx as nx | ||
from cgsmiles.resolve import MoleculeResolver | ||
from cgsmiles.read_cgsmiles import read_cgsmiles | ||
from vermouth.graph_utils import make_residue_graph | ||
from vermouth.log_helpers import StyleAdapter, get_logger | ||
from vermouth.gmx.itp_read import read_itp | ||
from .graph_utils import find_nodes_with_attributes | ||
from .simple_seq_parsers import parse_txt, parse_ig, parse_fasta, parse_json | ||
from .simple_seq_parsers import parse_txt, parse_ig, parse_fasta, parse_json, Monomer | ||
|
||
Monomer = namedtuple('Monomer', 'resname, n_blocks') | ||
LOGGER = StyleAdapter(get_logger(__name__)) | ||
|
||
def _make_edges(force_field): | ||
|
@@ -360,3 +361,71 @@ def from_block(cls, force_field, mol_name): | |
meta_mol = cls(graph, force_field=force_field, mol_name=mol_name) | ||
meta_mol.molecule = force_field.blocks[mol_name].to_molecule() | ||
return meta_mol | ||
|
||
@classmethod | ||
def from_cgsmiles_str(cls,force_field, cgsmiles_str, mol_name, seq_only=True, all_atom=False): | ||
""" | ||
Constructs a :class::`MetaMolecule` from an CGSmiles string. | ||
The force-field must contain the block with mol_name from | ||
which to create the MetaMolecule. This function automatically | ||
sets the MetaMolecule.molecule attribute. | ||
|
||
Parameters | ||
---------- | ||
force_field: :class:`vermouth.forcefield.ForceField` | ||
the force-field that must contain the block | ||
cgsmiles_str: | ||
the CGSmiles string describing the molecule graph | ||
mol_name: str | ||
name of the block matching a key in ForceField.blocks | ||
seq_only: bool | ||
if the string only describes the sequence; if this is False | ||
then the molecule attribute is set | ||
all_atom: bool | ||
if the last molecule in the sequence is at all-atom resolution | ||
can only be used if seq_only is False | ||
|
||
Returns | ||
------- | ||
:class:`polyply.MetaMolecule` | ||
""" | ||
if seq_only and all_atom: | ||
msg = "You cannot define a sequence at all-atom level.\n" | ||
raise IOError(msg) | ||
|
||
# check if we have multiple resolutions | ||
if cgsmiles_str.count('{') == 1: | ||
meta_graph = read_cgsmiles(cgsmiles_str) | ||
take_resname_from = 'fragname' | ||
elif seq_only: | ||
# initalize the cgsmiles molecule resolver | ||
resolver = MoleculeResolver(cgsmiles_str, last_all_atom=all_atom) | ||
# grep the last graph of the resolve iter | ||
*_, (_, meta_graph) = resolver.resolve_iter() | ||
take_resname_from = 'atomname' | ||
else: | ||
# initalize the cgsmiles molecule resolver | ||
resolver = MoleculeResolver(cgsmiles_str, last_all_atom=all_atom) | ||
*_, (meta_graph, molecule) = resolver.resolve_iter() | ||
|
||
# we have to set some node attribute accoding to polyply specs | ||
for node in meta_graph.nodes: | ||
if seq_only: | ||
resname = meta_graph.nodes[node][take_resname_from] | ||
meta_graph.nodes[node]['resname'] = resname | ||
else: | ||
for atom in meta_graph.nodes['graph'].nodes: | ||
meta_graph.nodes['graph'].nodes[atom]['resname'] = resname | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. resname is undefined here |
||
meta_graph.nodes['graph'].nodes[atom]['resid'] = node + 1 | ||
molecule.nodes[atom]['resname'] = resname | ||
molecule.nodes[atom]['resid'] = node + 1 | ||
|
||
if 'atomname' in meta_graph.nodes[node]: | ||
del meta_graph.nodes[node]['atomname'] | ||
meta_graph.nodes[node]['resid'] = node + 1 | ||
|
||
meta_mol = cls(meta_graph, force_field=force_field, mol_name=mol_name) | ||
if not seq_only: | ||
meta_mol.molecule = molecule | ||
|
||
return meta_mol |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
@@ -11,14 +11,16 @@ | |||||||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||||||||||
# See the License for the specific language governing permissions and | ||||||||||
# limitations under the License. | ||||||||||
from collections import OrderedDict | ||||||||||
from collections import (namedtuple, OrderedDict) | ||||||||||
from functools import partial | ||||||||||
import json | ||||||||||
import networkx as nx | ||||||||||
from networkx.readwrite import json_graph | ||||||||||
from vermouth.parser_utils import split_comments | ||||||||||
from vermouth.log_helpers import StyleAdapter, get_logger | ||||||||||
|
||||||||||
Monomer = namedtuple('Monomer', 'resname, n_blocks') | ||||||||||
|
||||||||||
LOGGER = StyleAdapter(get_logger(__name__)) | ||||||||||
|
||||||||||
ONE_LETTER_DNA = {"A": "DA", | ||||||||||
|
@@ -342,3 +344,26 @@ def parse_json(filepath): | |||||||||
seq_graph.add_nodes_from(nodes) | ||||||||||
seq_graph.add_edges_from(init_json_graph.edges(data=True)) | ||||||||||
return seq_graph | ||||||||||
|
||||||||||
def parse_simple_seq_string(sequence): | ||||||||||
""" | ||||||||||
Split a string definition for a linear sequence into monomer | ||||||||||
blocks and raise errors if the sequence is not valid. | ||||||||||
|
||||||||||
Parameters | ||||||||||
----------- | ||||||||||
sequence: str | ||||||||||
string of residues format name:number | ||||||||||
Comment on lines
+355
to
+356
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||
|
||||||||||
Returns: | ||||||||||
---------- | ||||||||||
list | ||||||||||
list of `polyply.Monomers` | ||||||||||
""" | ||||||||||
raw_monomers = sequence | ||||||||||
monomers = [] | ||||||||||
for monomer in raw_monomers: | ||||||||||
resname, n_blocks = monomer.split(":") | ||||||||||
n_blocks = int(n_blocks) | ||||||||||
monomers.append(Monomer(resname=resname, n_blocks=n_blocks)) | ||||||||||
return monomers |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.