annotate tools/new_operations/gops_cluster.py @ 1:cdcb0ce84a1b

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