-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathextract.sequences.py
65 lines (51 loc) · 2.07 KB
/
extract.sequences.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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# By providing a one-column list of sequence ID (with or without ">") and a .fasta/.fas/.fa file with sequences
# inside, this script could extract the listed sequences, and report summary information.
import sys
from Bio import SeqIO
import os
# target dictionary
target_id = list()
my_target = open(sys.argv[1], 'r')
for line in my_target:
if line.startswith('>'):
line_1 = line.strip().split('>')
target_id.append(line_1[1])
elif line.startswith('>') is False:
line_2 = line.strip()
target_id.append(line_2)
my_target.close()
# header and sequence dictionary
fasta_dict = {}
fasta = open(sys.argv[2], 'r')
for line in SeqIO.parse(fasta, 'fasta'):
header = str(line.id).strip()
seq = str(line.seq).strip()
fasta_dict[header] = seq
fasta.close()
# print the targeted sequence in fasta format
total = list()
target_fasta = open(sys.argv[3], 'w+')
for scaffold in fasta_dict.keys():
if scaffold in target_id:
total.append(scaffold)
print(">" + scaffold, file=target_fasta)
print(fasta_dict[scaffold], file=target_fasta)
target_fasta.close()
# print summary information to the screen
not_found = open('Targets_not_retrieved.txt', 'w')
print("The sequence / These sequences could not be found in the input .fasta/.fas/.fa file", file=not_found)
print("************************************************************************************************************")
print("Done! You have " + str(len(target_id)-len(set(target_id))) + " replicate targets!")
print("You retrieved " + str(len(total)) + " out of " + str(len(set(target_id))) + " non-replicate targets!")
# print information of not extracted ones if detected
no = []
for seq in set(target_id):
if seq not in fasta_dict.keys():
print(seq, file=not_found)
no.append(seq)
if len(no) > 0:
print("Check 'Targets_not_retrieved.txt' for not retrieved ones.")
not_found.close()
else:
os.system('rm Targets_not_retrieved.txt')
print("************************************************************************************************************")