annotate gops_merge.py @ 0:10ac6097acdb

Imported from capsule None
author devteam
date Tue, 01 Apr 2014 09:13:25 -0400
parents
children b9c97d3233bb
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
10ac6097acdb Imported from capsule None
devteam
parents:
diff changeset
1 #!/usr/bin/env python
10ac6097acdb Imported from capsule None
devteam
parents:
diff changeset
2 """
10ac6097acdb Imported from capsule None
devteam
parents:
diff changeset
3 Merge overlaping regions.
10ac6097acdb Imported from capsule None
devteam
parents:
diff changeset
4
10ac6097acdb Imported from capsule None
devteam
parents:
diff changeset
5 usage: %prog in_file out_file
10ac6097acdb Imported from capsule None
devteam
parents:
diff changeset
6 -1, --cols1=N,N,N,N: Columns for start, end, strand in first file
10ac6097acdb Imported from capsule None
devteam
parents:
diff changeset
7 -m, --mincols=N: Require this much overlap (default 1bp)
10ac6097acdb Imported from capsule None
devteam
parents:
diff changeset
8 -3, --threecol: Output 3 column bed
10ac6097acdb Imported from capsule None
devteam
parents:
diff changeset
9 """
10ac6097acdb Imported from capsule None
devteam
parents:
diff changeset
10 import sys, traceback, fileinput
10ac6097acdb Imported from capsule None
devteam
parents:
diff changeset
11 from warnings import warn
10ac6097acdb Imported from capsule None
devteam
parents:
diff changeset
12 from bx.intervals import *
10ac6097acdb Imported from capsule None
devteam
parents:
diff changeset
13 from bx.intervals.io import *
10ac6097acdb Imported from capsule None
devteam
parents:
diff changeset
14 from bx.intervals.operations.merge import *
10ac6097acdb Imported from capsule None
devteam
parents:
diff changeset
15 from bx.cookbook import doc_optparse
10ac6097acdb Imported from capsule None
devteam
parents:
diff changeset
16 from galaxy.tools.util.galaxyops import *
10ac6097acdb Imported from capsule None
devteam
parents:
diff changeset
17
10ac6097acdb Imported from capsule None
devteam
parents:
diff changeset
18 assert sys.version_info[:2] >= ( 2, 4 )
10ac6097acdb Imported from capsule None
devteam
parents:
diff changeset
19
10ac6097acdb Imported from capsule None
devteam
parents:
diff changeset
20 def main():
10ac6097acdb Imported from capsule None
devteam
parents:
diff changeset
21 mincols = 1
10ac6097acdb Imported from capsule None
devteam
parents:
diff changeset
22 upstream_pad = 0
10ac6097acdb Imported from capsule None
devteam
parents:
diff changeset
23 downstream_pad = 0
10ac6097acdb Imported from capsule None
devteam
parents:
diff changeset
24
10ac6097acdb Imported from capsule None
devteam
parents:
diff changeset
25 options, args = doc_optparse.parse( __doc__ )
10ac6097acdb Imported from capsule None
devteam
parents:
diff changeset
26 try:
10ac6097acdb Imported from capsule None
devteam
parents:
diff changeset
27 chr_col_1, start_col_1, end_col_1, strand_col_1 = parse_cols_arg( options.cols1 )
10ac6097acdb Imported from capsule None
devteam
parents:
diff changeset
28 if options.mincols: mincols = int( options.mincols )
10ac6097acdb Imported from capsule None
devteam
parents:
diff changeset
29 in_fname, out_fname = args
10ac6097acdb Imported from capsule None
devteam
parents:
diff changeset
30 except:
10ac6097acdb Imported from capsule None
devteam
parents:
diff changeset
31 doc_optparse.exception()
10ac6097acdb Imported from capsule None
devteam
parents:
diff changeset
32
10ac6097acdb Imported from capsule None
devteam
parents:
diff changeset
33 g1 = NiceReaderWrapper( fileinput.FileInput( in_fname ),
10ac6097acdb Imported from capsule None
devteam
parents:
diff changeset
34 chrom_col=chr_col_1,
10ac6097acdb Imported from capsule None
devteam
parents:
diff changeset
35 start_col=start_col_1,
10ac6097acdb Imported from capsule None
devteam
parents:
diff changeset
36 end_col=end_col_1,
10ac6097acdb Imported from capsule None
devteam
parents:
diff changeset
37 strand_col = strand_col_1,
10ac6097acdb Imported from capsule None
devteam
parents:
diff changeset
38 fix_strand=True )
10ac6097acdb Imported from capsule None
devteam
parents:
diff changeset
39
10ac6097acdb Imported from capsule None
devteam
parents:
diff changeset
40 out_file = open( out_fname, "w" )
10ac6097acdb Imported from capsule None
devteam
parents:
diff changeset
41
10ac6097acdb Imported from capsule None
devteam
parents:
diff changeset
42 try:
10ac6097acdb Imported from capsule None
devteam
parents:
diff changeset
43 for line in merge(g1,mincols=mincols):
10ac6097acdb Imported from capsule None
devteam
parents:
diff changeset
44 if options.threecol:
10ac6097acdb Imported from capsule None
devteam
parents:
diff changeset
45 if type( line ) is GenomicInterval:
10ac6097acdb Imported from capsule None
devteam
parents:
diff changeset
46 out_file.write( "%s\t%s\t%s\n" % ( line.chrom, str( line.startCol ), str( line.endCol ) ) )
10ac6097acdb Imported from capsule None
devteam
parents:
diff changeset
47 elif type( line ) is list:
10ac6097acdb Imported from capsule None
devteam
parents:
diff changeset
48 out_file.write( "%s\t%s\t%s\n" % ( line[chr_col_1], str( line[start_col_1] ), str( line[end_col_1] ) ) )
10ac6097acdb Imported from capsule None
devteam
parents:
diff changeset
49 else:
10ac6097acdb Imported from capsule None
devteam
parents:
diff changeset
50 out_file.write( "%s\n" % line )
10ac6097acdb Imported from capsule None
devteam
parents:
diff changeset
51 else:
10ac6097acdb Imported from capsule None
devteam
parents:
diff changeset
52 if type( line ) is GenomicInterval:
10ac6097acdb Imported from capsule None
devteam
parents:
diff changeset
53 out_file.write( "%s\n" % "\t".join( line.fields ) )
10ac6097acdb Imported from capsule None
devteam
parents:
diff changeset
54 elif type( line ) is list:
10ac6097acdb Imported from capsule None
devteam
parents:
diff changeset
55 out_file.write( "%s\n" % "\t".join( line ) )
10ac6097acdb Imported from capsule None
devteam
parents:
diff changeset
56 else:
10ac6097acdb Imported from capsule None
devteam
parents:
diff changeset
57 out_file.write( "%s\n" % line )
10ac6097acdb Imported from capsule None
devteam
parents:
diff changeset
58 except ParseError, exc:
10ac6097acdb Imported from capsule None
devteam
parents:
diff changeset
59 out_file.close()
10ac6097acdb Imported from capsule None
devteam
parents:
diff changeset
60 fail( "Invalid file format: %s" % str( exc ) )
10ac6097acdb Imported from capsule None
devteam
parents:
diff changeset
61
10ac6097acdb Imported from capsule None
devteam
parents:
diff changeset
62 out_file.close()
10ac6097acdb Imported from capsule None
devteam
parents:
diff changeset
63
10ac6097acdb Imported from capsule None
devteam
parents:
diff changeset
64 if g1.skipped > 0:
10ac6097acdb Imported from capsule None
devteam
parents:
diff changeset
65 print skipped( g1, filedesc=" of 1st dataset" )
10ac6097acdb Imported from capsule None
devteam
parents:
diff changeset
66
10ac6097acdb Imported from capsule None
devteam
parents:
diff changeset
67 if __name__ == "__main__":
10ac6097acdb Imported from capsule None
devteam
parents:
diff changeset
68 main()