comparison fasta_filter_to_description.py @ 0:bc23da9d464c draft default tip

planemo upload for repository http://172.20.124.12:3000/bvalot3/PythonScript commit 9676573ee48ce5343600ab45cd3ac1a6adddabe4
author bvalot
date Tue, 14 Jun 2022 08:15:55 +0000
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:bc23da9d464c
1 #!/usr/bin/env python3
2 # -*- coding: utf-8 -*-
3
4 """Return a filter fasta file containing key words on description"""
5
6 import argparse
7 import sys
8
9 from Bio import SeqIO
10
11 desc = "Filter a fasta file based on description."
12 command = argparse.ArgumentParser(
13 prog="fasta_filter_to_description", description=desc, usage="%(prog)s [options] key"
14 )
15 command.add_argument(
16 "-i",
17 "--input",
18 default=sys.stdin,
19 type=argparse.FileType("r"),
20 nargs="?",
21 help="Input fasta file to, default:stdin",
22 )
23 command.add_argument(
24 "-o",
25 "--output",
26 default=sys.stdout,
27 type=argparse.FileType("w"),
28 nargs="?",
29 help="Output result to, default:stdout",
30 )
31 # command.add_argument('-e','--perl', action='store_true', \
32 # help='Key word is an perl expression')
33 command.add_argument(
34 "-V",
35 "--reverse",
36 action="store_true",
37 help="Return sequence without key in description",
38 )
39 command.add_argument(
40 "key", type=str, help="Key words that must be present in description"
41 )
42
43 if __name__ == "__main__":
44 """Performed job on execution script"""
45 args = command.parse_args()
46 output = args.output
47 count = 0
48 for seq in SeqIO.parse(args.input, "fasta"):
49 valid = False
50 if args.reverse:
51 if args.key not in seq.description:
52 valid = True
53 else:
54 if args.key in seq.description:
55 valid = True
56 if valid:
57 SeqIO.write(seq, output, "fasta")
58 count += 1
59 sys.stderr.write("Number of filter sequences : " + str(count) + "\n")