forked from Ashish438/Python-for-Bioinformatics
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathremove_seq.py
More file actions
65 lines (44 loc) · 2.46 KB
/
Copy pathremove_seq.py
File metadata and controls
65 lines (44 loc) · 2.46 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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# Removing all sequences(contigs) less than a particular length from a fasta file derived from metagenomic data
# Here the length of the sequences to be removed is taken as 300 bp
# Type "python remove_seq.py -i Path/of/your/fasta/file -l Cut off length of the sequences to be removed
# - o Output file name with extension" for running the code
# Example: python remove_seq.py -i C:\Users\dell\PycharmProject\fb_page\metagenomic.fasta -l 300
# -o metagenomic_out.fasta
# Type "python remove_seq.py -h" for help/usage description
# Output: Fasta file with only those sequences which are greater than or equal to the specified length(e.g. 300)
# Sample input file: metagenomic.fasta
# Sample output file: metagenomic_out.fasta
import argparse
from pathlib import Path
from Bio import SeqIO
parser = argparse.ArgumentParser(description="Removing all sequences(contigs) less than a particular length from a fasta file",
usage= "remove_seq.py -i path/to/fasta/file -l Cut off length of the sequences to be removed -o Output filename with extension")
parser.add_argument("-i", help="ENTER FULL PATH OF THE FASTA FILE")
parser.add_argument("-l", help="ENTER CUT OFF LENGTH OF THE SEQUENCES TO BE REMOVED", type=int)
parser.add_argument("-o", help="ENTER OUTPUT FILE NAME WITH EXTENSION")
args = parser.parse_args()
fasta_path = Path(args.i)
cut_off_length = args.l
# generating out file path using the fasta path
out_filepath = fasta_path.parent/args.o
# parsing all the fasta sequences as seq records in the variable 'fasta_records'
fasta_records = SeqIO.parse(fasta_path,"fasta")
# an empty list to store the names of all the removed sequences
removed_seq = []
with open(out_filepath, "w") as fw:
# looping over each fasta record stored inside 'fasta_records'
for record in fasta_records:
print(len(record.seq))
# checking for length of each fasta sequence
if len(record.seq) >= cut_off_length:
# writing the required fasta sequences to output file
SeqIO.write(record, fw, format="fasta")
fw.write("\n")
else:
removed_seq.append(record.description)
print("The following sequences have been removed: ")
for seq_name in removed_seq:
print(seq_name)
print(f"Output file successfully created at {out_filepath}")
## please contact us if you face any issues while using the code
## your personal queries are also invited