comparison gops_coverage.py @ 0:1e864693a1c0

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