annotate gops_coverage.py @ 0:6b81ee6c18d3

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