|
| 1 | +#!/usr/bin/env python |
| 2 | + |
| 3 | +import os |
| 4 | +import sys |
| 5 | +import math |
| 6 | + |
| 7 | +import numpy as np |
| 8 | +import matplotlib as mpl |
| 9 | +mpl.use('agg') |
| 10 | +import matplotlib.pyplot as plt |
| 11 | +import matplotlib.mlab as mlab |
| 12 | +import matplotlib as mpl |
| 13 | + |
| 14 | + |
| 15 | +import pylab as pl |
| 16 | + |
| 17 | +import subprocess |
| 18 | + |
| 19 | +from hagfishUtils import * |
| 20 | + |
| 21 | +## Arguments: General options |
| 22 | +parser = getHagfishOptparser() |
| 23 | +parser.add_option('-S', dest='score', action='store_true', |
| 24 | + help='plot the hagfish score' ) |
| 25 | + |
| 26 | +addPlotParameters(parser) |
| 27 | +options, args = parser.parse_args() |
| 28 | + |
| 29 | +l = getLogger('main', options.verbose) |
| 30 | + |
| 31 | +l.info("starting %s" % sys.argv[0]) |
| 32 | + |
| 33 | +seqId = args[0] |
| 34 | + |
| 35 | +indir1 = args[1] |
| 36 | +indir2 = args[2] |
| 37 | + |
| 38 | +infile1 = os.path.join( |
| 39 | + indir1, 'combined', |
| 40 | + '%s.combined.coverage.npz' % seqId) |
| 41 | +l.info("input file 1: %s" % infile1) |
| 42 | +infile2 = os.path.join( |
| 43 | + indir2, 'combined', |
| 44 | + '%s.combined.coverage.npz' % seqId) |
| 45 | +l.info("input file 2: %s" % infile2) |
| 46 | + |
| 47 | +#load the coverage plots |
| 48 | +data = hagfishData(options, args, inputFile = infile1) |
| 49 | +data2 = hagfishData(options, args, inputFile = infile2) |
| 50 | + |
| 51 | +#prepare the plot |
| 52 | +ym1 = quant(data.ok, options.yfrac) |
| 53 | +ym2 = quant(data2.ok, options.yfrac) |
| 54 | + |
| 55 | +ymax = max(ym1, ym2) |
| 56 | + |
| 57 | +l.critical('y1 %s %s' % (ym1, data.median)) |
| 58 | +l.critical('y2 %s %s' % (ym2, data2.median)) |
| 59 | +l.critical('ym %s' % (ymax)) |
| 60 | + |
| 61 | +if indir1[-1] == '/': indir1 = indir1[:-1] |
| 62 | +if indir2[-1] == '/': indir2 = indir2[:-1] |
| 63 | + |
| 64 | +title1 = os.path.basename(indir1) |
| 65 | +title2 = os.path.basename(indir2) |
| 66 | + |
| 67 | +plot = hagfishPlot( |
| 68 | + options, data, |
| 69 | + data2=data2, ymax = ymax, |
| 70 | + title='comparative plot %s vs %s' % (title1, title2)) |
| 71 | + |
| 72 | +class plotter(hagfishPlotBand): |
| 73 | + |
| 74 | + def plotBand(self): |
| 75 | + self.l.debug("Start plotting") |
| 76 | + yc = self.yCorrection |
| 77 | + |
| 78 | + median1 = self.data.median |
| 79 | + median2 = self.data2.median |
| 80 | + |
| 81 | + self.ax.fill_between( |
| 82 | + self.locx, |
| 83 | + self.zero + yc, |
| 84 | + (self.d1.low) + yc, |
| 85 | + color = COLOR3) |
| 86 | + |
| 87 | + self.ax.fill_between( |
| 88 | + self.locx, |
| 89 | + self.d1.low + yc, |
| 90 | + self.d1.low + self.d1.ok + yc, |
| 91 | + color = COLOR1) |
| 92 | + |
| 93 | + self.ax.fill_between( |
| 94 | + self.locx, |
| 95 | + self.d1.low + self.d1.ok + yc, |
| 96 | + self.d1.low + self.d1.ok + self.d1.high + yc, |
| 97 | + color = COLOR2) |
| 98 | + |
| 99 | + self.ax.fill_between( |
| 100 | + self.locx, |
| 101 | + self.zero + yc, |
| 102 | + -self.d2.low + yc, |
| 103 | + color = COLOR3) |
| 104 | + |
| 105 | + self.ax.fill_between( |
| 106 | + self.locx, |
| 107 | + -self.d2.low + yc, |
| 108 | + -self.d2.low - self.d2.ok + yc, |
| 109 | + color = COLOR1) |
| 110 | + |
| 111 | + self.ax.fill_between( |
| 112 | + self.locx, |
| 113 | + -self.d2.low - self.d2.ok + yc, |
| 114 | + -self.d2.low - self.d2.ok - self.d2.high + yc, |
| 115 | + color = COLOR2) |
| 116 | + |
| 117 | + self.ax.plot(self.locx, self.d1.low + yc, zorder=5, |
| 118 | + linewidth=0.5, color='black') |
| 119 | + self.ax.plot(self.locx, -self.d2.low + yc, zorder=5, |
| 120 | + linewidth=0.5, color='black') |
| 121 | + |
| 122 | + self.ax.plot(self.locx, self.d1.low + self.d1.ok + yc, zorder=5, |
| 123 | + linewidth=0.5, color='black') |
| 124 | + self.ax.plot(self.locx, -self.d2.low - self.d2.ok + yc, zorder=5, |
| 125 | + linewidth=0.5, color='black') |
| 126 | + |
| 127 | + self.ax.plot(self.locx, self.d1.low + self.d1.ok + self.d1.high + yc, |
| 128 | + zorder=5, linewidth=0.5, color='black') |
| 129 | + self.ax.plot(self.locx, -self.d2.low - self.d2.ok -self.d2.high + yc, |
| 130 | + zorder=5, linewidth=0.5, color='black') |
| 131 | + |
| 132 | + |
| 133 | + |
| 134 | + if options.score: |
| 135 | + |
| 136 | + score1 = 0.5 * self.plot.maxY * ( |
| 137 | + 1 - 2 * np.exp(-1 * (self.d1.ok / median1)) |
| 138 | + + np.exp(-1 * ( ( self.d1.ok + self.d1.low + self.d1.high) / median1)) |
| 139 | + ) |
| 140 | + |
| 141 | + score2 = 0.5 * self.plot.maxY * ( |
| 142 | + 1 - 2 * np.exp(-1 * (self.d2.ok / median2)) |
| 143 | + + np.exp(-1 * ( ( self.d2.ok + self.d2.low + self.d2.high) / median2)) |
| 144 | + ) |
| 145 | + |
| 146 | + self.ax.plot(self.locx, (score1 - score2) + yc, color='black', linewidth=2) |
| 147 | + |
| 148 | + self.ax.text(0.01 * len(self.locx), yc + (0.9 * self.plot.maxY), title1, |
| 149 | + verticalalignment='center', |
| 150 | + bbox=dict(facecolor='white', alpha=0.6, zorder=20), |
| 151 | + ) |
| 152 | + self.ax.text(0.01 * len(self.locx), yc - (0.9 * self.plot.maxY), title2, |
| 153 | + verticalalignment='center', |
| 154 | + bbox=dict(facecolor='white', alpha=0.6, zorder=20), |
| 155 | + ) |
| 156 | + |
| 157 | + |
| 158 | + |
| 159 | + def setYticks2(self): |
| 160 | + self.plot.yTicks2.append(self.yCorrection - self.data.medianH) |
| 161 | + self.plot.yTicks2.append(self.yCorrection) |
| 162 | + self.plot.yTicks2.append(self.yCorrection + self.data.medianH) |
| 163 | + self.plot.yTickLabels2.append("%s" % -self.data.medianH) |
| 164 | + self.plot.yTickLabels2.append("0") |
| 165 | + self.plot.yTickLabels2.append("%s" % self.data.medianH) |
| 166 | + self.ax.axhline( |
| 167 | + self.yCorrection - self.data.medianH, |
| 168 | + alpha=0.3, |
| 169 | + color='black') |
| 170 | + self.ax.axhline( |
| 171 | + self.yCorrection + self.data.medianH, |
| 172 | + alpha=0.3, |
| 173 | + color='black') |
| 174 | + |
| 175 | +plot.plotBands(plotter) |
| 176 | +plot.save(tag='%s.%s.comp' % (title1,title2)) |
| 177 | + |
| 178 | + |
0 commit comments