comparison unified-histogram.py @ 0:ef5f8bbf7730 draft

planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/circos commit a41493893bdcbe330434db9c5851719012b62fa8
author iuc
date Wed, 09 Aug 2017 09:52:52 -0400
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:ef5f8bbf7730
1 #!/usr/bin/env python
2 import logging
3 import sys
4
5 import wiggle
6 from BCBio import GFF
7
8 logging.basicConfig(level=logging.INFO)
9 log = logging.getLogger()
10
11
12 MODE = sys.argv[1]
13
14 # Pair up (file, extension) pairs from sys.argv
15 files = zip(sys.argv[2:][0::2], sys.argv[2:][1::2])
16
17 # Our output data structure. This could be much more efficient.
18 data = {}
19
20
21 def bed(idx, path):
22 # chrom - The name of the chromosome (e.g. chr3, chrY, chr2_random) or scaffold (e.g. scaffold10671).
23 # chromStart - The starting position of the feature in the chromosome or scaffold. The first base in a chromosome is numbered 0.
24 # chromEnd - The ending position of the feature in the chromosome or scaffold. The chromEnd base is not included in the display of the feature. For example, the first 100 bases of a chromosome are defined as chromStart=0, chromEnd=100, and span the bases numbered 0-99.
25 # name - Defines the name of the BED line. This label is displayed to the left of the BED line in the Genome Browser window when the track is open to full display mode or directly to the left of the item in pack mode.
26 # score - A score between 0 and 1000. If the track line useScore attribute is set to 1 for this annotation data set, the score value will determine the level of gray in which this feature is displayed (higher numbers = darker gray). This table shows the Genome Browser's translation of BED score values into shades of gray:
27 # strand - Defines the strand - either '+' or '-'.
28 # thickStart - The starting position at which the feature is drawn thickly (for example, the start codon in gene displays). When there is no thick part, thickStart and thickEnd are usually set to the chromStart position.
29 # thickEnd - The ending position at which the feature is drawn thickly (for example, the stop codon in gene displays).
30 # itemRgb - An RGB value of the form R,G,B (e.g. 255,0,0). If the track line itemRgb attribute is set to "On", this RBG value will determine the display color of the data contained in this BED line. NOTE: It is recommended that a simple color scheme (eight colors or less) be used with this attribute to avoid overwhelming the color resources of the Genome Browser and your Internet browser.
31
32 with open(path, 'r') as handle:
33 for line in handle:
34 lineData = line.strip().split()
35 chrom = lineData[0]
36 chromStart = lineData[1]
37 chromEnd = lineData[2]
38
39 if chrom not in data:
40 data[chrom] = {}
41
42 for i in range(chromStart, chromEnd):
43 if i not in data[chrom]:
44 data[chrom][i] = {}
45
46 data[chrom][i][idx] = lineData[5]
47
48
49 # Handlers
50 def gff3(idx, path):
51 for record in GFF.parse(path):
52 if len(record.features) == 0:
53 continue
54
55 if record.id not in data:
56 data[record.id] = {}
57
58 for feature in record.features:
59 if 'score' in feature.qualifiers:
60 for i in range(feature.location.start, feature.location.end):
61 if i not in data[record.id]:
62 data[record.id][i] = {}
63
64 data[record.id][i][idx] = feature.qualifiers['score'][0]
65
66
67 def wig(idx, path):
68 walker = wiggle.Wiggle()
69 with open(path, 'r') as handle:
70 for region, position, value in walker.walk(handle):
71 if region not in data:
72 data[region] = {}
73
74 if position not in data[region]:
75 data[region][position] = {}
76
77 data[region][position][idx] = value
78
79
80 if __name__ == '__main__':
81 mode_tiles_possible = True
82
83 for idx, (file_path, file_type) in enumerate(files):
84 log.info("Processing %s.%s", file_path, file_type)
85
86 if file_type in globals():
87 func = globals()[file_type]
88 func(idx, file_path)
89
90 if file_type == 'wig':
91 mode_tiles_possible = False
92
93 if MODE == 'tile' and not mode_tiles_possible:
94 raise Exception("You requested a 'tile' plot with wig data, which is impossible")
95
96 # Max number of files
97 max_idx = range(len(files))
98
99 serialized_values = None
100 region_start, region_end = (None, None)
101
102 for genome in data:
103 for position in sorted(data[genome]):
104 values = [
105 '' if x not in data[genome][position] else data[genome][position][x]
106 for x in max_idx
107 ]
108 if serialized_values is None:
109 serialized_values = values
110 if region_start is None:
111 region_start = position
112 region_end = position
113
114 if values == serialized_values:
115 region_end = position
116 else:
117 if MODE == 'histogram':
118 # histogram
119 # hs4 0 1999999 5.0000,3.0000,1.0000,19.0000
120 sys.stdout.write(' '.join(
121 (genome, str(region_start), str(region_end), ','.join(map(str, values)))
122 ) + '\n')
123 elif MODE == 'heatmap':
124 # heatmap
125 # hs1 2000000 3999999 0.0000 id=hs4
126 # hs1 4000000 5999999 2.0000 id=hs1
127 # hs1 4000000 5999999 0.0000 id=hs2
128 # hs1 4000000 5999999 0.0000 id=hs3
129 # hs1 4000000 5999999 0.0000 id=hs4
130 # hs1 6000000 7999999 4.0000 id=hs2
131 for x in max_idx:
132 if x in data[genome][position]:
133 sys.stdout.write(' '.join(
134 (genome, str(region_start), str(region_end), data[genome][position][x], 'id=hm%s' % x)
135 ) + '\n')
136 else:
137 sys.stdout.write(' '.join(
138 (genome, str(region_start), str(region_end), 0.0, 'id=hm%s' % x)
139 ) + '\n')
140 elif MODE == 'line':
141 # multiple=False
142 sys.stdout.write(' '.join(
143 (genome, str(region_start), str(region_end), data[genome][position][0])
144 ) + '\n')
145 elif MODE == 'scatter':
146 # multiple=False
147 sys.stdout.write(' '.join(
148 (genome, str(region_start), str(region_end), data[genome][position][0])
149 ) + '\n')
150
151 # Update start of next array
152 region_start = position
153 region_end = position
154 # And update with new array
155 serialized_values = values