comparison unified-histogram.py @ 0:e8475d0195fe draft

planemo upload for repository https://github.com/TAMU-CPT/galaxy-circos-tool commit 358dd35a2150af4183d9303af1df4f63be0737cd
author eric-rasche
date Wed, 01 Mar 2017 22:47:20 -0500
parents
children b56f47c58779
comparison
equal deleted inserted replaced
-1:000000000000 0:e8475d0195fe
1 #!/usr/bin/env python
2 from BCBio import GFF
3 import wiggle
4 import sys
5 import logging
6 logging.basicConfig(level=logging.INFO)
7 log = logging.getLogger()
8
9
10 MODE = sys.argv[1]
11
12 # Pair up (file, extension) pairs from sys.argv
13 files = zip(sys.argv[2:][0::2], sys.argv[2:][1::2])
14
15 # Our output data structure. This could be much more efficient.
16 data = {}
17
18
19 def bed(idx, path):
20 # chrom - The name of the chromosome (e.g. chr3, chrY, chr2_random) or scaffold (e.g. scaffold10671).
21 # chromStart - The starting position of the feature in the chromosome or scaffold. The first base in a chromosome is numbered 0.
22 # 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.
23 # 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.
24 # 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:
25 # strand - Defines the strand - either '+' or '-'.
26 # 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.
27 # thickEnd - The ending position at which the feature is drawn thickly (for example, the stop codon in gene displays).
28 # 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.
29
30 with open(path, 'r') as handle:
31 for line in handle:
32 lineData = line.strip().split()
33 chrom = lineData[0]
34 chromStart = lineData[1]
35 chromEnd = lineData[2]
36
37 if chrom not in data:
38 data[chrom] = {}
39
40 for i in xrange(chromStart, chromEnd):
41 if i not in data[chrom]:
42 data[chrom][i] = {}
43
44 data[chrom][i][idx] = lineData[5]
45
46
47 # Handlers
48 def gff3(idx, path):
49 for record in GFF.parse(path):
50 if len(record.features) == 0:
51 continue
52
53 if record.id not in data:
54 data[record.id] = {}
55
56 for feature in record.features:
57 if 'score' in feature.qualifiers:
58 for i in xrange(feature.location.start, feature.location.end):
59 if i not in data[record.id]:
60 data[record.id][i] = {}
61
62 data[record.id][i][idx] = feature.qualifiers['score'][0]
63
64
65 def wig(idx, path):
66 walker = wiggle.Wiggle()
67 with open(path, 'r') as handle:
68 for region, position, value in walker.walk(handle):
69 if region not in data:
70 data[region] = {}
71
72 if position not in data[region]:
73 data[region][position] = {}
74
75 data[region][position][idx] = value
76
77
78 if __name__ == '__main__':
79 mode_tiles_possible = True
80
81 for idx, (file_path, file_type) in enumerate(files):
82 log.info("Processing %s.%s", file_path, file_type)
83
84 if file_type in globals():
85 func = globals()[file_type]
86 func(idx, file_path)
87
88 if file_type == 'wig':
89 mode_tiles_possible = False
90
91 if MODE == 'tile' and not mode_tiles_possible:
92 raise Exception("You requested a 'tile' plot with wig data, which is impossible")
93
94 # Max number of files
95 max_idx = range(len(files))
96
97 serialized_values = None
98 region_start, region_end = (None, None)
99
100 for genome in data:
101 for position in sorted(data[genome]):
102 values = [
103 '' if x not in data[genome][position] else data[genome][position][x]
104 for x in max_idx
105 ]
106 if serialized_values is None:
107 serialized_values = values
108 if region_start is None:
109 region_start = position
110 region_end = position
111
112 if values == serialized_values:
113 region_end = position
114 else:
115 if MODE == 'histogram':
116 # histogram
117 # hs4 0 1999999 5.0000,3.0000,1.0000,19.0000
118 print genome, region_start, region_end, ','.join(values)
119 elif MODE == 'heatmap':
120 # heatmap
121 # hs1 2000000 3999999 0.0000 id=hs4
122 # hs1 4000000 5999999 2.0000 id=hs1
123 # hs1 4000000 5999999 0.0000 id=hs2
124 # hs1 4000000 5999999 0.0000 id=hs3
125 # hs1 4000000 5999999 0.0000 id=hs4
126 # hs1 6000000 7999999 4.0000 id=hs2
127 for x in max_idx:
128 if x in data[genome][position]:
129 print genome, region_start, region_end, data[genome][position][x], 'id=hm%s' % x
130 else:
131 print genome, region_start, region_end, 0.0, 'id=hm%s' % x
132 elif MODE == 'line':
133 # multiple=False
134 print genome, region_start, region_end, data[genome][position][0]
135 elif MODE == 'scatter':
136 # multiple=False
137 print genome, region_start, region_end, data[genome][position][0]
138
139 # Update start of next array
140 region_start = position
141 region_end = position
142 # And update with new array
143 serialized_values = values