-
Notifications
You must be signed in to change notification settings - Fork 10
/
fasta2len.py
executable file
·36 lines (34 loc) · 976 Bytes
/
fasta2len.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
#!/usr/bin/env python
docstring='''fasta2len.py seq.fasta > seq.len
print the length of each entry in the input fasta file
'''
import sys
def fasta2len(infile="seq.fasta"):
'''calculat the length of each entry in the input fasta file "infile"'''
len_txt=''
if infile=='-':
txt=sys.stdin.read()
else:
fp=open(infile,'rU')
txt=fp.read()
fp.close()
for block in ('\n'+txt).split('\n>'):
block=block.strip()
if not block:
continue
lines=block.splitlines()
header=lines[0].split()[0]
sequence=''.join(lines[1:])
len_txt+="%s\t%u\n"%(header,len(sequence))
return len_txt
if __name__=="__main__":
if len(sys.argv)<2:
sys.stderr.write(docstring)
exit()
len_txt=fasta2len(infile=sys.argv[1])
if len(sys.argv)==2:
sys.stdout.write(len_txt)
else:
fp=open(sys.argv[2],'w')
fp.write(len_txt)
fp.close()