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