annotate gops_cluster.py @ 5:2108e2dc3d32 draft default tip

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