Search in sources :

Example 71 with Probe

use of uk.ac.babraham.SeqMonk.DataTypes.Probes.Probe in project SeqMonk by s-andrews.

the class FeaturePositionSelectorPanel method getUpstreamProbes.

/**
 * Gets the set of additional upstream context regions for each feature given
 * the current options.  If the current options don't allow for any upstream
 * context then this returns null.
 * @return
 */
public Probe[] getUpstreamProbes() {
    // This is horribly inefficient since we make the core list
    // multiple times
    Probe[] coreProbes = getCoreProbes();
    if (positionType().equals("Over feature") && startOffset() > 0) {
        Vector<Probe> upstreamProbes = new Vector<Probe>();
        for (int p = 0; p < coreProbes.length; p++) {
            if (coreProbes[p].strand() == Probe.REVERSE) {
                int start = coreProbes[p].end() + 1;
                int end = coreProbes[p].end() + startOffset();
                if (start < 1 || end > coreProbes[p].chromosome().length())
                    continue;
                upstreamProbes.add(new Probe(coreProbes[p].chromosome(), start, end, coreProbes[p].strand()));
            } else {
                int start = coreProbes[p].start() - startOffset();
                int end = coreProbes[p].start() - 1;
                if (start < 1 || end > coreProbes[p].chromosome().length())
                    continue;
                upstreamProbes.add(new Probe(coreProbes[p].chromosome(), start, end, coreProbes[p].strand()));
            }
        }
        return upstreamProbes.toArray(new Probe[0]);
    } else {
        return null;
    }
}
Also used : Probe(uk.ac.babraham.SeqMonk.DataTypes.Probes.Probe) Vector(java.util.Vector)

Example 72 with Probe

use of uk.ac.babraham.SeqMonk.DataTypes.Probes.Probe in project SeqMonk by s-andrews.

the class FeaturePositionSelectorPanel method makeProbes.

private void makeProbes(Feature feature, Chromosome chromosome, Location location, Vector<Probe> newProbes, boolean justCore) {
    int start;
    int end;
    int strand = location.strand();
    if (ignoreDirection())
        strand = Location.UNKNOWN;
    if (positionType().equals("Upstream of feature")) {
        if (strand == Location.FORWARD || strand == Location.UNKNOWN) {
            start = location.start() - startOffset();
            end = location.start() + endOffset();
        } else {
            start = location.end() - endOffset();
            end = location.end() + startOffset();
        }
        Probe p = makeProbe(feature.name() + "_upstream", chromosome, start, end, strand);
        if (p != null) {
            newProbes.add(p);
        }
    } else if (positionType().equals("Over feature")) {
        if (strand == Location.FORWARD || strand == Location.UNKNOWN) {
            if (justCore && startOffset() > 0) {
                start = location.start();
            } else {
                start = location.start() - startOffset();
            }
            if (justCore && endOffset() > 0) {
                end = location.end();
            } else {
                end = location.end() + endOffset();
            }
        } else {
            if (justCore && endOffset() > 0) {
                start = location.start();
            } else {
                start = location.start() - endOffset();
            }
            if (justCore && startOffset() > 0) {
                end = location.end();
            } else {
                end = location.end() + startOffset();
            }
        }
        Probe p = makeProbe(feature.name(), chromosome, start, end, strand);
        if (p != null) {
            newProbes.add(p);
        }
    } else if (positionType().equals("Downstream of feature")) {
        if (strand == Location.FORWARD || strand == Location.UNKNOWN) {
            start = location.end() - startOffset();
            end = location.end() + endOffset();
        } else {
            start = location.start() - endOffset();
            end = location.start() + startOffset();
        }
        Probe p = makeProbe(feature.name() + "_downstream", chromosome, start, end, strand);
        if (p != null) {
            newProbes.add(p);
        }
    } else if (positionType().equals("Centered on feature")) {
        int center = location.start() + ((location.end() - location.start()) / 2);
        if (strand == Location.FORWARD || strand == Location.UNKNOWN) {
            start = center - startOffset();
            end = center + endOffset();
        } else {
            start = center - endOffset();
            end = center + startOffset();
        }
        Probe p = makeProbe(feature.name(), chromosome, start, end, strand);
        if (p != null) {
            newProbes.add(p);
        }
    } else {
        throw new IllegalStateException("Unknown position type " + positionType());
    }
}
Also used : Probe(uk.ac.babraham.SeqMonk.DataTypes.Probes.Probe)

Example 73 with Probe

use of uk.ac.babraham.SeqMonk.DataTypes.Probes.Probe in project SeqMonk by s-andrews.

the class FeaturePositionSelectorPanel method removeDuplicates.

/**
 * Removes the duplicates.
 *
 * @param original the original
 * @return the probe[]
 */
private Probe[] removeDuplicates(Probe[] original) {
    if (original == null || original.length == 0)
        return original;
    Arrays.sort(original);
    Vector<Probe> keepers = new Vector<Probe>();
    Probe lastProbe = original[0];
    keepers.add(original[0]);
    for (int i = 1; i < original.length; i++) {
        if (original[i].start() == lastProbe.start() && original[i].end() == lastProbe.end() && original[i].chromosome().equals(lastProbe.chromosome())) {
            // This is a duplicate
            continue;
        }
        keepers.add(original[i]);
        lastProbe = original[i];
    }
    return keepers.toArray(new Probe[0]);
}
Also used : Probe(uk.ac.babraham.SeqMonk.DataTypes.Probes.Probe) Vector(java.util.Vector)

Example 74 with Probe

use of uk.ac.babraham.SeqMonk.DataTypes.Probes.Probe in project SeqMonk by s-andrews.

the class HeatmapGenomePanel method paint.

public void paint(Graphics g) {
    super.paint(g);
    if (drawnPixels.length != getWidth() || drawnPixels[0].length != getHeight()) {
        drawnPixels = new boolean[getWidth()][getHeight()];
    } else {
        for (int i = 0; i < getWidth(); i++) {
            for (int j = 0; j < getHeight(); j++) {
                drawnPixels[i][j] = false;
            }
        }
    }
    g.setColor(Color.WHITE);
    g.fillRect(0, 0, getWidth(), getHeight());
    g.setColor(Color.BLACK);
    // Add a label at the top to signify current filters
    StringBuffer topLabel = new StringBuffer();
    // First include the position
    Chromosome xStartChr = getChrForPosition(currentXStartBp);
    if (xStartChr != null) {
        topLabel.append("X=Chr");
        topLabel.append(xStartChr.name());
        topLabel.append(":");
        topLabel.append(PositionFormat.formatLength(currentXStartBp - chromosomeBaseOffsets.get(xStartChr)));
        topLabel.append("-");
        Chromosome xEndChr = getChrForPosition(currentXEndBp);
        if (xStartChr != xEndChr) {
            topLabel.append("Chr");
            topLabel.append(xEndChr.name());
            topLabel.append(":");
        }
        topLabel.append(PositionFormat.formatLength(currentXEndBp - chromosomeBaseOffsets.get(xEndChr)));
        if (probeSortingOrder == null) {
            topLabel.append(" Y=Chr");
            Chromosome yStartChr = getChrForPosition(currentYStartBp);
            topLabel.append(yStartChr.name());
            topLabel.append(":");
            topLabel.append(PositionFormat.formatLength(currentYStartBp - chromosomeBaseOffsets.get(yStartChr)));
            topLabel.append("-");
            Chromosome yEndChr = getChrForPosition(currentYEndBp);
            if (yStartChr != yEndChr) {
                topLabel.append("Chr");
                topLabel.append(yEndChr.name());
                topLabel.append(":");
            }
            topLabel.append(PositionFormat.formatLength(currentYEndBp - chromosomeBaseOffsets.get(yEndChr)));
        }
        topLabel.append(" ");
    }
    // Now append any current limits
    if (matrix.currentMinStrength() > 0) {
        topLabel.append("Strength > ");
        topLabel.append(df.format(matrix.currentMinStrength()));
        topLabel.append(" ");
    }
    if (matrix.minDifference() > 0) {
        topLabel.append("Difference > ");
        topLabel.append(df.format(matrix.minDifference()));
        topLabel.append(" ");
    }
    if (matrix.currentMaxSignficance() < 1) {
        topLabel.append("P-value < ");
        topLabel.append(df.format(matrix.currentMaxSignficance()));
        topLabel.append(" ");
    }
    if (topLabel.length() == 0) {
        topLabel.append("No filters");
    }
    g.drawString(topLabel.toString(), getWidth() / 2 - (g.getFontMetrics().stringWidth(topLabel.toString()) / 2), 15 + (g.getFontMetrics().getAscent() / 2));
    Chromosome[] chrs = genome.getAllChromosomes();
    Arrays.sort(chrs);
    // Find the max height and width of the chromosome names in this genome
    if (maxNameWidth == 0) {
        nameHeight = g.getFontMetrics().getHeight();
        maxNameWidth = 0;
        for (int c = 0; c < chrs.length; c++) {
            int thisWidth = g.getFontMetrics().stringWidth(chrs[c].name());
            if (thisWidth > maxNameWidth)
                maxNameWidth = thisWidth;
        }
        // Give both the width and height a bit of breathing space
        nameHeight += 6;
        maxNameWidth += 6;
    }
    // Make the background of the plot black
    g.setColor(Color.WHITE);
    g.fillRect(maxNameWidth, 30, getWidth() - (maxNameWidth + 10), getHeight() - (nameHeight + 30));
    // Draw the actual data
    InteractionProbePair[] interactions = matrix.filteredInteractions();
    // Cache some values for use with the quantitation colouring
    double minQuantitatedValue;
    double maxQuantitatedValue;
    if (DisplayPreferences.getInstance().getScaleType() == DisplayPreferences.SCALE_TYPE_POSITIVE) {
        minQuantitatedValue = 0;
        maxQuantitatedValue = DisplayPreferences.getInstance().getMaxDataValue();
    } else {
        maxQuantitatedValue = DisplayPreferences.getInstance().getMaxDataValue();
        minQuantitatedValue = 0 - maxQuantitatedValue;
    }
    for (int i = 0; i < interactions.length; i++) {
        // the plot symmetrical
        for (int forRev = 0; forRev <= 1; forRev++) {
            int yIndex;
            Probe probe1;
            Probe probe2;
            if (forRev == 0) {
                yIndex = interactions[i].probe2Index();
                probe1 = interactions[i].probe1();
                probe2 = interactions[i].probe2();
            } else {
                yIndex = interactions[i].probe1Index();
                probe2 = interactions[i].probe1();
                probe1 = interactions[i].probe2();
            }
            if (probeSortingOrder != null) {
                yIndex = probeSortingOrder[yIndex];
                if (yIndex < currentYStartIndex || yIndex > currentYEndIndex) {
                    // System.err.println("Skipping for y zoom");
                    continue;
                }
            }
            int xStart = getXForPosition(chromosomeBaseOffsets.get(probe1.chromosome()) + probe1.start());
            if (xStart < maxNameWidth)
                continue;
            if (chromosomeBaseOffsets.get(probe1.chromosome()) + probe1.start() > currentXEndBp) {
                // System.err.println("Skipping for x end");
                continue;
            }
            int xEnd = getXForPosition(chromosomeBaseOffsets.get(probe1.chromosome()) + probe1.end());
            if (xEnd > getWidth() - 10)
                continue;
            if (chromosomeBaseOffsets.get(probe1.chromosome()) + probe1.end() < currentXStartBp) {
                // System.err.println("Skipping for x start");
                continue;
            }
            int yStart;
            int yEnd;
            if (probeSortingOrder == null) {
                if (chromosomeBaseOffsets.get(probe2.chromosome()) + probe2.start() > currentYEndBp)
                    continue;
                if (chromosomeBaseOffsets.get(probe2.chromosome()) + probe2.end() < currentYStartBp)
                    continue;
                yStart = getYForPosition(chromosomeBaseOffsets.get(probe2.chromosome()) + probe2.start());
                yEnd = getYForPosition(chromosomeBaseOffsets.get(probe2.chromosome()) + probe2.end());
            } else {
                yStart = getYForIndex(probeSortingOrder[yIndex]);
                yEnd = getYForIndex(probeSortingOrder[yIndex] + 1);
            }
            if (yStart > getHeight() - nameHeight) {
                continue;
            }
            if (yEnd < 30)
                continue;
            if (xEnd - xStart < 3) {
                xEnd += 1;
                xStart -= 1;
            }
            if (yStart - yEnd < 3) {
                // System.err.println("Expanding y selection");
                yEnd -= 1;
                yStart += 1;
            }
            // To be skipped there has to be colour at two corners and the middle.
            if (drawnPixels[xStart][yEnd] && drawnPixels[xEnd][yStart] && drawnPixels[xStart + ((xEnd - xStart) / 2)][yEnd + ((yStart - yEnd) / 2)]) {
                // System.err.println("Skipping for overlap with existing");
                continue;
            }
            switch(matrix.currentColourSetting()) {
                case HeatmapMatrix.COLOUR_BY_OBS_EXP:
                    if (matrix.initialMinStrength() < 1) {
                        // They're interested in depletion as well as enrichment.
                        // Make a symmetrical gradient around 0 and the max strength
                        g.setColor(matrix.colourGradient().getColor(Math.log10(interactions[i].strength()), Math.log10(1 / matrix.maxValue()), Math.log10(matrix.maxValue())));
                    } else {
                        g.setColor(matrix.colourGradient().getColor(Math.log10(interactions[i].strength() - matrix.initialMinStrength()), Math.log10(matrix.initialMinStrength()), Math.log10(matrix.maxValue() - matrix.initialMinStrength())));
                    }
                    break;
                case HeatmapMatrix.COLOUR_BY_INTERACTIONS:
                    g.setColor(matrix.colourGradient().getColor(interactions[i].absolute(), matrix.initialMinAbsolute(), 50));
                    break;
                case HeatmapMatrix.COLOUR_BY_P_VALUE:
                    g.setColor(matrix.colourGradient().getColor(Math.log10(interactions[i].signficance()) * -10, Math.log10(matrix.initialMaxSignificance()) * -10, 50));
                    break;
                case HeatmapMatrix.COLOUR_BY_QUANTITATION:
                    Probe probeForQuantitation;
                    if (forRev == 0) {
                        probeForQuantitation = interactions[i].lowestProbe();
                    } else {
                        probeForQuantitation = interactions[i].highestProbe();
                    }
                    try {
                        g.setColor(matrix.colourGradient().getColor(((DataStore) dataSet).getValueForProbe(probeForQuantitation), minQuantitatedValue, maxQuantitatedValue));
                    } catch (SeqMonkException e) {
                    }
                    break;
            }
            g.fillRect(xStart, yEnd, xEnd - xStart, yStart - yEnd);
            // If we're looking for selected probes check this now.
            if (displayXProbe || displayYProbe) {
                if (xStart <= displayX && xEnd >= displayX && yEnd <= displayY && yStart >= displayY) {
                    if (displayXProbe) {
                        DisplayPreferences.getInstance().setLocation(interactions[i].probe1().chromosome(), interactions[i].probe1().packedPosition());
                        displayXProbe = false;
                    }
                    if (displayYProbe) {
                        DisplayPreferences.getInstance().setLocation(interactions[i].probe2().chromosome(), interactions[i].probe2().packedPosition());
                        displayYProbe = false;
                    }
                }
            }
            // them again
            for (int x = Math.min(xStart, xEnd); x <= Math.min(xStart, xEnd) + Math.abs(xStart - xEnd); x++) {
                for (int y = Math.min(yStart, yEnd); y <= Math.min(yStart, yEnd) + Math.abs(yStart - yEnd); y++) {
                    drawnPixels[x][y] = true;
                }
            }
        }
    }
    // System.err.println("Skipped "+skipped+" and drew "+drawn+" out of "+(interactions.length*2)+" interactions");
    // Draw the chromosome lines
    g.setColor(Color.GRAY);
    // Draw Chr Lines on X axis
    long runningGenomeLength = 0;
    for (int c = 0; c < chrs.length; c++) {
        int startPos = getXForPosition(runningGenomeLength);
        int endPos = getXForPosition(runningGenomeLength + chrs[c].length());
        if (c > 0) {
            if (startPos >= maxNameWidth && startPos <= getWidth() - 10) {
                g.drawLine(startPos, 30, startPos, getHeight() - nameHeight);
            }
        }
        if (c + 1 == chrs.length) {
            if (endPos >= maxNameWidth && endPos <= getWidth() - 10) {
                g.drawLine(endPos, 30, endPos, getHeight() - nameHeight);
            }
        }
        int nameWidth = g.getFontMetrics().stringWidth(chrs[c].name());
        g.drawString(chrs[c].name(), (startPos + ((endPos - startPos) / 2)) - (nameWidth / 2), getHeight() - 3);
        runningGenomeLength += chrs[c].length();
    }
    // Draw Chr Lines on Y axis
    if (probeSortingOrder == null) {
        runningGenomeLength = 0;
        for (int c = 0; c < chrs.length; c++) {
            int startPos = getYForPosition(runningGenomeLength);
            int endPos = getYForPosition(runningGenomeLength + chrs[c].length());
            if (c > 0) {
                if (startPos <= getHeight() - nameHeight && startPos >= 30) {
                    g.drawLine(maxNameWidth, startPos, getWidth() - 10, startPos);
                }
            }
            if (c + 1 == chrs.length) {
                if (endPos <= getHeight() - nameHeight && endPos >= 30) {
                    g.drawLine(maxNameWidth, endPos, getWidth() - 10, endPos);
                }
            }
            int nameWidth = g.getFontMetrics().stringWidth(chrs[c].name());
            g.drawString(chrs[c].name(), (maxNameWidth / 2) - (nameWidth / 2), (endPos + ((startPos - endPos) / 2)) + (g.getFontMetrics().getAscent() / 2));
            runningGenomeLength += chrs[c].length();
        }
    } else {
        int runningListPosition = 0;
        for (int l = 0; l < clusterIntervals.length; l++) {
            runningListPosition += clusterIntervals[l];
            if (runningListPosition < currentYStartIndex)
                continue;
            if (runningListPosition > currentYEndIndex)
                break;
            int pos = getYForIndex(runningListPosition);
            g.drawLine(maxNameWidth, pos, getWidth() - 10, pos);
        }
    }
    // Draw the axes
    g.drawLine(maxNameWidth, getHeight() - nameHeight, getWidth() - 10, getHeight() - nameHeight);
    g.drawLine(maxNameWidth, getHeight() - nameHeight, maxNameWidth, 30);
    // Draw a selection if we're making one
    if (makingSelection) {
        g.setColor(ColourScheme.DRAGGED_SELECTION);
        g.drawRect(Math.min(selectionEndX, selectionStartX), Math.min(selectionEndY, selectionStartY), Math.abs(selectionEndX - selectionStartX), Math.abs(selectionEndY - selectionStartY));
    }
    displayXProbe = false;
    displayYProbe = false;
}
Also used : InteractionProbePair(uk.ac.babraham.SeqMonk.DataTypes.Interaction.InteractionProbePair) DataStore(uk.ac.babraham.SeqMonk.DataTypes.DataStore) HiCDataStore(uk.ac.babraham.SeqMonk.DataTypes.HiCDataStore) Chromosome(uk.ac.babraham.SeqMonk.DataTypes.Genome.Chromosome) SeqMonkException(uk.ac.babraham.SeqMonk.SeqMonkException) Probe(uk.ac.babraham.SeqMonk.DataTypes.Probes.Probe)

Example 75 with Probe

use of uk.ac.babraham.SeqMonk.DataTypes.Probes.Probe in project SeqMonk by s-andrews.

the class CisTransScatterPlotPanel 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();
    Arrays.sort(probes);
    for (int p = 0; p < probes.length; p++) {
        if (filterChromosome != null && probes[p].chromosome() != filterChromosome)
            continue;
        float xValue = xData[p];
        float yValue = yData[p];
        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 {
            grid[x][y].count++;
            grid[x][y].setProbe(null);
        }
    }
    // 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 = nonred[0].count;
    int 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++) {
        nonred[i].color = gradient.getColor(nonred[i].count, minCount, maxCount);
    }
    nonRedundantValues = nonred;
    lastNonredWidth = getWidth();
    lastNonredHeight = getHeight();
// System.out.println("Nonred was "+nonRedundantValues.length+" from "+probes.length);
}
Also used : HotColdColourGradient(uk.ac.babraham.SeqMonk.Gradients.HotColdColourGradient) Probe(uk.ac.babraham.SeqMonk.DataTypes.Probes.Probe) ColourGradient(uk.ac.babraham.SeqMonk.Gradients.ColourGradient) HotColdColourGradient(uk.ac.babraham.SeqMonk.Gradients.HotColdColourGradient)

Aggregations

Probe (uk.ac.babraham.SeqMonk.DataTypes.Probes.Probe)125 ProbeList (uk.ac.babraham.SeqMonk.DataTypes.Probes.ProbeList)54 SeqMonkException (uk.ac.babraham.SeqMonk.SeqMonkException)52 Vector (java.util.Vector)48 Chromosome (uk.ac.babraham.SeqMonk.DataTypes.Genome.Chromosome)47 ProbeSet (uk.ac.babraham.SeqMonk.DataTypes.Probes.ProbeSet)26 DataStore (uk.ac.babraham.SeqMonk.DataTypes.DataStore)21 Feature (uk.ac.babraham.SeqMonk.DataTypes.Genome.Feature)20 HashSet (java.util.HashSet)9 Location (uk.ac.babraham.SeqMonk.DataTypes.Genome.Location)9 File (java.io.File)8 PrintWriter (java.io.PrintWriter)8 ProbeTTestValue (uk.ac.babraham.SeqMonk.Analysis.Statistics.ProbeTTestValue)8 SplitLocation (uk.ac.babraham.SeqMonk.DataTypes.Genome.SplitLocation)7 HiCDataStore (uk.ac.babraham.SeqMonk.DataTypes.HiCDataStore)7 BufferedReader (java.io.BufferedReader)6 FileReader (java.io.FileReader)6 Hashtable (java.util.Hashtable)6 DataSet (uk.ac.babraham.SeqMonk.DataTypes.DataSet)6 HiCHitCollection (uk.ac.babraham.SeqMonk.DataTypes.Sequence.HiCHitCollection)6