def skew(genome): ''' Input: A String genome Output: A list consisting of Skew_i(Genome) (as i ranges from 0 to |Genome|) being Skew_i(Genome) the difference between the total number of occurrences of G and the total number of occurrences of C in the first i nucleotides of Genome. ''' values = [0] for nucleotide in genome: if nucleotide == "C": values.append(values[-1] -1) elif nucleotide == "G": values.append(values[-1] +1) else: values.append(values[-1]) return values def minimun_skew(genome): ''' Input: A DNA string genome. Output: All integer(s) i minimizing Skew_i(Genome) among all values of i (from 0 to |Genome|). ''' skew_list = skew(genome) minimun = min(skew_list) return [i for i, value in enumerate(skew_list) if value == minimun] def hamming_distance(string1, string2): ''' Compute the Hamming distance between two strings. Input: Two strings of equal length. Output: The Hamming distance between these strings, i.e. the number of mismatches ''' count = 0 for index in range(len(string1)): if not string1[index] == string2[index]: count += 1 return count def approximate_pattern_matching(pattern, text, d): ''' Find all approximate occurrences of a pattern in a string. Input: Strings pattern and text along with an integer d. Output: All starting positions where pattern appears as a substring of text with at most d mismatches. ''' positions = [] for index in range(len(text) - len(pattern) + 1): if hamming_distance(text[index: index + len(pattern)], pattern) <= d: positions.append(index) return positions #print approximate_pattern_matching("AAA","TTTTTTAAATTTTAAATTTTTT",2) #print approximate_pattern_matching("GAGCGCTGG","GAGCGCTGGGTTAACTCGCTACTTCCCGACGAGCGCTGTGGCGCAAATTGGCGATGAAACTGCAGAGAGAACTGGTCATCCAACTGAATTCTCCCCGCTATCGCATTTTGATGCGCGCCGCGTCGATT", 2) #print approximate_pattern_matching("AATCCTTTCA","CCAAATCCCCTCATGGCATGCATTCCCGCAGTATTTAATCCTTTCATTCTGCATATAAGTAGTGAAGGTATAGAAACCCGTTCAAGCCCGCAGCGGTAAAACCGAGAACCATGATGAATGCACGGCGATTGCGCCATAATCCAAACA", 3) #print approximate_pattern_matching("CCGTCATCC","CCGTCATCCGTCATCCTCGCCACGTTGGCATGCATTCCGTCATCCCGTCAGGCATACTTCTGCATATAAGTACAAACATCCGTCATGTCAAAGGGAGCCCGCAGCGGTAAAACCGAGAACCATGATGAATGCACGGCGATTGC", 3)
Run
Reset
Share
Import
Link
Embed
Language▼
English
中文
Python Fiddle
Python Cloud IDE
Follow @python_fiddle
Browser Version Not Supported
Due to Python Fiddle's reliance on advanced JavaScript techniques, older browsers might have problems running it correctly. Please download the latest version of your favourite browser.
Chrome 10+
Firefox 4+
Safari 5+
IE 10+
Let me try anyway!
url:
Go
Python Snippet
Stackoverflow Question