forked from likit/BioUtils
-
Notifications
You must be signed in to change notification settings - Fork 0
/
unmapped_seq.py
30 lines (25 loc) · 860 Bytes
/
unmapped_seq.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
'''The script reads alignments in PSL format and prints out
unmapped sequences in FASTA format.
'''
import sys
from Bio import SeqIO
def parse_mapped(pslfile):
mapped = set()
for line in open(pslfile):
name = line.strip().split()[9]
mapped.add(name)
return mapped
def select_seq(mapped_seqs, fasta_file):
for record in SeqIO.parse(fasta_file, 'fasta'):
if record.id not in mapped_seqs:
print >> sys.stderr, record.id
yield record
if __name__=='__main__':
try:
pslfile = sys.argv[1]
fastafile = sys.argv[2]
except IndexError:
print >> sys.stderr, 'unmapped_seq.py <psl file> <fasta file>'
mapped_seqs = parse_mapped(pslfile)
print >> sys.stderr, 'Writing unmapped sequences ...'
SeqIO.write(select_seq(mapped_seqs, fastafile), sys.stdout, 'fasta')