Mercurial > repos > devteam > coverage
annotate gops_coverage.py @ 4:e3363e087468 draft default tip
planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/coverage commit d7b1a60c0aecc46b7f625c3e32f882562b512fd9
author | devteam |
---|---|
date | Mon, 13 Jun 2022 16:21:57 +0000 |
parents | 4ef9819dc7fb |
children |
rev | line source |
---|---|
0 | 1 #!/usr/bin/env python |
2 """ | |
3 Calculate coverage of one query on another, and append the coverage to | |
4 the last two columns as bases covered and percent coverage. | |
5 | |
6 usage: %prog bed_file_1 bed_file_2 out_file | |
7 -1, --cols1=N,N,N,N: Columns for start, end, strand in first file | |
8 -2, --cols2=N,N,N,N: Columns for start, end, strand in second file | |
9 """ | |
3
4ef9819dc7fb
planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/coverage commit cae3e05d02e60f595bb8b6d77a84f030e9bd1689
devteam
parents:
2
diff
changeset
|
10 from __future__ import print_function |
4ef9819dc7fb
planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/coverage commit cae3e05d02e60f595bb8b6d77a84f030e9bd1689
devteam
parents:
2
diff
changeset
|
11 |
2
bc9c66c1e9c9
planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/coverage commit a1517c9d22029095120643bbe2c8fa53754dd2b7
devteam
parents:
0
diff
changeset
|
12 import fileinput |
bc9c66c1e9c9
planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/coverage commit a1517c9d22029095120643bbe2c8fa53754dd2b7
devteam
parents:
0
diff
changeset
|
13 import sys |
3
4ef9819dc7fb
planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/coverage commit cae3e05d02e60f595bb8b6d77a84f030e9bd1689
devteam
parents:
2
diff
changeset
|
14 |
4ef9819dc7fb
planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/coverage commit cae3e05d02e60f595bb8b6d77a84f030e9bd1689
devteam
parents:
2
diff
changeset
|
15 from bx.cookbook import doc_optparse |
2
bc9c66c1e9c9
planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/coverage commit a1517c9d22029095120643bbe2c8fa53754dd2b7
devteam
parents:
0
diff
changeset
|
16 from bx.intervals.io import GenomicInterval, NiceReaderWrapper |
bc9c66c1e9c9
planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/coverage commit a1517c9d22029095120643bbe2c8fa53754dd2b7
devteam
parents:
0
diff
changeset
|
17 from bx.intervals.operations.coverage import coverage |
bc9c66c1e9c9
planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/coverage commit a1517c9d22029095120643bbe2c8fa53754dd2b7
devteam
parents:
0
diff
changeset
|
18 from bx.tabular.io import ParseError |
bc9c66c1e9c9
planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/coverage commit a1517c9d22029095120643bbe2c8fa53754dd2b7
devteam
parents:
0
diff
changeset
|
19 from galaxy.tools.util.galaxyops import fail, parse_cols_arg, skipped |
0 | 20 |
21 assert sys.version_info[:2] >= ( 2, 4 ) | |
22 | |
2
bc9c66c1e9c9
planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/coverage commit a1517c9d22029095120643bbe2c8fa53754dd2b7
devteam
parents:
0
diff
changeset
|
23 |
0 | 24 def main(): |
25 options, args = doc_optparse.parse( __doc__ ) | |
26 try: | |
27 chr_col_1, start_col_1, end_col_1, strand_col_1 = parse_cols_arg( options.cols1 ) | |
2
bc9c66c1e9c9
planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/coverage commit a1517c9d22029095120643bbe2c8fa53754dd2b7
devteam
parents:
0
diff
changeset
|
28 chr_col_2, start_col_2, end_col_2, strand_col_2 = parse_cols_arg( options.cols2 ) |
0 | 29 in_fname, in2_fname, out_fname = args |
30 except: | |
31 doc_optparse.exception() | |
32 | |
33 g1 = NiceReaderWrapper( fileinput.FileInput( in_fname ), | |
34 chrom_col=chr_col_1, | |
35 start_col=start_col_1, | |
36 end_col=end_col_1, | |
37 strand_col=strand_col_1, | |
38 fix_strand=True ) | |
39 g2 = NiceReaderWrapper( fileinput.FileInput( in2_fname ), | |
40 chrom_col=chr_col_2, | |
41 start_col=start_col_2, | |
42 end_col=end_col_2, | |
43 strand_col=strand_col_2, | |
44 fix_strand=True ) | |
45 | |
46 out_file = open( out_fname, "w" ) | |
47 | |
48 try: | |
2
bc9c66c1e9c9
planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/coverage commit a1517c9d22029095120643bbe2c8fa53754dd2b7
devteam
parents:
0
diff
changeset
|
49 for line in coverage( [g1, g2] ): |
0 | 50 if type( line ) is GenomicInterval: |
51 out_file.write( "%s\n" % "\t".join( line.fields ) ) | |
52 else: | |
53 out_file.write( "%s\n" % line ) | |
3
4ef9819dc7fb
planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/coverage commit cae3e05d02e60f595bb8b6d77a84f030e9bd1689
devteam
parents:
2
diff
changeset
|
54 except ParseError as exc: |
0 | 55 out_file.close() |
56 fail( "Invalid file format: %s" % str( exc ) ) | |
57 | |
58 out_file.close() | |
59 | |
60 if g1.skipped > 0: | |
3
4ef9819dc7fb
planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/coverage commit cae3e05d02e60f595bb8b6d77a84f030e9bd1689
devteam
parents:
2
diff
changeset
|
61 print(skipped( g1, filedesc=" of 1st dataset" )) |
0 | 62 if g2.skipped > 0: |
3
4ef9819dc7fb
planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/coverage commit cae3e05d02e60f595bb8b6d77a84f030e9bd1689
devteam
parents:
2
diff
changeset
|
63 print(skipped( g2, filedesc=" of 2nd dataset" )) |
4ef9819dc7fb
planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tool_collections/gops/coverage commit cae3e05d02e60f595bb8b6d77a84f030e9bd1689
devteam
parents:
2
diff
changeset
|
64 |
0 | 65 |
66 if __name__ == "__main__": | |
67 main() |