annotate gops_cluster.py @ 3:765ceb06c3e2 draft

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