def configure(self, profile, statsResults): configDlg = ConfigureDialog(Ui_ScatterPlotDialog) configDlg.ui.spinNumBins.setEnabled(self.bShowHistograms) configDlg.ui.spinHistogramSize.setEnabled(self.bShowHistograms) configDlg.ui.spinFigWidth.setValue(self.figWidth) configDlg.ui.spinFigHeight.setValue(self.figHeight) configDlg.ui.chkShowCIs.setChecked(self.bShowCIs) configDlg.ui.spinNumBins.setValue(self.numBins) configDlg.ui.spinHistogramSize.setValue(self.histogramSize) configDlg.ui.chkShowHistogram.setChecked(self.bShowHistograms) configDlg.ui.spinMarkerSize.setValue(self.markerSize) configDlg.ui.chkShowR2.setChecked(self.bShowR2) if configDlg.exec_() == QtGui.QDialog.Accepted: self.figWidth = configDlg.ui.spinFigWidth.value() self.figHeight = configDlg.ui.spinFigHeight.value() self.bShowCIs = configDlg.ui.chkShowCIs.isChecked() self.numBins = configDlg.ui.spinNumBins.value() self.histogramSize = configDlg.ui.spinHistogramSize.value() self.bShowHistograms = configDlg.ui.chkShowHistogram.isChecked() self.markerSize = configDlg.ui.spinMarkerSize.value() self.bShowR2 = configDlg.ui.chkShowR2.isChecked() self.settings.setValue(self.name + '/width', self.figWidth) self.settings.setValue(self.name + '/height', self.figHeight) self.settings.setValue(self.name + '/show CIs', self.bShowCIs) self.settings.setValue(self.name + '/bin', self.numBins) self.settings.setValue(self.name + '/bin size', self.histogramSize) self.settings.setValue(self.name + '/show histograms', self.bShowHistograms) self.settings.setValue(self.name + '/marker size', self.markerSize) self.settings.setValue(self.name + '/show R2', self.bShowR2) self.plot(profile, statsResults)
class pValueHistogram(AbstractSamplePlotPlugin): ''' p-value histogram plot. ''' def __init__(self, preferences, parent=None): AbstractSamplePlotPlugin.__init__(self, preferences, parent) self.preferences = preferences self.name = 'p-value histogram' self.type = 'Statistical' self.settings = preferences['Settings'] self.figWidth = self.settings.value(self.name + '/width', 7.0).toDouble()[0] self.figHeight = self.settings.value(self.name + '/height', 7.0).toDouble()[0] self.fieldToPlot = self.settings.value( self.name + '/field to plot', 'p-values (corrected)').toString() self.yAxisLogScale = self.settings.value( self.name + '/histogram log scale', False).toBool() self.binWidth = self.settings.value(self.name + '/histogram bin width', 0.01).toDouble()[0] self.bShowInset = self.settings.value(self.name + '/show inset', True).toBool() self.insetWidth = self.settings.value(self.name + '/inset width %', 60.0).toDouble()[0] self.insetHeight = self.settings.value(self.name + '/inset height %', 60.0).toDouble()[0] self.insetLogScale = self.settings.value( self.name + '/inset log scale', False).toBool() self.insetBinWidth = self.settings.value( self.name + '/inset bin width', 0.002).toDouble()[0] self.xLimit = self.settings.value(self.name + '/inset x-axis limit', 0.05).toDouble()[0] def mirrorProperties(self, plotToCopy): self.name = plotToCopy.name self.figWidth = plotToCopy.figWidth self.figHeight = plotToCopy.figHeight self.yAxisLogScale = plotToCopy.yAxisLogScale self.fieldToPlot = plotToCopy.fieldToPlot self.bShowInset = plotToCopy.bShowInset self.insetWidth = plotToCopy.insetWidth self.insetHeight = plotToCopy.insetHeight self.insetBinWidth = plotToCopy.insetBinWidth self.xLimit = plotToCopy.xLimit self.insetLogScale = plotToCopy.insetLogScale def plot(self, profile, statsResults): if len(statsResults.activeData) <= 0: self.emptyAxis() return axesColour = str(self.preferences['Axes colour'].name()) # *** Get data to plot if self.fieldToPlot == 'p-values': data = statsResults.getColumn('pValues') xLabel = 'p-value' elif self.fieldToPlot == 'p-values (corrected)': data = statsResults.getColumn('pValuesCorrected') xLabel = 'p-value (corrected)' # *** Set size of figure self.fig.clear() self.fig.set_size_inches(self.figWidth, self.figHeight) heightBottomLabels = 0.4 # inches widthSideLabel = 0.5 # inches padding = 0.2 # inches axesHist = self.fig.add_axes([widthSideLabel/self.figWidth,heightBottomLabels/self.figHeight,\ 1.0-(widthSideLabel+padding)/self.figWidth,\ 1.0-(heightBottomLabels+padding)/self.figHeight]) # *** Histogram plot bins = [0.0] binWidth = self.binWidth binEnd = binWidth while binEnd <= 1.0: bins.append(binEnd) binEnd += binWidth n, bins, patch = axesHist.hist(data, bins=bins, log=self.yAxisLogScale, color=(0.5, 0.5, 0.5)) axesHist.set_xlabel(xLabel) axesHist.set_ylabel('Number of features') # *** Prettify plot for a in axesHist.yaxis.majorTicks: a.tick1On = True a.tick2On = False for a in axesHist.xaxis.majorTicks: a.tick1On = True a.tick2On = False for line in axesHist.yaxis.get_ticklines(): line.set_color(axesColour) for line in axesHist.xaxis.get_ticklines(): line.set_color(axesColour) for loc, spine in axesHist.spines.iteritems(): if loc in ['right', 'top']: spine.set_color('none') else: spine.set_color(axesColour) # *** Plot inset if self.bShowInset: bins = [0.0] binWidth = self.insetBinWidth binEnd = binWidth while binEnd <= self.xLimit: bins.append(binEnd) binEnd += binWidth widthStr = str(self.insetWidth) + '%' heightStr = str(self.insetHeight) + '%' axins = inset_axes(axesHist, width=widthStr, height=heightStr, loc=1) filteredData = [d for d in data if d <= self.xLimit] if filteredData: n, bins, patch = axins.hist(filteredData, bins=bins, log=self.insetLogScale, color=(0.5, 0.5, 0.5)) axins.set_xlim(0, self.xLimit) # *** Prettify inset for a in axins.yaxis.majorTicks: a.tick1On = True a.tick2On = False for a in axins.xaxis.majorTicks: a.tick1On = True a.tick2On = False for line in axins.yaxis.get_ticklines(): line.set_color(axesColour) for line in axins.xaxis.get_ticklines(): line.set_color(axesColour) for loc, spine in axins.spines.iteritems(): if loc in ['right', 'top']: spine.set_color('none') else: spine.set_color(axesColour) self.updateGeometry() self.draw() def configure(self, profile, statsResults): self.statsResults = statsResults self.configDlg = ConfigureDialog(Ui_pValueHistogramDialog) self.connect(self.configDlg.ui.btnXmax, QtCore.SIGNAL('clicked()'), self.setXaxisMax) self.configDlg.ui.cboFieldToPlot.setCurrentIndex( self.configDlg.ui.cboFieldToPlot.findText(self.fieldToPlot)) self.configDlg.ui.spinFigWidth.setValue(self.figWidth) self.configDlg.ui.spinFigHeight.setValue(self.figHeight) self.configDlg.ui.spinBinWidth.setValue(self.binWidth) self.configDlg.ui.chkLogScale.setChecked(self.yAxisLogScale) self.configDlg.ui.chkShowInset.setChecked(self.bShowInset) self.configDlg.ui.spinInsetWidth.setValue(self.insetWidth) self.configDlg.ui.spinInsetHeight.setValue(self.insetHeight) self.configDlg.ui.spinInsetBinWidth.setValue(self.insetBinWidth) self.configDlg.ui.spinXlimit.setValue(self.xLimit) self.configDlg.ui.chkInsetLogScale.setChecked(self.insetLogScale) if self.configDlg.exec_() == QtGui.QDialog.Accepted: self.figWidth = self.configDlg.ui.spinFigWidth.value() self.figHeight = self.configDlg.ui.spinFigHeight.value() self.binWidth = self.configDlg.ui.spinBinWidth.value() self.yAxisLogScale = self.configDlg.ui.chkLogScale.isChecked() self.fieldToPlot = self.configDlg.ui.cboFieldToPlot.currentText() self.bShowInset = self.configDlg.ui.chkShowInset.isChecked() self.insetWidth = self.configDlg.ui.spinInsetWidth.value() self.insetHeight = self.configDlg.ui.spinInsetHeight.value() self.insetBinWidth = self.configDlg.ui.spinInsetBinWidth.value() self.xLimit = self.configDlg.ui.spinXlimit.value() self.insetLogScale = self.configDlg.ui.chkInsetLogScale.isChecked() self.settings.setValue(self.name + '/width', self.figWidth) self.settings.setValue(self.name + '/height', self.figHeight) self.settings.setValue(self.name + '/field to plot', self.fieldToPlot) self.settings.setValue(self.name + '/histogram log scale', self.yAxisLogScale) self.settings.setValue(self.name + '/histogram bin width', self.binWidth) self.settings.setValue(self.name + '/show inset', self.bShowInset) self.settings.setValue(self.name + '/inset width %', self.insetWidth) self.settings.setValue(self.name + '/inset height %', self.insetHeight) self.settings.setValue(self.name + '/inset log scale', self.insetLogScale) self.settings.setValue(self.name + '/inset bin width', self.insetBinWidth) self.settings.setValue(self.name + '/inset x-axis limit', self.xLimit) self.plot(profile, statsResults) def setXaxisMax(self): # *** Get data to plot if self.configDlg.ui.cboFieldToPlot.currentText() == 'p-values': data = self.statsResults.getColumn('pValues') else: data = self.statsResults.getColumn('pValuesCorrected') self.configDlg.ui.spinXlimit.setValue(max(data))
class ExtendedErrorBar(AbstractSamplePlotPlugin): ''' Extended error bar plot. ''' def __init__(self, preferences, parent=None): AbstractSamplePlotPlugin.__init__(self, preferences, parent) self.name = 'Extended error bar' self.type = 'Statistical' self.settings = preferences['Settings'] self.figWidth = self.settings.value(self.name + '/width', 7.0).toDouble()[0] self.figHeightPerRow = self.settings.value(self.name + '/row height', 0.2).toDouble()[0] self.sortingField = self.settings.value(self.name + '/field', 'p-values').toString() self.bShowBarPlot = self.settings.value(self.name + '/sequences subplot', True).toBool() self.bShowPValueLabels = self.settings.value(self.name + '/p-value labels', True).toBool() self.bShowCorrectedPvalues = self.settings.value(self.name + '/show corrected p-values', True).toBool() self.bCustomLimits = self.settings.value(self.name + '/use custom limits', False).toBool() self.minX = self.settings.value(self.name + '/minimum', 0.0).toDouble()[0] self.maxX = self.settings.value(self.name + '/maximum', 1.0).toDouble()[0] self.markerSize = self.settings.value(self.name + '/marker size', 30).toInt()[0] self.percentageOrSeqCount = self.settings.value(self.name + '/percentage or seq count', 'Proportion (%)').toString() self.legendPos = self.settings.value(self.name + '/legend position', -1).toInt()[0] def mirrorProperties(self, plotToCopy): self.name = plotToCopy.name self.figWidth = plotToCopy.figWidth self.figHeightPerRow = plotToCopy.figHeightPerRow self.sortingField = plotToCopy.sortingField self.bShowBarPlot = plotToCopy.bShowBarPlot self.bShowPValueLabels = plotToCopy.bShowPValueLabels self.bShowCorrectedPvalues = plotToCopy.bShowCorrectedPvalues self.bCustomLimits = plotToCopy.bCustomLimits self.minX = plotToCopy.minX self.maxX = plotToCopy.maxX self.markerSize = plotToCopy.markerSize self.percentageOrSeqCount = plotToCopy.percentageOrSeqCount self.legendPos = plotToCopy.legendPos def plot(self, profile, statsResults): # *** Check if there is sufficient data to generate the plot if len(statsResults.activeData) <= 0: self.emptyAxis() return features = statsResults.getColumn('Features') if len(features) > 200: QtGui.QApplication.instance().setOverrideCursor(QtGui.QCursor(QtCore.Qt.ArrowCursor)) reply = QtGui.QMessageBox.question(self, 'Continue?', 'Profile contains ' + str(len(features)) + ' features. ' + 'It may take several seconds to generate this plot. We recommend filtering your profile first. ' + 'Do you wish to continue?', QtGui.QMessageBox.Yes, QtGui.QMessageBox.No) QtGui.QApplication.instance().restoreOverrideCursor() if reply == QtGui.QMessageBox.No: self.emptyAxis() return # *** Colour of plot elements axesColour = str(self.preferences['Axes colour'].name()) profile1Colour = str(self.preferences['Sample 1 colour'].name()) profile2Colour = str(self.preferences['Sample 2 colour'].name()) # *** Colour of plot elements highlightColor = (0.9, 0.9, 0.9) # *** Sort data if self.sortingField == 'p-values': statsResults.activeData = TableHelper.SortTable(statsResults.activeData,\ [statsResults.dataHeadings['pValues']], False) elif self.sortingField == 'Effect sizes': statsResults.activeData = TableHelper.SortTable(statsResults.activeData,\ [statsResults.dataHeadings['EffectSize']], True, True, statsResults.confIntervMethod.bRatio) elif self.sortingField == 'Feature labels': statsResults.activeData = TableHelper.SortTableStrCol(statsResults.activeData,\ statsResults.dataHeadings['Features'], False) features = statsResults.getColumn('Features') # get sorted feature labels # *** Create lists for each quantity of interest if statsResults.multCompCorrection.method == 'False discovery rate': pValueTitle = 'q-value' else: pValueTitle = 'p-value' if self.bShowCorrectedPvalues: pValueLabels = statsResults.getColumnAsStr('pValuesCorrected') if statsResults.multCompCorrection.method != 'No correction': pValueTitle += ' (corrected)' else: pValueLabels = statsResults.getColumnAsStr('pValues') effectSizes = statsResults.getColumn('EffectSize') lowerCIs = statsResults.getColumn('LowerCI') upperCIs = statsResults.getColumn('UpperCI') ciTitle = ('%.3g' % (statsResults.oneMinusAlpha()*100)) + '% confidence intervals' seqs1 = statsResults.getColumn('Seq1') seqs2 = statsResults.getColumn('Seq2') parentSeqs1 = statsResults.getColumn('ParentalSeq1') parentSeqs2 = statsResults.getColumn('ParentalSeq2') # *** Truncate feature labels highlightedFeatures = list(self.preferences['Highlighted sample features']) if self.preferences['Truncate feature names']: length = self.preferences['Length of truncated feature names'] for i in xrange(0, len(features)): if len(features[i]) > length+3: features[i] = features[i][0:length] + '...' for i in xrange(0, len(highlightedFeatures)): if len(highlightedFeatures[i]) > length+3: highlightedFeatures[i] = highlightedFeatures[i][0:length] + '...' # *** Adjust effect size for axis scale dominateInSample2 = [] percentage1 = [] percentage2 = [] for i in xrange(0, len(effectSizes)): percentage1.append(float(seqs1[i])*100 / parentSeqs1[i]) percentage2.append(float(seqs2[i])*100 / parentSeqs2[i]) if statsResults.confIntervMethod.bRatio: if effectSizes[i] < 1: # mirror CI across y-axis effectSizes[i] = 1.0 / effectSizes[i] lowerCI = effectSizes[i] - (1.0 / upperCIs[i]) upperCI = (1.0 / lowerCIs[i]) - effectSizes[i] lowerCIs[i] = lowerCI upperCIs[i] = upperCI dominateInSample2.append(i) else: lowerCIs[i] = effectSizes[i] - lowerCIs[i] upperCIs[i] = upperCIs[i] - effectSizes[i] else: lowerCIs[i] = effectSizes[i] - lowerCIs[i] upperCIs[i] = upperCIs[i] - effectSizes[i] if effectSizes[i] < 0.0: dominateInSample2.append(i) # *** Set figure size if self.legendPos == 3 or self.legendPos == 4 or self.legendPos == 8: # bottom legend heightBottomLabels = 0.56 # inches else: heightBottomLabels = 0.4 # inches heightTopLabels = 0.25 plotHeight = self.figHeightPerRow*len(features) self.imageWidth = self.figWidth self.imageHeight = plotHeight + heightBottomLabels + heightTopLabels if self.imageWidth > 256 or self.imageHeight > 256: QtGui.QApplication.instance().setOverrideCursor(QtGui.QCursor(QtCore.Qt.ArrowCursor)) self.emptyAxis() reply = QtGui.QMessageBox.question(self, 'Excessively large plot', 'The resulting plot is too large to display.') QtGui.QApplication.instance().restoreOverrideCursor() return self.fig.set_size_inches(self.imageWidth, self.imageHeight) # *** Determine width of y-axis labels yLabelBounds = self.yLabelExtents(features, 8) # *** Size plots which comprise the extended errorbar plot self.fig.clear() spacingBetweenPlots = 0.25 # inches widthNumSeqPlot = 1.25 # inches if self.bShowBarPlot == False: widthNumSeqPlot = 0.0 spacingBetweenPlots = 0.0 widthPvalueLabels = 0.75 # inches if self.bShowPValueLabels == False: widthPvalueLabels = 0.1 yPlotOffsetFigSpace = heightBottomLabels / self.imageHeight heightPlotFigSpace = plotHeight / self.imageHeight xPlotOffsetFigSpace = yLabelBounds.width + 0.1 / self.imageWidth pValueLabelWidthFigSpace = widthPvalueLabels / self.imageWidth widthPlotFigSpace = 1.0 - pValueLabelWidthFigSpace - xPlotOffsetFigSpace widthErrorBarPlot = widthPlotFigSpace*self.imageWidth - widthNumSeqPlot - spacingBetweenPlots axInitAxis = self.fig.add_axes([xPlotOffsetFigSpace,yPlotOffsetFigSpace,widthPlotFigSpace,heightPlotFigSpace]) divider = make_axes_locatable(axInitAxis) divider.get_vertical()[0] = Size.Fixed(len(features)*self.figHeightPerRow) if self.bShowBarPlot == True: divider.get_horizontal()[0] = Size.Fixed(widthNumSeqPlot) axErrorbar = divider.new_horizontal(widthErrorBarPlot, pad=spacingBetweenPlots, sharey=axInitAxis) self.fig.add_axes(axErrorbar) else: divider.get_horizontal()[0] = Size.Fixed(widthErrorBarPlot) axErrorbar = axInitAxis # *** Plot of sequences for each subsystem if self.bShowBarPlot == True: axNumSeq = axInitAxis if self.percentageOrSeqCount == 'Proportion (%)': # plot percentage axNumSeq.barh(np.arange(len(features))+0.0, percentage1, height = 0.3, color=profile1Colour, zorder=10, ecolor='black') axNumSeq.barh(np.arange(len(features))-0.3, percentage2, height = 0.3, color=profile2Colour, zorder=10, ecolor='black') for value in np.arange(-0.5, len(features)-1, 2): axNumSeq.axhspan(value, value+1, facecolor=highlightColor,edgecolor='none',zorder=1) axNumSeq.set_xlabel(self.percentageOrSeqCount) maxPercentage = max(max(percentage1), max(percentage2)) axNumSeq.set_xticks([0, maxPercentage]) axNumSeq.set_xlim([0, maxPercentage*1.05]) maxPercentageStr = '%.1f' % maxPercentage axNumSeq.set_xticklabels(['0.0', maxPercentageStr]) else: # plot sequence count axNumSeq.barh(np.arange(len(features))+0.0, seqs1, height = 0.3, color=profile1Colour, zorder=10, ecolor='black') axNumSeq.barh(np.arange(len(features))-0.3, seqs2, height = 0.3, color=profile2Colour, zorder=10, ecolor='black') for value in np.arange(-0.5, len(features)-1, 2): axNumSeq.axhspan(value, value+1, facecolor=highlightColor,edgecolor='none',zorder=1) axNumSeq.set_xlabel(self.percentageOrSeqCount) maxSeqs = max(max(seqs1), max(seqs2)) axNumSeq.set_xticks([0, maxSeqs]) axNumSeq.set_xlim([0, maxSeqs*1.05]) axNumSeq.set_xticklabels([0, str(maxSeqs)]) axNumSeq.set_yticks(np.arange(len(features))) axNumSeq.set_yticklabels(features) axNumSeq.set_ylim([-1, len(features)]) for label in axNumSeq.get_yticklabels(): if label.get_text() in highlightedFeatures: label.set_color('red') for a in axNumSeq.yaxis.majorTicks: a.tick1On=False a.tick2On=False for a in axNumSeq.xaxis.majorTicks: a.tick1On=True a.tick2On=False for line in axNumSeq.yaxis.get_ticklines(): line.set_color(axesColour) for line in axNumSeq.xaxis.get_ticklines(): line.set_color(axesColour) for loc, spine in axNumSeq.spines.iteritems(): if loc in ['left', 'right','top']: spine.set_color('none') else: spine.set_color(axesColour) # *** Plot confidence intervals for each subsystem lastAxes = axErrorbar markerSize = math.sqrt(float(self.markerSize)) axErrorbar.errorbar(effectSizes, np.arange(len(features)), xerr=[lowerCIs,upperCIs], fmt='o', ms=markerSize, mfc=profile1Colour, mec='black', ecolor='black', zorder=10) effectSizesSample2 = [effectSizes[value] for value in dominateInSample2] axErrorbar.plot(effectSizesSample2, dominateInSample2, ls='', marker='o', ms=markerSize, mfc=profile2Colour, mec='black', zorder=100) if statsResults.confIntervMethod.bRatio: axErrorbar.vlines(1, -1, len(features), linestyle='dashed', color=axesColour) else: axErrorbar.vlines(0, -1, len(features), linestyle='dashed', color=axesColour) for value in np.arange(-0.5, len(features)-1, 2): axErrorbar.axhspan(value, value+1, facecolor=highlightColor,edgecolor='none',zorder=1) axErrorbar.set_title(ciTitle) axErrorbar.set_xlabel(statsResults.confIntervMethod.plotLabel) if self.bCustomLimits: axErrorbar.set_xlim([self.minX, self.maxX]) else: self.minX, self.maxX = axErrorbar.get_xlim() if self.bShowBarPlot == False: axErrorbar.set_yticks(np.arange(len(features))) axErrorbar.set_yticklabels(features) axErrorbar.set_ylim([-1, len(features)]) for label in axErrorbar.get_yticklabels(): if label.get_text() in highlightedFeatures: label.set_color('red') else: for label in axErrorbar.get_yticklabels(): label.set_visible(False) for a in axErrorbar.yaxis.majorTicks: a.set_visible(False) for a in axErrorbar.xaxis.majorTicks: a.tick1On=True a.tick2On=False for a in axErrorbar.yaxis.majorTicks: a.tick1On=False a.tick2On=False for line in axErrorbar.yaxis.get_ticklines(): line.set_visible(False) for line in axErrorbar.xaxis.get_ticklines(): line.set_color(axesColour) for loc, spine in axErrorbar.spines.iteritems(): if loc in ['left','right','top']: spine.set_color('none') else: spine.set_color(axesColour) # *** Show p-values on right of last plot if self.bShowPValueLabels == True: axRight = lastAxes.twinx() axRight.set_yticks(np.arange(len(pValueLabels))) axRight.set_yticklabels(pValueLabels) axRight.set_ylim([-1, len(pValueLabels)]) axRight.set_ylabel(pValueTitle) for a in axRight.yaxis.majorTicks: a.tick1On=False a.tick2On=False for loc, spine in axRight.spines.iteritems(): spine.set_color('none') # *** Legend # *** Legend if self.legendPos != -1: legend1 = Rectangle((0, 0), 1, 1, fc=profile1Colour) legend2 = Rectangle((0, 0), 1, 1, fc=profile2Colour) legend = self.fig.legend([legend1, legend2], (statsResults.profile.sampleNames[0], statsResults.profile.sampleNames[1]), loc=self.legendPos, ncol=2) legend.get_frame().set_linewidth(0) self.updateGeometry() self.draw() def configure(self, profile, statsResults): self.statsResults = statsResults self.configDlg = ConfigureDialog(Ui_ExtendedErrorBarDialog) # set enabled state of controls self.configDlg.ui.cboPercentageOrSeqCount.setEnabled(self.bShowBarPlot) self.configDlg.ui.spinMinimumX.setEnabled(self.bCustomLimits) self.configDlg.ui.spinMaximumX.setEnabled(self.bCustomLimits) # set current value of controls self.configDlg.ui.cboSortingField.setCurrentIndex(self.configDlg.ui.cboSortingField.findText(self.sortingField)) self.configDlg.ui.spinFigWidth.setValue(self.figWidth) self.configDlg.ui.spinFigRowHeight.setValue(self.figHeightPerRow) self.configDlg.ui.chkShowBarPlot.setChecked(self.bShowBarPlot) self.configDlg.ui.chkPValueLabels.setChecked(self.bShowPValueLabels) self.configDlg.ui.chkCorrectedPvalues.setChecked(self.bShowCorrectedPvalues) self.configDlg.ui.chkCustomLimits.setChecked(self.bCustomLimits) self.configDlg.ui.spinMinimumX.setValue(self.minX) self.configDlg.ui.spinMaximumX.setValue(self.maxX) self.configDlg.ui.spinMarkerSize.setValue(self.markerSize) self.configDlg.ui.cboPercentageOrSeqCount.setCurrentIndex(self.configDlg.ui.cboPercentageOrSeqCount.findText(self.percentageOrSeqCount)) if self.legendPos == 2: self.configDlg.ui.radioLegendPosUpperLeft.setChecked(True) elif self.legendPos == 3: self.configDlg.ui.radioLegendPosLowerLeft.setChecked(True) elif self.legendPos == 4: self.configDlg.ui.radioLegendPosLowerRight.setChecked(True) elif self.legendPos == 8: self.configDlg.ui.radioLegendPosLowerCentre.setChecked(True) else: self.configDlg.ui.radioLegendPosNone.setChecked(True) if self.configDlg.exec_() == QtGui.QDialog.Accepted: QtGui.QApplication.instance().setOverrideCursor(QtGui.QCursor(QtCore.Qt.WaitCursor)) self.sortingField = str(self.configDlg.ui.cboSortingField.currentText()) self.figWidth = self.configDlg.ui.spinFigWidth.value() self.figHeightPerRow = self.configDlg.ui.spinFigRowHeight.value() self.bShowBarPlot = self.configDlg.ui.chkShowBarPlot.isChecked() self.bShowPValueLabels = self.configDlg.ui.chkPValueLabels.isChecked() self.bShowCorrectedPvalues = self.configDlg.ui.chkCorrectedPvalues.isChecked() self.bCustomLimits = self.configDlg.ui.chkCustomLimits.isChecked() self.minX = self.configDlg.ui.spinMinimumX.value() self.maxX = self.configDlg.ui.spinMaximumX.value() self.markerSize = self.configDlg.ui.spinMarkerSize.value() self.percentageOrSeqCount = self.configDlg.ui.cboPercentageOrSeqCount.currentText() # legend position if self.configDlg.ui.radioLegendPosUpperLeft.isChecked() == True: self.legendPos = 2 elif self.configDlg.ui.radioLegendPosLowerLeft.isChecked() == True: self.legendPos = 3 elif self.configDlg.ui.radioLegendPosLowerCentre.isChecked() == True: self.legendPos = 8 elif self.configDlg.ui.radioLegendPosLowerRight.isChecked() == True: self.legendPos = 4 else: self.legendPos = -1 self.settings.setValue(self.name + '/width', self.figWidth) self.settings.setValue(self.name + '/row height', self.figHeightPerRow) self.settings.setValue(self.name + '/field', self.sortingField) self.settings.setValue(self.name + '/sequences subplot', self.bShowBarPlot) self.settings.setValue(self.name + '/p-value labels', self.bShowPValueLabels) self.settings.setValue(self.name + '/show corrected p-values', self.bShowCorrectedPvalues) self.settings.setValue(self.name + 'use custom limits', self.bCustomLimits) self.settings.setValue(self.name + '/minimum', self.minX) self.settings.setValue(self.name + '/maximum', self.maxX) self.settings.setValue(self.name + '/marker size', self.markerSize) self.settings.setValue(self.name + '/percentage or seq count', self.percentageOrSeqCount) self.settings.setValue(self.name + '/legend position', self.legendPos) self.plot(profile, statsResults) QtGui.QApplication.instance().restoreOverrideCursor()
class pValueHistogram(AbstractSamplePlotPlugin): ''' p-value histogram plot. ''' def __init__(self, preferences, parent=None): AbstractSamplePlotPlugin.__init__(self, preferences, parent) self.preferences = preferences self.name = 'p-value histogram' self.type = 'Statistical' self.settings = preferences['Settings'] self.figWidth = self.settings.value(self.name + '/width', 7.0).toDouble()[0] self.figHeight = self.settings.value(self.name + '/height', 7.0).toDouble()[0] self.fieldToPlot = self.settings.value(self.name + '/field to plot', 'p-values (corrected)').toString() self.yAxisLogScale = self.settings.value(self.name + '/histogram log scale', False).toBool() self.binWidth = self.settings.value(self.name + '/histogram bin width', 0.01).toDouble()[0] self.bShowInset = self.settings.value(self.name + '/show inset', True).toBool() self.insetWidth = self.settings.value(self.name + '/inset width %', 60.0).toDouble()[0] self.insetHeight = self.settings.value(self.name + '/inset height %', 60.0).toDouble()[0] self.insetLogScale = self.settings.value(self.name + '/inset log scale', False).toBool() self.insetBinWidth = self.settings.value(self.name + '/inset bin width', 0.002).toDouble()[0] self.xLimit = self.settings.value(self.name + '/inset x-axis limit', 0.05).toDouble()[0] def mirrorProperties(self, plotToCopy): self.name = plotToCopy.name self.figWidth = plotToCopy.figWidth self.figHeight = plotToCopy.figHeight self.yAxisLogScale = plotToCopy.yAxisLogScale self.fieldToPlot = plotToCopy.fieldToPlot self.bShowInset = plotToCopy.bShowInset self.insetWidth = plotToCopy.insetWidth self.insetHeight = plotToCopy.insetHeight self.insetBinWidth = plotToCopy.insetBinWidth self.xLimit = plotToCopy.xLimit self.insetLogScale = plotToCopy.insetLogScale def plot(self, profile, statsResults): if len(statsResults.activeData) <= 0: self.emptyAxis() return axesColour = str(self.preferences['Axes colour'].name()) # *** Get data to plot if self.fieldToPlot == 'p-values': data = statsResults.getColumn('pValues') xLabel = 'p-value' elif self.fieldToPlot == 'p-values (corrected)': data = statsResults.getColumn('pValuesCorrected') xLabel = 'p-value (corrected)' # *** Set size of figure self.fig.clear() self.fig.set_size_inches(self.figWidth, self.figHeight) heightBottomLabels = 0.4 # inches widthSideLabel = 0.5 # inches padding = 0.2 # inches axesHist = self.fig.add_axes([widthSideLabel/self.figWidth,heightBottomLabels/self.figHeight,\ 1.0-(widthSideLabel+padding)/self.figWidth,\ 1.0-(heightBottomLabels+padding)/self.figHeight]) # *** Histogram plot bins = [0.0] binWidth = self.binWidth binEnd = binWidth while binEnd <= 1.0: bins.append(binEnd) binEnd += binWidth n, bins, patch = axesHist.hist(data, bins=bins, log=self.yAxisLogScale,color=(0.5,0.5,0.5)) axesHist.set_xlabel(xLabel) axesHist.set_ylabel('Number of features') # *** Prettify plot for a in axesHist.yaxis.majorTicks: a.tick1On=True a.tick2On=False for a in axesHist.xaxis.majorTicks: a.tick1On=True a.tick2On=False for line in axesHist.yaxis.get_ticklines(): line.set_color(axesColour) for line in axesHist.xaxis.get_ticklines(): line.set_color(axesColour) for loc, spine in axesHist.spines.iteritems(): if loc in ['right','top']: spine.set_color('none') else: spine.set_color(axesColour) # *** Plot inset if self.bShowInset: bins = [0.0] binWidth = self.insetBinWidth binEnd = binWidth while binEnd <= self.xLimit: bins.append(binEnd) binEnd += binWidth widthStr = str(self.insetWidth) + '%' heightStr = str(self.insetHeight) + '%' axins = inset_axes(axesHist, width=widthStr, height=heightStr, loc=1) filteredData = [d for d in data if d <= self.xLimit] if filteredData: n, bins, patch = axins.hist(filteredData, bins=bins, log=self.insetLogScale, color=(0.5,0.5,0.5)) axins.set_xlim(0, self.xLimit) # *** Prettify inset for a in axins.yaxis.majorTicks: a.tick1On=True a.tick2On=False for a in axins.xaxis.majorTicks: a.tick1On=True a.tick2On=False for line in axins.yaxis.get_ticklines(): line.set_color(axesColour) for line in axins.xaxis.get_ticklines(): line.set_color(axesColour) for loc, spine in axins.spines.iteritems(): if loc in ['right','top']: spine.set_color('none') else: spine.set_color(axesColour) self.updateGeometry() self.draw() def configure(self, profile, statsResults): self.statsResults = statsResults self.configDlg = ConfigureDialog(Ui_pValueHistogramDialog) self.connect(self.configDlg.ui.btnXmax, QtCore.SIGNAL('clicked()'), self.setXaxisMax) self.configDlg.ui.cboFieldToPlot.setCurrentIndex(self.configDlg.ui.cboFieldToPlot.findText(self.fieldToPlot)) self.configDlg.ui.spinFigWidth.setValue(self.figWidth) self.configDlg.ui.spinFigHeight.setValue(self.figHeight) self.configDlg.ui.spinBinWidth.setValue(self.binWidth) self.configDlg.ui.chkLogScale.setChecked(self.yAxisLogScale) self.configDlg.ui.chkShowInset.setChecked(self.bShowInset) self.configDlg.ui.spinInsetWidth.setValue(self.insetWidth) self.configDlg.ui.spinInsetHeight.setValue(self.insetHeight) self.configDlg.ui.spinInsetBinWidth.setValue(self.insetBinWidth) self.configDlg.ui.spinXlimit.setValue(self.xLimit) self.configDlg.ui.chkInsetLogScale.setChecked(self.insetLogScale) if self.configDlg.exec_() == QtGui.QDialog.Accepted: self.figWidth = self.configDlg.ui.spinFigWidth.value() self.figHeight = self.configDlg.ui.spinFigHeight.value() self.binWidth = self.configDlg.ui.spinBinWidth.value() self.yAxisLogScale = self.configDlg.ui.chkLogScale.isChecked() self.fieldToPlot = self.configDlg.ui.cboFieldToPlot.currentText() self.bShowInset = self.configDlg.ui.chkShowInset.isChecked() self.insetWidth = self.configDlg.ui.spinInsetWidth.value() self.insetHeight = self.configDlg.ui.spinInsetHeight.value() self.insetBinWidth = self.configDlg.ui.spinInsetBinWidth.value() self.xLimit = self.configDlg.ui.spinXlimit.value() self.insetLogScale = self.configDlg.ui.chkInsetLogScale.isChecked() self.settings.setValue(self.name + '/width', self.figWidth) self.settings.setValue(self.name + '/height', self.figHeight) self.settings.setValue(self.name + '/field to plot', self.fieldToPlot) self.settings.setValue(self.name + '/histogram log scale', self.yAxisLogScale) self.settings.setValue(self.name + '/histogram bin width', self.binWidth) self.settings.setValue(self.name + '/show inset', self.bShowInset) self.settings.setValue(self.name + '/inset width %', self.insetWidth) self.settings.setValue(self.name + '/inset height %', self.insetHeight) self.settings.setValue(self.name + '/inset log scale', self.insetLogScale) self.settings.setValue(self.name + '/inset bin width', self.insetBinWidth) self.settings.setValue(self.name + '/inset x-axis limit', self.xLimit) self.plot(profile, statsResults) def setXaxisMax(self): # *** Get data to plot if self.configDlg.ui.cboFieldToPlot.currentText() == 'p-values': data = self.statsResults.getColumn('pValues') else: data = self.statsResults.getColumn('pValuesCorrected') self.configDlg.ui.spinXlimit.setValue(max(data))
class MultCompCorrectionPlots(AbstractSamplePlotPlugin): ''' Sequence histogram plot. ''' def __init__(self, preferences, parent=None): AbstractSamplePlotPlugin.__init__(self, preferences, parent) self.preferences = preferences self.name = 'Multiple comparison plots' self.type = 'Statistical' self.settings = preferences['Settings'] self.figWidth = self.settings.value(self.name + '/width', 7.0).toDouble()[0] self.figHeight = self.settings.value(self.name + '/height', 3.5).toDouble()[0] self.yAxisLogScale = self.settings.value( self.name + '/histogram log scale', False).toBool() self.binWidth = self.settings.value(self.name + '/bin width', 0.01).toDouble()[0] self.xLimitFig1 = self.settings.value( self.name + '/histogram x-axis limit', 0.1).toDouble()[0] self.xLimitFig2 = self.settings.value( self.name + '/correction plot x-axis limit', 1.0).toDouble()[0] self.xLimitFig3 = self.settings.value( self.name + '/significant features x-axis limit', 0.1).toDouble()[0] self.xMax = 0 def mirrorProperties(self, plotToCopy): self.name = plotToCopy.name self.figWidth = plotToCopy.figWidth self.figHeight = plotToCopy.figHeight self.yAxisLogScale = plotToCopy.yAxisLogScale self.binWidth = plotToCopy.binWidth self.xLimitFig1 = plotToCopy.xLimitFig1 self.xLimitFig2 = plotToCopy.xLimitFig2 self.xLimitFig3 = plotToCopy.xLimitFig3 self.xMax = plotToCopy.xMax def plot(self, profile, statsResults): if len(statsResults.activeData) <= 0: self.emptyAxis() return axesColour = str(self.preferences['Axes colour'].name()) # *** Get p-values and effect size rawValues = statsResults.getColumn('pValues') correctedValues = statsResults.getColumn('pValuesCorrected') # *** Get labels if statsResults.multCompCorrection.method == 'False discovery rate': xLabel = 'q-value' yLabel = 'p-value' else: xLabel = 'corrected p-value' yLabel = 'raw p-value' # *** Set size of figure self.fig.clear() self.fig.set_size_inches(self.figWidth, self.figHeight) self.fig.subplots_adjust(wspace=0.4) self.fig.subplots_adjust(bottom=0.15) self.fig.subplots_adjust(top=0.95) self.fig.subplots_adjust(left=0.07) self.fig.subplots_adjust(right=0.97) # Sort p-values pValues = zip(correctedValues, rawValues) pValues.sort() correctedValues = [pValue[0] for pValue in pValues] rawValues = [pValue[1] for pValue in pValues] self.xMax = max(correctedValues) # Plot effect size vs. corrected p-value ax1 = self.fig.add_subplot(1, 3, 1) bins = [0.0] binWidth = self.binWidth binEnd = binWidth while binEnd <= self.xLimitFig1: bins.append(binEnd) binEnd += binWidth x = [value for value in correctedValues if value <= self.xLimitFig1] if x: n, bins, patch = ax1.hist(x, bins=bins, log=self.yAxisLogScale, color=(0.5, 0.5, 0.5)) ax1.set_xlabel(xLabel) ax1.set_ylabel('Number of features') ax1.set_xlim(0, self.xLimitFig1) for label in ax1.get_xticklabels(): label.set_rotation(90) for a in ax1.yaxis.majorTicks: a.tick1On = True a.tick2On = False for a in ax1.xaxis.majorTicks: a.tick1On = True a.tick2On = False for line in ax1.yaxis.get_ticklines(): line.set_color(axesColour) for line in ax1.xaxis.get_ticklines(): line.set_color(axesColour) for loc, spine in ax1.spines.iteritems(): if loc in ['right', 'top']: spine.set_color('none') else: spine.set_color(axesColour) # Plot corrected p-values vs raw p-values ax2 = self.fig.add_subplot(1, 3, 2) x = [value for value in correctedValues if value <= self.xLimitFig2] y = rawValues[0:len(x)] ax2.plot(x, y, 'o', markersize=4, mfc=(0.5, 0.5, 0.5)) ax2.set_xlabel(xLabel) ax2.set_ylabel(yLabel) ax2.set_xlim(0, self.xLimitFig2) for label in ax2.get_xticklabels(): label.set_rotation(90) for a in ax2.yaxis.majorTicks: a.tick1On = True a.tick2On = False for a in ax2.xaxis.majorTicks: a.tick1On = True a.tick2On = False for line in ax2.yaxis.get_ticklines(): line.set_color(axesColour) for line in ax2.xaxis.get_ticklines(): line.set_color(axesColour) for loc, spine in ax2.spines.iteritems(): if loc in ['right', 'top']: spine.set_color('none') else: spine.set_color(axesColour) # Plot corrected p-values vs. number of significant features ax3 = self.fig.add_subplot(1, 3, 3) x = [value for value in correctedValues if value <= self.xLimitFig3] x = correctedValues[ 0:len(x) + 1] # add in one extra point so the plot fills the entire x-axis ax3.plot(x, np.arange(len(x)), 'k-') ax3.set_xlabel(xLabel) ax3.set_ylabel('# significant features') ax3.set_xlim(0, self.xLimitFig3) for label in ax3.get_xticklabels(): label.set_rotation(90) for a in ax3.yaxis.majorTicks: a.tick1On = True a.tick2On = False for a in ax3.xaxis.majorTicks: a.tick1On = True a.tick2On = False for line in ax3.yaxis.get_ticklines(): line.set_color(axesColour) for line in ax3.xaxis.get_ticklines(): line.set_color(axesColour) for loc, spine in ax3.spines.iteritems(): if loc in ['right', 'top']: spine.set_color('none') else: spine.set_color(axesColour) self.updateGeometry() self.draw() def configure(self, profile, statsResults): self.configDlg = ConfigureDialog(Ui_MultCompCorrectionDialog) self.connect(self.configDlg.ui.btnXmaxFig1, QtCore.SIGNAL('clicked()'), self.setXaxisMax1) self.connect(self.configDlg.ui.btnXmaxFig2, QtCore.SIGNAL('clicked()'), self.setXaxisMax2) self.connect(self.configDlg.ui.btnXmaxFig3, QtCore.SIGNAL('clicked()'), self.setXaxisMax3) self.configDlg.ui.spinFigWidth.setValue(self.figWidth) self.configDlg.ui.spinFigHeight.setValue(self.figHeight) self.configDlg.ui.spinBinWidth.setValue(self.binWidth) self.configDlg.ui.chkLogScale.setChecked(self.yAxisLogScale) self.configDlg.ui.spinXlimitFig1.setValue(self.xLimitFig1) self.configDlg.ui.spinXlimitFig2.setValue(self.xLimitFig2) self.configDlg.ui.spinXlimitFig3.setValue(self.xLimitFig3) if self.configDlg.exec_() == QtGui.QDialog.Accepted: self.figWidth = self.configDlg.ui.spinFigWidth.value() self.figHeight = self.configDlg.ui.spinFigHeight.value() self.binWidth = self.configDlg.ui.spinBinWidth.value() self.yAxisLogScale = self.configDlg.ui.chkLogScale.isChecked() self.xLimitFig1 = self.configDlg.ui.spinXlimitFig1.value() self.xLimitFig2 = self.configDlg.ui.spinXlimitFig2.value() self.xLimitFig3 = self.configDlg.ui.spinXlimitFig3.value() self.settings.setValue(self.name + '/width', self.figWidth) self.settings.setValue(self.name + '/height', self.figHeight) self.settings.setValue(self.name + '/histogram log scale', self.yAxisLogScale) self.settings.setValue(self.name + '/bin width', self.binWidth) self.settings.setValue(self.name + '/histogram x-axis limit', self.xLimitFig1) self.settings.setValue(self.name + '/correction plot x-axis limit', self.xLimitFig2) self.settings.setValue( self.name + '/significant features x-axis limit', self.xLimitFig3) self.plot(profile, statsResults) def setXaxisMax1(self): self.configDlg.ui.spinXlimitFig1.setValue(self.xMax) def setXaxisMax2(self): self.configDlg.ui.spinXlimitFig2.setValue(self.xMax) def setXaxisMax3(self): self.configDlg.ui.spinXlimitFig3.setValue(self.xMax)
def configure(self, profile, statsResults): self.statsResults = statsResults configDlg = ConfigureDialog(Ui_BarConfigDialog) configDlg.ui.cboFieldToPlot.clear() configDlg.ui.cboFieldToPlot.addItem('Effect size') configDlg.ui.cboFieldToPlot.addItem('Number of sequences') configDlg.ui.cboFieldToPlot.addItem('Number of parental sequences') configDlg.ui.cboFieldToPlot.addItem('p-values') configDlg.ui.cboFieldToPlot.addItem('p-values (corrected)') configDlg.ui.cboFieldToPlot.addItem('Proportion of sequences (%)') configDlg.ui.cboFieldToPlot.setCurrentIndex(configDlg.ui.cboFieldToPlot.findText(self.fieldToPlot)) configDlg.ui.chkSort.setChecked(self.bSortFeatures) configDlg.ui.spinFigWidth.setValue(self.figWidth) configDlg.ui.spinFigRowHeight.setValue(self.figHeightPerRow) # legend position if self.legendPos == 0: configDlg.ui.radioLegendPosBest.setChecked(True) elif self.legendPos == 1: configDlg.ui.radioLegendPosUpperRight.setChecked(True) elif self.legendPos == 7: configDlg.ui.radioLegendPosCentreRight.setChecked(True) elif self.legendPos == 4: configDlg.ui.radioLegendPosLowerRight.setChecked(True) elif self.legendPos == 2: configDlg.ui.radioLegendPosUpperLeft.setChecked(True) elif self.legendPos == 6: configDlg.ui.radioLegendPosCentreLeft.setChecked(True) elif self.legendPos == 3: configDlg.ui.radioLegendPosLowerLeft.setChecked(True) else: configDlg.ui.radioLegendPosNone.setChecked(True) if configDlg.exec_() == QtGui.QDialog.Accepted: self.fieldToPlot = str(configDlg.ui.cboFieldToPlot.currentText()) self.bSortFeatures = configDlg.ui.chkSort.isChecked() self.figWidth = configDlg.ui.spinFigWidth.value() self.figHeightPerRow = configDlg.ui.spinFigRowHeight.value() # legend position self.bShowLegend = True if configDlg.ui.radioLegendPosBest.isChecked() == True: self.legendPos = 0 elif configDlg.ui.radioLegendPosUpperRight.isChecked() == True: self.legendPos = 1 elif configDlg.ui.radioLegendPosCentreRight.isChecked() == True: self.legendPos = 7 elif configDlg.ui.radioLegendPosLowerRight.isChecked() == True: self.legendPos = 4 elif configDlg.ui.radioLegendPosUpperLeft.isChecked() == True: self.legendPos = 2 elif configDlg.ui.radioLegendPosCentreLeft.isChecked() == True: self.legendPos = 6 elif configDlg.ui.radioLegendPosLowerLeft.isChecked() == True: self.legendPos = 3 else: self.legendPos = -1 self.settings.setValue(self.name + '/width', self.figWidth) self.settings.setValue(self.name + '/row height', self.figHeightPerRow) self.settings.setValue(self.name + '/field to plot', self.fieldToPlot) self.settings.setValue(self.name + '/legend position', self.legendPos) self.settings.setValue(self.name + '/sort values', self.bSortFeatures) self.plot(profile, statsResults)
def configure(self, profile, statsResults): configDlg = ConfigureDialog(Ui_ProfileBarPlotDialog) # set enabled state of widgets configDlg.ui.spinEndCapSize.setEnabled(self.bShowCIs) configDlg.ui.spinPvalueThreshold.setEnabled(self.bShowPvalue) # set current values configDlg.ui.cboFieldToPlot.setCurrentIndex( configDlg.ui.cboFieldToPlot.findText(self.fieldToPlot)) configDlg.ui.spinFigColWidth.setValue(self.figColWidth) configDlg.ui.spinFigHeight.setValue(self.figHeight) configDlg.ui.chkShowCIs.setChecked(self.bShowCIs) configDlg.ui.spinEndCapSize.setValue(self.endCapSize) configDlg.ui.spinFeaturesToShow.setValue(self.numFeaturesToShow) configDlg.ui.chkOnlyActiveFeatures.setChecked(self.bOnlyActiveFeatures) # legend position if self.legendPos == 0: configDlg.ui.radioLegendPosBest.setDown(True) elif self.legendPos == 1: configDlg.ui.radioLegendPosUpperRight.setChecked(True) elif self.legendPos == 7: configDlg.ui.radioLegendPosCentreRight.setChecked(True) elif self.legendPos == 4: configDlg.ui.radioLegendPosLowerRight.setChecked(True) elif self.legendPos == 2: configDlg.ui.radioLegendPosUpperLeft.setChecked(True) elif self.legendPos == 6: configDlg.ui.radioLegendPosCentreLeft.setChecked(True) elif self.legendPos == 3: configDlg.ui.radioLegendPosLowerLeft.setChecked(True) else: configDlg.ui.radioLegendPosNone.setChecked(True) configDlg.ui.spinBarWidth.setValue(self.barWidth) configDlg.ui.chkShowPvalue.setChecked(self.bShowPvalue) configDlg.ui.spinPvalueThreshold.setValue(self.pValueThreshold) if configDlg.exec_() == QtGui.QDialog.Accepted: self.fieldToPlot = str(configDlg.ui.cboFieldToPlot.currentText()) self.figColWidth = configDlg.ui.spinFigColWidth.value() self.figHeight = configDlg.ui.spinFigHeight.value() self.bShowCIs = configDlg.ui.chkShowCIs.isChecked() self.endCapSize = configDlg.ui.spinEndCapSize.value() self.numFeaturesToShow = configDlg.ui.spinFeaturesToShow.value() self.bOnlyActiveFeatures = configDlg.ui.chkOnlyActiveFeatures.isChecked( ) # legend position if configDlg.ui.radioLegendPosBest.isChecked() == True: self.legendPos = 0 elif configDlg.ui.radioLegendPosUpperRight.isChecked() == True: self.legendPos = 1 elif configDlg.ui.radioLegendPosCentreRight.isChecked() == True: self.legendPos = 7 elif configDlg.ui.radioLegendPosLowerRight.isChecked() == True: self.legendPos = 4 elif configDlg.ui.radioLegendPosUpperLeft.isChecked() == True: self.legendPos = 2 elif configDlg.ui.radioLegendPosCentreLeft.isChecked() == True: self.legendPos = 6 elif configDlg.ui.radioLegendPosLowerLeft.isChecked() == True: self.legendPos = 3 else: self.legendPos = -1 self.barWidth = configDlg.ui.spinBarWidth.value() self.bShowPvalue = configDlg.ui.chkShowPvalue.isChecked() self.pValueThreshold = configDlg.ui.spinPvalueThreshold.value() self.settings.setValue(self.name + '/column width', self.figColWidth) self.settings.setValue(self.name + '/height', self.figHeight) self.settings.setValue(self.name + '/field to plot', self.fieldToPlot) self.settings.setValue(self.name + '/legend position', self.legendPos) self.settings.setValue(self.name + '/show cis', self.bShowCIs) self.settings.setValue(self.name + '/end cap size', self.endCapSize) self.settings.setValue(self.name + '/features to show', self.numFeaturesToShow) self.settings.setValue(self.name + '/bar width (%)', self.barWidth) self.settings.setValue(self.name + '/show p-value', self.bShowPvalue) self.settings.setValue(self.name + '/p-value threshold', self.pValueThreshold) self.settings.setValue(self.name + '/only active features', self.bOnlyActiveFeatures) self.plot(profile, statsResults)
class MultCompCorrectionPlots(AbstractSamplePlotPlugin): ''' Sequence histogram plot. ''' def __init__(self, preferences, parent=None): AbstractSamplePlotPlugin.__init__(self, preferences, parent) self.preferences = preferences self.name = 'Multiple comparison plots' self.type = 'Statistical' self.settings = preferences['Settings'] self.figWidth = self.settings.value(self.name + '/width', 7.0).toDouble()[0] self.figHeight = self.settings.value(self.name + '/height', 3.5).toDouble()[0] self.yAxisLogScale = self.settings.value(self.name + '/histogram log scale', False).toBool() self.binWidth = self.settings.value(self.name + '/bin width', 0.01).toDouble()[0] self.xLimitFig1 = self.settings.value(self.name + '/histogram x-axis limit', 0.1).toDouble()[0] self.xLimitFig2 = self.settings.value(self.name + '/correction plot x-axis limit', 1.0).toDouble()[0] self.xLimitFig3 = self.settings.value(self.name + '/significant features x-axis limit', 0.1).toDouble()[0] self.xMax = 0 def mirrorProperties(self, plotToCopy): self.name = plotToCopy.name self.figWidth = plotToCopy.figWidth self.figHeight = plotToCopy.figHeight self.yAxisLogScale = plotToCopy.yAxisLogScale self.binWidth = plotToCopy.binWidth self.xLimitFig1 = plotToCopy.xLimitFig1 self.xLimitFig2 = plotToCopy.xLimitFig2 self.xLimitFig3 = plotToCopy.xLimitFig3 self.xMax = plotToCopy.xMax def plot(self, profile, statsResults): if len(statsResults.activeData) <= 0: self.emptyAxis() return axesColour = str(self.preferences['Axes colour'].name()) # *** Get p-values and effect size rawValues = statsResults.getColumn('pValues') correctedValues = statsResults.getColumn('pValuesCorrected') # *** Get labels if statsResults.multCompCorrection.method == 'False discovery rate': xLabel = 'q-value' yLabel = 'p-value' else: xLabel = 'corrected p-value' yLabel = 'raw p-value' # *** Set size of figure self.fig.clear() self.fig.set_size_inches(self.figWidth, self.figHeight) self.fig.subplots_adjust(wspace = 0.4) self.fig.subplots_adjust(bottom = 0.15) self.fig.subplots_adjust(top = 0.95) self.fig.subplots_adjust(left = 0.07) self.fig.subplots_adjust(right = 0.97) # Sort p-values pValues = zip(correctedValues, rawValues) pValues.sort() correctedValues = [pValue[0] for pValue in pValues] rawValues = [pValue[1] for pValue in pValues] self.xMax = max(correctedValues) # Plot effect size vs. corrected p-value ax1 = self.fig.add_subplot(1,3,1) bins = [0.0] binWidth = self.binWidth binEnd = binWidth while binEnd <= self.xLimitFig1: bins.append(binEnd) binEnd += binWidth x = [value for value in correctedValues if value <= self.xLimitFig1] if x: n, bins, patch = ax1.hist(x, bins=bins, log=self.yAxisLogScale, color=(0.5,0.5,0.5)) ax1.set_xlabel(xLabel) ax1.set_ylabel('Number of features') ax1.set_xlim(0, self.xLimitFig1) for label in ax1.get_xticklabels(): label.set_rotation(90) for a in ax1.yaxis.majorTicks: a.tick1On=True a.tick2On=False for a in ax1.xaxis.majorTicks: a.tick1On=True a.tick2On=False for line in ax1.yaxis.get_ticklines(): line.set_color(axesColour) for line in ax1.xaxis.get_ticklines(): line.set_color(axesColour) for loc, spine in ax1.spines.iteritems(): if loc in ['right','top']: spine.set_color('none') else: spine.set_color(axesColour) # Plot corrected p-values vs raw p-values ax2 = self.fig.add_subplot(1,3,2) x = [value for value in correctedValues if value <= self.xLimitFig2] y = rawValues[0:len(x)] ax2.plot(x,y,'o', markersize=4, mfc=(0.5,0.5,0.5)) ax2.set_xlabel(xLabel) ax2.set_ylabel(yLabel) ax2.set_xlim(0, self.xLimitFig2) for label in ax2.get_xticklabels(): label.set_rotation(90) for a in ax2.yaxis.majorTicks: a.tick1On=True a.tick2On=False for a in ax2.xaxis.majorTicks: a.tick1On=True a.tick2On=False for line in ax2.yaxis.get_ticklines(): line.set_color(axesColour) for line in ax2.xaxis.get_ticklines(): line.set_color(axesColour) for loc, spine in ax2.spines.iteritems(): if loc in ['right','top']: spine.set_color('none') else: spine.set_color(axesColour) # Plot corrected p-values vs. number of significant features ax3 = self.fig.add_subplot(1,3,3) x = [value for value in correctedValues if value <= self.xLimitFig3] x = correctedValues[0:len(x)+1] # add in one extra point so the plot fills the entire x-axis ax3.plot(x, np.arange(len(x)),'k-') ax3.set_xlabel(xLabel) ax3.set_ylabel('# significant features') ax3.set_xlim(0, self.xLimitFig3) for label in ax3.get_xticklabels(): label.set_rotation(90) for a in ax3.yaxis.majorTicks: a.tick1On=True a.tick2On=False for a in ax3.xaxis.majorTicks: a.tick1On=True a.tick2On=False for line in ax3.yaxis.get_ticklines(): line.set_color(axesColour) for line in ax3.xaxis.get_ticklines(): line.set_color(axesColour) for loc, spine in ax3.spines.iteritems(): if loc in ['right','top']: spine.set_color('none') else: spine.set_color(axesColour) self.updateGeometry() self.draw() def configure(self, profile, statsResults): self.configDlg = ConfigureDialog(Ui_MultCompCorrectionDialog) self.connect(self.configDlg.ui.btnXmaxFig1, QtCore.SIGNAL('clicked()'), self.setXaxisMax1) self.connect(self.configDlg.ui.btnXmaxFig2, QtCore.SIGNAL('clicked()'), self.setXaxisMax2) self.connect(self.configDlg.ui.btnXmaxFig3, QtCore.SIGNAL('clicked()'), self.setXaxisMax3) self.configDlg.ui.spinFigWidth.setValue(self.figWidth) self.configDlg.ui.spinFigHeight.setValue(self.figHeight) self.configDlg.ui.spinBinWidth.setValue(self.binWidth) self.configDlg.ui.chkLogScale.setChecked(self.yAxisLogScale) self.configDlg.ui.spinXlimitFig1.setValue(self.xLimitFig1) self.configDlg.ui.spinXlimitFig2.setValue(self.xLimitFig2) self.configDlg.ui.spinXlimitFig3.setValue(self.xLimitFig3) if self.configDlg.exec_() == QtGui.QDialog.Accepted: self.figWidth = self.configDlg.ui.spinFigWidth.value() self.figHeight = self.configDlg.ui.spinFigHeight.value() self.binWidth = self.configDlg.ui.spinBinWidth.value() self.yAxisLogScale = self.configDlg.ui.chkLogScale.isChecked() self.xLimitFig1 = self.configDlg.ui.spinXlimitFig1.value() self.xLimitFig2 = self.configDlg.ui.spinXlimitFig2.value() self.xLimitFig3 = self.configDlg.ui.spinXlimitFig3.value() self.settings.setValue(self.name + '/width', self.figWidth) self.settings.setValue(self.name + '/height', self.figHeight) self.settings.setValue(self.name + '/histogram log scale', self.yAxisLogScale) self.settings.setValue(self.name + '/bin width', self.binWidth) self.settings.setValue(self.name + '/histogram x-axis limit', self.xLimitFig1) self.settings.setValue(self.name + '/correction plot x-axis limit', self.xLimitFig2) self.settings.setValue(self.name + '/significant features x-axis limit', self.xLimitFig3) self.plot(profile, statsResults) def setXaxisMax1(self): self.configDlg.ui.spinXlimitFig1.setValue(self.xMax) def setXaxisMax2(self): self.configDlg.ui.spinXlimitFig2.setValue(self.xMax) def setXaxisMax3(self): self.configDlg.ui.spinXlimitFig3.setValue(self.xMax)
def configure(self, profile, statsResults): configDlg = ConfigureDialog(Ui_ProfileBarPlotDialog) # set enabled state of widgets configDlg.ui.spinEndCapSize.setEnabled(self.bShowCIs) configDlg.ui.spinPvalueThreshold.setEnabled(self.bShowPvalue) # set current values configDlg.ui.cboFieldToPlot.setCurrentIndex(configDlg.ui.cboFieldToPlot.findText(self.fieldToPlot)) configDlg.ui.spinFigColWidth.setValue(self.figColWidth) configDlg.ui.spinFigHeight.setValue(self.figHeight) configDlg.ui.chkShowCIs.setChecked(self.bShowCIs) configDlg.ui.spinEndCapSize.setValue(self.endCapSize) configDlg.ui.spinFeaturesToShow.setValue(self.numFeaturesToShow) configDlg.ui.chkOnlyActiveFeatures.setChecked(self.bOnlyActiveFeatures) # legend position if self.legendPos == 0: configDlg.ui.radioLegendPosBest.setDown(True) elif self.legendPos == 1: configDlg.ui.radioLegendPosUpperRight.setChecked(True) elif self.legendPos == 7: configDlg.ui.radioLegendPosCentreRight.setChecked(True) elif self.legendPos == 4: configDlg.ui.radioLegendPosLowerRight.setChecked(True) elif self.legendPos == 2: configDlg.ui.radioLegendPosUpperLeft.setChecked(True) elif self.legendPos == 6: configDlg.ui.radioLegendPosCentreLeft.setChecked(True) elif self.legendPos == 3: configDlg.ui.radioLegendPosLowerLeft.setChecked(True) else: configDlg.ui.radioLegendPosNone.setChecked(True) configDlg.ui.spinBarWidth.setValue(self.barWidth) configDlg.ui.chkShowPvalue.setChecked(self.bShowPvalue) configDlg.ui.spinPvalueThreshold.setValue(self.pValueThreshold) if configDlg.exec_() == QtGui.QDialog.Accepted: self.fieldToPlot = str(configDlg.ui.cboFieldToPlot.currentText()) self.figColWidth = configDlg.ui.spinFigColWidth.value() self.figHeight = configDlg.ui.spinFigHeight.value() self.bShowCIs = configDlg.ui.chkShowCIs.isChecked() self.endCapSize = configDlg.ui.spinEndCapSize.value() self.numFeaturesToShow = configDlg.ui.spinFeaturesToShow.value() self.bOnlyActiveFeatures = configDlg.ui.chkOnlyActiveFeatures.isChecked() # legend position if configDlg.ui.radioLegendPosBest.isChecked() == True: self.legendPos = 0 elif configDlg.ui.radioLegendPosUpperRight.isChecked() == True: self.legendPos = 1 elif configDlg.ui.radioLegendPosCentreRight.isChecked() == True: self.legendPos = 7 elif configDlg.ui.radioLegendPosLowerRight.isChecked() == True: self.legendPos = 4 elif configDlg.ui.radioLegendPosUpperLeft.isChecked() == True: self.legendPos = 2 elif configDlg.ui.radioLegendPosCentreLeft.isChecked() == True: self.legendPos = 6 elif configDlg.ui.radioLegendPosLowerLeft.isChecked() == True: self.legendPos = 3 else: self.legendPos = -1 self.barWidth = configDlg.ui.spinBarWidth.value() self.bShowPvalue = configDlg.ui.chkShowPvalue.isChecked() self.pValueThreshold = configDlg.ui.spinPvalueThreshold.value() self.settings.setValue(self.name + '/column width', self.figColWidth) self.settings.setValue(self.name + '/height', self.figHeight) self.settings.setValue(self.name + '/field to plot', self.fieldToPlot) self.settings.setValue(self.name + '/legend position', self.legendPos) self.settings.setValue(self.name + '/show cis', self.bShowCIs) self.settings.setValue(self.name + '/end cap size', self.endCapSize) self.settings.setValue(self.name + '/features to show', self.numFeaturesToShow) self.settings.setValue(self.name + '/bar width (%)', self.barWidth) self.settings.setValue(self.name + '/show p-value', self.bShowPvalue) self.settings.setValue(self.name + '/p-value threshold', self.pValueThreshold) self.settings.setValue(self.name + '/only active features', self.bOnlyActiveFeatures) self.plot(profile, statsResults)
class ExtendedErrorBar(AbstractSamplePlotPlugin): ''' Extended error bar plot. ''' def __init__(self, preferences, parent=None): AbstractSamplePlotPlugin.__init__(self, preferences, parent) self.name = 'Extended error bar' self.type = 'Statistical' self.settings = preferences['Settings'] self.figWidth = self.settings.value(self.name + '/width', 7.0).toDouble()[0] self.figHeightPerRow = self.settings.value(self.name + '/row height', 0.2).toDouble()[0] self.sortingField = self.settings.value(self.name + '/field', 'p-values').toString() self.bShowBarPlot = self.settings.value( self.name + '/sequences subplot', True).toBool() self.bShowPValueLabels = self.settings.value( self.name + '/p-value labels', True).toBool() self.bShowCorrectedPvalues = self.settings.value( self.name + '/show corrected p-values', True).toBool() self.bCustomLimits = self.settings.value( self.name + '/use custom limits', False).toBool() self.minX = self.settings.value(self.name + '/minimum', 0.0).toDouble()[0] self.maxX = self.settings.value(self.name + '/maximum', 1.0).toDouble()[0] self.markerSize = self.settings.value(self.name + '/marker size', 30).toInt()[0] self.percentageOrSeqCount = self.settings.value( self.name + '/percentage or seq count', 'Proportion (%)').toString() self.legendPos = self.settings.value(self.name + '/legend position', -1).toInt()[0] def mirrorProperties(self, plotToCopy): self.name = plotToCopy.name self.figWidth = plotToCopy.figWidth self.figHeightPerRow = plotToCopy.figHeightPerRow self.sortingField = plotToCopy.sortingField self.bShowBarPlot = plotToCopy.bShowBarPlot self.bShowPValueLabels = plotToCopy.bShowPValueLabels self.bShowCorrectedPvalues = plotToCopy.bShowCorrectedPvalues self.bCustomLimits = plotToCopy.bCustomLimits self.minX = plotToCopy.minX self.maxX = plotToCopy.maxX self.markerSize = plotToCopy.markerSize self.percentageOrSeqCount = plotToCopy.percentageOrSeqCount self.legendPos = plotToCopy.legendPos def plot(self, profile, statsResults): # *** Check if there is sufficient data to generate the plot if len(statsResults.activeData) <= 0: self.emptyAxis() return features = statsResults.getColumn('Features') if len(features) > 200: QtGui.QApplication.instance().setOverrideCursor( QtGui.QCursor(QtCore.Qt.ArrowCursor)) reply = QtGui.QMessageBox.question( self, 'Continue?', 'Profile contains ' + str(len(features)) + ' features. ' + 'It may take several seconds to generate this plot. We recommend filtering your profile first. ' + 'Do you wish to continue?', QtGui.QMessageBox.Yes, QtGui.QMessageBox.No) QtGui.QApplication.instance().restoreOverrideCursor() if reply == QtGui.QMessageBox.No: self.emptyAxis() return # *** Colour of plot elements axesColour = str(self.preferences['Axes colour'].name()) profile1Colour = str(self.preferences['Sample 1 colour'].name()) profile2Colour = str(self.preferences['Sample 2 colour'].name()) # *** Colour of plot elements highlightColor = (0.9, 0.9, 0.9) # *** Sort data if self.sortingField == 'p-values': statsResults.activeData = TableHelper.SortTable(statsResults.activeData,\ [statsResults.dataHeadings['pValues']], False) elif self.sortingField == 'Effect sizes': statsResults.activeData = TableHelper.SortTable(statsResults.activeData,\ [statsResults.dataHeadings['EffectSize']], True, True, statsResults.confIntervMethod.bRatio) elif self.sortingField == 'Feature labels': statsResults.activeData = TableHelper.SortTableStrCol(statsResults.activeData,\ statsResults.dataHeadings['Features'], False) features = statsResults.getColumn( 'Features') # get sorted feature labels # *** Create lists for each quantity of interest if statsResults.multCompCorrection.method == 'False discovery rate': pValueTitle = 'q-value' else: pValueTitle = 'p-value' if self.bShowCorrectedPvalues: pValueLabels = statsResults.getColumnAsStr('pValuesCorrected') if statsResults.multCompCorrection.method != 'No correction': pValueTitle += ' (corrected)' else: pValueLabels = statsResults.getColumnAsStr('pValues') effectSizes = statsResults.getColumn('EffectSize') lowerCIs = statsResults.getColumn('LowerCI') upperCIs = statsResults.getColumn('UpperCI') ciTitle = ( '%.3g' % (statsResults.oneMinusAlpha() * 100)) + '% confidence intervals' seqs1 = statsResults.getColumn('Seq1') seqs2 = statsResults.getColumn('Seq2') parentSeqs1 = statsResults.getColumn('ParentalSeq1') parentSeqs2 = statsResults.getColumn('ParentalSeq2') # *** Truncate feature labels highlightedFeatures = list( self.preferences['Highlighted sample features']) if self.preferences['Truncate feature names']: length = self.preferences['Length of truncated feature names'] for i in xrange(0, len(features)): if len(features[i]) > length + 3: features[i] = features[i][0:length] + '...' for i in xrange(0, len(highlightedFeatures)): if len(highlightedFeatures[i]) > length + 3: highlightedFeatures[ i] = highlightedFeatures[i][0:length] + '...' # *** Adjust effect size for axis scale dominateInSample2 = [] percentage1 = [] percentage2 = [] for i in xrange(0, len(effectSizes)): percentage1.append(float(seqs1[i]) * 100 / parentSeqs1[i]) percentage2.append(float(seqs2[i]) * 100 / parentSeqs2[i]) if statsResults.confIntervMethod.bRatio: if effectSizes[i] < 1: # mirror CI across y-axis effectSizes[i] = 1.0 / effectSizes[i] lowerCI = effectSizes[i] - (1.0 / upperCIs[i]) upperCI = (1.0 / lowerCIs[i]) - effectSizes[i] lowerCIs[i] = lowerCI upperCIs[i] = upperCI dominateInSample2.append(i) else: lowerCIs[i] = effectSizes[i] - lowerCIs[i] upperCIs[i] = upperCIs[i] - effectSizes[i] else: lowerCIs[i] = effectSizes[i] - lowerCIs[i] upperCIs[i] = upperCIs[i] - effectSizes[i] if effectSizes[i] < 0.0: dominateInSample2.append(i) # *** Set figure size if self.legendPos == 3 or self.legendPos == 4 or self.legendPos == 8: # bottom legend heightBottomLabels = 0.56 # inches else: heightBottomLabels = 0.4 # inches heightTopLabels = 0.25 plotHeight = self.figHeightPerRow * len(features) self.imageWidth = self.figWidth self.imageHeight = plotHeight + heightBottomLabels + heightTopLabels if self.imageWidth > 256 or self.imageHeight > 256: QtGui.QApplication.instance().setOverrideCursor( QtGui.QCursor(QtCore.Qt.ArrowCursor)) self.emptyAxis() reply = QtGui.QMessageBox.question( self, 'Excessively large plot', 'The resulting plot is too large to display.') QtGui.QApplication.instance().restoreOverrideCursor() return self.fig.set_size_inches(self.imageWidth, self.imageHeight) # *** Determine width of y-axis labels yLabelBounds = self.yLabelExtents(features, 8) # *** Size plots which comprise the extended errorbar plot self.fig.clear() spacingBetweenPlots = 0.25 # inches widthNumSeqPlot = 1.25 # inches if self.bShowBarPlot == False: widthNumSeqPlot = 0.0 spacingBetweenPlots = 0.0 widthPvalueLabels = 0.75 # inches if self.bShowPValueLabels == False: widthPvalueLabels = 0.1 yPlotOffsetFigSpace = heightBottomLabels / self.imageHeight heightPlotFigSpace = plotHeight / self.imageHeight xPlotOffsetFigSpace = yLabelBounds.width + 0.1 / self.imageWidth pValueLabelWidthFigSpace = widthPvalueLabels / self.imageWidth widthPlotFigSpace = 1.0 - pValueLabelWidthFigSpace - xPlotOffsetFigSpace widthErrorBarPlot = widthPlotFigSpace * self.imageWidth - widthNumSeqPlot - spacingBetweenPlots axInitAxis = self.fig.add_axes([ xPlotOffsetFigSpace, yPlotOffsetFigSpace, widthPlotFigSpace, heightPlotFigSpace ]) divider = make_axes_locatable(axInitAxis) divider.get_vertical()[0] = Size.Fixed( len(features) * self.figHeightPerRow) if self.bShowBarPlot == True: divider.get_horizontal()[0] = Size.Fixed(widthNumSeqPlot) axErrorbar = divider.new_horizontal(widthErrorBarPlot, pad=spacingBetweenPlots, sharey=axInitAxis) self.fig.add_axes(axErrorbar) else: divider.get_horizontal()[0] = Size.Fixed(widthErrorBarPlot) axErrorbar = axInitAxis # *** Plot of sequences for each subsystem if self.bShowBarPlot == True: axNumSeq = axInitAxis if self.percentageOrSeqCount == 'Proportion (%)': # plot percentage axNumSeq.barh(np.arange(len(features)) + 0.0, percentage1, height=0.3, color=profile1Colour, zorder=10, ecolor='black') axNumSeq.barh(np.arange(len(features)) - 0.3, percentage2, height=0.3, color=profile2Colour, zorder=10, ecolor='black') for value in np.arange(-0.5, len(features) - 1, 2): axNumSeq.axhspan(value, value + 1, facecolor=highlightColor, edgecolor='none', zorder=1) axNumSeq.set_xlabel(self.percentageOrSeqCount) maxPercentage = max(max(percentage1), max(percentage2)) axNumSeq.set_xticks([0, maxPercentage]) axNumSeq.set_xlim([0, maxPercentage * 1.05]) maxPercentageStr = '%.1f' % maxPercentage axNumSeq.set_xticklabels(['0.0', maxPercentageStr]) else: # plot sequence count axNumSeq.barh(np.arange(len(features)) + 0.0, seqs1, height=0.3, color=profile1Colour, zorder=10, ecolor='black') axNumSeq.barh(np.arange(len(features)) - 0.3, seqs2, height=0.3, color=profile2Colour, zorder=10, ecolor='black') for value in np.arange(-0.5, len(features) - 1, 2): axNumSeq.axhspan(value, value + 1, facecolor=highlightColor, edgecolor='none', zorder=1) axNumSeq.set_xlabel(self.percentageOrSeqCount) maxSeqs = max(max(seqs1), max(seqs2)) axNumSeq.set_xticks([0, maxSeqs]) axNumSeq.set_xlim([0, maxSeqs * 1.05]) axNumSeq.set_xticklabels([0, str(maxSeqs)]) axNumSeq.set_yticks(np.arange(len(features))) axNumSeq.set_yticklabels(features) axNumSeq.set_ylim([-1, len(features)]) for label in axNumSeq.get_yticklabels(): if label.get_text() in highlightedFeatures: label.set_color('red') for a in axNumSeq.yaxis.majorTicks: a.tick1On = False a.tick2On = False for a in axNumSeq.xaxis.majorTicks: a.tick1On = True a.tick2On = False for line in axNumSeq.yaxis.get_ticklines(): line.set_color(axesColour) for line in axNumSeq.xaxis.get_ticklines(): line.set_color(axesColour) for loc, spine in axNumSeq.spines.iteritems(): if loc in ['left', 'right', 'top']: spine.set_color('none') else: spine.set_color(axesColour) # *** Plot confidence intervals for each subsystem lastAxes = axErrorbar markerSize = math.sqrt(float(self.markerSize)) axErrorbar.errorbar(effectSizes, np.arange(len(features)), xerr=[lowerCIs, upperCIs], fmt='o', ms=markerSize, mfc=profile1Colour, mec='black', ecolor='black', zorder=10) effectSizesSample2 = [ effectSizes[value] for value in dominateInSample2 ] axErrorbar.plot(effectSizesSample2, dominateInSample2, ls='', marker='o', ms=markerSize, mfc=profile2Colour, mec='black', zorder=100) if statsResults.confIntervMethod.bRatio: axErrorbar.vlines(1, -1, len(features), linestyle='dashed', color=axesColour) else: axErrorbar.vlines(0, -1, len(features), linestyle='dashed', color=axesColour) for value in np.arange(-0.5, len(features) - 1, 2): axErrorbar.axhspan(value, value + 1, facecolor=highlightColor, edgecolor='none', zorder=1) axErrorbar.set_title(ciTitle) axErrorbar.set_xlabel(statsResults.confIntervMethod.plotLabel) if self.bCustomLimits: axErrorbar.set_xlim([self.minX, self.maxX]) else: self.minX, self.maxX = axErrorbar.get_xlim() if self.bShowBarPlot == False: axErrorbar.set_yticks(np.arange(len(features))) axErrorbar.set_yticklabels(features) axErrorbar.set_ylim([-1, len(features)]) for label in axErrorbar.get_yticklabels(): if label.get_text() in highlightedFeatures: label.set_color('red') else: for label in axErrorbar.get_yticklabels(): label.set_visible(False) for a in axErrorbar.yaxis.majorTicks: a.set_visible(False) for a in axErrorbar.xaxis.majorTicks: a.tick1On = True a.tick2On = False for a in axErrorbar.yaxis.majorTicks: a.tick1On = False a.tick2On = False for line in axErrorbar.yaxis.get_ticklines(): line.set_visible(False) for line in axErrorbar.xaxis.get_ticklines(): line.set_color(axesColour) for loc, spine in axErrorbar.spines.iteritems(): if loc in ['left', 'right', 'top']: spine.set_color('none') else: spine.set_color(axesColour) # *** Show p-values on right of last plot if self.bShowPValueLabels == True: axRight = lastAxes.twinx() axRight.set_yticks(np.arange(len(pValueLabels))) axRight.set_yticklabels(pValueLabels) axRight.set_ylim([-1, len(pValueLabels)]) axRight.set_ylabel(pValueTitle) for a in axRight.yaxis.majorTicks: a.tick1On = False a.tick2On = False for loc, spine in axRight.spines.iteritems(): spine.set_color('none') # *** Legend # *** Legend if self.legendPos != -1: legend1 = Rectangle((0, 0), 1, 1, fc=profile1Colour) legend2 = Rectangle((0, 0), 1, 1, fc=profile2Colour) legend = self.fig.legend([legend1, legend2], (statsResults.profile.sampleNames[0], statsResults.profile.sampleNames[1]), loc=self.legendPos, ncol=2) legend.get_frame().set_linewidth(0) self.updateGeometry() self.draw() def configure(self, profile, statsResults): self.statsResults = statsResults self.configDlg = ConfigureDialog(Ui_ExtendedErrorBarDialog) # set enabled state of controls self.configDlg.ui.cboPercentageOrSeqCount.setEnabled(self.bShowBarPlot) self.configDlg.ui.spinMinimumX.setEnabled(self.bCustomLimits) self.configDlg.ui.spinMaximumX.setEnabled(self.bCustomLimits) # set current value of controls self.configDlg.ui.cboSortingField.setCurrentIndex( self.configDlg.ui.cboSortingField.findText(self.sortingField)) self.configDlg.ui.spinFigWidth.setValue(self.figWidth) self.configDlg.ui.spinFigRowHeight.setValue(self.figHeightPerRow) self.configDlg.ui.chkShowBarPlot.setChecked(self.bShowBarPlot) self.configDlg.ui.chkPValueLabels.setChecked(self.bShowPValueLabels) self.configDlg.ui.chkCorrectedPvalues.setChecked( self.bShowCorrectedPvalues) self.configDlg.ui.chkCustomLimits.setChecked(self.bCustomLimits) self.configDlg.ui.spinMinimumX.setValue(self.minX) self.configDlg.ui.spinMaximumX.setValue(self.maxX) self.configDlg.ui.spinMarkerSize.setValue(self.markerSize) self.configDlg.ui.cboPercentageOrSeqCount.setCurrentIndex( self.configDlg.ui.cboPercentageOrSeqCount.findText( self.percentageOrSeqCount)) if self.legendPos == 2: self.configDlg.ui.radioLegendPosUpperLeft.setChecked(True) elif self.legendPos == 3: self.configDlg.ui.radioLegendPosLowerLeft.setChecked(True) elif self.legendPos == 4: self.configDlg.ui.radioLegendPosLowerRight.setChecked(True) elif self.legendPos == 8: self.configDlg.ui.radioLegendPosLowerCentre.setChecked(True) else: self.configDlg.ui.radioLegendPosNone.setChecked(True) if self.configDlg.exec_() == QtGui.QDialog.Accepted: QtGui.QApplication.instance().setOverrideCursor( QtGui.QCursor(QtCore.Qt.WaitCursor)) self.sortingField = str( self.configDlg.ui.cboSortingField.currentText()) self.figWidth = self.configDlg.ui.spinFigWidth.value() self.figHeightPerRow = self.configDlg.ui.spinFigRowHeight.value() self.bShowBarPlot = self.configDlg.ui.chkShowBarPlot.isChecked() self.bShowPValueLabels = self.configDlg.ui.chkPValueLabels.isChecked( ) self.bShowCorrectedPvalues = self.configDlg.ui.chkCorrectedPvalues.isChecked( ) self.bCustomLimits = self.configDlg.ui.chkCustomLimits.isChecked() self.minX = self.configDlg.ui.spinMinimumX.value() self.maxX = self.configDlg.ui.spinMaximumX.value() self.markerSize = self.configDlg.ui.spinMarkerSize.value() self.percentageOrSeqCount = self.configDlg.ui.cboPercentageOrSeqCount.currentText( ) # legend position if self.configDlg.ui.radioLegendPosUpperLeft.isChecked() == True: self.legendPos = 2 elif self.configDlg.ui.radioLegendPosLowerLeft.isChecked() == True: self.legendPos = 3 elif self.configDlg.ui.radioLegendPosLowerCentre.isChecked( ) == True: self.legendPos = 8 elif self.configDlg.ui.radioLegendPosLowerRight.isChecked( ) == True: self.legendPos = 4 else: self.legendPos = -1 self.settings.setValue(self.name + '/width', self.figWidth) self.settings.setValue(self.name + '/row height', self.figHeightPerRow) self.settings.setValue(self.name + '/field', self.sortingField) self.settings.setValue(self.name + '/sequences subplot', self.bShowBarPlot) self.settings.setValue(self.name + '/p-value labels', self.bShowPValueLabels) self.settings.setValue(self.name + '/show corrected p-values', self.bShowCorrectedPvalues) self.settings.setValue(self.name + 'use custom limits', self.bCustomLimits) self.settings.setValue(self.name + '/minimum', self.minX) self.settings.setValue(self.name + '/maximum', self.maxX) self.settings.setValue(self.name + '/marker size', self.markerSize) self.settings.setValue(self.name + '/percentage or seq count', self.percentageOrSeqCount) self.settings.setValue(self.name + '/legend position', self.legendPos) self.plot(profile, statsResults) QtGui.QApplication.instance().restoreOverrideCursor()
class SeqHistogram(AbstractSamplePlotPlugin): ''' Sequence histogram plot. ''' def __init__(self, preferences, parent=None): AbstractSamplePlotPlugin.__init__(self, preferences, parent) self.preferences = preferences self.name = 'Sequence histogram' self.type = 'Exploratory' self.settings = preferences['Settings'] self.figWidth = self.settings.value(self.name + '/width', 7.0).toDouble()[0] self.figHeight = self.settings.value(self.name + '/height', 7.0).toDouble()[0] self.bCustomBinWidth = self.settings.value(self.name + '/custom bin width', False).toBool() self.binWidth = self.settings.value(self.name + '/bin width', 100.0).toDouble()[0] self.yAxisLogScale = self.settings.value(self.name + '/log scale', False).toBool() self.bCustomXaxis = self.settings.value(self.name + '/custom x-axis extents', False).toBool() self.xLimitLeft = self.settings.value(self.name + '/min value', 0.0).toDouble()[0] self.xLimitRight = self.settings.value(self.name + '/max value', 1.0).toDouble()[0] self.legendPos = self.settings.value(self.name + '/legend position', 0).toInt()[0] def mirrorProperties(self, plotToCopy): self.name = plotToCopy.name self.figWidth = plotToCopy.figWidth self.figHeight = plotToCopy.figHeight self.bCustomBinWidth = plotToCopy.bCustomBinWidth self.binWidth = plotToCopy.binWidth self.yAxisLogScale = plotToCopy.yAxisLogScale self.bCustomXaxis = plotToCopy.bCustomXaxis self.xLimitLeft = plotToCopy.xLimitLeft self.xLimitRight = plotToCopy.xLimitRight self.legendPos = plotToCopy.legendPos def plot(self, profile, statsResults): if len(profile.profileDict) <= 0: self.emptyAxis() return # *** Colour of plot elements axesColour = str(self.preferences['Axes colour'].name()) profile1Colour = str(self.preferences['Sample 1 colour'].name()) profile2Colour = str(self.preferences['Sample 2 colour'].name()) # *** Get sequence counts seqs1 = profile.getSequenceCounts(0) seqs2 = profile.getSequenceCounts(1) # *** Set x-axis limit self.xMin = min(min(seqs1),min(seqs2)) if self.xLimitLeft == None: self.xLimitLeft = self.xMin self.xMax = max(max(seqs1),max(seqs2)) if self.xLimitRight == None: self.xLimitRight = self.xMax # Set bin width if not self.bCustomBinWidth: self.binWidth = (self.xMax - self.xMin) / 40 # *** Set size of figure self.fig.clear() self.fig.set_size_inches(self.figWidth, self.figHeight) heightBottomLabels = 0.4 # inches widthSideLabel = 0.5 # inches padding = 0.2 # inches axesHist = self.fig.add_axes([widthSideLabel/self.figWidth,heightBottomLabels/self.figHeight,\ 1.0-(widthSideLabel+padding)/self.figWidth,\ 1.0-(heightBottomLabels+padding)/self.figHeight]) # *** Histogram plot bins = [0] binEnd = self.binWidth while binEnd <= self.xMax: bins.append(binEnd) binEnd += self.binWidth bins.append(binEnd) n, b, patches = axesHist.hist([seqs1, seqs2], bins=bins, log=self.yAxisLogScale) for patch in patches[0]: patch.set_facecolor(profile1Colour) for patch in patches[1]: patch.set_facecolor(profile2Colour) if self.bCustomXaxis: axesHist.set_xlim(self.xLimitLeft, self.xLimitRight) axesHist.set_xlabel('Sequences') axesHist.set_ylabel('Number of features') # *** Prettify plot if self.legendPos != -1: legend = axesHist.legend([patches[0][0], patches[1][0]], (profile.sampleNames[0], profile.sampleNames[1]), loc=self.legendPos) legend.get_frame().set_linewidth(0) for a in axesHist.yaxis.majorTicks: a.tick1On=True a.tick2On=False for a in axesHist.xaxis.majorTicks: a.tick1On=True a.tick2On=False for line in axesHist.yaxis.get_ticklines(): line.set_color(axesColour) for line in axesHist.xaxis.get_ticklines(): line.set_color(axesColour) for loc, spine in axesHist.spines.iteritems(): if loc in ['right','top']: spine.set_color('none') else: spine.set_color(axesColour) self.updateGeometry() self.draw() def configure(self, profile, statsResults): self.profile = profile self.configDlg = ConfigureDialog(Ui_SeqHistogramDialog) self.connect(self.configDlg.ui.chkCustomBinWidth, QtCore.SIGNAL('toggled(bool)'), self.changeCustomBinWidth) self.connect(self.configDlg.ui.chkCustomXaxis, QtCore.SIGNAL('toggled(bool)'), self.changeCustomXaxis) self.connect(self.configDlg.ui.btnXmin, QtCore.SIGNAL('clicked()'), self.setXaxisMin) self.connect(self.configDlg.ui.btnXmax, QtCore.SIGNAL('clicked()'), self.setXaxisMax) self.configDlg.ui.spinFigWidth.setValue(self.figWidth) self.configDlg.ui.spinFigHeight.setValue(self.figHeight) self.configDlg.ui.chkCustomBinWidth.setChecked(self.bCustomBinWidth) self.configDlg.ui.spinBinWidth.setValue(self.binWidth) self.configDlg.ui.chkLogScale.setChecked(self.yAxisLogScale) self.configDlg.ui.chkCustomXaxis.setChecked(self.bCustomXaxis) self.configDlg.ui.spinXmin.setValue(self.xLimitLeft) self.configDlg.ui.spinXmax.setValue(self.xLimitRight) self.changeCustomBinWidth() self.changeCustomXaxis() # legend position if self.legendPos == 0: self.configDlg.ui.radioLegendPosBest.setDown(True) elif self.legendPos == 1: self.configDlg.ui.radioLegendPosUpperRight.setChecked(True) elif self.legendPos == 7: self.configDlg.ui.radioLegendPosCentreRight.setChecked(True) elif self.legendPos == 4: self.configDlg.ui.radioLegendPosLowerRight.setChecked(True) elif self.legendPos == 2: self.configDlg.ui.radioLegendPosUpperLeft.setChecked(True) elif self.legendPos == 6: self.configDlg.ui.radioLegendPosCentreLeft.setChecked(True) elif self.legendPos == 3: self.configDlg.ui.radioLegendPosLowerLeft.setChecked(True) else: self.configDlg.ui.radioLegendPosNone.setChecked(True) if self.configDlg.exec_() == QtGui.QDialog.Accepted: self.figWidth = self.configDlg.ui.spinFigWidth.value() self.figHeight = self.configDlg.ui.spinFigHeight.value() self.bCustomBinWidth = self.configDlg.ui.chkCustomBinWidth.isChecked() self.binWidth = self.configDlg.ui.spinBinWidth.value() self.yAxisLogScale = self.configDlg.ui.chkLogScale.isChecked() self.bCustomXaxis = self.configDlg.ui.chkCustomXaxis.isChecked() self.xLimitLeft = self.configDlg.ui.spinXmin.value() self.xLimitRight = self.configDlg.ui.spinXmax.value() # legend position if self.configDlg.ui.radioLegendPosBest.isChecked() == True: self.legendPos = 0 elif self.configDlg.ui.radioLegendPosUpperRight.isChecked() == True: self.legendPos = 1 elif self.configDlg.ui.radioLegendPosCentreRight.isChecked() == True: self.legendPos = 7 elif self.configDlg.ui.radioLegendPosLowerRight.isChecked() == True: self.legendPos = 4 elif self.configDlg.ui.radioLegendPosUpperLeft.isChecked() == True: self.legendPos = 2 elif self.configDlg.ui.radioLegendPosCentreLeft.isChecked() == True: self.legendPos = 6 elif self.configDlg.ui.radioLegendPosLowerLeft.isChecked() == True: self.legendPos = 3 else: self.legendPos = -1 self.settings.setValue(self.name + '/width', self.figWidth) self.settings.setValue(self.name + '/height', self.figHeight) self.settings.setValue(self.name + '/custom bin width', self.bCustomBinWidth) self.settings.setValue(self.name + '/bin width', self.binWidth) self.settings.setValue(self.name + '/log scale', self.yAxisLogScale) self.settings.setValue(self.name + '/custom x-axis extents', self.bCustomXaxis) self.settings.setValue(self.name + '/min value', self.xLimitLeft) self.settings.setValue(self.name + '/max value', self.xLimitRight) self.settings.setValue(self.name + '/legend position', self.legendPos) self.plot(profile, statsResults) def changeCustomBinWidth(self): self.configDlg.ui.spinBinWidth.setEnabled(self.configDlg.ui.chkCustomBinWidth.isChecked()) def changeCustomXaxis(self): self.configDlg.ui.spinXmin.setEnabled(self.configDlg.ui.chkCustomXaxis.isChecked()) self.configDlg.ui.spinXmax.setEnabled(self.configDlg.ui.chkCustomXaxis.isChecked()) def setXaxisMin(self): seqs1 = self.profile.getSequenceCounts(0) seqs2 = self.profile.getSequenceCounts(1) self.configDlg.ui.spinXmin.setValue(min(min(seqs1), min(seqs2))) def setXaxisMax(self): seqs1 = self.profile.getSequenceCounts(0) seqs2 = self.profile.getSequenceCounts(1) self.configDlg.ui.spinXmax.setValue(max(max(seqs1), max(seqs2)))