-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathall2fasta.py
More file actions
198 lines (191 loc) · 6.09 KB
/
all2fasta.py
File metadata and controls
198 lines (191 loc) · 6.09 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
# Shebang line here
#!/usr/bin/env python3
import argparse
import sys
import re
parser = argparse.ArgumentParser()
parser.add_argument("-i", "--file_name", help="Input file name")
parser.add_argument("-f", "--FOLD", default=70, help="Ithe line fole,defalut is 70")
args = parser.parse_args()
file_name=args.file_name
FOLD=args.FOLD
def type_of_file(file_name):
with open(file_name,'r') as f:
f=f.readlines()
for i in f:
if re.search('#MEGA',i):
mega(file_name)
elif re.search('@.+',i):
fastq(file_name)
elif re.search('LOCUS\s',i):
gb(file_name)
elif re.search('ID\s',i):
embl(file_name)
def fastq(file_name):
seq=''
s=''
l=''
with open(file_name,'r') as f:
f=f.readlines()
#print(f)
for i in range(len(f)):
f[i]=f[i].strip('\n')
#print(f[3])
search=re.match('@.+',f[i])
search_else1=re.match('\+',f[i])
search_else2=re.match('.*\?.+',f[i])
if search:
s+='\n'+f[i]+'\n'
elif search_else1:
l+=f[i]
elif search_else2:
l+=f[i]
else:
s+=f[i]
seq+=f[i]
st=extensiontype(seq)
p=s.split("\n")[1:]
#print(p)
with open(writeextension(st), 'w') as out:
for r in range(len(p)):
search1=re.search('@.+',p[r])
if search1:
if r>0:
out.write('\n'+'>'+p[r][1:]+'\n')
else:
out.write('>'+p[r][1:]+'\n')
else:
for k in range(0, len(p[r]), int(FOLD)):
out.write(p[r][k:k+int(FOLD)])
def embl(file_name):
seq=''
title=''
dna_or_aa=''
with open(file_name,'r') as f:
f=f.readlines()
for i in f:
if re.search('SQ',i):
index_1=f.index(i)
if re.search('//',i):
index_2=f.index(i)
if re.search('AC\s+',i):
i=i.replace(re.search('AC\s+',i).group(),'>')
title+=i
title=title.replace(';','|')
title=title.strip('\n')
if re.search('DE\s+',i):
i=i.replace(re.search('DE\s+',i).group(),'')
i=i.strip('\n')
title+=i
#print(title)
f=f[index_1+1:index_2]
for x in f:
if re.search('\s[0-9]+',x):
x=x.replace(re.search('\s[0-9]+',x).group(),'')
if re.search('\s',x):
x=x.replace(re.search('\s',x).group(),'')
x=x.strip('\n')
seq+=x
seq=seq.upper()
st=extensiontype(seq)
with open(writeextension(st),'w') as out:
out.write(title+'\n')
start=0
seq_length=len(seq)
while seq_length-start>=int(FOLD):
out.write(seq[start:start+int(FOLD)]+'\n')
start+=int(FOLD)
while seq_length-start<=int(FOLD):
out.write(seq[start:start+int(FOLD)])
break
def mega(file_name):
seq=''
s=''
with open(file_name,'r') as f:
f=f.readlines()
f=f[3:]
for i in range(len(f)):
f[i]=f[i].strip('\n')
search=re.search('#.+',f[i])
if search:
s+='\n'+f[i]+'\n'
else:
s+=f[i]
seq+=f[i]
st=extensiontype(seq)
#print(st)
p=s.split("\n")[1:]
with open(writeextension(st), 'w') as out:
for r in range(len(p)):
search1=re.search('#.+',p[r])
if search1:
if r>0:
out.write('\n'+'>'+p[r][1:]+'\n')
else:
out.write('>'+p[r][1:]+'\n')
else:
for k in range(0, len(p[r]), int(FOLD)):
out.write(p[r][k:k+int(FOLD)]+'\n')
def gb(file_name):
seq=''
title_ACCESSION=''
title_DEFINITION=''
dna_or_aa=''
with open(file_name,'r') as f:
f=f.readlines()
for i in f:
if re.search('ORIGIN',i):
index_1A=f.index(i)
if re.search('//',i):
index_2A=f.index(i)
if re.search('VERSION\s+',i):
i=i.replace(re.search('VERSION\s+',i).group(),'>')
title_ACCESSION+=i
title_ACCESSION=title_ACCESSION.strip('\n')
if re.search('DEFINITION\s+',i):
i=i.replace(re.search('DEFINITION\s+',i).group(),' ')
i=i.strip('\n')
title_DEFINITION+=i
title="{0}{1}" .format(title_ACCESSION, title_DEFINITION)
f=f[index_1A+1:index_2A]
for x in f:
if re.search('\s[0-9]+',x):
x=x.replace(re.search('\s[0-9]+',x).group(),'')
if re.search('\s',x):
x=x.replace(re.search('\s',x).group(),'')
x=x.strip('\n')
seq+=x
seq=seq.upper()
#print(seq)
st=extensiontype(seq)
with open(writeextension(st),'w') as out:
out.write(title+'\n')
start=0
seq_length=len(seq)
while seq_length-start>=int(FOLD):
out.write(seq[start:start+int(FOLD)]+'\n')
start+=int(FOLD)
while seq_length-start<=int(FOLD):
out.write(seq[start:start+int(FOLD)])
break
def extensiontype(seq):
dna_or_aa=''
if re.search('[ACGTNacgtn]*',seq):
dna_or_aa+='dna'
else:
dna_or_aa+='aa'
return dna_or_aa
def writeextension(st):
if '.' in file_name:
sep='.'
name = file_name.split(sep, 1)[0]
if st=='dna':
output_file=name+'.fna'
elif st=='aa':
output_file=name+'.faa'
return output_file
def main():
type_of_file(file_name)
if __name__ == '__main__':
main()
# Your code here