python - Convert a letter in string to different letters with multiple output -
so have dna sequence
dna = "tannnt"
where n = ["a", "g", "c", "t"]
i want have possible output of taaaat, taaagt, taaact, taaatt..... , on.
right online found solution of permutations can perms = [''.join(p) p in permutations(n, 3)] iterate dna sequence ta + perms + t
but wonder if there easier way this, because have lot more dna sequences , make take lot more time hard code it.
edit:
the hard code part in have state
n1 = [''.join(p) p in permutations(n, 1)] n2 = [''.join(p) p in permutations(n, 2)] n3 = [''.join(p) p in permutations(n, 3)] then in n3:
key = "ta" + n3[i] + "t" since sequence quite long, don't want count how many consecutive n have in sequence , want see if there better way this.
you can use permutation results format string like:
code:
import itertools import re def convert_sequence(base_string, target_letter, perms): regex = re.compile('(%s+)' % target_letter) match = regex.search(base_string).group(0) pattern = regex.sub('%s', base_string) return [pattern % ''.join(p) p in it.permutations(perms, len(match))] test code:
print(convert_sequence('tannnt', 'n', ['a', 'g', 'c', 't'])) results:
['taagct', 'taagtt', 'taacgt', 'taactt', 'taatgt', 'taatct', 'tagact', 'tagatt', 'tagcat', 'tagctt', 'tagtat', 'tagtct', 'tacagt', 'tacatt', 'tacgat', 'tacgtt', 'tactat', 'tactgt', 'tatagt', 'tatact', 'tatgat', 'tatgct', 'tatcat', 'tatcgt']
Comments
Post a Comment