Forked from hovissimo/Generating nucleotide degeneracy permutations
Created
January 20, 2015 10:16
-
-
Save Neemox/dc9f32594ad8d1cf24e9 to your computer and use it in GitHub Desktop.
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
import re | |
import csv | |
re_degenerates = re.compile(r"[wsmkrybdhvn]") | |
def get_patterns(filename): | |
with open(filename) as f: | |
lines = f.readlines()[2:] # the first two lines aren't data | |
for record in csv.reader(lines): | |
index = record[0] # the first column is the pattern id | |
pattern = "".join(record[1:]) # the rest of the columns are nucleotides | |
yield index, pattern | |
def has_degenerate(pattern): | |
return re_degenerates.search(pattern) is not None | |
def get_permutations(pattern): | |
""" Return all of the possible interpretation/permutations of pattern | |
pattern is a string that matches /[actgwsmkrybdhvn]+/ | |
Usage: | |
>>> tuple(get_permutations("acww")) | |
('acaa', 'acat', 'acta', 'actt') | |
""" | |
_degeneracy = { | |
"w": ("a", "t"), | |
"s": ("c", "g"), | |
"m": ("a", "c"), | |
"k": ("g", "t"), | |
"r": ("a", "g"), | |
"y": ("c", "t"), | |
"b": ("c", "g", "t"), | |
"d": ("a", "g", "t"), | |
"h": ("a", "c", "t"), | |
"v": ("a", "c", "g"), | |
"n": ("a", "c", "g", "t"), | |
} | |
if has_degenerate(pattern): | |
for nuc in pattern: | |
if nuc in _degeneracy: | |
for resolved_nuc in _degeneracy[nuc]: | |
new_pattern = pattern.replace(nuc, resolved_nuc, 1) | |
yield from get_permutations(new_pattern) | |
break | |
else: | |
yield pattern | |
def find_sequence(sequence, degenerate_pattern): | |
""" | |
sequence must consist only of real base pairs. (it must match /[actg]+/) | |
""" | |
re_sequence = re.compile(sequence) | |
for permutation in get_permutations(degenerate_pattern): | |
match = re_sequence.search(permutation) | |
if match: | |
print("Matched sequence '{0}' in permutation '{1}' at position {2}".format(sequence, swaprange(permutation, *match.span()), match.span())) | |
def main(sequence, filename="Primer_list.csv"): | |
for id, pattern in get_patterns(filename): | |
print("Permuting pattern {0}".format(id)) | |
find_sequence(sequence, pattern) | |
print() | |
def swaprange(s, left, right): | |
return s[:left] + s[left:right].swapcase() + s[right:] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment