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 | """ |
---|
10 | from galaxy import eggs |
---|
11 | import pkg_resources |
---|
12 | pkg_resources.require( "bx-python" ) |
---|
13 | import sys, traceback, fileinput |
---|
14 | from warnings import warn |
---|
15 | from bx.intervals import * |
---|
16 | from bx.intervals.io import * |
---|
17 | from bx.intervals.operations.coverage import * |
---|
18 | from bx.cookbook import doc_optparse |
---|
19 | from galaxy.tools.util.galaxyops import * |
---|
20 | |
---|
21 | assert sys.version_info[:2] >= ( 2, 4 ) |
---|
22 | |
---|
23 | def main(): |
---|
24 | upstream_pad = 0 |
---|
25 | downstream_pad = 0 |
---|
26 | |
---|
27 | options, args = doc_optparse.parse( __doc__ ) |
---|
28 | try: |
---|
29 | chr_col_1, start_col_1, end_col_1, strand_col_1 = parse_cols_arg( options.cols1 ) |
---|
30 | chr_col_2, start_col_2, end_col_2, strand_col_2 = parse_cols_arg( options.cols2 ) |
---|
31 | in_fname, in2_fname, out_fname = args |
---|
32 | except: |
---|
33 | doc_optparse.exception() |
---|
34 | |
---|
35 | g1 = NiceReaderWrapper( fileinput.FileInput( in_fname ), |
---|
36 | chrom_col=chr_col_1, |
---|
37 | start_col=start_col_1, |
---|
38 | end_col=end_col_1, |
---|
39 | strand_col=strand_col_1, |
---|
40 | fix_strand=True ) |
---|
41 | g2 = NiceReaderWrapper( fileinput.FileInput( in2_fname ), |
---|
42 | chrom_col=chr_col_2, |
---|
43 | start_col=start_col_2, |
---|
44 | end_col=end_col_2, |
---|
45 | strand_col=strand_col_2, |
---|
46 | fix_strand=True ) |
---|
47 | |
---|
48 | out_file = open( out_fname, "w" ) |
---|
49 | |
---|
50 | try: |
---|
51 | for line in coverage( [g1,g2] ): |
---|
52 | if type( line ) is GenomicInterval: |
---|
53 | out_file.write( "%s\n" % "\t".join( line.fields ) ) |
---|
54 | else: |
---|
55 | out_file.write( "%s\n" % line ) |
---|
56 | except ParseError, exc: |
---|
57 | out_file.close() |
---|
58 | fail( "Invalid file format: %s" % str( exc ) ) |
---|
59 | |
---|
60 | out_file.close() |
---|
61 | |
---|
62 | if g1.skipped > 0: |
---|
63 | print skipped( g1, filedesc=" of 1st dataset" ) |
---|
64 | if g2.skipped > 0: |
---|
65 | print skipped( g2, filedesc=" of 2nd dataset" ) |
---|
66 | |
---|
67 | if __name__ == "__main__": |
---|
68 | main() |
---|