Skip to content

Commit

Permalink
make max_resid a class variable that gets updated automagically; upda…
Browse files Browse the repository at this point in the history
…te DNA convention
  • Loading branch information
fgrunewald committed Apr 5, 2022
1 parent b12036f commit cecc940
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 11 deletions.
4 changes: 2 additions & 2 deletions polyply/src/gen_dna.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ def complement_dsDNA(meta_molecule):
"""
last_node = list(meta_molecule.nodes)[-1]
resname = BASE_LIBRARY[meta_molecule.nodes[last_node]["resname"]]
meta_molecule.add_monomer(last_node+1, resname, [], last_node+2)
meta_molecule.add_monomer(last_node+1, resname, [])

correspondance = {last_node: last_node+1}
total = last_node+1
Expand All @@ -91,7 +91,7 @@ def complement_dsDNA(meta_molecule):

if next_node not in correspondance:
new_node = total + 1
meta_molecule.add_monomer(total+1, resname, [(correspondance[prev_node], total+1)], resid=total+2)
meta_molecule.add_monomer(total+1, resname, [(correspondance[prev_node], total+1)])
correspondance[next_node] = new_node
else:
new_node = correspondance[next_node]
Expand Down
19 changes: 10 additions & 9 deletions polyply/src/meta_molecule.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ def __init__(self, *args, **kwargs):
self.__search_tree = None
self.root = None
self.dfs = False
self.max_resid = 0

# add resids to polyply meta-molecule nodes if they are not
# present. All algorithms rely on proper resids
Expand All @@ -116,6 +117,13 @@ def __init__(self, *args, **kwargs):
msg = "Couldn't add 1 to node. Either provide resids or use integers as node keys."
raise IOError(msg)

if self.max_resid < self.nodes[node]["resid"]:
self.max_resid = self.nodes[node]["resid"]

def add_node(self, *args, **kwargs):
self.max_resid = self.max_resid + 1
kwargs["resid"] = self.max_resid
super().add_node(*args, **kwargs)

def add_monomer(self, current, resname, connections):
"""
Expand All @@ -124,14 +132,7 @@ def add_monomer(self, current, resname, connections):
that matches may only refer to already existing nodes.
But connections can be an empty list.
"""
resids = nx.get_node_attributes(self, "resid")

if resids:
resid = max(resids.values()) + 1
else:
resid = 1

self.add_node(current, resname=resname, resid=resid, build=True)
self.add_node(current, resname=resname, build=True)
for edge in connections:
if self.has_node(edge[0]) and self.has_node(edge[1]):
self.add_edge(edge[0], edge[1])
Expand All @@ -154,7 +155,7 @@ def relabel_and_redo_res_graph(self, mapping):
mapping of node-key to new residue name
"""
# find the maximum resiude id
max_resid = max(nx.get_node_attributes(self.molecule, "resid").values())
max_resid = self.max_resid
# resname the residues and increase with pseudo-resid
for node, resname in mapping.items():
self.molecule.nodes[node]["resname"] = resname
Expand Down
3 changes: 3 additions & 0 deletions polyply/tests/test_gen_params.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,9 @@ def test_gen_params(args_in, ref_file):
ff_group.add_argument('-list-links', action='store_true', dest='list_links',
help='List all Links known to the'
' force field, and exit.')
dna_group = parser.add_argument_group('DNA specifc options')
dna_group.add_argument('-dsdna', dest='dsdna', action='store_true',
help='complement single sequence to dsDNA sequence')

args = parser.parse_args(args_in)
gen_params(args)
Expand Down

0 comments on commit cecc940

Please sign in to comment.