Search in sources :

Example 51 with Chromosome

use of uk.ac.babraham.SeqMonk.DataTypes.Genome.Chromosome 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 52 with Chromosome

use of uk.ac.babraham.SeqMonk.DataTypes.Genome.Chromosome in project SeqMonk by s-andrews.

the class HeatmapGenomePanel method getChrForPosition.

private Chromosome getChrForPosition(long basePosition) {
    // We find the maximum offset which is lower than this basePosition
    Enumeration<Chromosome> en = chromosomeBaseOffsets.keys();
    long highest = -1;
    Chromosome chr = null;
    while (en.hasMoreElements()) {
        Chromosome thisChr = en.nextElement();
        long offset = basePosition - chromosomeBaseOffsets.get(thisChr);
        if (offset >= 0 && chromosomeBaseOffsets.get(thisChr) > highest) {
            highest = chromosomeBaseOffsets.get(thisChr);
            chr = thisChr;
        }
    }
    return chr;
}
Also used : Chromosome(uk.ac.babraham.SeqMonk.DataTypes.Genome.Chromosome)

Example 53 with Chromosome

use of uk.ac.babraham.SeqMonk.DataTypes.Genome.Chromosome in project SeqMonk by s-andrews.

the class GotoWindowDialog method doGoto.

/**
 * Do goto.
 */
private void doGoto() {
    Chromosome chr = (Chromosome) chromosome.getSelectedItem();
    int centreValue = chr.length() / 2;
    int windowValue = 1000;
    int startValue;
    int endValue;
    if (centre.getText().length() > 0) {
        centreValue = Integer.parseInt(centre.getText());
    }
    if (window.getText().length() > 0) {
        windowValue = Integer.parseInt(window.getText());
    }
    if (windowValue < 1)
        windowValue = 1;
    startValue = centreValue - (windowValue / 2);
    endValue = startValue + (windowValue - 1);
    DisplayPreferences.getInstance().setLocation(chr, SequenceRead.packPosition(startValue, endValue, Location.UNKNOWN));
    setVisible(false);
    dispose();
}
Also used : Chromosome(uk.ac.babraham.SeqMonk.DataTypes.Genome.Chromosome)

Example 54 with Chromosome

use of uk.ac.babraham.SeqMonk.DataTypes.Genome.Chromosome in project SeqMonk by s-andrews.

the class GotoDialog method doGoto.

/**
 * Do goto.
 */
private void doGoto() {
    Chromosome chr = (Chromosome) chromosome.getSelectedItem();
    int startValue = 1;
    int endValue = chr.length();
    if (start.getText().length() > 0) {
        startValue = Integer.parseInt(start.getText());
    }
    if (end.getText().length() > 0) {
        endValue = Integer.parseInt(end.getText());
        if (endValue > chr.length())
            endValue = chr.length();
    }
    if (startValue > endValue) {
        int temp = startValue;
        startValue = endValue;
        endValue = temp;
    }
    if (endValue - startValue < 5) {
        // This is too small, don't do it
        return;
    }
    DisplayPreferences.getInstance().setLocation(chr, SequenceRead.packPosition(startValue, endValue, Location.UNKNOWN));
    setVisible(false);
    dispose();
}
Also used : Chromosome(uk.ac.babraham.SeqMonk.DataTypes.Genome.Chromosome)

Example 55 with Chromosome

use of uk.ac.babraham.SeqMonk.DataTypes.Genome.Chromosome in project SeqMonk by s-andrews.

the class FeaturePositionSelectorPanel method getProbes.

/**
 * Gets the set of probes with appropriate context for the options
 * currently set.
 * @return
 */
public Probe[] getProbes() {
    Chromosome[] chromosomes = collection.genome().getAllChromosomes();
    Vector<Probe> newProbes = new Vector<Probe>();
    for (int c = 0; c < chromosomes.length; c++) {
        Vector<Feature> allFeatures = new Vector<Feature>();
        String[] selectedFeatureTypes = selectedFeatureTypes();
        for (int f = 0; f < selectedFeatureTypes.length; f++) {
            Feature[] features = collection.genome().annotationCollection().getFeaturesForType(chromosomes[c], selectedFeatureTypes[f]);
            for (int i = 0; i < features.length; i++) {
                allFeatures.add(features[i]);
            }
        }
        Feature[] features = allFeatures.toArray(new Feature[0]);
        for (int f = 0; f < features.length; f++) {
            if (useSubFeatures()) {
                // We need to split this up so get the sub-features
                if (features[f].location() instanceof SplitLocation) {
                    SplitLocation location = (SplitLocation) features[f].location();
                    Location[] subLocations = location.subLocations();
                    if (useExonSubfeatures()) {
                        // System.err.println("Making exon probes");
                        for (int s = 0; s < subLocations.length; s++) {
                            makeProbes(features[f], chromosomes[c], subLocations[s], newProbes, false);
                        }
                    } else {
                        // We're making introns
                        for (int s = 1; s < subLocations.length; s++) {
                            makeProbes(features[f], chromosomes[c], new Location(subLocations[s - 1].end() + 1, subLocations[s].start() - 1, features[f].location().strand()), newProbes, false);
                        }
                    }
                } else {
                    if (useExonSubfeatures()) {
                        // We can still make a single probe
                        makeProbes(features[f], chromosomes[c], features[f].location(), newProbes, false);
                    }
                // If we're making introns then we're stuffed and we give up.
                }
            } else {
                makeProbes(features[f], chromosomes[c], features[f].location(), newProbes, false);
            }
        }
    }
    Probe[] finalList = newProbes.toArray(new Probe[0]);
    if (removeDuplicates()) {
        finalList = removeDuplicates(finalList);
    }
    return finalList;
}
Also used : Chromosome(uk.ac.babraham.SeqMonk.DataTypes.Genome.Chromosome) Probe(uk.ac.babraham.SeqMonk.DataTypes.Probes.Probe) Feature(uk.ac.babraham.SeqMonk.DataTypes.Genome.Feature) SplitLocation(uk.ac.babraham.SeqMonk.DataTypes.Genome.SplitLocation) Vector(java.util.Vector) Location(uk.ac.babraham.SeqMonk.DataTypes.Genome.Location) SplitLocation(uk.ac.babraham.SeqMonk.DataTypes.Genome.SplitLocation)

Aggregations

Chromosome (uk.ac.babraham.SeqMonk.DataTypes.Genome.Chromosome)78 Probe (uk.ac.babraham.SeqMonk.DataTypes.Probes.Probe)47 Vector (java.util.Vector)36 Feature (uk.ac.babraham.SeqMonk.DataTypes.Genome.Feature)23 SeqMonkException (uk.ac.babraham.SeqMonk.SeqMonkException)23 ProbeSet (uk.ac.babraham.SeqMonk.DataTypes.Probes.ProbeSet)22 ProbeList (uk.ac.babraham.SeqMonk.DataTypes.Probes.ProbeList)12 DataStore (uk.ac.babraham.SeqMonk.DataTypes.DataStore)11 DataSet (uk.ac.babraham.SeqMonk.DataTypes.DataSet)8 ReadsWithCounts (uk.ac.babraham.SeqMonk.DataTypes.Sequence.ReadsWithCounts)8 Location (uk.ac.babraham.SeqMonk.DataTypes.Genome.Location)7 SplitLocation (uk.ac.babraham.SeqMonk.DataTypes.Genome.SplitLocation)7 ProgressListener (uk.ac.babraham.SeqMonk.DataTypes.ProgressListener)7 HiCHitCollection (uk.ac.babraham.SeqMonk.DataTypes.Sequence.HiCHitCollection)7 IOException (java.io.IOException)6 File (java.io.File)5 Hashtable (java.util.Hashtable)5 HiCDataStore (uk.ac.babraham.SeqMonk.DataTypes.HiCDataStore)5 QuantitationStrandType (uk.ac.babraham.SeqMonk.DataTypes.Sequence.QuantitationStrandType)5 PairedDataSet (uk.ac.babraham.SeqMonk.DataTypes.PairedDataSet)4