-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmod_seqfiles.py
256 lines (205 loc) · 6.31 KB
/
mod_seqfiles.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
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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
#!/usr/local/python/bin/python
# Copyright 2002,2003 Rasmus Wernersson, Technical University of Denmark
#
# This file is part of RevTrans.
#
# RevTrans is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# RevTrans is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with RevTrans; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
# $Id: mod_seqfiles.py,v 1.6 2005/06/09 09:58:54 raz Exp $
#
# $Log: mod_seqfiles.py,v $
# Revision 1.6 2005/06/09 09:58:54 raz
# Handles the simutation where multiple DNA sequences gives rise to the same
# peptide sequence.
#
# Revision 1.5 2004/12/06 09:32:57 raz
# Spaces is now automatically removed from FASTA sequences.
#
# Revision 1.4 2004/07/20 09:44:19 raz
# Minor changes for better error repporting
#
# Revision 1.3 2004/06/30 09:35:01 raz
# CVS now in sync with RevTrans 1.2
#
# Revision 1.2 2003/04/04 20:24:16 raz
# All revtrans files now released under the GPL
#
# Revision 1.1 2003/04/04 20:03:17 raz
# Translation has been moved into it's own module, and improved quite a lot. Supports
# full IUPAC.
#
# Handling of files hae also been moved into it's own module. When reading FASTA files
# the entire line after ">" counts as the name. Comments might just as well be phased
# out.
#
# Revtrans now makes sure no illegal characters exist in the read files. This also fixes
# problems with sequences with white-spaces.
# 25/3-2004: Fixed a problem where unexpected whitespace/newlines in FASTA files caused problems
"""
RevTrans - Module for handling sequence files
"""
import sys,string
def addEntry(name,seq,note,dict):
uniqename = name
c = 1
while dict.has_key(uniqename):
uniqename = "%s_%i" % (name,c)
c += 1
dict[uniqename] = (seq,note)
def readfasta(stream):
result = {}
name, seq, note = "",[],""
for line in stream.readlines():
#line.strip() # Remove whitespace at ends - including newline
if line.startswith(">"):
if name:
addEntry(name,"".join(seq),note,result)
name = line[1:].strip() # skip leading ">"
note = ""
seq = []
else:
seqline = line.strip().replace(" ","")
seq.append(seqline)
if name:
addEntry(name,"".join(seq),note,result)
return result
def writefasta(stream,seqs,charsperline):
for key in seqs.keys():
seq,note = seqs[key]
stream.write(">"+key+" "+note+"\n")
while len(seq) > 0:
stream.write(seq[0:charsperline]+"\n")
seq = seq [charsperline:]
def generic_writefasta(stream,seqs):
#print "writefasta:",seqs
writefasta(stream,seqs,50)
def readmsf(stream):
#print "readmsf"
result = {}
name, seq, note = "","",""
header = 1
for line in stream.readlines():
s = line.strip()
tokens = s.split()
if header:
if s == "//":
header = 0
validnames = result.keys()
continue
if tokens and (tokens[0].lower()) == "name:":
result[tokens[1]] = ""
elif tokens:
if tokens[0] in validnames:
key = tokens[0]
seq = string.join(tokens[1:],"")
result[key] += seq
for key in validnames:
seq = result[key]
result[key] = (seq,"")
return result
def chop(s,interval):
result = ""
for i in range(0,(len(s)/interval) +1):
result += s[i*interval:(i+1)*interval]+" "
return result.strip()
def writemsf(stream,seqs,filetype):
cpl = 50 #Chars per line
key = seqs.keys()[0]
seq,note = seqs[key]
stream.write("PileUp\n\n")
stream.write("MSF: "+str(len(seq))+"\tType: "+filetype+"\tCheck: 0\t..\n\n")
for key in seqs.keys():
seq,note = seqs[key]
stream.write("Name: "+key+"\tLen: "+str(len(seq))+"\n")
stream.write("\n\n//\n\n")
glob_len = len(seq)
i = 0
while i < glob_len:
for key in seqs.keys():
#print seqs[key]
seq,note = seqs[key]
seq = seq[i:i+cpl]
stream.write(key.ljust(16)+chop(seq,10)+"\n")
i +=cpl
stream.write("\n")
def generic_writemsf(stream,seqs):
writemsf(stream,seqs,"N")
def readaln(stream):
result = {}
firstline = 1
for line in stream.readlines():
if firstline:
if not (line[:7]).lower() == "clustal":
raise ValueError, "Not an ALN file"
firstline = 0
continue
if line[0:1] == " ": continue
tokens = line.split()
if not tokens: continue
name = tokens[0]
seq = tokens[1]
#seq = string.join(tokens[1:],"")
if name in result.keys():
result[name] += seq
else:
result[name] = seq
for key in result.keys():
seq = result[key]
result[key] = (seq,"")
return result
def writealn(stream,seqs):
stream.write("CLUSTAL X (1.64b) multiple sequence alignment - created by revtrans\n\n")
cpl = 60 # Chars per line
glob_len = 0
for key in seqs.keys():
seq,note = seqs[key]
glob_len = len(seq)
i = 0
while i < glob_len:
for key in seqs.keys():
#print seqs[key]
seq,note = seqs[key]
seq = seq[i:i+cpl]
stream.write(key.ljust(16)[:16]+seq+"\n")
i +=cpl
stream.write("\n\n")
readers = {"fasta":readfasta,"msf":readmsf,"aln":readaln}
#writers = {"fasta":generic_writefasta,"msf":generic_writemsf}
def autotype(filename):
f = open(filename,"r")
line = f.readline()
f.close()
line = line.lower()
if line.find("clustal") == 0: return "aln"
elif line.find("pileup") == 0: return "msf"
elif line[0] == ">": return "fasta"
return "unknown"
def readfileauto(filename):
filetype = autotype(filename)
return readfile(filename,filetype)
def readfile(filename, filetype):
if not filetype in readers.keys():
raise ValueError, "No suitable reader for file type: "+filetype
reader = readers[filetype]
stream = open(filename,"r")
result = reader(stream)
stream.close()
return result
def writestream(stream,seqs,filetype,seqtype):
if filetype == "msf" : writemsf(stream,seqs,seqtype)
elif filetype == "aln" : writealn(stream,seqs)
else : writefasta(stream,seqs,50)
if __name__ == "__main__":
seqs = readfileauto(sys.argv[1])
print "#seqs:"+str(len(seqs))