Skip to content

Commit 3127a20

Browse files
author
Oliver Scott
committed
Add option to return data when retrieving singleton/virtual scaffolds.
1 parent a4e400c commit 3127a20

1 file changed

Lines changed: 31 additions & 7 deletions

File tree

scaffoldgraph/analysis/general.py

Lines changed: 31 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55
"""
66

77

8-
def get_virtual_scaffolds(scaffoldgraph):
9-
"""Get 'virtual' scaffolds in within a scaffold graph.
8+
def get_virtual_scaffolds(scaffoldgraph, data=False, default=None):
9+
"""Get 'virtual' scaffolds within a scaffold graph.
1010
1111
Virtual scaffolds represent scaffolds that are not directly obtained from
1212
any molecule of the collection, but generated by the pruning process.
@@ -17,6 +17,13 @@ def get_virtual_scaffolds(scaffoldgraph):
1717
----------
1818
scaffoldgraph : ScaffoldGraph
1919
A ScaffoldGraph object to query
20+
data : str, bool, optional
21+
The scaffold node attribute returned in 2-tuple (n, ddict[data]).
22+
If True, return entire node attribute dict as (n, ddict).
23+
If False, return just the nodes n. The default is False.
24+
default : value, bool, optional
25+
Value used for nodes that don't have the requested attribute.
26+
Only relevant if data is not True or False.
2027
2128
Returns
2229
-------
@@ -25,17 +32,22 @@ def get_virtual_scaffolds(scaffoldgraph):
2532
2633
"""
2734
virtual = []
28-
for scaffold in scaffoldgraph.get_scaffold_nodes():
35+
for scaffold, d in scaffoldgraph.get_scaffold_nodes(True):
2936
mol_count = 0
3037
for succ in scaffoldgraph.successors(scaffold):
3138
if scaffoldgraph.nodes[succ].get('type') == 'molecule':
3239
mol_count += 1
3340
if mol_count == 0:
34-
virtual.append(scaffold)
41+
if data is False:
42+
virtual.append(scaffold)
43+
elif data is True:
44+
virtual.append((scaffold, d))
45+
else:
46+
virtual.append((scaffold, d.get(data, default)))
3547
return virtual
3648

3749

38-
def get_singleton_scaffolds(scaffoldgraph):
50+
def get_singleton_scaffolds(scaffoldgraph, data=False, default=None):
3951
"""Get singleton scaffolds within a scaffold graph.
4052
4153
Singleton scaffolds represent scaffolds that are direct members of only
@@ -45,6 +57,13 @@ def get_singleton_scaffolds(scaffoldgraph):
4557
----------
4658
scaffoldgraph : ScaffoldGraph
4759
A ScaffoldGraph object to query
60+
data : str, bool, optional
61+
The scaffold node attribute returned in 2-tuple (n, ddict[data]).
62+
If True, return entire node attribute dict as (n, ddict).
63+
If False, return just the nodes n. The default is False.
64+
default : value, bool, optional
65+
Value used for nodes that don't have the requested attribute.
66+
Only relevant if data is not True or False.
4867
4968
Returns
5069
-------
@@ -53,11 +72,16 @@ def get_singleton_scaffolds(scaffoldgraph):
5372
5473
"""
5574
singletons = []
56-
for scaffold in scaffoldgraph.get_scaffold_nodes():
75+
for scaffold, d in scaffoldgraph.get_scaffold_nodes(True):
5776
mol_count = 0
5877
for succ in scaffoldgraph.successors(scaffold):
5978
if scaffoldgraph.nodes[succ].get('type') == 'molecule':
6079
mol_count += 1
6180
if mol_count == 1:
62-
singletons.append(scaffold)
81+
if data is False:
82+
singletons.append(scaffold)
83+
elif data is True:
84+
singletons.append((scaffold, d))
85+
else:
86+
singletons.append((scaffold, d.get(data, default)))
6387
return singletons

0 commit comments

Comments
 (0)