annotate tools/new_operations/gops_complement.py @ 0:9071e359b9a3

Uploaded
author xuebing
date Fri, 09 Mar 2012 19:37:19 -0500
parents
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
1 #!/usr/bin/env python
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
2 """
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
3 Complement regions.
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
4
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
5 usage: %prog in_file out_file
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
6 -1, --cols1=N,N,N,N: Columns for chrom, start, end, strand in file
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
7 -l, --lengths=N: Filename of .len file for species (chromosome lengths)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
8 -a, --all: Complement all chromosomes (Genome-wide complement)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
9 """
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
10 from galaxy import eggs
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
11 import pkg_resources
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
12 pkg_resources.require( "bx-python" )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
13 import sys, traceback, fileinput
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
14 from warnings import warn
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
15 from bx.intervals import *
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
16 from bx.intervals.io import *
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
17 from bx.intervals.operations.complement import complement
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
18 from bx.intervals.operations.subtract import subtract
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
19 from bx.cookbook import doc_optparse
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
20 from galaxy.tools.util.galaxyops import *
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
21
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
22 assert sys.version_info[:2] >= ( 2, 4 )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
23
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
24 def main():
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
25 allchroms = False
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
26 upstream_pad = 0
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
27 downstream_pad = 0
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
28
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
29 options, args = doc_optparse.parse( __doc__ )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
30 try:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
31 chr_col_1, start_col_1, end_col_1, strand_col_1 = parse_cols_arg( options.cols1 )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
32 lengths = options.lengths
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
33 if options.all: allchroms = True
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
34 in_fname, out_fname = args
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
35 except:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
36 doc_optparse.exception()
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
37
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
38 g1 = NiceReaderWrapper( fileinput.FileInput( in_fname ),
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
39 chrom_col=chr_col_1,
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
40 start_col=start_col_1,
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
41 end_col=end_col_1,
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
42 strand_col=strand_col_1,
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
43 fix_strand=True )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
44
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
45 lens = dict()
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
46 chroms = list()
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
47 # dbfile is used to determine the length of each chromosome. The lengths
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
48 # are added to the lens dict and passed copmlement operation code in bx.
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
49 dbfile = fileinput.FileInput( lengths )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
50
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
51 if dbfile:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
52 if not allchroms:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
53 try:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
54 for line in dbfile:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
55 fields = line.split("\t")
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
56 lens[fields[0]] = int(fields[1])
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
57 except:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
58 # assume LEN doesn't exist or is corrupt somehow
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
59 pass
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
60 elif allchroms:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
61 try:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
62 for line in dbfile:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
63 fields = line.split("\t")
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
64 end = int(fields[1])
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
65 chroms.append("\t".join([fields[0],"0",str(end)]))
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
66 except:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
67 pass
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
68
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
69 # Safety...if the dbfile didn't exist and we're on allchroms, then
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
70 # default to generic complement
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
71 if allchroms and len(chroms) == 0:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
72 allchroms = False
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
73
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
74 if allchroms:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
75 chromReader = GenomicIntervalReader(chroms)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
76 generator = subtract([chromReader, g1])
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
77 else:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
78 generator = complement(g1, lens)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
79
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
80 out_file = open( out_fname, "w" )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
81
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
82 try:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
83 for interval in generator:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
84 if type( interval ) is GenomicInterval:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
85 out_file.write( "%s\n" % "\t".join( interval ) )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
86 else:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
87 out_file.write( "%s\n" % interval )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
88 except ParseError, exc:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
89 out_file.close()
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
90 fail( "Invalid file format: %s" % str( exc ) )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
91
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
92 out_file.close()
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
93
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
94 if g1.skipped > 0:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
95 print skipped( g1, filedesc="" )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
96
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
97 if __name__ == "__main__":
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
98 main()