Search in sources :

Example 1 with InteractionProbePair

use of uk.ac.babraham.SeqMonk.DataTypes.Interaction.InteractionProbePair in project SeqMonk by s-andrews.

the class HeatmapProbeListPanel method saveData.

public void saveData(File file) throws IOException {
    PrintWriter pr = new PrintWriter(new BufferedWriter(new FileWriter(file)));
    StringBuffer sb = new StringBuffer();
    sb.append("Probe 1");
    sb.append("\t");
    sb.append("Chr1");
    sb.append("\t");
    sb.append("Start1");
    sb.append("\t");
    sb.append("End1");
    sb.append("\t");
    sb.append("Strand1");
    sb.append("\t");
    sb.append("Probe 2");
    sb.append("\t");
    sb.append("Chr2");
    sb.append("\t");
    sb.append("Start2");
    sb.append("\t");
    sb.append("End2");
    sb.append("\t");
    sb.append("Strand2");
    sb.append("\t");
    sb.append(dataSet);
    sb.append("\n");
    pr.print(sb.toString());
    InteractionProbePair[] interactions = matrix.filteredInteractions();
    for (int i = 0; i < interactions.length; i++) {
        int xStart = getXForPosition(interactions[i].probe1Index());
        if (xStart < maxNameWidth)
            continue;
        int xEnd = getXForPosition(interactions[i].probe1Index() + 1);
        if (xEnd > getWidth() - 10)
            continue;
        int yStart = getYForPosition(interactions[i].probe2Index());
        if (yStart > getHeight() - nameHeight)
            continue;
        int yEnd = getYForPosition(interactions[i].probe2Index() + 1);
        if (yEnd < 30)
            continue;
        sb = new StringBuffer();
        sb.append(interactions[i].probe1());
        sb.append("\t");
        sb.append(interactions[i].probe1().chromosome());
        sb.append("\t");
        sb.append(interactions[i].probe1().start());
        sb.append("\t");
        sb.append(interactions[i].probe1().end());
        sb.append("\t");
        if (interactions[i].probe1().strand() == Probe.FORWARD) {
            sb.append("+");
        } else if (interactions[i].probe1().strand() == Probe.REVERSE) {
            sb.append("");
        }
        sb.append("\t");
        sb.append(interactions[i].probe2());
        sb.append("\t");
        sb.append(interactions[i].probe2().chromosome());
        sb.append("\t");
        sb.append(interactions[i].probe2().start());
        sb.append("\t");
        sb.append(interactions[i].probe2().end());
        sb.append("\t");
        if (interactions[i].probe2().strand() == Probe.FORWARD) {
            sb.append("+");
        } else if (interactions[i].probe2().strand() == Probe.REVERSE) {
            sb.append("");
        }
        sb.append("\t");
        sb.append(interactions[i].strength());
        sb.append("\n");
        pr.print(sb.toString());
    }
    pr.close();
}
Also used : InteractionProbePair(uk.ac.babraham.SeqMonk.DataTypes.Interaction.InteractionProbePair) FileWriter(java.io.FileWriter) PrintWriter(java.io.PrintWriter) BufferedWriter(java.io.BufferedWriter)

Example 2 with InteractionProbePair

use of uk.ac.babraham.SeqMonk.DataTypes.Interaction.InteractionProbePair in project SeqMonk by s-andrews.

the class HeatmapProbeListPanel 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();
    // 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));
    // Find the max height and width of the probe list names in this genome
    if (maxNameWidth == 0) {
        nameHeight = g.getFontMetrics().getHeight();
        maxNameWidth = 0;
        long runningBaseOffset = 0;
        for (int l = 0; l < probeLists.length; l++) {
            probeListIndexOffsets.put(probeLists[l], runningBaseOffset);
            int thisWidth = g.getFontMetrics().stringWidth(probeLists[l].name());
            if (thisWidth > maxNameWidth)
                maxNameWidth = thisWidth;
            runningBaseOffset += probeLists[l].getAllProbes().length;
        }
        // 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++) {
        for (int forRev = 0; forRev <= 1; forRev++) {
            int probe1Index;
            int probe2Index;
            if (forRev == 0) {
                probe1Index = interactions[i].probe1Index();
                probe2Index = interactions[i].probe2Index();
            } else {
                probe2Index = interactions[i].probe1Index();
                probe1Index = interactions[i].probe2Index();
            }
            int xIndex = probe1Index;
            if (probeSortingOrder != null) {
                xIndex = probeSortingOrder[xIndex];
            }
            if (xIndex < currentXStartIndex)
                continue;
            if (xIndex > currentXEndIndex)
                continue;
            int xStart = getXForPosition(xIndex);
            if (xStart < maxNameWidth)
                continue;
            int xEnd = getXForPosition(xIndex + 1);
            if (xEnd > getWidth() - 10)
                continue;
            int yIndex = probe2Index;
            if (probeSortingOrder != null) {
                yIndex = probeSortingOrder[yIndex];
            }
            if (yIndex < currentYStartIndex)
                continue;
            if (yIndex > currentYEndIndex)
                continue;
            int yStart = getYForPosition(yIndex);
            if (yStart > getHeight() - nameHeight)
                continue;
            int yEnd = getYForPosition(yIndex + 1);
            if (yEnd < 30)
                continue;
            if (xEnd - xStart < 3) {
                xEnd += 1;
                xStart -= 1;
            }
            if (yEnd - yStart < 3) {
                yEnd -= 1;
                yStart += 1;
            }
            // See if we can skip drawing this because something else is already there
            if (drawnPixels[xStart][yEnd] && drawnPixels[xEnd][yStart]) {
                continue;
            }
            switch(matrix.currentColourSetting()) {
                case HeatmapMatrix.COLOUR_BY_OBS_EXP:
                    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);
            // 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+" interactions");
    // Draw the probe list lines
    g.setColor(Color.GRAY);
    // lines but we will bracket around related groups.
    if (currentCluster == null) {
        // Draw Probe List Lines on X axis
        int runningGenomeLength = 0;
        for (int l = 0; l < probeLists.length; l++) {
            int startPos = getXForPosition(runningGenomeLength);
            int endPos = getXForPosition(runningGenomeLength + probeLists[l].getAllProbes().length);
            if (l > 0) {
                if (startPos >= maxNameWidth && startPos <= getWidth() - 10) {
                    g.drawLine(startPos, 30, startPos, getHeight() - nameHeight);
                }
            }
            if (l + 1 == probeLists.length) {
                if (endPos >= maxNameWidth && endPos <= getWidth() - 10) {
                    g.drawLine(endPos, 30, endPos, getHeight() - nameHeight);
                }
            }
            int nameWidth = g.getFontMetrics().stringWidth(probeLists[l].name());
            g.drawString(probeLists[l].name(), (startPos + ((endPos - startPos) / 2)) - (nameWidth / 2), getHeight() - 3);
            runningGenomeLength += probeLists[l].getAllProbes().length;
        }
        // Draw Chr Lines on Y axis
        runningGenomeLength = 0;
        for (int l = 0; l < probeLists.length; l++) {
            int startPos = getYForPosition(runningGenomeLength);
            int endPos = getYForPosition(runningGenomeLength + probeLists[l].getAllProbes().length);
            if (l > 0) {
                if (startPos <= getHeight() - nameHeight && startPos >= 30) {
                    g.drawLine(maxNameWidth, startPos, getWidth() - 10, startPos);
                }
            }
            if (l + 1 == probeLists.length) {
                if (endPos <= getHeight() - nameHeight && endPos >= 30) {
                    g.drawLine(maxNameWidth, endPos, getWidth() - 10, endPos);
                }
            }
            int nameWidth = g.getFontMetrics().stringWidth(probeLists[l].name());
            g.drawString(probeLists[l].name(), (maxNameWidth / 2) - (nameWidth / 2), (endPos + ((startPos - endPos) / 2)) + (g.getFontMetrics().getAscent() / 2));
            runningGenomeLength += probeLists[l].getAllProbes().length;
        }
    } else // If we are clustered then we draw bracketed sets around the current R value cutoff
    {
        // Draw Cluster Lines on X axis
        int runningListPosition = 0;
        for (int l = 0; l < clusterIntervals.length; l++) {
            runningListPosition += clusterIntervals[l];
            if (runningListPosition < currentXStartIndex)
                continue;
            if (runningListPosition > currentXEndIndex)
                break;
            int pos = getXForPosition(runningListPosition);
            g.drawLine(pos, 30, pos, getHeight() - nameHeight);
        }
        // Draw Cluster Lines on Y axis
        runningListPosition = 0;
        for (int l = 0; l < clusterIntervals.length; l++) {
            runningListPosition += clusterIntervals[l];
            if (runningListPosition < currentYStartIndex)
                continue;
            if (runningListPosition > currentYEndIndex)
                break;
            int pos = getYForPosition(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));
    }
}
Also used : InteractionProbePair(uk.ac.babraham.SeqMonk.DataTypes.Interaction.InteractionProbePair) DataStore(uk.ac.babraham.SeqMonk.DataTypes.DataStore) HiCDataStore(uk.ac.babraham.SeqMonk.DataTypes.HiCDataStore) SeqMonkException(uk.ac.babraham.SeqMonk.SeqMonkException) Probe(uk.ac.babraham.SeqMonk.DataTypes.Probes.Probe)

Example 3 with InteractionProbePair

use of uk.ac.babraham.SeqMonk.DataTypes.Interaction.InteractionProbePair in project SeqMonk by s-andrews.

the class HeatmapGenomePanel method saveData.

public void saveData(File file) throws IOException {
    PrintWriter pr = new PrintWriter(new BufferedWriter(new FileWriter(file)));
    StringBuffer sb = new StringBuffer();
    sb.append("Probe 1");
    sb.append("\t");
    sb.append("Chr1");
    sb.append("\t");
    sb.append("Start1");
    sb.append("\t");
    sb.append("End1");
    sb.append("\t");
    sb.append("Strand1");
    sb.append("\t");
    sb.append("Probe 2");
    sb.append("\t");
    sb.append("Chr2");
    sb.append("\t");
    sb.append("Start2");
    sb.append("\t");
    sb.append("End2");
    sb.append("\t");
    sb.append("Strand2");
    sb.append("\t");
    sb.append(dataSet);
    sb.append("\n");
    pr.print(sb.toString());
    InteractionProbePair[] interactions = matrix.filteredInteractions();
    for (int i = 0; i < interactions.length; i++) {
        // Check that this interaction is within the currently visible region
        long xStart = chromosomeBaseOffsets.get(interactions[i].probe1().chromosome()) + interactions[i].probe1().start();
        if (xStart < currentXStartBp || xStart > currentXEndBp)
            continue;
        long xEnd = chromosomeBaseOffsets.get(interactions[i].probe1().chromosome()) + interactions[i].probe1().end();
        if (xEnd < currentXStartBp || xEnd > currentXEndBp)
            continue;
        long yStart = chromosomeBaseOffsets.get(interactions[i].probe2().chromosome()) + interactions[i].probe2().start();
        if (yStart < currentYStartBp || yStart > currentYEndBp)
            continue;
        long yEnd = chromosomeBaseOffsets.get(interactions[i].probe2().chromosome()) + interactions[i].probe2().end();
        if (yEnd < currentYStartBp || yEnd > currentYEndBp)
            continue;
        sb = new StringBuffer();
        sb.append(interactions[i].probe1());
        sb.append("\t");
        sb.append(interactions[i].probe1().chromosome());
        sb.append("\t");
        sb.append(interactions[i].probe1().start());
        sb.append("\t");
        sb.append(interactions[i].probe1().end());
        sb.append("\t");
        if (interactions[i].probe1().strand() == Probe.FORWARD) {
            sb.append("+");
        } else if (interactions[i].probe1().strand() == Probe.REVERSE) {
            sb.append("");
        }
        sb.append("\t");
        sb.append(interactions[i].probe2());
        sb.append("\t");
        sb.append(interactions[i].probe2().chromosome());
        sb.append("\t");
        sb.append(interactions[i].probe2().start());
        sb.append("\t");
        sb.append(interactions[i].probe2().end());
        sb.append("\t");
        if (interactions[i].probe2().strand() == Probe.FORWARD) {
            sb.append("+");
        } else if (interactions[i].probe2().strand() == Probe.REVERSE) {
            sb.append("");
        }
        sb.append("\t");
        sb.append(interactions[i].strength());
        sb.append("\n");
        pr.print(sb.toString());
    }
    pr.close();
}
Also used : InteractionProbePair(uk.ac.babraham.SeqMonk.DataTypes.Interaction.InteractionProbePair) FileWriter(java.io.FileWriter) PrintWriter(java.io.PrintWriter) BufferedWriter(java.io.BufferedWriter)

Example 4 with InteractionProbePair

use of uk.ac.babraham.SeqMonk.DataTypes.Interaction.InteractionProbePair in project SeqMonk by s-andrews.

the class AnnotatedInteractionReport method run.

/* (non-Javadoc)
	 * @see java.lang.Runnable#run()
	 */
public void run() {
    String annotationTypeValue = (String) annotationType.getSelectedItem();
    int distanceLimit = 0;
    // Check what to do with unannotated probes
    boolean includeAll = true;
    if (((String) excludes.getSelectedItem()).equals("Exclude")) {
        includeAll = false;
    }
    String annotationPositionValue = (String) annotationPosition.getSelectedItem();
    // We're going to set up a set of booleans which tell us which kinds
    // of relationships we're allowed to look for later.
    boolean surrounding = true;
    boolean upstream = true;
    boolean downstream = true;
    boolean matchname = false;
    if (annotationPositionValue.equals("[Don't annotate]")) {
        upstream = false;
        downstream = false;
        surrounding = false;
    } else if (annotationPositionValue.equals("overlapping")) {
        upstream = false;
        downstream = false;
    } else if (annotationPositionValue.equals("surrounding or upstream")) {
        downstream = false;
    } else if (annotationPositionValue.equals("surrounding or downstream")) {
        upstream = false;
    } else if (annotationPositionValue.equals("upstream")) {
        surrounding = false;
        downstream = false;
    } else if (annotationPositionValue.equals("downstream")) {
        surrounding = false;
        upstream = false;
    } else if (annotationPositionValue.equals("closest")) {
    // Leave things as they are!
    } else if (annotationPositionValue.equals("name matched")) {
        matchname = true;
        upstream = false;
        surrounding = false;
        downstream = false;
    } else {
        System.err.println("Didn't recognise position value '" + annotationPositionValue + "'");
    }
    // surrounding.
    if (!annotationPositionValue.equals("surrounding")) {
        if (annotationLimit.getText().length() > 0) {
            distanceLimit = Integer.parseInt(annotationLimit.getText());
        }
    }
    // Since we're going to be making the annotations on the
    // basis of position we should go through all probes one
    // chromosome at a time.
    Feature[] features = null;
    Chromosome lastChr = null;
    // We can now step through the probes looking for the best feature match
    for (int p = 0; p < probes.length; p++) {
        if (cancel) {
            progressCancelled();
            return;
        }
        if (p % 100 == 0) {
            progressUpdated("Processed " + p + " probes", p, probes.length);
        }
        if (!probes[p].chromosome().equals(lastChr)) {
            features = collection.genome().annotationCollection().getFeaturesForType(probes[p].chromosome(), annotationTypeValue);
            lastChr = probes[p].chromosome();
        }
        String nameWithoutExtensions = "";
        String nameWithoutTranscript = "";
        if (matchname) {
            nameWithoutExtensions = probes[p].name().replaceFirst("_upstream$", "").replaceAll("_downstream$", "").replaceAll("_gene$", "");
            nameWithoutTranscript = nameWithoutExtensions.replaceAll("-\\d\\d\\d$", "");
        }
        Feature bestFeature = null;
        int closestDistance = 0;
        for (int f = 0; f < features.length; f++) {
            if (matchname) {
                // Simplest check is if the name matches exactly
                if (features[f].name().equals(probes[p].name()) || features[f].name().equals(nameWithoutExtensions) || features[f].name().equals(nameWithoutTranscript)) {
                    bestFeature = features[f];
                    closestDistance = 0;
                    break;
                }
            }
            if (surrounding) {
                if (probes[p].start() <= features[f].location().end() && probes[p].end() >= features[f].location().start()) {
                    bestFeature = features[f];
                    closestDistance = 0;
                    // Once we've found an overlapping feature we quit.
                    break;
                }
            }
            if (downstream) {
                // Check if the feature is downstream
                // Get the distance to the start
                int d = 0;
                if (features[f].location().strand() == Location.FORWARD) {
                    d = features[f].location().start() - probes[p].end();
                } else {
                    d = probes[p].start() - features[f].location().end();
                }
                if (d >= 0) {
                    if (d > distanceLimit || (bestFeature != null && d > closestDistance)) {
                        continue;
                    }
                    // See if this is the closest feature we have so far...
                    if (bestFeature == null || d < closestDistance) {
                        bestFeature = features[f];
                        closestDistance = d;
                    }
                    continue;
                }
            }
            if (upstream) {
                // Check if the feature is upstream
                // Get the distance to the start
                int d = 0;
                if (features[f].location().strand() == Location.FORWARD) {
                    d = probes[p].start() - features[f].location().end();
                } else {
                    d = features[f].location().start() - probes[p].end();
                }
                if (d >= 0) {
                    if (d > distanceLimit || (bestFeature != null && d > closestDistance)) {
                        continue;
                    }
                    // See if this is the closest feature we have so far...
                    if (bestFeature == null || d < closestDistance) {
                        bestFeature = features[f];
                        closestDistance = d;
                    }
                    continue;
                }
            }
        }
        if (bestFeature == null) {
            continue;
        }
        probeAnnotations.put(probes[p], bestFeature);
    }
    if (!includeAll) {
        // We need to filter the interaction list to include only those which
        // have annotations on both probes
        Vector<InteractionProbePair> filteredInteractions = new Vector<InteractionProbePair>();
        for (int i = 0; i < interactions.length; i++) {
            if (probeAnnotations.containsKey(interactions[i].probe1()) && probeAnnotations.containsKey(interactions[i].probe2())) {
                filteredInteractions.add(interactions[i]);
            }
        }
        interactions = filteredInteractions.toArray(new InteractionProbePair[0]);
    }
    TableModel model = new AnnotationTableModel();
    reportComplete(model);
}
Also used : InteractionProbePair(uk.ac.babraham.SeqMonk.DataTypes.Interaction.InteractionProbePair) Chromosome(uk.ac.babraham.SeqMonk.DataTypes.Genome.Chromosome) Feature(uk.ac.babraham.SeqMonk.DataTypes.Genome.Feature) Vector(java.util.Vector) AbstractTableModel(javax.swing.table.AbstractTableModel) TableModel(javax.swing.table.TableModel)

Example 5 with InteractionProbePair

use of uk.ac.babraham.SeqMonk.DataTypes.Interaction.InteractionProbePair 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)

Aggregations

InteractionProbePair (uk.ac.babraham.SeqMonk.DataTypes.Interaction.InteractionProbePair)5 BufferedWriter (java.io.BufferedWriter)2 FileWriter (java.io.FileWriter)2 PrintWriter (java.io.PrintWriter)2 DataStore (uk.ac.babraham.SeqMonk.DataTypes.DataStore)2 Chromosome (uk.ac.babraham.SeqMonk.DataTypes.Genome.Chromosome)2 HiCDataStore (uk.ac.babraham.SeqMonk.DataTypes.HiCDataStore)2 Probe (uk.ac.babraham.SeqMonk.DataTypes.Probes.Probe)2 SeqMonkException (uk.ac.babraham.SeqMonk.SeqMonkException)2 Vector (java.util.Vector)1 AbstractTableModel (javax.swing.table.AbstractTableModel)1 TableModel (javax.swing.table.TableModel)1 Feature (uk.ac.babraham.SeqMonk.DataTypes.Genome.Feature)1