Commit e374167e authored by Roman Sarrazin-Gendron's avatar Roman Sarrazin-Gendron
Browse files

removed submodule

parent ffa140f3
scanning @ 510ea632
Subproject commit 510ea632270109d5941f1e7c54b68c7fb8225b2b
This diff is collapsed.
import pickle
from src.classes import SSE, ScanResult
from anytree.iterators.preorderiter import PreOrderIter
## import exact module database, a dictionary, in which the key is class representation and value is the module indices list
MODULES = pickle.load(open("modules.db","rb"))
def naive_scanning(tree, module):
"""
For a given structure and a module, return the list of SSEs that matches with the module in the terms of structure.
"""
scan = lambda t: SSE.is_same_form(t, SSE.from_bracket(module))
return PreOrderIter(tree.root, filter_=scan)
def exact_matching(tree):
"""
For a given RNA secondary structure in tree presentation, add exact matching module list to each node
"""
for node in PreOrderIter(tree.root):
node.modules = []
ind = node.get_strands_len()
for i in range(len(ind)):
node.modules += MODULES.get(ind[i:]+ind[:i],[])
def yield_matching(tree):
for node in PreOrderIter(tree.root):
for m in node.modules:
yield ScanResult.from_exact_matching(node,m)
if __name__ == "__main__":
s = "(((.((...))((...)))))"
m = "(.((*))((*)))"
for node in naive_scanning(s,m):
print(node.name, node.closing_bp)
from anytree import RenderTree
from .SSE import SSENode
from ..utils.structure import bracket_to_index
class SSETree:
def __init__(self):
self.root = None
@classmethod
def from_bracket(cls, structure, seq=""):
tree = cls()
tree.structure = structure
tree.seq = seq
index = bracket_to_index(structure)
seq = "#"+seq+"#"
def aux(ind, parent=None):
tmp = []
k = ind+1
j = ind
res = []
w = []
while True:
if index[k] == -1:
k += 1
elif index[k] > k:
tmp.append((k,index[k]))
if seq:
w.append(seq[j:k+1])
res.append(aux(k))
j = index[k]
k = index[k] + 1
else:
if seq:
w.append(seq[j:k+1])
break
node = SSENode([(ind,index[ind])]+tmp, root=(not ind), seq="&".join(w))
for s in res:
s.parent = node
return node
tree.root = aux(0)
return tree
def print_tree(self):
"""
A simple tree printer
"""
for pre,_,node in RenderTree(self.root):
treestr = u"%s%s" % (pre,node.label)
print(treestr.ljust(0),node.get_strands_len())
def get_sequence(self):
if tree.seq:
return tree.seq
else:
raise Exception("No assigned sequence")
if __name__ == "__main__":
s = "(((.((...))((...)))))"
tree = SSETree.from_bracket(s)
tree.print_tree()
from collections import namedtuple
from anytree import NodeMixin
from ..utils.structure import decomposition
BasePair = namedtuple('BasePair', ('fst','snd'))
class SSE:
"""
A class for basic components of RNA secondary structure, including Stacking Pair, Hairpin Loop,
Internal Loop and Multiple Loop
"""
def __init__(self, lst, root=False, seq=""):
bp_list = list(map(lambda t: BasePair(*t), lst))
nb_bp = len(bp_list)
self.closing_bp = bp_list[0]
self.enclosed_bp = bp_list[1:]
self.get_strands_len()
self.seq=seq
# Hairpin Loop
if nb_bp == 1:
self.name = 'Hairpin Loop'
self.label = 'H'
# Stacking Pair and Internal Loop
elif nb_bp == 2:
bp_1 = self.closing_bp
bp_2 = self.enclosed_bp[0]
if (abs(bp_1.fst-bp_2.fst) == 1) and (abs(bp_1.snd-bp_2.snd) == 1):
self.name = 'Stacking Pair'
self.label = 'S'
else:
self.name = 'Internal Loop'
self.label = 'I'
# Multiple Loop
elif nb_bp > 2:
self.name = 'Multiple Loop'
self.label = 'M'
# The base is the root of tree-presented structure
if root:
self.name = 'Root'
self.label = 'R'
@classmethod
def from_bracket(cls, structure):
index = decomposition(structure)
return cls(index[1])
def get_strands_len(self):
"""
Return the length for each strand in the secondary structure element (SSE).
For example, two strands for the internal loop.
"""
try:
return self.strands_len
except:
tmp = BasePair(self.closing_bp.snd,self.closing_bp.fst)
l = [tmp]+self.enclosed_bp+[tmp]
res = []
for i in range(len(l)-1):
res.append(l[i+1].fst-l[i].snd+1)
t = tuple(res)
self.strands_len = t
return t
def get_sequence(self):
try:
return self.seq
except:
pass
def is_same_form(self, sse):
"""
Return True if the given SSE has the same form (in structure)
More precisely, the functions return True if two SSE have same number of bases of each strands
"""
return self.get_strands_len() == sse.get_strands_len()
def __str__(self):
return self.name
class SSENode(SSE, NodeMixin):
def __init__(self, lst, root=False, parent=None, seq=""):
super().__init__(lst, root=root, seq=seq)
self.parent = parent
def __str__(self):
return self.name
class ScanResult:
def __init__(self):
pass
@classmethod
def from_exact_matching(cls, node,m):
res = cls()
res.seq = node.seq
res.module_id = m
return res
def eval():
"""
Method to calculate score
"""
pass
def __str__(self):
return "{}: Sequence: {}, module ID: {}".format(self.__class__, self.seq, self.module_id)
from .SSE import SSE
from .RNAStructure import SSETree
from .ScanResult import ScanResult
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment