annotate gops_complement.py @ 0:d958d5a0d1e8

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