annotate gops_coverage.py @ 2:bc9c66c1e9c9 draft

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