-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathfasta2pfam.py
More file actions
executable file
·45 lines (40 loc) · 1.17 KB
/
fasta2pfam.py
File metadata and controls
executable file
·45 lines (40 loc) · 1.17 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
#!/usr/bin/env python
docstring='''fasta2pfam [options] alignment.fasta
Convert Fasta format file to Pfam format file
Options:
-seqnamelen=10 maximum sequence name length
'''
import sys
def fasta2pfam(fasta_txt="seq.fasta",seqnamelen=0):
'''read fasta text "fasta_txt". return text in pfam format
seqnamelen - maximum sequence name length.
'''
pfam_txt=''
for block in fasta_txt.split('>'):
if not block.strip():
continue
header=block.split()[0]
sequence=''.join(block.splitlines()[1:])
if seqnamelen:
header=header[:seqnamelen]
pfam_txt+=header+' '+sequence+'\n'
return pfam_txt
if __name__=="__main__":
seqnamelen=0
argv=[]
for arg in sys.argv[1:]:
if arg.startswith("-seqnamelen="):
seqnamelen=int(arg[len("-seqnamelen="):])
else:
argv.append(arg)
if len(argv)<1:
sys.stderr.write(docstring)
exit()
for arg in argv:
if arg=='-':
fasta_txt=sys.stdin.read()
else:
fp=open(arg,'rU')
fasta_txt=fp.read()
fp.close()
sys.stdout.write(fasta2pfam(fasta_txt))