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
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
d958d5a0d1e8 Imported from capsule None
devteam
parents:
diff changeset
1 #!/usr/bin/env python
d958d5a0d1e8 Imported from capsule None
devteam
parents:
diff changeset
2 """
d958d5a0d1e8 Imported from capsule None
devteam
parents:
diff changeset
3 Complement regions.
d958d5a0d1e8 Imported from capsule None
devteam
parents:
diff changeset
4
d958d5a0d1e8 Imported from capsule None
devteam
parents:
diff changeset
5 usage: %prog in_file out_file
d958d5a0d1e8 Imported from capsule None
devteam
parents:
diff changeset
6 -1, --cols1=N,N,N,N: Columns for chrom, start, end, strand in file
d958d5a0d1e8 Imported from capsule None
devteam
parents:
diff changeset
7 -l, --lengths=N: Filename of .len file for species (chromosome lengths)
d958d5a0d1e8 Imported from capsule None
devteam
parents:
diff changeset
8 -a, --all: Complement all chromosomes (Genome-wide complement)
d958d5a0d1e8 Imported from capsule None
devteam
parents:
diff changeset
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
d958d5a0d1e8 Imported from capsule None
devteam
parents:
diff changeset
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
d958d5a0d1e8 Imported from capsule None
devteam
parents:
diff changeset
17 from bx.intervals.operations.complement import complement
d958d5a0d1e8 Imported from capsule None
devteam
parents:
diff changeset
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
d958d5a0d1e8 Imported from capsule None
devteam
parents:
diff changeset
21
d958d5a0d1e8 Imported from capsule None
devteam
parents:
diff changeset
22 assert sys.version_info[:2] >= ( 2, 4 )
d958d5a0d1e8 Imported from capsule None
devteam
parents:
diff changeset
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
d958d5a0d1e8 Imported from capsule None
devteam
parents:
diff changeset
25 def main():
d958d5a0d1e8 Imported from capsule None
devteam
parents:
diff changeset
26 allchroms = False
d958d5a0d1e8 Imported from capsule None
devteam
parents:
diff changeset
27
d958d5a0d1e8 Imported from capsule None
devteam
parents:
diff changeset
28 options, args = doc_optparse.parse( __doc__ )
d958d5a0d1e8 Imported from capsule None
devteam
parents:
diff changeset
29 try:
d958d5a0d1e8 Imported from capsule None
devteam
parents:
diff changeset
30 chr_col_1, start_col_1, end_col_1, strand_col_1 = parse_cols_arg( options.cols1 )
d958d5a0d1e8 Imported from capsule None
devteam
parents:
diff changeset
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
d958d5a0d1e8 Imported from capsule None
devteam
parents:
diff changeset
34 in_fname, out_fname = args
d958d5a0d1e8 Imported from capsule None
devteam
parents:
diff changeset
35 except:
d958d5a0d1e8 Imported from capsule None
devteam
parents:
diff changeset
36 doc_optparse.exception()
d958d5a0d1e8 Imported from capsule None
devteam
parents:
diff changeset
37
d958d5a0d1e8 Imported from capsule None
devteam
parents:
diff changeset
38 g1 = NiceReaderWrapper( fileinput.FileInput( in_fname ),
d958d5a0d1e8 Imported from capsule None
devteam
parents:
diff changeset
39 chrom_col=chr_col_1,
d958d5a0d1e8 Imported from capsule None
devteam
parents:
diff changeset
40 start_col=start_col_1,
d958d5a0d1e8 Imported from capsule None
devteam
parents:
diff changeset
41 end_col=end_col_1,
d958d5a0d1e8 Imported from capsule None
devteam
parents:
diff changeset
42 strand_col=strand_col_1,
d958d5a0d1e8 Imported from capsule None
devteam
parents:
diff changeset
43 fix_strand=True )
d958d5a0d1e8 Imported from capsule None
devteam
parents:
diff changeset
44
d958d5a0d1e8 Imported from capsule None
devteam
parents:
diff changeset
45 lens = dict()
d958d5a0d1e8 Imported from capsule None
devteam
parents:
diff changeset
46 chroms = list()
d958d5a0d1e8 Imported from capsule None
devteam
parents:
diff changeset
47 # dbfile is used to determine the length of each chromosome. The lengths
d958d5a0d1e8 Imported from capsule None
devteam
parents:
diff changeset
48 # are added to the lens dict and passed copmlement operation code in bx.
d958d5a0d1e8 Imported from capsule None
devteam
parents:
diff changeset
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
d958d5a0d1e8 Imported from capsule None
devteam
parents:
diff changeset
51 if dbfile:
d958d5a0d1e8 Imported from capsule None
devteam
parents:
diff changeset
52 if not allchroms:
d958d5a0d1e8 Imported from capsule None
devteam
parents:
diff changeset
53 try:
d958d5a0d1e8 Imported from capsule None
devteam
parents:
diff changeset
54 for line in dbfile:
d958d5a0d1e8 Imported from capsule None
devteam
parents:
diff changeset
55 fields = line.split("\t")
d958d5a0d1e8 Imported from capsule None
devteam
parents:
diff changeset
56 lens[fields[0]] = int(fields[1])
d958d5a0d1e8 Imported from capsule None
devteam
parents:
diff changeset
57 except:
d958d5a0d1e8 Imported from capsule None
devteam
parents:
diff changeset
58 # assume LEN doesn't exist or is corrupt somehow
d958d5a0d1e8 Imported from capsule None
devteam
parents:
diff changeset
59 pass
d958d5a0d1e8 Imported from capsule None
devteam
parents:
diff changeset
60 elif allchroms:
d958d5a0d1e8 Imported from capsule None
devteam
parents:
diff changeset
61 try:
d958d5a0d1e8 Imported from capsule None
devteam
parents:
diff changeset
62 for line in dbfile:
d958d5a0d1e8 Imported from capsule None
devteam
parents:
diff changeset
63 fields = line.split("\t")
d958d5a0d1e8 Imported from capsule None
devteam
parents:
diff changeset
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
d958d5a0d1e8 Imported from capsule None
devteam
parents:
diff changeset
66 except:
d958d5a0d1e8 Imported from capsule None
devteam
parents:
diff changeset
67 pass
d958d5a0d1e8 Imported from capsule None
devteam
parents:
diff changeset
68
d958d5a0d1e8 Imported from capsule None
devteam
parents:
diff changeset
69 # Safety...if the dbfile didn't exist and we're on allchroms, then
d958d5a0d1e8 Imported from capsule None
devteam
parents:
diff changeset
70 # default to generic complement
d958d5a0d1e8 Imported from capsule None
devteam
parents:
diff changeset
71 if allchroms and len(chroms) == 0:
d958d5a0d1e8 Imported from capsule None
devteam
parents:
diff changeset
72 allchroms = False
d958d5a0d1e8 Imported from capsule None
devteam
parents:
diff changeset
73
d958d5a0d1e8 Imported from capsule None
devteam
parents:
diff changeset
74 if allchroms:
d958d5a0d1e8 Imported from capsule None
devteam
parents:
diff changeset
75 chromReader = GenomicIntervalReader(chroms)
d958d5a0d1e8 Imported from capsule None
devteam
parents:
diff changeset
76 generator = subtract([chromReader, g1])
d958d5a0d1e8 Imported from capsule None
devteam
parents:
diff changeset
77 else:
d958d5a0d1e8 Imported from capsule None
devteam
parents:
diff changeset
78 generator = complement(g1, lens)
d958d5a0d1e8 Imported from capsule None
devteam
parents:
diff changeset
79
d958d5a0d1e8 Imported from capsule None
devteam
parents:
diff changeset
80 out_file = open( out_fname, "w" )
d958d5a0d1e8 Imported from capsule None
devteam
parents:
diff changeset
81
d958d5a0d1e8 Imported from capsule None
devteam
parents:
diff changeset
82 try:
d958d5a0d1e8 Imported from capsule None
devteam
parents:
diff changeset
83 for interval in generator:
d958d5a0d1e8 Imported from capsule None
devteam
parents:
diff changeset
84 if type( interval ) is GenomicInterval:
d958d5a0d1e8 Imported from capsule None
devteam
parents:
diff changeset
85 out_file.write( "%s\n" % "\t".join( interval ) )
d958d5a0d1e8 Imported from capsule None
devteam
parents:
diff changeset
86 else:
d958d5a0d1e8 Imported from capsule None
devteam
parents:
diff changeset
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
d958d5a0d1e8 Imported from capsule None
devteam
parents:
diff changeset
89 out_file.close()
d958d5a0d1e8 Imported from capsule None
devteam
parents:
diff changeset
90 fail( "Invalid file format: %s" % str( exc ) )
d958d5a0d1e8 Imported from capsule None
devteam
parents:
diff changeset
91
d958d5a0d1e8 Imported from capsule None
devteam
parents:
diff changeset
92 out_file.close()
d958d5a0d1e8 Imported from capsule None
devteam
parents:
diff changeset
93
d958d5a0d1e8 Imported from capsule None
devteam
parents:
diff changeset
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
d958d5a0d1e8 Imported from capsule None
devteam
parents:
diff changeset
97
d958d5a0d1e8 Imported from capsule None
devteam
parents:
diff changeset
98 if __name__ == "__main__":
d958d5a0d1e8 Imported from capsule None
devteam
parents:
diff changeset
99 main()