use of uk.ac.babraham.SeqMonk.DataTypes.Probes.Probe in project SeqMonk by s-andrews.
the class MAPlotPanel method getFilteredProbes.
/**
* Gets the filtered probes.
*
* @param probeset the probeset
* @return the filtered probes
*/
public ProbeList getFilteredProbes(ProbeSet probeset) {
double minY = Math.min(ySelectionStart, ySelectionEnd);
double maxY = Math.max(ySelectionStart, ySelectionEnd);
ProbeList list = new ProbeList(probeList, "Difference between " + df.format(minY) + " and " + df.format(maxY), "Difference between " + xStore.name() + " and " + yStore.name() + " was between " + df.format(minY) + " and " + df.format(maxY), null);
if (madeSelection) {
Probe[] probes = probeList.getAllProbes();
for (int p = 0; p < probes.length; p++) {
try {
double diff = xStore.getValueForProbe(probes[p]) - yStore.getValueForProbe(probes[p]);
if (diff < minY)
continue;
if (diff > maxY)
continue;
list.addProbe(probes[p], null);
} catch (SeqMonkException e) {
e.printStackTrace();
}
}
}
return list;
}
use of uk.ac.babraham.SeqMonk.DataTypes.Probes.Probe in project SeqMonk by s-andrews.
the class MAPlotPanel method calculateNonredundantSet.
/**
* This collapses individual points which are over the same
* pixel when redrawing the plot at a different scale
*/
private synchronized void calculateNonredundantSet() {
closestPoint = null;
ProbePairValue[][] grid = new ProbePairValue[getWidth()][getHeight()];
Probe[] probes = probeList.getAllProbes();
try {
for (int p = 0; p < probes.length; p++) {
float xValue = (xStore.getValueForProbe(probes[p]) + yStore.getValueForProbe(probes[p])) / 2;
float yValue = xStore.getValueForProbe(probes[p]) - yStore.getValueForProbe(probes[p]);
if (Float.isNaN(xValue) || Float.isInfinite(xValue) || Float.isNaN(yValue) || Float.isInfinite(yValue)) {
continue;
}
int x = getX(xValue);
int y = getY(yValue);
if (grid[x][y] == null) {
grid[x][y] = new ProbePairValue(xValue, yValue, x, y);
grid[x][y].setProbe(probes[p]);
} else {
// belong to
if (subLists == null)
grid[x][y].count++;
// As we have multiple probes at this point we remove the
// specific probe annotation.
grid[x][y].setProbe(null);
}
}
if (subLists != null) {
for (int s = 0; s < subLists.length; s++) {
Probe[] subListProbes = subLists[s].getAllProbes();
for (int p = 0; p < subListProbes.length; p++) {
float xValue = (xStore.getValueForProbe(subListProbes[p]) + yStore.getValueForProbe(subListProbes[p])) / 2;
float yValue = xStore.getValueForProbe(subListProbes[p]) - yStore.getValueForProbe(subListProbes[p]);
int x = getX(xValue);
int y = getY(yValue);
if (grid[x][y] == null) {
// This messes up where we catch it in the middle of a redraw
continue;
// throw new IllegalArgumentException("Found subList position not in main list");
}
// 1 = no list so 2 is the lowest sublist index
grid[x][y].count = s + 2;
}
}
}
} catch (SeqMonkException e) {
throw new IllegalStateException(e);
}
// Now we need to put all of the ProbePairValues into
// a single array;
int count = 0;
for (int x = 0; x < grid.length; x++) {
for (int y = 0; y < grid[x].length; y++) {
if (grid[x][y] != null)
count++;
}
}
ProbePairValue[] nonred = new ProbePairValue[count];
count--;
for (int x = 0; x < grid.length; x++) {
for (int y = 0; y < grid[x].length; y++) {
if (grid[x][y] != null) {
nonred[count] = grid[x][y];
count--;
}
}
}
Arrays.sort(nonred);
// Work out the 95% percentile count
int minCount = 1;
int maxCount = 2;
if (nonred.length > 0) {
minCount = nonred[0].count;
maxCount = nonred[((nonred.length - 1) * 95) / 100].count;
}
// Go through every nonred assigning a suitable colour
ColourGradient gradient = new HotColdColourGradient();
for (int i = 0; i < nonred.length; i++) {
if (subLists == null) {
nonred[i].color = gradient.getColor(nonred[i].count, minCount, maxCount);
} else {
if (nonred[i].count > subLists.length + 1) {
throw new IllegalArgumentException("Count above threshold when showing sublists");
}
if (nonred[i].count == 1) {
nonred[i].color = VERY_LIGHT_GREY;
} else {
nonred[i].color = ColourIndexSet.getColour(nonred[i].count - 2);
}
}
}
nonRedundantValues = nonred;
lastNonredWidth = getWidth();
lastNonredHeight = getHeight();
// System.out.println("Nonred was "+nonRedundantValues.length+" from "+probes.length);
}
use of uk.ac.babraham.SeqMonk.DataTypes.Probes.Probe in project SeqMonk by s-andrews.
the class QuantitationTrendPlotDialog method setupFrame.
private void setupFrame(ProbeList probeList, DataStore[] stores, QuantitationTrendPlotPreferencesPanel prefs) {
// The trend panel is now going to be made of a number of different components,
// all arranged horizontally.
/*
* 1) A y-axis
*
* 2) An upstream fixed width region (if there is one)
*
* 3) A central fixed or variable region (if there is one)
*
* 4) A downstream fixed width region (if there is one)
*/
trendPanel = new JPanel();
trendPanel.setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.weightx = 0.5;
gbc.weighty = 0.5;
gbc.gridx = 1;
gbc.gridy = 1;
gbc.fill = GridBagConstraints.BOTH;
// Upstream panel
Probe[] upstreamProbes = prefs.getUpstreamProbes();
if (upstreamProbes != null) {
waitingCounter.increment();
upstreamTrendPanel = new QuantitationTrendPlotPanel(upstreamProbes, stores, probeList.getAllProbes(), prefs.selectedFeatureTypes()[0]);
trendPanel.add(upstreamTrendPanel, gbc);
gbc.gridx++;
}
// Central panel
Probe[] centralProbes = prefs.getCoreProbes();
if (centralProbes != null) {
waitingCounter.increment();
centralTrendPanel = new QuantitationTrendPlotPanel(centralProbes, stores, probeList.getAllProbes(), prefs.selectedFeatureTypes()[0]);
trendPanel.add(centralTrendPanel, gbc);
gbc.gridx++;
}
// Downstream panel
Probe[] downstreamProbes = prefs.getDownstreamProbes();
if (downstreamProbes != null) {
waitingCounter.increment();
downstreamTrendPanel = new QuantitationTrendPlotPanel(downstreamProbes, stores, probeList.getAllProbes(), prefs.selectedFeatureTypes()[0]);
trendPanel.add(downstreamTrendPanel, gbc);
gbc.gridx++;
}
// Figure out which is the leftmost panel
if (upstreamProbes != null) {
upstreamTrendPanel.setLeftmost(true);
} else if (centralProbes != null) {
centralTrendPanel.setLeftmost(true);
} else {
downstreamTrendPanel.setLeftmost(true);
}
// Figure out which is the rightmost panel
if (downstreamProbes != null) {
downstreamTrendPanel.setRightmost(true);
} else if (centralProbes != null) {
centralTrendPanel.setRightmost(true);
} else {
upstreamTrendPanel.setRightmost(true);
}
getContentPane().setLayout(new BorderLayout());
getContentPane().add(trendPanel, BorderLayout.CENTER);
smoothingSlider = new JSlider(JSlider.VERTICAL, 1, 1000, 1);
// This call is left in to work around a bug in the Windows 7 LAF
// which makes the slider stupidly thin in ticks are not drawn.
smoothingSlider.setPaintTicks(true);
smoothingSlider.addChangeListener(this);
getContentPane().add(smoothingSlider, BorderLayout.EAST);
JPanel buttonPanel = new JPanel();
JButton closeButton = new JButton("Close");
closeButton.setActionCommand("close");
closeButton.addActionListener(this);
buttonPanel.add(closeButton);
// I may not put this back, but it's broken at the moment anyway.
// JButton saveDataButton = new JButton("Save Data");
// saveDataButton.setActionCommand("save_data");
// saveDataButton.addActionListener(this);
// buttonPanel.add(saveDataButton);
JButton saveImageButton = new JButton("Save Image");
saveImageButton.setActionCommand("save_image");
saveImageButton.addActionListener(this);
buttonPanel.add(saveImageButton);
JButton saveDataButton = new JButton("Save Data");
saveDataButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
JFileChooser chooser = new JFileChooser(SeqMonkPreferences.getInstance().getSaveLocation());
chooser.setMultiSelectionEnabled(false);
chooser.setFileFilter(new FileFilter() {
public String getDescription() {
return "Text Files";
}
public boolean accept(File f) {
if (f.isDirectory() || f.getName().toLowerCase().endsWith(".txt")) {
return true;
}
return false;
}
});
int result = chooser.showSaveDialog(QuantitationTrendPlotDialog.this);
if (result == JFileChooser.CANCEL_OPTION)
return;
File file = chooser.getSelectedFile();
SeqMonkPreferences.getInstance().setLastUsedSaveLocation(file);
if (file.isDirectory())
return;
if (!file.getPath().toLowerCase().endsWith(".txt")) {
file = new File(file.getPath() + ".txt");
}
// Check if we're stepping on anyone's toes...
if (file.exists()) {
int answer = JOptionPane.showOptionDialog(QuantitationTrendPlotDialog.this, file.getName() + " exists. Do you want to overwrite the existing file?", "Overwrite file?", 0, JOptionPane.QUESTION_MESSAGE, null, new String[] { "Overwrite and Save", "Cancel" }, "Overwrite and Save");
if (answer > 0) {
return;
}
}
try {
PrintWriter pr = new PrintWriter(file);
boolean addHeader = true;
if (upstreamTrendPanel != null) {
upstreamTrendPanel.saveData(pr, addHeader);
addHeader = false;
}
if (centralTrendPanel != null) {
centralTrendPanel.saveData(pr, addHeader);
addHeader = false;
}
if (downstreamTrendPanel != null) {
downstreamTrendPanel.saveData(pr, addHeader);
addHeader = false;
}
pr.close();
} catch (IOException ioe) {
throw new IllegalStateException(ioe);
}
}
});
buttonPanel.add(saveDataButton);
getContentPane().add(buttonPanel, BorderLayout.SOUTH);
// setSize(800,600);
setSize(700, 350);
setLocationRelativeTo(SeqMonkApplication.getInstance());
boolean addedProgress = false;
if (upstreamTrendPanel != null) {
upstreamTrendPanel.addProgressListener(this);
if (!addedProgress) {
upstreamTrendPanel.addProgressListener(new ProgressDialog("Quantitation Trend Plot", upstreamTrendPanel));
addedProgress = true;
}
upstreamTrendPanel.startCalculating();
}
if (centralTrendPanel != null) {
centralTrendPanel.addProgressListener(this);
if (!addedProgress) {
centralTrendPanel.addProgressListener(new ProgressDialog("Quantitation Trend Plot", centralTrendPanel));
addedProgress = true;
}
centralTrendPanel.startCalculating();
}
if (downstreamTrendPanel != null) {
downstreamTrendPanel.addProgressListener(this);
if (!addedProgress) {
downstreamTrendPanel.addProgressListener(new ProgressDialog("Quantitation Trend Plot", downstreamTrendPanel));
addedProgress = true;
}
downstreamTrendPanel.startCalculating();
}
}
use of uk.ac.babraham.SeqMonk.DataTypes.Probes.Probe in project SeqMonk by s-andrews.
the class RNAQCCalcualtor method run.
public void run() {
updateProgress("Getting features", 0, 1);
this.geneFeatures = collection.genome().annotationCollection().getFeaturesForType(geneFeatureName);
if (transcriptFeatureName != null) {
this.transcriptFeatures = collection.genome().annotationCollection().getFeaturesForType(transcriptFeatureName);
}
if (rRNAFeatureName != null) {
this.rRNAFeatures = collection.genome().annotationCollection().getFeaturesForType(rRNAFeatureName);
}
updateProgress("Getting merged genes", 0, 1);
// Get the merged set of gene locations
Feature[] mergedGenes = FeatureMerging.getNonOverlappingLocationsForFeatures(geneFeatures, false);
if (cancel) {
progressCancelled();
return;
}
updateProgress("Getting merged transcripts", 0, 1);
// Get the merged set of transcript features
Feature[] mergedTranscripts = null;
if (transcriptFeatureName != null) {
mergedTranscripts = FeatureMerging.getNonOverlappingLocationsForFeatures(transcriptFeatures, true);
}
if (cancel) {
progressCancelled();
return;
}
// Quantitate the genes.
long[] geneBaseCounts = new long[stores.length];
int[] measuredGenesCounts = new int[stores.length];
for (int s = 0; s < stores.length; s++) {
updateProgress("Quantitating " + stores[s].name() + " over genes", s, stores.length * 4);
String lastChr = "";
Chromosome lastChrObject = null;
for (int f = 0; f < mergedGenes.length; f++) {
if (mergedGenes[f].chromosomeName() != lastChr) {
lastChr = mergedGenes[f].chromosomeName();
lastChrObject = collection.genome().getExactChromsomeNameMatch(lastChr);
}
long[] reads = stores[s].getReadsForProbe(new Probe(lastChrObject, mergedGenes[f].location().packedPosition()));
// See if we measured anything for this gene
if (reads.length > 0) {
++measuredGenesCounts[s];
}
for (int r = 0; r < reads.length; r++) {
if (cancel) {
progressCancelled();
return;
}
// Get the length of the overlap
int overlap = 1 + (Math.min(SequenceRead.end(reads[r]), mergedGenes[f].location().end()) - Math.max(SequenceRead.start(reads[r]), mergedGenes[f].location().start()));
geneBaseCounts[s] += overlap;
}
}
}
// Quantitate the transcripts
long[] transcriptBaseCounts = null;
long[] transcriptSameStrandBaseCounts = null;
long[] transcriptOpposingStrandBaseCounts = null;
if (transcriptFeatureName != null) {
transcriptBaseCounts = new long[stores.length];
transcriptSameStrandBaseCounts = new long[stores.length];
transcriptOpposingStrandBaseCounts = new long[stores.length];
for (int s = 0; s < stores.length; s++) {
updateProgress("Quantitating " + stores[s].name() + " over transcripts", s + stores.length, stores.length * 4);
String lastChr = "";
Chromosome lastChrObject = null;
for (int f = 0; f < mergedTranscripts.length; f++) {
if (mergedTranscripts[f].chromosomeName() != lastChr) {
lastChr = mergedTranscripts[f].chromosomeName();
lastChrObject = collection.genome().getExactChromsomeNameMatch(lastChr);
}
long[] reads = stores[s].getReadsForProbe(new Probe(lastChrObject, mergedTranscripts[f].location().packedPosition()));
for (int r = 0; r < reads.length; r++) {
if (cancel) {
progressCancelled();
return;
}
// Get the length of the overlap
int overlap = 1 + (Math.min(SequenceRead.end(reads[r]), mergedTranscripts[f].location().end()) - Math.max(SequenceRead.start(reads[r]), mergedTranscripts[f].location().start()));
transcriptBaseCounts[s] += overlap;
if (SequenceRead.strand(reads[r]) == mergedTranscripts[f].location().strand()) {
transcriptSameStrandBaseCounts[s] += overlap;
} else {
transcriptOpposingStrandBaseCounts[s] += overlap;
}
}
}
}
}
// Quantitate the rRNA
long[] rRNABaseCounts = null;
if (rRNAFeatureName != null) {
rRNABaseCounts = new long[stores.length];
for (int s = 0; s < stores.length; s++) {
updateProgress("Quantitating " + stores[s].name() + " over rRNAs", s + (stores.length * 2), stores.length * 4);
String lastChr = "";
Chromosome lastChrObject = null;
for (int f = 0; f < rRNAFeatures.length; f++) {
if (rRNAFeatures[f].chromosomeName() != lastChr) {
lastChr = rRNAFeatures[f].chromosomeName();
lastChrObject = collection.genome().getExactChromsomeNameMatch(lastChr);
}
long[] reads = stores[s].getReadsForProbe(new Probe(lastChrObject, rRNAFeatures[f].location().packedPosition()));
for (int r = 0; r < reads.length; r++) {
if (cancel) {
progressCancelled();
return;
}
// Get the length of the overlap
int overlap = 1 + (Math.min(SequenceRead.end(reads[r]), rRNAFeatures[f].location().end()) - Math.max(SequenceRead.start(reads[r]), rRNAFeatures[f].location().start()));
rRNABaseCounts[s] += overlap;
}
}
}
}
// Quantitate the chromosomes
long[][] chromosomeBaseCounts = new long[chromosomes.length][stores.length];
for (int s = 0; s < stores.length; s++) {
for (int c = 0; c < chromosomes.length; c++) {
updateProgress("Quantitating " + stores[s].name() + " over " + chromosomes[c].name(), s + (stores.length * 3), stores.length * 4);
ReadsWithCounts reads = stores[s].getReadsForChromosome(chromosomes[c]);
for (int r = 0; r < reads.reads.length; r++) {
chromosomeBaseCounts[c][s] += SequenceRead.length(reads.reads[r]) * reads.counts[r];
}
}
}
// Finally we make up the data sets we're going to pass back.
RNAQCResult result = new RNAQCResult(stores);
double[] percentInGene = new double[stores.length];
for (int i = 0; i < geneBaseCounts.length; i++) {
percentInGene[i] = (geneBaseCounts[i] / (double) stores[i].getTotalReadLength()) * 100;
if (percentInGene[i] > 100) {
progressWarning("Percent in gene was >100 for " + stores[i]);
percentInGene[i] = 100;
}
}
result.addPercentageSet("Percent in Gene", percentInGene);
double[] percentInTranscript = null;
if (transcriptFeatureName != null) {
percentInTranscript = new double[stores.length];
for (int i = 0; i < geneBaseCounts.length; i++) {
percentInTranscript[i] = (transcriptBaseCounts[i] / (double) geneBaseCounts[i]) * 100;
if (percentInTranscript[i] > 100) {
progressWarning("Percent in exons was >100 for " + stores[i]);
percentInTranscript[i] = 100;
}
}
result.addPercentageSet("Percent in exons", percentInTranscript);
}
double[] percentInrRNA = null;
if (rRNAFeatureName != null) {
percentInrRNA = new double[stores.length];
for (int i = 0; i < rRNABaseCounts.length; i++) {
percentInrRNA[i] = (rRNABaseCounts[i] / (double) stores[i].getTotalReadLength()) * 100;
if (percentInrRNA[i] > 100) {
progressWarning("Percent in rRNA was >100 for " + stores[i]);
percentInrRNA[i] = 100;
}
}
result.addPercentageSet("Percent in rRNA", percentInrRNA);
}
double[] percentageMeasuredGenes = new double[stores.length];
for (int i = 0; i < measuredGenesCounts.length; i++) {
percentageMeasuredGenes[i] = measuredGenesCounts[i] / (double) mergedGenes.length * 100;
}
result.addPercentageSet("Percent Genes Measured", percentageMeasuredGenes);
// Work out the relative coverage
double[] percentageOfMaxCoverage = new double[stores.length];
long maxLength = 0;
for (int i = 0; i < stores.length; i++) {
if (stores[i].getTotalReadLength() > maxLength)
maxLength = stores[i].getTotalReadLength();
}
for (int i = 0; i < stores.length; i++) {
percentageOfMaxCoverage[i] = (stores[i].getTotalReadLength() * 100d) / maxLength;
}
result.addPercentageSet("Percentage of max data size", percentageOfMaxCoverage);
double[][] percentInChromosomes = new double[chromosomes.length][stores.length];
for (int c = 0; c < percentInChromosomes.length; c++) {
for (int i = 0; i < chromosomeBaseCounts[c].length; i++) {
percentInChromosomes[c][i] = (chromosomeBaseCounts[c][i] / (double) stores[i].getTotalReadLength()) * 100;
if (percentInChromosomes[c][i] > 100) {
progressWarning("Percent in " + chromosomes[c] + " was >100 for " + stores[i]);
percentInChromosomes[c][i] = 100;
}
}
}
for (int c = 0; c < percentInChromosomes.length; c++) {
result.addPercentageSet("Percent in " + chromosomes[c].name(), percentInChromosomes[c]);
}
double[] percentOnSenseStrand = null;
if (transcriptFeatureName != null) {
percentOnSenseStrand = new double[stores.length];
for (int i = 0; i < transcriptBaseCounts.length; i++) {
percentOnSenseStrand[i] = (transcriptSameStrandBaseCounts[i] / (double) transcriptBaseCounts[i]) * 100;
if (percentOnSenseStrand[i] > 100) {
progressWarning("Percent on sense strand was >100 for " + stores[i]);
percentOnSenseStrand[i] = 100;
}
}
result.addPercentageSet("Percent on sense strand", percentOnSenseStrand);
}
progressComplete(result);
}
use of uk.ac.babraham.SeqMonk.DataTypes.Probes.Probe in project SeqMonk by s-andrews.
the class HierarchicalClusterPanel method saveClusters.
public ProbeList saveClusters(int minClusterSize) {
ProbeList allClusterList = new ProbeList(probeList, "Hierarchical Clusters", "Hierarchical Clusters with R > " + rValue, null);
// Now we need to work our way through the connected clusters
// to make the appropriate sub-lists
ClusterPair[] connectedClusters = clusters.getConnectedClusters((float) rValue);
int runningCount = 0;
for (int subListIndex = 0; subListIndex < connectedClusters.length; subListIndex++) {
Integer[] indices = connectedClusters[subListIndex].getAllIndices();
// System.err.println("Looking at index "+subListIndex+" which has "+indices.length+" probes");
runningCount += indices.length;
// If we're starting before the currently visible set then we can skip
if (runningCount - indices.length < currentYStartIndex) {
// System.err.println("Skipping index "+(runningCount-indices.length)+" which is less than "+currentYStartIndex);
continue;
}
// If we're after the end of the current visible set we can stop all together
if (runningCount > (currentYEndIndex + 1)) {
// System.err.println("Stopping because "+runningCount+" is bigger than "+currentYEndIndex);
break;
}
// We may get rid of the list later if there are duplicates in it.
if (indices.length < minClusterSize)
continue;
Probe[] theseProbes = new Probe[indices.length];
for (int i = 0; i < theseProbes.length; i++) {
theseProbes[i] = probes[indices[i]];
}
// We used to sort and deduplicate these clusters so that we could be sure
// the number of probes is correct. We're now going to leave this alone
// (probe lists deduplicate internally) and annotate on the index rather
// than the r-value of the specific cluster.
ProbeList thisList = new ProbeList(allClusterList, "Cluster " + (subListIndex + 1), "HiC cluster list number " + (subListIndex + 1), "Index");
thisList.addProbe(theseProbes[0], (float) 0);
allClusterList.addProbe(theseProbes[0], null);
for (int i = 1; i < theseProbes.length; i++) {
if (theseProbes[i] == theseProbes[i - 1])
continue;
thisList.addProbe(theseProbes[i], (float) i);
allClusterList.addProbe(theseProbes[i], null);
}
}
return allClusterList;
}
Aggregations