comparison extract_GFF_Features.py @ 0:696e702ebf74 draft

"planemo upload commit 0f6eca49bafc3c946189d793161a7f81d595e1a1-dirty"
author petr-novak
date Mon, 09 May 2022 08:26:30 +0000
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:696e702ebf74
1 #!/usr/bin/env python
2 # Guruprasad Ananda
3 """
4 Extract features from GFF file.
5
6 usage: %prog input1 out_file1 column features
7 """
8 from __future__ import print_function
9
10 import sys
11
12 from bx.cookbook import doc_optparse
13
14
15 def stop_err(msg):
16 sys.stderr.write(msg)
17 sys.exit()
18
19
20 def main():
21 # Parsing Command Line here
22 options, args = doc_optparse.parse(__doc__)
23
24 try:
25 inp_file, out_file, column, features = args
26 except ValueError:
27 stop_err("One or more arguments is missing or invalid.\nUsage: prog input output column features")
28 try:
29 column = int(column)
30 except ValueError:
31 stop_err("Column %s is an invalid column." % column)
32
33 if features is None:
34 stop_err("Column %d has no features to display, select another column." % (column + 1))
35
36 fo = open(out_file, "w")
37 for line in open(inp_file):
38 line = line.rstrip("\r\n")
39 if line and line.startswith("#"):
40 # Keep valid comment lines in the output
41 fo.write("%s\n" % line)
42 else:
43 try:
44 if line.split("\t")[column] in features.split(","):
45 fo.write("%s\n" % line)
46 except Exception:
47 pass
48 fo.close()
49
50 print("Column %d features: %s" % (column + 1, features))
51
52
53 if __name__ == "__main__":
54 main()