-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfasta.py
More file actions
194 lines (176 loc) · 4.11 KB
/
fasta.py
File metadata and controls
194 lines (176 loc) · 4.11 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
from die import *
import subprocess
import tempfile
import progress
TRUNC_LABELS=0
def isgap(c):
return c == '-' or c == '.'
def GetSeqCount(FileName):
Tmp = tempfile.TemporaryFile()
try:
TmpFile = Tmp.file
except:
TmpFile = Tmp
s = subprocess.call([ "grep", "-c", "^>", FileName ], stdout=TmpFile)
TmpFile.seek(0)
s = TmpFile.read()
return int(s)
def GetSeqsDict(FileName):
return ReadSeqsFast(FileName, False)
def ReadSeqsDict(FileName, Progress = False):
return ReadSeqsFast(FileName, Progress)
def ReadSeqsOnSeq(FileName, OnSeq, Progress = False):
ReadSeqs3(FileName, OnSeq, Progress)
def ReadSeqsFastFile(File, Progress = False):
Seqs = {}
Id = ""
N = 0
while 1:
if N%10000 == 0 and Progress:
sys.stderr.write("%u seqs\r" % (N))
Line = File.readline()
if len(Line) == 0:
if Progress:
sys.stderr.write("%u seqs\n" % (N))
return Seqs
if len(Line) == 0:
continue
Line = Line.strip()
if Line[0] == ">":
N += 1
Id = Line[1:]
if TRUNC_LABELS:
Id = Id.split()[0]
Seqs[Id] = ""
else:
if Id == "":
Die("FASTA file does not start with '>'")
Seqs[Id] = Seqs[Id] + Line
def ReadSeqsFast(FileName, Progress = True):
File = open(FileName)
return ReadSeqsFastFile(File, Progress)
def ReadSeqs(FileName, toupper=False, stripgaps=False, Progress=False):
if not toupper and not stripgaps:
return ReadSeqsFast(FileName, False)
Seqs = {}
Id = ""
File = open(FileName)
while 1:
Line = File.readline()
if len(Line) == 0:
return Seqs
Line = Line.strip()
if len(Line) == 0:
continue
if Line[0] == ">":
Id = Line[1:]
if TRUNC_LABELS:
Id = Id.split()[0]
if Id in Seqs.keys():
Die("Duplicate id '%s' in '%s'" % (Id, FileName))
Seqs[Id] = ""
else:
if Id == "":
Die("FASTA file '%s' does not start with '>'" % FileName)
if toupper:
Line = Line.upper()
if stripgaps:
Line = Line.replace("-", "")
Line = Line.replace(".", "")
Seqs[Id] = Seqs[Id] + Line
def ReadSeqs2(FileName, ShowProgress = True):
Seqs = []
Labels = []
File = open(FileName)
if ShowProgress:
progress.InitFile(File, FileName)
while 1:
progress.File()
Line = File.readline()
if len(Line) == 0:
if ShowProgress:
print >> sys.stderr, "\n"
return Labels, Seqs
Line = Line.strip()
if len(Line) == 0:
continue
if Line[0] == ">":
Id = Line[1:]
if TRUNC_LABELS:
Id = Id.split()[0]
Labels.append(Id)
Seqs.append("")
else:
i = len(Seqs)-1
Seqs[i] = Seqs[i] + Line
def ReadSeqs3(FileName, OnSeq, ShowProgress = True):
File = open(FileName)
if ShowProgress:
progress.InitFile(File, FileName)
Label = ""
Seq = ""
while 1:
Line = File.readline()
if len(Line) == 0:
if Seq != "":
OnSeq(Label, Seq)
if ShowProgress:
print >> sys.stderr, "\n"
return
Line = Line.strip()
if len(Line) == 0:
continue
if Line[0] == ">":
if Seq != "":
if ShowProgress:
progress.File()
if TRUNC_LABELS:
Label = Label.split()[0]
OnSeq(Label, Seq)
Label = Line[1:]
Seq = ""
else:
Seq += Line
def WriteSeq(File, Seq):
BLOCKLENGTH = 80
SeqLength = len(Seq)
BlockCount = int((SeqLength + (BLOCKLENGTH-1))/BLOCKLENGTH)
for BlockIndex in range(0, BlockCount):
Block = Seq[BlockIndex*BLOCKLENGTH:]
Block = Block[:BLOCKLENGTH]
print >> File, Block
def GetSizeFromLabel(Label, Default = -1):
Fields = Label.split(";")
for Field in Fields:
if Field.startswith("size="):
return int(Field[5:])
if Default == -1:
Die("Missing size >" + Label)
return Default
def StripSizeFromLabel(Label):
Fields = Label.split(";")
NewLabel = ""
for Field in Fields:
if Field.startswith("size="):
continue
if NewLabel != "":
NewLabel += ";"
NewLabel += Field
return NewLabel
def GetQualFromLabel(Label):
n = Label.find("qual=")
assert n >= 0
return Label[n+5:-1]
def StripQualFromLabel(Label):
n = Label.find("qual=")
assert n >= 0
return Label[:n]
def GetField(Label, Name, Default):
Fields = Label.split(';')
for Field in Fields:
if Field.startswith(Name + "="):
n = len(Name) + 1
return Field[n:]
if Default == "":
Die("Field %s= not found in >%s" % (Name, Label))
return Default