Mercurial > repos > devteam > complement
annotate gops_complement.py @ 5:8b6c6ec55c6e draft default tip
planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/complement commit d7b1a60c0aecc46b7f625c3e32f882562b512fd9
author | devteam |
---|---|
date | Mon, 13 Jun 2022 16:21:21 +0000 |
parents | 38c8bb402872 |
children |
rev | line source |
---|---|
0 | 1 #!/usr/bin/env python |
2 """ | |
3 Complement regions. | |
4 | |
5 usage: %prog in_file out_file | |
6 -1, --cols1=N,N,N,N: Columns for chrom, start, end, strand in file | |
7 -l, --lengths=N: Filename of .len file for species (chromosome lengths) | |
8 -a, --all: Complement all chromosomes (Genome-wide complement) | |
9 """ | |
4
38c8bb402872
planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/complement commit cae3e05d02e60f595bb8b6d77a84f030e9bd1689
devteam
parents:
3
diff
changeset
|
10 from __future__ import print_function |
0 | 11 |
4
38c8bb402872
planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/complement commit cae3e05d02e60f595bb8b6d77a84f030e9bd1689
devteam
parents:
3
diff
changeset
|
12 import fileinput |
3
e0a23ab32d7f
planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/complement commit a1517c9d22029095120643bbe2c8fa53754dd2b7
devteam
parents:
0
diff
changeset
|
13 import sys |
4
38c8bb402872
planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/complement commit cae3e05d02e60f595bb8b6d77a84f030e9bd1689
devteam
parents:
3
diff
changeset
|
14 |
38c8bb402872
planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/complement commit cae3e05d02e60f595bb8b6d77a84f030e9bd1689
devteam
parents:
3
diff
changeset
|
15 from bx.cookbook import doc_optparse |
3
e0a23ab32d7f
planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/complement commit a1517c9d22029095120643bbe2c8fa53754dd2b7
devteam
parents:
0
diff
changeset
|
16 from bx.intervals.io import GenomicInterval, GenomicIntervalReader, NiceReaderWrapper |
0 | 17 from bx.intervals.operations.complement import complement |
18 from bx.intervals.operations.subtract import subtract | |
3
e0a23ab32d7f
planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/complement commit a1517c9d22029095120643bbe2c8fa53754dd2b7
devteam
parents:
0
diff
changeset
|
19 from bx.tabular.io import ParseError |
e0a23ab32d7f
planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/complement commit a1517c9d22029095120643bbe2c8fa53754dd2b7
devteam
parents:
0
diff
changeset
|
20 from galaxy.tools.util.galaxyops import fail, parse_cols_arg, skipped |
0 | 21 |
22 assert sys.version_info[:2] >= ( 2, 4 ) | |
23 | |
3
e0a23ab32d7f
planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/complement commit a1517c9d22029095120643bbe2c8fa53754dd2b7
devteam
parents:
0
diff
changeset
|
24 |
0 | 25 def main(): |
26 allchroms = False | |
27 | |
28 options, args = doc_optparse.parse( __doc__ ) | |
29 try: | |
30 chr_col_1, start_col_1, end_col_1, strand_col_1 = parse_cols_arg( options.cols1 ) | |
31 lengths = options.lengths | |
3
e0a23ab32d7f
planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/complement commit a1517c9d22029095120643bbe2c8fa53754dd2b7
devteam
parents:
0
diff
changeset
|
32 if options.all: |
e0a23ab32d7f
planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/complement commit a1517c9d22029095120643bbe2c8fa53754dd2b7
devteam
parents:
0
diff
changeset
|
33 allchroms = True |
0 | 34 in_fname, out_fname = args |
35 except: | |
36 doc_optparse.exception() | |
37 | |
38 g1 = NiceReaderWrapper( fileinput.FileInput( in_fname ), | |
39 chrom_col=chr_col_1, | |
40 start_col=start_col_1, | |
41 end_col=end_col_1, | |
42 strand_col=strand_col_1, | |
43 fix_strand=True ) | |
44 | |
45 lens = dict() | |
46 chroms = list() | |
47 # dbfile is used to determine the length of each chromosome. The lengths | |
48 # are added to the lens dict and passed copmlement operation code in bx. | |
49 dbfile = fileinput.FileInput( lengths ) | |
3
e0a23ab32d7f
planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/complement commit a1517c9d22029095120643bbe2c8fa53754dd2b7
devteam
parents:
0
diff
changeset
|
50 |
0 | 51 if dbfile: |
52 if not allchroms: | |
53 try: | |
54 for line in dbfile: | |
55 fields = line.split("\t") | |
56 lens[fields[0]] = int(fields[1]) | |
57 except: | |
58 # assume LEN doesn't exist or is corrupt somehow | |
59 pass | |
60 elif allchroms: | |
61 try: | |
62 for line in dbfile: | |
63 fields = line.split("\t") | |
64 end = int(fields[1]) | |
3
e0a23ab32d7f
planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/complement commit a1517c9d22029095120643bbe2c8fa53754dd2b7
devteam
parents:
0
diff
changeset
|
65 chroms.append("\t".join([fields[0], "0", str(end)])) |
0 | 66 except: |
67 pass | |
68 | |
69 # Safety...if the dbfile didn't exist and we're on allchroms, then | |
70 # default to generic complement | |
71 if allchroms and len(chroms) == 0: | |
72 allchroms = False | |
73 | |
74 if allchroms: | |
75 chromReader = GenomicIntervalReader(chroms) | |
76 generator = subtract([chromReader, g1]) | |
77 else: | |
78 generator = complement(g1, lens) | |
79 | |
80 out_file = open( out_fname, "w" ) | |
81 | |
82 try: | |
83 for interval in generator: | |
84 if type( interval ) is GenomicInterval: | |
85 out_file.write( "%s\n" % "\t".join( interval ) ) | |
86 else: | |
87 out_file.write( "%s\n" % interval ) | |
4
38c8bb402872
planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/complement commit cae3e05d02e60f595bb8b6d77a84f030e9bd1689
devteam
parents:
3
diff
changeset
|
88 except ParseError as exc: |
0 | 89 out_file.close() |
90 fail( "Invalid file format: %s" % str( exc ) ) | |
91 | |
92 out_file.close() | |
93 | |
94 if g1.skipped > 0: | |
4
38c8bb402872
planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/complement commit cae3e05d02e60f595bb8b6d77a84f030e9bd1689
devteam
parents:
3
diff
changeset
|
95 print(skipped( g1, filedesc="" )) |
38c8bb402872
planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/complement commit cae3e05d02e60f595bb8b6d77a84f030e9bd1689
devteam
parents:
3
diff
changeset
|
96 |
0 | 97 |
98 if __name__ == "__main__": | |
99 main() |