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