7 from cistematic.genomes import Genome
15 print "%prog: version 2.1"
21 usage = "usage: python %prog genome outimage gofileroot1 title1 cohortsize1 [gofileroot2 title2 cohortsize2 ...] [--fontsize pts] [--length in] [--width in]"
23 parser = optparse.OptionParser(usage=usage)
24 parser.add_option("--fontsize", type="int", dest="fontSize")
25 parser.add_option("--length", type="int", dest="length")
26 parser.add_option("--width", type="int", dest="width")
27 parser.set_defaults(fontSize=5, length=10, width=7)
28 (options, args) = parser.parse_args(argv[1:])
37 conditionList = args[2:]
38 conditions = len(conditionList) / 3
41 for index in range(conditions):
42 conditionIndex = index * 3
43 fileroots.append(conditionList[conditionIndex])
44 titles.append((conditionList[conditionIndex + 1], "(%s)" % conditionList[conditionIndex + 2]))
46 getgosig(genome, imagename, fileroots, titles, options.fontSize, options.length, options.width)
49 def getgosig(genome, imagename, fileroots=[], titles=[], fontSize=5, length=10, width=7):
51 allgodesc = hg.allGOterms()
59 htmlname = imagename[:-4] + ".html"
70 for sigfile in fileroots:
71 infile = open(rootdir + sigfile + ".gosig", "r")
73 if "depleted" in line:
76 fields = line.split("\t")
77 if fields[0] not in goterms:
78 goterms.append(fields[0])
79 goscores[fields[0]] = []
80 numgenes[fields[0]] = []
81 possiblegenes[fields[0]] = 0
83 if float(fields[3]) > highestPval:
84 highestPval = float(fields[3])
86 if float(fields[3]) < lowestPval:
87 lowestPval = float(fields[3])
92 boundaryScore = score = -1 * log(highestPval) / (2.0 * ceiling) + 0.49
95 cdict = {"red": ((0.0, 1.0, 1.0), (boundaryScore, 1.0, 0.1), (1.0, 1.0, 1.0)),
96 "green": ((0.0, 1.0, 1.0), (boundaryScore, 1.0, 0.1), (1.0, 1.0, 1.0)),
97 "blue": ((0.0, 1.0, 1.0), (boundaryScore, 1.0, 0.75), (1.0, 0.0, 0.0))
100 mymap = matplotlib.colors.LinearSegmentedColormap("my_colormap", cdict, 1024)
103 for zfile in fileroots:
104 infile = open(rootdir + zfile + ".gozscore", "r")
106 fields = line.split()
108 if fields[0] not in goterms:
111 score = -1 * log(float(fields[7])) / (2.0 * ceiling)
122 goscores[fields[0]].append(score)
123 numgenes[fields[0]].append(fields[1])
124 possiblegenes[fields[0]] = int(fields[4])
126 goindex /= len(fileroots)
128 gokeys = goscores.keys()
131 gosortarray.append(goscores[term] + [term])
135 htmlfile = open(htmlname, "w")
136 htmlfile.write('<html><head><title>GO Analysis</title></head><body><table border="1">')
137 htmlfile.write("<tr><th>Description</th><th>possible</th>")
139 htmlfile.write("<th>%s<br>%s</th>" % entry)
141 htmlfile.write("</tr>\n")
144 for entry in gosortarray:
146 outline = "%s:\t" % term
147 for entry in goscores[term]:
148 outline += str(round(entry, 4)) + "\t"
151 htmlLine = "<tr><th>%s</th><th>%d</th>" % (allgodesc[term], possiblegenes[term])
153 for fileroot in fileroots:
154 gofile = fileroot + "." + term[3:]
155 ngene = numgenes[term][index]
156 if os.path.exists(gofile):
157 htmlLine += '<td><a href="%s">%s</a></td>' % (gofile, ngene)
159 htmlLine += "<td>%s</td>" % (ngene)
163 tableLines.append(htmlLine + "</tr>\n")
164 flatArray.append(goscores[term])
165 godesc.append(allgodesc[term])
168 for line in tableLines:
171 htmlfile.write("<tr><th>Cohort Size:</th>")
172 htmlfile.write("</tr>\n")
173 htmlfile.write("</table></body></html>")
175 figure(figsize=(length, width))
176 myaxe = axes([0.3, 0.1, 0.55, 0.75])
181 c = pcolor(Z, cmap=cm.gray, vmin=0.0, vmax=1.0)
183 c = pcolor(Z, cmap=mymap, vmin=0.0, vmax=1.0)
188 ind = arange(len(fileroots))
195 pcolorAxes = c.get_axes()
196 for entry in gosortarray:
199 for genenum in numgenes[term]:
200 if len(genenum) == 1:
201 genenum = " " + genenum
202 elif len(genenum) == 2:
203 genenum = " " + genenum
205 pcolorAxes.text(coordx, coordy, genenum, fontsize=fontSize)
211 for (line1,line2) in titles:
212 pcolorAxes.text(coordx + 0.1, coordy + 3 * deltaY + 0.5, line1, fontsize=int(fontSize*1.5))
213 pcolorAxes.text(coordx + 0.1, coordy + deltaY, line2, fontsize=int(fontSize*1.5))
216 setp(gca(), "xticks", [])
217 setp(gca(), "xticklabels", [])
218 setp(gca(), "yticks", arange(len(godesc)))
219 setp(gca(), "yticklabels", godesc)
220 locs, labels = yticks()
221 setp(labels, fontsize=fontSize)
222 setp(labels, verticalalignment="bottom")
223 setp(gca(), "ylim", [0, len(godesc)])
225 figtext(0.3,0.02, str(goindex - len(gokeys)) + " additional GO Terms below threshold of significance", fontsize=fontSize*2)
227 d = colorbar(orientation="vertical", drawedges=False)
228 for t in d.ax.get_yticklabels():
231 locs, labels = yticks()
232 setp(labels, fontsize=5)
233 pcolorAxes.text(conditions + 1,len(godesc), str(lowestPval), fontsize=fontSize*2)
234 pcolorAxes.text(conditions + 1,boundaryScore * len(godesc), str(highestPval), fontsize=fontSize*2)
236 savefig(imagename, dpi=250)
240 if __name__ == "__main__":