annotate gops_cluster.py @ 2:e84cc9cf3ba1

Added tool images.
author devteam <devteam@galaxyproject.org>
date Mon, 14 Apr 2014 09:05:51 -0400
parents 45fb880ab3bf
children 1e895b74f29f
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
45fb880ab3bf Imported from capsule None
devteam
parents:
diff changeset
1 #!/usr/bin/env python
45fb880ab3bf Imported from capsule None
devteam
parents:
diff changeset
2 """
45fb880ab3bf Imported from capsule None
devteam
parents:
diff changeset
3 Cluster regions of intervals.
45fb880ab3bf Imported from capsule None
devteam
parents:
diff changeset
4
45fb880ab3bf Imported from capsule None
devteam
parents:
diff changeset
5 usage: %prog in_file out_file
45fb880ab3bf Imported from capsule None
devteam
parents:
diff changeset
6 -1, --cols1=N,N,N,N: Columns for start, end, strand in file
45fb880ab3bf Imported from capsule None
devteam
parents:
diff changeset
7 -d, --distance=N: Maximum distance between clustered intervals
45fb880ab3bf Imported from capsule None
devteam
parents:
diff changeset
8 -v, --overlap=N: Minimum overlap require (negative distance)
45fb880ab3bf Imported from capsule None
devteam
parents:
diff changeset
9 -m, --minregions=N: Minimum regions per cluster
45fb880ab3bf Imported from capsule None
devteam
parents:
diff changeset
10 -o, --output=N: 1)merged 2)filtered 3)clustered 4) minimum 5) maximum
45fb880ab3bf Imported from capsule None
devteam
parents:
diff changeset
11 """
45fb880ab3bf Imported from capsule None
devteam
parents:
diff changeset
12 import sys, traceback, fileinput
45fb880ab3bf Imported from capsule None
devteam
parents:
diff changeset
13 from warnings import warn
45fb880ab3bf Imported from capsule None
devteam
parents:
diff changeset
14 from bx.intervals import *
45fb880ab3bf Imported from capsule None
devteam
parents:
diff changeset
15 from bx.intervals.io import *
45fb880ab3bf Imported from capsule None
devteam
parents:
diff changeset
16 from bx.intervals.operations.find_clusters import *
45fb880ab3bf Imported from capsule None
devteam
parents:
diff changeset
17 from bx.cookbook import doc_optparse
45fb880ab3bf Imported from capsule None
devteam
parents:
diff changeset
18 from galaxy.tools.util.galaxyops import *
45fb880ab3bf Imported from capsule None
devteam
parents:
diff changeset
19
45fb880ab3bf Imported from capsule None
devteam
parents:
diff changeset
20 assert sys.version_info[:2] >= ( 2, 4 )
45fb880ab3bf Imported from capsule None
devteam
parents:
diff changeset
21
45fb880ab3bf Imported from capsule None
devteam
parents:
diff changeset
22 def main():
45fb880ab3bf Imported from capsule None
devteam
parents:
diff changeset
23 distance = 0
45fb880ab3bf Imported from capsule None
devteam
parents:
diff changeset
24 minregions = 2
45fb880ab3bf Imported from capsule None
devteam
parents:
diff changeset
25 output = 1
45fb880ab3bf Imported from capsule None
devteam
parents:
diff changeset
26 upstream_pad = 0
45fb880ab3bf Imported from capsule None
devteam
parents:
diff changeset
27 downstream_pad = 0
45fb880ab3bf Imported from capsule None
devteam
parents:
diff changeset
28
45fb880ab3bf Imported from capsule None
devteam
parents:
diff changeset
29 options, args = doc_optparse.parse( __doc__ )
45fb880ab3bf Imported from capsule None
devteam
parents:
diff changeset
30 try:
45fb880ab3bf Imported from capsule None
devteam
parents:
diff changeset
31 chr_col_1, start_col_1, end_col_1, strand_col_1 = parse_cols_arg( options.cols1 )
45fb880ab3bf Imported from capsule None
devteam
parents:
diff changeset
32 if options.distance: distance = int( options.distance )
45fb880ab3bf Imported from capsule None
devteam
parents:
diff changeset
33 if options.overlap: distance = -1 * int( options.overlap )
45fb880ab3bf Imported from capsule None
devteam
parents:
diff changeset
34 if options.output: output = int( options.output )
45fb880ab3bf Imported from capsule None
devteam
parents:
diff changeset
35 if options.minregions: minregions = int( options.minregions )
45fb880ab3bf Imported from capsule None
devteam
parents:
diff changeset
36 in_fname, out_fname = args
45fb880ab3bf Imported from capsule None
devteam
parents:
diff changeset
37 except:
45fb880ab3bf Imported from capsule None
devteam
parents:
diff changeset
38 doc_optparse.exception()
45fb880ab3bf Imported from capsule None
devteam
parents:
diff changeset
39
45fb880ab3bf Imported from capsule None
devteam
parents:
diff changeset
40 g1 = NiceReaderWrapper( fileinput.FileInput( in_fname ),
45fb880ab3bf Imported from capsule None
devteam
parents:
diff changeset
41 chrom_col=chr_col_1,
45fb880ab3bf Imported from capsule None
devteam
parents:
diff changeset
42 start_col=start_col_1,
45fb880ab3bf Imported from capsule None
devteam
parents:
diff changeset
43 end_col=end_col_1,
45fb880ab3bf Imported from capsule None
devteam
parents:
diff changeset
44 strand_col=strand_col_1,
45fb880ab3bf Imported from capsule None
devteam
parents:
diff changeset
45 fix_strand=True )
45fb880ab3bf Imported from capsule None
devteam
parents:
diff changeset
46
45fb880ab3bf Imported from capsule None
devteam
parents:
diff changeset
47 # Get the cluster tree
45fb880ab3bf Imported from capsule None
devteam
parents:
diff changeset
48 try:
45fb880ab3bf Imported from capsule None
devteam
parents:
diff changeset
49 clusters, extra = find_clusters( g1, mincols=distance, minregions=minregions)
45fb880ab3bf Imported from capsule None
devteam
parents:
diff changeset
50 except ParseError, exc:
45fb880ab3bf Imported from capsule None
devteam
parents:
diff changeset
51 fail( "Invalid file format: %s" % str( exc ) )
45fb880ab3bf Imported from capsule None
devteam
parents:
diff changeset
52
45fb880ab3bf Imported from capsule None
devteam
parents:
diff changeset
53 f1 = open( in_fname, "r" )
45fb880ab3bf Imported from capsule None
devteam
parents:
diff changeset
54 out_file = open( out_fname, "w" )
45fb880ab3bf Imported from capsule None
devteam
parents:
diff changeset
55
45fb880ab3bf Imported from capsule None
devteam
parents:
diff changeset
56 # If "merge"
45fb880ab3bf Imported from capsule None
devteam
parents:
diff changeset
57 if output == 1:
45fb880ab3bf Imported from capsule None
devteam
parents:
diff changeset
58 fields = ["." for x in range(max(g1.chrom_col, g1.start_col, g1.end_col)+1)]
45fb880ab3bf Imported from capsule None
devteam
parents:
diff changeset
59 for chrom, tree in clusters.items():
45fb880ab3bf Imported from capsule None
devteam
parents:
diff changeset
60 for start, end, lines in tree.getregions():
45fb880ab3bf Imported from capsule None
devteam
parents:
diff changeset
61 fields[g1.chrom_col] = chrom
45fb880ab3bf Imported from capsule None
devteam
parents:
diff changeset
62 fields[g1.start_col] = str(start)
45fb880ab3bf Imported from capsule None
devteam
parents:
diff changeset
63 fields[g1.end_col] = str(end)
45fb880ab3bf Imported from capsule None
devteam
parents:
diff changeset
64 out_file.write( "%s\n" % "\t".join( fields ) )
45fb880ab3bf Imported from capsule None
devteam
parents:
diff changeset
65
45fb880ab3bf Imported from capsule None
devteam
parents:
diff changeset
66 # If "filtered" we preserve order of file and comments, etc.
45fb880ab3bf Imported from capsule None
devteam
parents:
diff changeset
67 if output == 2:
45fb880ab3bf Imported from capsule None
devteam
parents:
diff changeset
68 linenums = dict()
45fb880ab3bf Imported from capsule None
devteam
parents:
diff changeset
69 for chrom, tree in clusters.items():
45fb880ab3bf Imported from capsule None
devteam
parents:
diff changeset
70 for linenum in tree.getlines():
45fb880ab3bf Imported from capsule None
devteam
parents:
diff changeset
71 linenums[linenum] = 0
45fb880ab3bf Imported from capsule None
devteam
parents:
diff changeset
72 linenum = -1
45fb880ab3bf Imported from capsule None
devteam
parents:
diff changeset
73 f1.seek(0)
45fb880ab3bf Imported from capsule None
devteam
parents:
diff changeset
74 for line in f1.readlines():
45fb880ab3bf Imported from capsule None
devteam
parents:
diff changeset
75 linenum += 1
45fb880ab3bf Imported from capsule None
devteam
parents:
diff changeset
76 if linenum in linenums or linenum in extra:
45fb880ab3bf Imported from capsule None
devteam
parents:
diff changeset
77 out_file.write( "%s\n" % line.rstrip( "\n\r" ) )
45fb880ab3bf Imported from capsule None
devteam
parents:
diff changeset
78
45fb880ab3bf Imported from capsule None
devteam
parents:
diff changeset
79 # If "clustered" we output original intervals, but near each other (i.e. clustered)
45fb880ab3bf Imported from capsule None
devteam
parents:
diff changeset
80 if output == 3:
45fb880ab3bf Imported from capsule None
devteam
parents:
diff changeset
81 linenums = list()
45fb880ab3bf Imported from capsule None
devteam
parents:
diff changeset
82 f1.seek(0)
45fb880ab3bf Imported from capsule None
devteam
parents:
diff changeset
83 fileLines = f1.readlines()
45fb880ab3bf Imported from capsule None
devteam
parents:
diff changeset
84 for chrom, tree in clusters.items():
45fb880ab3bf Imported from capsule None
devteam
parents:
diff changeset
85 for linenum in tree.getlines():
45fb880ab3bf Imported from capsule None
devteam
parents:
diff changeset
86 out_file.write( "%s\n" % fileLines[linenum].rstrip( "\n\r" ) )
45fb880ab3bf Imported from capsule None
devteam
parents:
diff changeset
87
45fb880ab3bf Imported from capsule None
devteam
parents:
diff changeset
88 # If "minimum" we output the smallest interval in each cluster
45fb880ab3bf Imported from capsule None
devteam
parents:
diff changeset
89 if output == 4 or output == 5:
45fb880ab3bf Imported from capsule None
devteam
parents:
diff changeset
90 linenums = list()
45fb880ab3bf Imported from capsule None
devteam
parents:
diff changeset
91 f1.seek(0)
45fb880ab3bf Imported from capsule None
devteam
parents:
diff changeset
92 fileLines = f1.readlines()
45fb880ab3bf Imported from capsule None
devteam
parents:
diff changeset
93 for chrom, tree in clusters.items():
45fb880ab3bf Imported from capsule None
devteam
parents:
diff changeset
94 regions = tree.getregions()
45fb880ab3bf Imported from capsule None
devteam
parents:
diff changeset
95 for start, end, lines in tree.getregions():
45fb880ab3bf Imported from capsule None
devteam
parents:
diff changeset
96 outsize = -1
45fb880ab3bf Imported from capsule None
devteam
parents:
diff changeset
97 outinterval = None
45fb880ab3bf Imported from capsule None
devteam
parents:
diff changeset
98 for line in lines:
45fb880ab3bf Imported from capsule None
devteam
parents:
diff changeset
99 # three nested for loops?
45fb880ab3bf Imported from capsule None
devteam
parents:
diff changeset
100 # should only execute this code once per line
45fb880ab3bf Imported from capsule None
devteam
parents:
diff changeset
101 fileline = fileLines[line].rstrip("\n\r")
45fb880ab3bf Imported from capsule None
devteam
parents:
diff changeset
102 try:
45fb880ab3bf Imported from capsule None
devteam
parents:
diff changeset
103 cluster_interval = GenomicInterval( g1, fileline.split("\t"),
45fb880ab3bf Imported from capsule None
devteam
parents:
diff changeset
104 g1.chrom_col,
45fb880ab3bf Imported from capsule None
devteam
parents:
diff changeset
105 g1.start_col,
45fb880ab3bf Imported from capsule None
devteam
parents:
diff changeset
106 g1.end_col,
45fb880ab3bf Imported from capsule None
devteam
parents:
diff changeset
107 g1.strand_col,
45fb880ab3bf Imported from capsule None
devteam
parents:
diff changeset
108 g1.default_strand,
45fb880ab3bf Imported from capsule None
devteam
parents:
diff changeset
109 g1.fix_strand )
45fb880ab3bf Imported from capsule None
devteam
parents:
diff changeset
110 except Exception, exc:
45fb880ab3bf Imported from capsule None
devteam
parents:
diff changeset
111 print >> sys.stderr, str( exc )
45fb880ab3bf Imported from capsule None
devteam
parents:
diff changeset
112 f1.close()
45fb880ab3bf Imported from capsule None
devteam
parents:
diff changeset
113 sys.exit()
45fb880ab3bf Imported from capsule None
devteam
parents:
diff changeset
114 interval_size = cluster_interval.end - cluster_interval.start
45fb880ab3bf Imported from capsule None
devteam
parents:
diff changeset
115 if outsize == -1 or \
45fb880ab3bf Imported from capsule None
devteam
parents:
diff changeset
116 ( outsize > interval_size and output == 4 ) or \
45fb880ab3bf Imported from capsule None
devteam
parents:
diff changeset
117 ( outsize < interval_size and output == 5 ) :
45fb880ab3bf Imported from capsule None
devteam
parents:
diff changeset
118 outinterval = cluster_interval
45fb880ab3bf Imported from capsule None
devteam
parents:
diff changeset
119 outsize = interval_size
45fb880ab3bf Imported from capsule None
devteam
parents:
diff changeset
120 out_file.write( "%s\n" % outinterval )
45fb880ab3bf Imported from capsule None
devteam
parents:
diff changeset
121
45fb880ab3bf Imported from capsule None
devteam
parents:
diff changeset
122 f1.close()
45fb880ab3bf Imported from capsule None
devteam
parents:
diff changeset
123 out_file.close()
45fb880ab3bf Imported from capsule None
devteam
parents:
diff changeset
124
45fb880ab3bf Imported from capsule None
devteam
parents:
diff changeset
125 if g1.skipped > 0:
45fb880ab3bf Imported from capsule None
devteam
parents:
diff changeset
126 print skipped( g1, filedesc="" )
45fb880ab3bf Imported from capsule None
devteam
parents:
diff changeset
127
45fb880ab3bf Imported from capsule None
devteam
parents:
diff changeset
128 if __name__ == "__main__":
45fb880ab3bf Imported from capsule None
devteam
parents:
diff changeset
129 main()