Created
June 16, 2015 07:06
-
-
Save soh-i/c29d8b7b0a32309a636b to your computer and use it in GitHub Desktop.
Query single-straned and bulge region from given dot-bracket format
This file contains hidden or 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
| def parse_dot_bracket_format(dot): | |
| binding_dict = {} | |
| for i, j in enumerate(dot): | |
| binding_dict[i] = None | |
| stack = [] | |
| for i, j in enumerate(dot): | |
| if j == "(": | |
| stack.append(i) | |
| elif j == ")": | |
| bp1 = stack.pop() | |
| bp2 = i | |
| binding_dict[bp1] = bp2 | |
| binding_dict[bp2] = bp1 | |
| loops = [] | |
| p = re.compile('[(][.]{1,1000}[)]') | |
| for m in p.finditer(dot): | |
| loops.append(m.span()) | |
| stem_loops = [] | |
| minor_space = [] | |
| for loop in loops: | |
| i = loop[1] -1 | |
| while True: | |
| if (i < len(dot)) and (dot[i] == ")") and not ((i - binding_dict[i] < 0)): | |
| last_valid = i | |
| if i > len(dot)-1: | |
| stem_loops.append([binding_dict[last_valid], last_valid]) | |
| break | |
| if binding_dict[i] == None: | |
| i += 1 | |
| continue | |
| if (i - binding_dict[i] < 0) or (binding_dict[i] in minor_space): | |
| stem_loops.append([binding_dict[last_valid], last_valid]) | |
| break | |
| else: | |
| i += 1 | |
| strand = [] | |
| a = re.compile('[.]{1,1000}') | |
| dot_iter = a.finditer(dot) | |
| for m in dot_iter: | |
| strand.append(m.span()) | |
| ss_strand = [] | |
| bulge = [] | |
| for d in strand: | |
| left = d[0] | |
| right = d[1] | |
| if (left == 0) or (right > len(dot)-1): | |
| ss_strand.append((left, right-1)) | |
| continue | |
| if (dot[left-1] == ')' and dot[right] == '('): | |
| ss_strand.append((left, right-1)) | |
| continue | |
| if ((dot[left-1] == ')' and dot[right] == ')') or (dot[left-1] == '(' and dot[right] == '(')): | |
| bulge.append((left, right-1)) | |
| return ss_strand, bulge | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment