annotate chromatral.py @ 9:743e69c1e122 default tip

Uploaded
author cmmt
date Mon, 23 Jan 2012 05:28:00 -0500
parents 519e45d9539f
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
3
519e45d9539f Uploaded
cmmt
parents:
diff changeset
1 import os;
519e45d9539f Uploaded
cmmt
parents:
diff changeset
2 import re;
519e45d9539f Uploaded
cmmt
parents:
diff changeset
3 import csv;
519e45d9539f Uploaded
cmmt
parents:
diff changeset
4 import sys;
519e45d9539f Uploaded
cmmt
parents:
diff changeset
5 import math;
519e45d9539f Uploaded
cmmt
parents:
diff changeset
6 import numpy as np;
519e45d9539f Uploaded
cmmt
parents:
diff changeset
7 import matplotlib;
519e45d9539f Uploaded
cmmt
parents:
diff changeset
8 matplotlib.use('Agg');
519e45d9539f Uploaded
cmmt
parents:
diff changeset
9 from pylab import *;
519e45d9539f Uploaded
cmmt
parents:
diff changeset
10
519e45d9539f Uploaded
cmmt
parents:
diff changeset
11 def extractGffFeatures(gffString):
519e45d9539f Uploaded
cmmt
parents:
diff changeset
12 # GFF v3
519e45d9539f Uploaded
cmmt
parents:
diff changeset
13 # 1. seqname - Must be a chromosome or scaffold.
519e45d9539f Uploaded
cmmt
parents:
diff changeset
14 # 2. source - The program that generated this feature.
519e45d9539f Uploaded
cmmt
parents:
diff changeset
15 # 3. feature - The name of this type of feature. Some examples of standard feature types are "CDS", "start_codon", "stop_codon", and "exon".
519e45d9539f Uploaded
cmmt
parents:
diff changeset
16 # 4. start - The starting position of the feature in the sequence. The first base is numbered 1.
519e45d9539f Uploaded
cmmt
parents:
diff changeset
17 # 5. end - The ending position of the feature (inclusive).
519e45d9539f Uploaded
cmmt
parents:
diff changeset
18 # 6. score - A score between 0 and 1000. If there is no score value, enter ".".
519e45d9539f Uploaded
cmmt
parents:
diff changeset
19 # 7. strand - Valid entries include '+', '-', or '.' (for don't know/care).
519e45d9539f Uploaded
cmmt
parents:
diff changeset
20 # 8. frame - If the feature is a coding exon, frame should be a number between 0-2 that represents the reading frame of the first base. If the feature is not a coding exon, the value should be '.'.
519e45d9539f Uploaded
cmmt
parents:
diff changeset
21 # 9. group - All lines with the same group are linked together into a single item.
519e45d9539f Uploaded
cmmt
parents:
diff changeset
22
519e45d9539f Uploaded
cmmt
parents:
diff changeset
23 res = [];
519e45d9539f Uploaded
cmmt
parents:
diff changeset
24
519e45d9539f Uploaded
cmmt
parents:
diff changeset
25 if len(gffString) > 1: # check for empty line (i.e. row in GFF file only contains \n)
519e45d9539f Uploaded
cmmt
parents:
diff changeset
26 try:
519e45d9539f Uploaded
cmmt
parents:
diff changeset
27 gffSeqName, gffSource, gffFeature, gffStart, gffEnd, gffScr, gffStrand, gffFrame, gffGroup = gffString.split('\t');
519e45d9539f Uploaded
cmmt
parents:
diff changeset
28 except:
519e45d9539f Uploaded
cmmt
parents:
diff changeset
29 return []; # exit, since we don't have 9 features
519e45d9539f Uploaded
cmmt
parents:
diff changeset
30
519e45d9539f Uploaded
cmmt
parents:
diff changeset
31 # handle the gffSeqName
519e45d9539f Uploaded
cmmt
parents:
diff changeset
32 try:
519e45d9539f Uploaded
cmmt
parents:
diff changeset
33 chrNo = int(re.findall(r'[0-9]+', gffSeqName)[0]);
519e45d9539f Uploaded
cmmt
parents:
diff changeset
34 except:
519e45d9539f Uploaded
cmmt
parents:
diff changeset
35 return [];
519e45d9539f Uploaded
cmmt
parents:
diff changeset
36
519e45d9539f Uploaded
cmmt
parents:
diff changeset
37 # handle gffStart
519e45d9539f Uploaded
cmmt
parents:
diff changeset
38 try:
519e45d9539f Uploaded
cmmt
parents:
diff changeset
39 probeStart = int(gffStart);
519e45d9539f Uploaded
cmmt
parents:
diff changeset
40 except ValueError:
519e45d9539f Uploaded
cmmt
parents:
diff changeset
41 return []; # exit, invalid gffStart
519e45d9539f Uploaded
cmmt
parents:
diff changeset
42
519e45d9539f Uploaded
cmmt
parents:
diff changeset
43 # handle gffEnd
519e45d9539f Uploaded
cmmt
parents:
diff changeset
44 try:
519e45d9539f Uploaded
cmmt
parents:
diff changeset
45 probeEnd = int(gffEnd);
519e45d9539f Uploaded
cmmt
parents:
diff changeset
46 except ValueError:
519e45d9539f Uploaded
cmmt
parents:
diff changeset
47 return []; # exit, invalid gffEnd
519e45d9539f Uploaded
cmmt
parents:
diff changeset
48
519e45d9539f Uploaded
cmmt
parents:
diff changeset
49 # handle gffScr
519e45d9539f Uploaded
cmmt
parents:
diff changeset
50 try:
519e45d9539f Uploaded
cmmt
parents:
diff changeset
51 probeScr = float(gffScr);
519e45d9539f Uploaded
cmmt
parents:
diff changeset
52 except ValueError:
519e45d9539f Uploaded
cmmt
parents:
diff changeset
53 return []; # exit, invalid gffScr
519e45d9539f Uploaded
cmmt
parents:
diff changeset
54
519e45d9539f Uploaded
cmmt
parents:
diff changeset
55 # if the input string was well-formed, return the extracted features
519e45d9539f Uploaded
cmmt
parents:
diff changeset
56 res = [chrNo, probeStart, probeEnd, probeScr];
519e45d9539f Uploaded
cmmt
parents:
diff changeset
57 return res; # return feature set (if any was found in this record)
519e45d9539f Uploaded
cmmt
parents:
diff changeset
58
519e45d9539f Uploaded
cmmt
parents:
diff changeset
59
519e45d9539f Uploaded
cmmt
parents:
diff changeset
60 # Define Constants
519e45d9539f Uploaded
cmmt
parents:
diff changeset
61 tickPlotAdj = 0.5; # adjust tick marks on x-axis such that they are exactly at the bin boundary (and not in the bin center)
519e45d9539f Uploaded
cmmt
parents:
diff changeset
62
519e45d9539f Uploaded
cmmt
parents:
diff changeset
63 # Initialize Variables
519e45d9539f Uploaded
cmmt
parents:
diff changeset
64 feaNames = [];
519e45d9539f Uploaded
cmmt
parents:
diff changeset
65 feaChrNoList = [];
519e45d9539f Uploaded
cmmt
parents:
diff changeset
66 feaStrandList = [];
519e45d9539f Uploaded
cmmt
parents:
diff changeset
67 feaStartList = [];
519e45d9539f Uploaded
cmmt
parents:
diff changeset
68 feaEndList = [];
519e45d9539f Uploaded
cmmt
parents:
diff changeset
69
519e45d9539f Uploaded
cmmt
parents:
diff changeset
70 gffChrNoList = [];
519e45d9539f Uploaded
cmmt
parents:
diff changeset
71 gffProbeCenterList = [];
519e45d9539f Uploaded
cmmt
parents:
diff changeset
72 gffMatScrList = [];
519e45d9539f Uploaded
cmmt
parents:
diff changeset
73
519e45d9539f Uploaded
cmmt
parents:
diff changeset
74 # get command line arguments
519e45d9539f Uploaded
cmmt
parents:
diff changeset
75 gffFileName = sys.argv[1];
519e45d9539f Uploaded
cmmt
parents:
diff changeset
76 feaFileName = sys.argv[2];
519e45d9539f Uploaded
cmmt
parents:
diff changeset
77 outFileFormat = sys.argv[3];
519e45d9539f Uploaded
cmmt
parents:
diff changeset
78 stepWidth = int(sys.argv[4]);
519e45d9539f Uploaded
cmmt
parents:
diff changeset
79 upStreamStretch = int(sys.argv[5]); # in bp
519e45d9539f Uploaded
cmmt
parents:
diff changeset
80 maxX = int(sys.argv[6]); # in bp, if -1 maxX <= max(feature length), see below
519e45d9539f Uploaded
cmmt
parents:
diff changeset
81 tickSpacing = int(sys.argv[7]); # in bp
519e45d9539f Uploaded
cmmt
parents:
diff changeset
82 plotTitle = sys.argv[8];
519e45d9539f Uploaded
cmmt
parents:
diff changeset
83 outFileName = sys.argv[9];
519e45d9539f Uploaded
cmmt
parents:
diff changeset
84
519e45d9539f Uploaded
cmmt
parents:
diff changeset
85 # Read transcript information
519e45d9539f Uploaded
cmmt
parents:
diff changeset
86 feaFileReader = csv.reader(open(feaFileName, 'rb'), delimiter = '\t');
519e45d9539f Uploaded
cmmt
parents:
diff changeset
87 for row in feaFileReader:
519e45d9539f Uploaded
cmmt
parents:
diff changeset
88 feaName, feaChrNo, feaStrand, feaStart, feaEnd = row;
519e45d9539f Uploaded
cmmt
parents:
diff changeset
89 feaNames.append(feaName);
519e45d9539f Uploaded
cmmt
parents:
diff changeset
90 feaChrNoList.append(int(feaChrNo));
519e45d9539f Uploaded
cmmt
parents:
diff changeset
91 feaStrandList.append(int(feaStrand));
519e45d9539f Uploaded
cmmt
parents:
diff changeset
92 feaStartList.append(int(feaStart));
519e45d9539f Uploaded
cmmt
parents:
diff changeset
93 feaEndList.append(int(feaEnd));
519e45d9539f Uploaded
cmmt
parents:
diff changeset
94
519e45d9539f Uploaded
cmmt
parents:
diff changeset
95 # Read enrichment scores
519e45d9539f Uploaded
cmmt
parents:
diff changeset
96 gffFileHandle = open(gffFileName, "rU");
519e45d9539f Uploaded
cmmt
parents:
diff changeset
97 for row in gffFileHandle:
519e45d9539f Uploaded
cmmt
parents:
diff changeset
98 cache = extractGffFeatures(row);
519e45d9539f Uploaded
cmmt
parents:
diff changeset
99 if len(cache) == 4: # if curr row wasn't well-formed, skip row
519e45d9539f Uploaded
cmmt
parents:
diff changeset
100 chrNo, probeStart, probeEnd, probeScr = cache;
519e45d9539f Uploaded
cmmt
parents:
diff changeset
101 gffChrNoList.append(chrNo);
519e45d9539f Uploaded
cmmt
parents:
diff changeset
102 gffProbeCenterList.append(int(float(probeEnd - probeStart)/2.0 + probeStart)); # calc the center pos of each probe
519e45d9539f Uploaded
cmmt
parents:
diff changeset
103 gffMatScrList.append(probeScr);
519e45d9539f Uploaded
cmmt
parents:
diff changeset
104
519e45d9539f Uploaded
cmmt
parents:
diff changeset
105 # convert numerical lists into vectors
519e45d9539f Uploaded
cmmt
parents:
diff changeset
106 feaChrNos = np.array(feaChrNoList);
519e45d9539f Uploaded
cmmt
parents:
diff changeset
107 feaStrands = np.array(feaStrandList);
519e45d9539f Uploaded
cmmt
parents:
diff changeset
108 feaStarts = np.array(feaStartList);
519e45d9539f Uploaded
cmmt
parents:
diff changeset
109 feaEnds = np.array(feaEndList);
519e45d9539f Uploaded
cmmt
parents:
diff changeset
110
519e45d9539f Uploaded
cmmt
parents:
diff changeset
111 gfChrNos = np.array(gffChrNoList);
519e45d9539f Uploaded
cmmt
parents:
diff changeset
112 gfProbeLocs = np.array(gffProbeCenterList);
519e45d9539f Uploaded
cmmt
parents:
diff changeset
113 gfMatScrs = np.array(gffMatScrList);
519e45d9539f Uploaded
cmmt
parents:
diff changeset
114
519e45d9539f Uploaded
cmmt
parents:
diff changeset
115 # preallocate the results array, bins and 1 extra cols for feature length
519e45d9539f Uploaded
cmmt
parents:
diff changeset
116 avgFeatureMatScrs = np.empty((len(feaStarts),\
519e45d9539f Uploaded
cmmt
parents:
diff changeset
117 (int(math.ceil(float(max(feaEnds - feaStarts))/float(stepWidth)) + upStreamStretch/stepWidth + 1))),\
519e45d9539f Uploaded
cmmt
parents:
diff changeset
118 );
519e45d9539f Uploaded
cmmt
parents:
diff changeset
119 avgFeatureMatScrs[:] = np.NAN;
519e45d9539f Uploaded
cmmt
parents:
diff changeset
120
519e45d9539f Uploaded
cmmt
parents:
diff changeset
121 featureCounter = 0;
519e45d9539f Uploaded
cmmt
parents:
diff changeset
122
519e45d9539f Uploaded
cmmt
parents:
diff changeset
123 for chr in range(min(feaChrNos),max(feaChrNos) + 1):
519e45d9539f Uploaded
cmmt
parents:
diff changeset
124 #get all rows from the feature file that contain information about the i'th chromosome
519e45d9539f Uploaded
cmmt
parents:
diff changeset
125 currChrIdxs = (feaChrNos == chr);
519e45d9539f Uploaded
cmmt
parents:
diff changeset
126 currStrands = feaStrands[currChrIdxs];
519e45d9539f Uploaded
cmmt
parents:
diff changeset
127 currStarts = feaStarts[currChrIdxs];
519e45d9539f Uploaded
cmmt
parents:
diff changeset
128 currEnds = feaEnds[currChrIdxs];
519e45d9539f Uploaded
cmmt
parents:
diff changeset
129
519e45d9539f Uploaded
cmmt
parents:
diff changeset
130 #get all rows from ChIP-* data sets
519e45d9539f Uploaded
cmmt
parents:
diff changeset
131 currGfChrIdxs = (gfChrNos == chr);
519e45d9539f Uploaded
cmmt
parents:
diff changeset
132 currGfProbeLocs = gfProbeLocs[currGfChrIdxs];
519e45d9539f Uploaded
cmmt
parents:
diff changeset
133 currGfScrs = gfMatScrs[currGfChrIdxs];
519e45d9539f Uploaded
cmmt
parents:
diff changeset
134
519e45d9539f Uploaded
cmmt
parents:
diff changeset
135 # iterate over all features of the current chromosome
519e45d9539f Uploaded
cmmt
parents:
diff changeset
136 for j in range(0,len(currStarts)):
519e45d9539f Uploaded
cmmt
parents:
diff changeset
137 feaStrand = currStrands[j];
519e45d9539f Uploaded
cmmt
parents:
diff changeset
138 feaStart = currStarts[j];
519e45d9539f Uploaded
cmmt
parents:
diff changeset
139 feaEnd = currEnds[j];
519e45d9539f Uploaded
cmmt
parents:
diff changeset
140
519e45d9539f Uploaded
cmmt
parents:
diff changeset
141 #calc the corresponding probe positions for the feature start
519e45d9539f Uploaded
cmmt
parents:
diff changeset
142 if feaStrand == 1: # +strand
519e45d9539f Uploaded
cmmt
parents:
diff changeset
143 refFeaSt = 0;
519e45d9539f Uploaded
cmmt
parents:
diff changeset
144 while feaStart >= currGfProbeLocs[refFeaSt]:
519e45d9539f Uploaded
cmmt
parents:
diff changeset
145 refFeaSt += 1;
519e45d9539f Uploaded
cmmt
parents:
diff changeset
146 else: # -strand
519e45d9539f Uploaded
cmmt
parents:
diff changeset
147 refFeaSt = 0;
519e45d9539f Uploaded
cmmt
parents:
diff changeset
148 while feaEnd >= currGfProbeLocs[refFeaSt]:
519e45d9539f Uploaded
cmmt
parents:
diff changeset
149 refFeaSt += 1;
519e45d9539f Uploaded
cmmt
parents:
diff changeset
150 #now refFeaSt points to the correct probe according to strand orientation
519e45d9539f Uploaded
cmmt
parents:
diff changeset
151
519e45d9539f Uploaded
cmmt
parents:
diff changeset
152 #calc the avg probe scores per bin
519e45d9539f Uploaded
cmmt
parents:
diff changeset
153 if feaStrand == 1:
519e45d9539f Uploaded
cmmt
parents:
diff changeset
154 feaChunkStart = feaStart; # bp level pointer
519e45d9539f Uploaded
cmmt
parents:
diff changeset
155 refChunkStart = refFeaSt; # probe level pointer
519e45d9539f Uploaded
cmmt
parents:
diff changeset
156 refChunkEnd = refFeaSt; # probe level pointer
519e45d9539f Uploaded
cmmt
parents:
diff changeset
157
519e45d9539f Uploaded
cmmt
parents:
diff changeset
158 # determine values for the upstream bins
519e45d9539f Uploaded
cmmt
parents:
diff changeset
159 for k in range(1,upStreamStretch/stepWidth + 1):
519e45d9539f Uploaded
cmmt
parents:
diff changeset
160 feaChunkEnd = feaStart - k * stepWidth; # bp level pointer
519e45d9539f Uploaded
cmmt
parents:
diff changeset
161
519e45d9539f Uploaded
cmmt
parents:
diff changeset
162 # find the correct ref for the current feaChunkEnd; catch potential IndexOutOfBoundEx when upstream region too close to chr boundary
519e45d9539f Uploaded
cmmt
parents:
diff changeset
163 try:
519e45d9539f Uploaded
cmmt
parents:
diff changeset
164 while feaChunkEnd <= currGfProbeLocs[refChunkEnd]:
519e45d9539f Uploaded
cmmt
parents:
diff changeset
165 refChunkEnd -= 1;
519e45d9539f Uploaded
cmmt
parents:
diff changeset
166 except:
519e45d9539f Uploaded
cmmt
parents:
diff changeset
167 pass; # don't handle the exception further
519e45d9539f Uploaded
cmmt
parents:
diff changeset
168
519e45d9539f Uploaded
cmmt
parents:
diff changeset
169 #calc probe mean
519e45d9539f Uploaded
cmmt
parents:
diff changeset
170 avgFeatureMatScrs[featureCounter, upStreamStretch/stepWidth - k] = np.mean(currGfScrs[refChunkEnd:refChunkStart + 1]);
519e45d9539f Uploaded
cmmt
parents:
diff changeset
171
519e45d9539f Uploaded
cmmt
parents:
diff changeset
172 #adjust pointers
519e45d9539f Uploaded
cmmt
parents:
diff changeset
173 refChunkStart = refChunkEnd;
519e45d9539f Uploaded
cmmt
parents:
diff changeset
174
519e45d9539f Uploaded
cmmt
parents:
diff changeset
175 # determine values for bins in feature body
519e45d9539f Uploaded
cmmt
parents:
diff changeset
176 for k in range(1, int(math.ceil(float(feaEnd - feaStart)/float(stepWidth)) + 1)):
519e45d9539f Uploaded
cmmt
parents:
diff changeset
177 feaChunkEnd = feaStart + k*stepWidth; # bp level pointer
519e45d9539f Uploaded
cmmt
parents:
diff changeset
178
519e45d9539f Uploaded
cmmt
parents:
diff changeset
179 try:
519e45d9539f Uploaded
cmmt
parents:
diff changeset
180 while feaChunkEnd >= currGfProbeLocs[refChunkEnd]:
519e45d9539f Uploaded
cmmt
parents:
diff changeset
181 refChunkEnd += 1;
519e45d9539f Uploaded
cmmt
parents:
diff changeset
182 except:
519e45d9539f Uploaded
cmmt
parents:
diff changeset
183 pass; # don't handle the exception further
519e45d9539f Uploaded
cmmt
parents:
diff changeset
184
519e45d9539f Uploaded
cmmt
parents:
diff changeset
185 # calc probe mean
519e45d9539f Uploaded
cmmt
parents:
diff changeset
186 avgFeatureMatScrs[featureCounter, k - 1 + upStreamStretch/stepWidth] = np.mean(currGfScrs[refChunkStart:refChunkEnd + 1]);
519e45d9539f Uploaded
cmmt
parents:
diff changeset
187
519e45d9539f Uploaded
cmmt
parents:
diff changeset
188 # adjust chunk pointers
519e45d9539f Uploaded
cmmt
parents:
diff changeset
189 refChunkStart = refChunkEnd;
519e45d9539f Uploaded
cmmt
parents:
diff changeset
190
519e45d9539f Uploaded
cmmt
parents:
diff changeset
191 # write feature length in last col of the array
519e45d9539f Uploaded
cmmt
parents:
diff changeset
192 avgFeatureMatScrs[featureCounter, -1] = feaEnd - feaStart;
519e45d9539f Uploaded
cmmt
parents:
diff changeset
193 featureCounter += 1;
519e45d9539f Uploaded
cmmt
parents:
diff changeset
194 else: # -1 strand
519e45d9539f Uploaded
cmmt
parents:
diff changeset
195 feaChunkStart = feaEnd; # bp level pointer
519e45d9539f Uploaded
cmmt
parents:
diff changeset
196 refChunkStart = refFeaSt; # probe level pointer
519e45d9539f Uploaded
cmmt
parents:
diff changeset
197 refChunkEnd = refFeaSt; # probe level pointer
519e45d9539f Uploaded
cmmt
parents:
diff changeset
198
519e45d9539f Uploaded
cmmt
parents:
diff changeset
199 # determine values for the upStream region bins
519e45d9539f Uploaded
cmmt
parents:
diff changeset
200 for k in range(1,upStreamStretch/stepWidth + 1):
519e45d9539f Uploaded
cmmt
parents:
diff changeset
201 feaChunkEnd = feaEnd + k*stepWidth; # bp level pointer
519e45d9539f Uploaded
cmmt
parents:
diff changeset
202
519e45d9539f Uploaded
cmmt
parents:
diff changeset
203 # find the ref for the current feaChunkEnd, catch potential IndexOutOfBoundEx when upstream region too close to chr boundary
519e45d9539f Uploaded
cmmt
parents:
diff changeset
204 try:
519e45d9539f Uploaded
cmmt
parents:
diff changeset
205 while feaChunkEnd >= currGfProbeLocs[refChunkEnd]:
519e45d9539f Uploaded
cmmt
parents:
diff changeset
206 refChunkEnd += 1;
519e45d9539f Uploaded
cmmt
parents:
diff changeset
207 except:
519e45d9539f Uploaded
cmmt
parents:
diff changeset
208 pass;
519e45d9539f Uploaded
cmmt
parents:
diff changeset
209
519e45d9539f Uploaded
cmmt
parents:
diff changeset
210 # calc probe mean
519e45d9539f Uploaded
cmmt
parents:
diff changeset
211 avgFeatureMatScrs[featureCounter, upStreamStretch/stepWidth - k] = np.mean(currGfScrs[refChunkStart:refChunkEnd+1]);
519e45d9539f Uploaded
cmmt
parents:
diff changeset
212
519e45d9539f Uploaded
cmmt
parents:
diff changeset
213 # adjust chunk pointers
519e45d9539f Uploaded
cmmt
parents:
diff changeset
214 refChunkStart = refChunkEnd;
519e45d9539f Uploaded
cmmt
parents:
diff changeset
215
519e45d9539f Uploaded
cmmt
parents:
diff changeset
216 # determine values for bins in feature body
519e45d9539f Uploaded
cmmt
parents:
diff changeset
217 for k in range(1, int(math.ceil(float(feaEnd - feaStart)/float(stepWidth)) + 1)):
519e45d9539f Uploaded
cmmt
parents:
diff changeset
218 feaChunkEnd = feaEnd - k*stepWidth; # bp level pointer
519e45d9539f Uploaded
cmmt
parents:
diff changeset
219
519e45d9539f Uploaded
cmmt
parents:
diff changeset
220 try:
519e45d9539f Uploaded
cmmt
parents:
diff changeset
221 while feaChunkEnd <= currGfProbeLocs[refChunkEnd]:
519e45d9539f Uploaded
cmmt
parents:
diff changeset
222 refChunkEnd -= 1;
519e45d9539f Uploaded
cmmt
parents:
diff changeset
223 except:
519e45d9539f Uploaded
cmmt
parents:
diff changeset
224 pass;
519e45d9539f Uploaded
cmmt
parents:
diff changeset
225
519e45d9539f Uploaded
cmmt
parents:
diff changeset
226 # calc probe mean
519e45d9539f Uploaded
cmmt
parents:
diff changeset
227 avgFeatureMatScrs[featureCounter, k-1 + upStreamStretch/stepWidth] = np.mean(currGfScrs[refChunkEnd:refChunkStart+1]);
519e45d9539f Uploaded
cmmt
parents:
diff changeset
228
519e45d9539f Uploaded
cmmt
parents:
diff changeset
229 # adjust chunk pointers
519e45d9539f Uploaded
cmmt
parents:
diff changeset
230 refChunkStart = refChunkEnd;
519e45d9539f Uploaded
cmmt
parents:
diff changeset
231
519e45d9539f Uploaded
cmmt
parents:
diff changeset
232 # write feature length in last col of the array
519e45d9539f Uploaded
cmmt
parents:
diff changeset
233 avgFeatureMatScrs[featureCounter, -1] = feaEnd - feaStart;
519e45d9539f Uploaded
cmmt
parents:
diff changeset
234 featureCounter += 1;
519e45d9539f Uploaded
cmmt
parents:
diff changeset
235
519e45d9539f Uploaded
cmmt
parents:
diff changeset
236
519e45d9539f Uploaded
cmmt
parents:
diff changeset
237 # assign names and data format to all cols of the results array and sort array by feature length (last column)
519e45d9539f Uploaded
cmmt
parents:
diff changeset
238 sortByLengthCol = 'col' + str(np.size(avgFeatureMatScrs,1)-1);
519e45d9539f Uploaded
cmmt
parents:
diff changeset
239 matDtype = {'names':['col%i'%i for i in range(np.size(avgFeatureMatScrs,1))],'formats':(np.size(avgFeatureMatScrs,1))*[np.float]};
519e45d9539f Uploaded
cmmt
parents:
diff changeset
240 avgFeatureMatScrs.dtype = matDtype;
519e45d9539f Uploaded
cmmt
parents:
diff changeset
241 avgFeatureMatScrs.sort(axis = 0, order = sortByLengthCol);
519e45d9539f Uploaded
cmmt
parents:
diff changeset
242
519e45d9539f Uploaded
cmmt
parents:
diff changeset
243 # generate colormap
519e45d9539f Uploaded
cmmt
parents:
diff changeset
244 BYcmapData = {'red': ((0.0, 0.0, 0.094), (0.5, 0.0, 0.0), (1.0, 0.992, 1.0)), 'green': ((0.0, 0.0,0.658), (0.5, 0.0, 0.0), (1.0, 0.996, 1.0)), 'blue': ((0.0, 1.0, 0.828), (0.5, 0.0, 0.0), (1.0, 0.0, 0.0))};
519e45d9539f Uploaded
cmmt
parents:
diff changeset
245 BYcmap = matplotlib.colors.LinearSegmentedColormap('BYcmap', BYcmapData,9);
519e45d9539f Uploaded
cmmt
parents:
diff changeset
246
519e45d9539f Uploaded
cmmt
parents:
diff changeset
247 lowerHinge = matplotlib.mlab.prctile(gfMatScrs, 25.0);
519e45d9539f Uploaded
cmmt
parents:
diff changeset
248 upperHinge = matplotlib.mlab.prctile(gfMatScrs, 75.0);
519e45d9539f Uploaded
cmmt
parents:
diff changeset
249 cmLim = (upperHinge - lowerHinge) * 1.5 + upperHinge; # upper inner fence
519e45d9539f Uploaded
cmmt
parents:
diff changeset
250
519e45d9539f Uploaded
cmmt
parents:
diff changeset
251 # remove custom array dtype used for sorting and plot avgScrs (without the last col which stores the length info)
519e45d9539f Uploaded
cmmt
parents:
diff changeset
252 imgplot = imshow(avgFeatureMatScrs.view(np.float)[:,0:-1], aspect='auto', interpolation='nearest', origin='lower', vmin = -cmLim, vmax = cmLim);
519e45d9539f Uploaded
cmmt
parents:
diff changeset
253 axvline(x = int(float(upStreamStretch)/float(stepWidth)) - tickPlotAdj, color = 'w', linewidth = 1);
519e45d9539f Uploaded
cmmt
parents:
diff changeset
254
519e45d9539f Uploaded
cmmt
parents:
diff changeset
255 # if maxX wasn't set by hand, set it to max feature length and set x-coord accordingly
519e45d9539f Uploaded
cmmt
parents:
diff changeset
256 if maxX == -1:
519e45d9539f Uploaded
cmmt
parents:
diff changeset
257 maxX = max(feaEnds - feaStarts);
519e45d9539f Uploaded
cmmt
parents:
diff changeset
258 imgplot.axes.set_xbound(-tickPlotAdj, int(float(maxX)/float(stepWidth)) + int(float(upStreamStretch)/float(stepWidth)) - tickPlotAdj);
519e45d9539f Uploaded
cmmt
parents:
diff changeset
259 else:
519e45d9539f Uploaded
cmmt
parents:
diff changeset
260 imgplot.axes.set_xbound(-tickPlotAdj, int(float(maxX)/float(stepWidth)) - tickPlotAdj);
519e45d9539f Uploaded
cmmt
parents:
diff changeset
261
519e45d9539f Uploaded
cmmt
parents:
diff changeset
262 xTicks = [k * int(float(tickSpacing)/float(stepWidth)) + int(float(upStreamStretch)/float(stepWidth)) - tickPlotAdj for k in range(0, int(math.ceil(float(maxX)/float(tickSpacing)))+1)];
519e45d9539f Uploaded
cmmt
parents:
diff changeset
263 xTicks.insert(-1,-tickPlotAdj);
519e45d9539f Uploaded
cmmt
parents:
diff changeset
264 xTicksLabel = [k * tickSpacing for k in range(0, int(math.ceil(float(maxX)/float(tickSpacing)))+1)];
519e45d9539f Uploaded
cmmt
parents:
diff changeset
265 xTicksLabel.insert(-1, str(-upStreamStretch));
519e45d9539f Uploaded
cmmt
parents:
diff changeset
266 imgplot.axes.set_xticks(xTicks);
519e45d9539f Uploaded
cmmt
parents:
diff changeset
267 imgplot.axes.set_xticklabels(xTicksLabel);
519e45d9539f Uploaded
cmmt
parents:
diff changeset
268
519e45d9539f Uploaded
cmmt
parents:
diff changeset
269 imgplot.axes.set_title(plotTitle);
519e45d9539f Uploaded
cmmt
parents:
diff changeset
270 imgplot.axes.set_xlabel('Distance from feature start (bp)');
519e45d9539f Uploaded
cmmt
parents:
diff changeset
271 imgplot.axes.set_ylabel('');
519e45d9539f Uploaded
cmmt
parents:
diff changeset
272 imgplot.axes.set_yticks([]);
519e45d9539f Uploaded
cmmt
parents:
diff changeset
273
519e45d9539f Uploaded
cmmt
parents:
diff changeset
274 imgplot.set_cmap(BYcmap);
519e45d9539f Uploaded
cmmt
parents:
diff changeset
275 colorbar();
519e45d9539f Uploaded
cmmt
parents:
diff changeset
276 show();
519e45d9539f Uploaded
cmmt
parents:
diff changeset
277
519e45d9539f Uploaded
cmmt
parents:
diff changeset
278 savefig('chromatra_l_tmp_plot', format=outFileFormat);
519e45d9539f Uploaded
cmmt
parents:
diff changeset
279 data = file('chromatra_l_tmp_plot', 'rb').read();
519e45d9539f Uploaded
cmmt
parents:
diff changeset
280 fp = open(outFileName, 'wb');
519e45d9539f Uploaded
cmmt
parents:
diff changeset
281 fp.write(data);
519e45d9539f Uploaded
cmmt
parents:
diff changeset
282 fp.close();
519e45d9539f Uploaded
cmmt
parents:
diff changeset
283 os.remove('chromatra_l_tmp_plot');