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