annotate gops_complement.py @ 2:a3c7fd2ae338

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