forked from NCGAS/Useful-Scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathExtract_Interested_Seq.py
49 lines (39 loc) · 1.53 KB
/
Extract_Interested_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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#!/usr/bin/env python
# This code is a biopython code that extracts the fasta sequences from a list of interested seqIDs.
# Written from https://www.biostars.org/p/1709/, modified by Bhavya on Oct 31st.
# Contact [email protected]
# Use python ExtractSeq -in <fasta.file> -i <list of interested seqIDs> -o <output file>
#
import argparse
import sys
from Bio import SeqIO
from Bio.SeqUtils import GC
def ExtractSeq(fastaIn,Indexlist,fastaOut):
fasta_file =open(fastaIn) #input fasta file
wanted_file=open(Indexlist) #input a list interested sequeences
result_file=open(fastaOut,"w") #output fasta file
wanted = set()
with wanted_file as f:
for line in f:
line = line.strip()
if line != "":
wanted.add(line)
#print line
fasta_sequences = SeqIO.parse(fasta_file,'fasta')
with result_file as f:
for seq in fasta_sequences:
#print seq.id
if seq.id in wanted:
SeqIO.write([seq], f, "fasta")
parser = argparse.ArgumentParser()
parser.add_argument('-in', action='store', dest='contigs',
help='fasta files')
parser.add_argument('-i', action='store', dest='index',
help='list of interested sequences')
parser.add_argument('-o', action='store', dest='output',
help='Output fasta file')
results = parser.parse_args()
print 'Contigs File =', results.contigs
print 'Index list =', results.index
print 'Output stored in =', results.output
ExtractSeq(results.contigs, results.index, results.output)