Repository navigation
Expand file tree
/
Copy pathdnautil.py
More file actions
207 lines (197 loc) · 5.47 KB
/
Copy pathdnautil.py
File metadata and controls
207 lines (197 loc) · 5.47 KB
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
#!/usr/bin/python
"""
Module of sequence manipulating functions
"""
def readfastafile(Fastafile):
"this function will import a Fasta file and put the data into a dictionary"
try:
f = open(Fastafile)
except IOError:
print("File %s does not exist!"% Fastafile)
seqs={}
for line in f:
line = line.rstrip()
if line.startswith('>'):
words = line.split()
name = words[0][1:]
seqs[name]=''
else:
seqs[name] = seqs[name]+line
f.close()
return seqs
def printsequencelength(sequences):
"this function will take a dictionary of sequences and report the length of them"
lengths={}
for name,seq in sequences.items():
lengths[name]=len(seq)
print(name,len(seq))
return lengths
def maxminseqs(sequences):
"this function finds the max(s) & min(s) of a set of sequences and reports the names"
maxlen = 0
maxnames = []
minlen = 0
minnames = []
seqnum = 1
for name,seq in sequences.items():
if seqnum ==1:
#first record
maxlen = len(seq)
minlen = len(seq)
maxnames=[name]
minnames=[name]
else:
if len(seq) < minlen:
#new shortest = clear old values
minlen = len(seq)
minnames=[name]
elif len(seq) == minlen:
#match shortest - add to list
minnames.append(name)
elif len(seq) > maxlen:
#new longest = clear old values
maxlen = len(seq)
maxnames=[name]
elif len(seq) == maxlen:
#match longest - add to list
maxnames.append(name)
seqnum +=1
print("Longest sequence is %i basepairs %i in strand(s):%s"% (maxlen, len(maxnames), maxnames))
print("Shortest sequence is %i basepairs %i in strand(s):%s"% (minlen, len(minnames), minnames))
print("Checked across %i sequences"% (seqnum-1))
return sequences
def lookforframe(dna):
"Searches a string of basepairs to find a frame reference"
dna = dna.upper()
startcodon = "ATG"
frameref = {1:False, 2:False, 3:False}
for frame in range(0,3):
for i in range(frame,len(dna),3):
codon=dna[i:i+3]
if codon == startcodon :
frameref[frame+1] = True
break
validframes = 0
totalvalids = 0
if frameref[1] :
validframe =1
print("Found frame1")
totalvalids +=1
if frameref[2] :
validframe =2
print("Found frame2")
totalvalids +=1
if frameref[3] :
validframe =3
print("Found frame3")
totalvalids +=1
if totalvalids ==1:
return validframe
elif totalvalids==0:
return 99
# return "no valid frames"
else:
return 98
# return "More than one valid frame"
def stripORFs(dna, frame=1):
"searches a dna sequence for Open Reading Frames and returns a dictionary of the start position and the ORF"
dna = dna.upper()
startcodon = "ATG"
stopcodons = ["TAA", "TAG", "TGA"]
orfs = {}
codonopened = False
for i in range(frame-1, len(dna),3):
codon=dna[i:i+3]
if codon == startcodon :
if codonopened:
pass
else:
startbp = i+1
codonopened = True
elif codonopened and codon in stopcodons:
codonopened = False #start looking for the next start
orfs[startbp] = dna[startbp-1:i+3]
else:
pass
return orfs
def fileORFs(fastafile):
"takes a fastafile strips the sequences and then searches for ORFs"
seqs = readfastafile(fastafile)
fullseq={}
for name,seq in seqs.items():
for i in range(1,4):
subname = name + "Frame" + str(i)
fullseq[subname] = stripORFs(seq,i)
return fullseq
def longestORFinfile(fastafile,frame):
"takes a fastafile strips the sequences and then searches for ORFs on a specific frame"
seqs = readfastafile(fastafile)
fullseq={}
longestORF = 0
seqname = []
Longestitem = {}
for name,seq in seqs.items():
fullseq[name] = stripORFs(seq,frame)
for name,orfs in fullseq.items():
for startbp,bases in orfs.items():
thislength = len(bases)
if thislength > longestORF:
seqname = [name]
tmp = startbp
longestORF = thislength
elif thislength== longestORF:
seqname.append(name)
else:
pass
print("Longest ORF starts at %i"% tmp)
Longestitem[longestORF] = seqname
return Longestitem
def longestORFinname(seqname, fastafile):
"finds a sequence in a file then will find the longest ORF the start position and the frame which it's on"
seqs = readfastafile(fastafile)
strippedorfs = {}
longestORF = []
lengthlongest = 0
longeststart = []
longestframe = []
for i in range(1,4):
thisframe = stripORFs (seqs[seqname],i)
for startbp, bases in thisframe.items():
if len(bases) > lengthlongest:
lengthlongest = len(bases)
longestORF = [bases]
longeststart = [startbp]
longestframe = [i]
elif len(bases) == lengthlongest:
longestORF.append(bases)
longeststart.append[startbp]
longestframe.append[i]
else:
pass
print("Longest ORF is %ibp starting at base %s on frame %s"% (lengthlongest, longeststart, longestframe))
print(longestORF)
return
def findrepeats(fastafile, replen):
"Reads a fastafile and looks for all repeats, reports the number of repeats of length replen and the most frequent repeat"
seqs = readfastafile(fastafile)
repeatlist = {}
maxrepeat = []
maxrepno = 0
for name,seq in seqs.items():
for i in range(0,len(seq)-replen+1):
searchstring = seq[i:replen+i]
count = 0
for seqname,seqbps in seqs.items():
occur = seqbps.count(searchstring)
count = count + occur
if count > 1:
repeatlist[searchstring] = count
if count>maxrepno:
maxrepno = count
maxrepeat = [searchstring]
elif count == maxrepno and searchstring not in maxrepeat:
maxrepeat.append(searchstring)
else:
pass
print("The most repeated sequence of %ibps occurs %i times and is %s"% (replen, maxrepno, maxrepeat))
return repeatlist