-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathknockoutMaster.py
141 lines (119 loc) · 4.26 KB
/
knockoutMaster.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
# -*- coding: utf-8 -*-
'''
Proof-of-concept module for pyViKO. [Examples and source](https://github.com/louiejtaylor/pyViKO)
'''
#from collections import Counter
try:
from pyviko.core import codonify, seqify, insertMutation, translate, stopCodons
from pyviko.restriction import findNcutters
except ImportError as e:
print "ImportError!",
print e
#workhorse: find stop codons that can be generated given codons, mutation
def findPossibleStopCodons(codons, n):
if codons[-1] in stopCodons:
codons = codons[:-1] #remove c-terminal stop codon
almostStopCodons = {}
#build dict of codons that can be mutated to a stop codon
for c in stopCodons:
for i in range(0,3):
for nt in 'ACTG':
if c[:i]+nt+c[i+1:] not in stopCodons:
try:
almostStopCodons[c[:i]+nt+c[i+1:]].append(c)
except KeyError:
almostStopCodons[c[:i]+nt+c[i+1:]] = [c]
# does this include duplicates??? Yes!
if n == 2:
#TESTING: 2 MUTATIONS
# issue resolved, just need to take sets instead of using all possible mutants!
for c in almostStopCodons.keys():
for i in range(0,3):
for nt in 'ACTG':
if c[:i]+nt+c[i+1:] not in stopCodons:
try:
almostStopCodons[c[:i]+nt+c[i+1:]] += almostStopCodons[c]
except KeyError:
almostStopCodons[c[:i]+nt+c[i+1:]] = almostStopCodons[c]
for i in almostStopCodons.keys():
almostStopCodons[i] = list(set(almostStopCodons[i]))
# creates a list of tuples of the form (index, ['list', 'of', 'stop', 'codons']) to be further pre-processed
preMatches = [(i, almostStopCodons[codons[i]]) for i in range(0,len(codons)) if codons[i] in almostStopCodons.keys()]
matches = []
# further processing to create actual tuples (index, 'codon')
for m in preMatches:
for codon in m[1]:
matches.append((m[0],codon))
print len(matches)
#print matches
return matches
#finds overprinted gene, given input sequence, frameshift, and bool startsBefore
# TODO: change startsBefore to an **index** for gene
def findOverprintedGene(seq, frame, startsBefore):
codons = codonify(seq[frame - 1:])[:-1] #remove last (incomplete) codon
for i in range(0,len(codons)):
if codons[i] in stopCodons:
codons = codons[:i]
break
if not startsBefore:
x = 1
#
# what
#
#To fill in, although I don't need this case yet
#
#
#
return codons
def findNonHarmfulMutations(seq, frame, startsBefore, numMutations):
codons = codonify(seq)
stops = findPossibleStopCodons(codons, numMutations)
overAAs = translate(findOverprintedGene(seq, frame, startsBefore))
winners = []
for poss in stops:
nCodons = [codon for codon in codons]
newCodons = insertMutation(nCodons, poss)
newOverAAs = translate(findOverprintedGene(seqify(newCodons), frame, startsBefore))
if newOverAAs == overAAs:
winners.append(poss)
return winners
def findRestrictionSiteChanges(seq, frame, startsBefore, numMutations):
safeMutations = findNonHarmfulMutations(seq, frame, startsBefore, numMutations)
rSites = findNcutters(seq,6)
newSites = []
for i in safeMutations:
newSites.append((i, findNcutters(seqify(insertMutation(codonify(seq),i)), 6)))
winners = []
print len(newSites)
for j in newSites:
if j[1] <> rSites:
re = [r for r in rSites]
for site in j[1]:
try:
re.remove(site)
except ValueError:
re.append((site,'+++'))
k = (j[0], re)
winners.append(k)
for w in winners:
print w
return winners
# print all6mers[0], all6mers[1], all6mers[-2], all6mers[-1]
sequence = '''ATGGAACAGGCACCAGAAGATCAAGGACCACAGAGGGAGCCATACAACGAATGGGCTTTA
GAATTGTTGGAAGACCTAAAGAATGAGGCTCTGCGCCACTTTCCTCGGCCTTGGCTACAT
GGACTAGGGCAATACTTCTATAATACATATGGAGATACCTGGGAGGGAGTAGAGGCCATC
ATTAGGACACTACAACAACTGTTGTTTATACATTATAGGATTGGCTGTCAACATAGCAGG
ATAGGAATCACTCCTCAAAGGAGAAGGAATGGAGCCAGTAGATCCTGA
'''.replace('\n','')
# print findOverprintedGene(sequence,3,True)
if __name__ == "__main__":
mutFile = open('test/mutations.fasta',"w")
#print findNonHarmfulMutations(sequence, 3, True, 1)
mutFile.write("> Original sequence\n")
mutFile.write(sequence+"\n")
for f in findRestrictionSiteChanges(sequence, 2, True, 1):
mutFile.write('> Mutant at codon ' + str(f[0][0]+1) +' '+ str(f[1])+'\n')
#print f #for debugging, prints output to screen
mutFile.write(seqify(insertMutation(codonify(sequence),f[0]))+'\n')
#break
mutFile.close()