Search in sources :

Example 1 with IntVector

use of uk.ac.babraham.SeqMonk.Utilities.IntVector in project SeqMonk by s-andrews.

the class DataSet method getReadsForProbe.

/* (non-Javadoc)
	 * @see uk.ac.babraham.SeqMonk.DataTypes.DataStore#getReadsForProbe(uk.ac.babraham.SeqMonk.DataTypes.Probes.Probe)
	 */
public long[] getReadsForProbe(Probe p) {
    if (!isFinalised)
        finalise();
    ReadsWithCounts allReads;
    loadCacheForChromosome(p.chromosome());
    // We take a copy of the arrays now so that we don't get a problem if something
    // else updates them whilst we're still working otherwise we get index errors.
    allReads = lastCachedReads;
    if (allReads.reads.length == 0)
        return new long[0];
    LongVector reads = new LongVector();
    IntVector counts = new IntVector();
    int startPos;
    if (lastCachedChromosome != null && p.chromosome() == lastCachedChromosome && (lastProbeLocation == 0 || SequenceRead.compare(p.packedPosition(), lastProbeLocation) >= 0)) {
        startPos = lastIndex;
    // System.out.println("Using cached start pos "+lastIndex);
    } else // enough back that we can't have missed even the longest read in the set.
    if (lastCachedChromosome != null && p.chromosome() == lastCachedChromosome) {
        // System.out.println("Last chr="+lastCachedChromosome+" this chr="+p.chromosome()+" lastProbeLocation="+lastProbeLocation+" diff="+SequenceRead.compare(p.packedPosition(), lastProbeLocation));
        int longestRead = getMaxReadLength();
        for (; lastIndex > 0; lastIndex--) {
            if (p.start() - SequenceRead.start(allReads.reads[lastIndex]) > longestRead) {
                break;
            }
        }
        // System.out.println("Starting from index "+lastIndex+" which starts at "+SequenceRead.start(allReads[lastIndex])+" for "+p.start()+" when max length is "+longestRead);
        startPos = lastIndex;
    } else // If we're on a different chromosome then start from the very beginning
    {
        startPos = 0;
        lastIndex = 0;
    // System.out.println("Starting from the beginning");
    }
    // Can't see how this would happen, but we had a report showing this.
    if (startPos < 0)
        startPos = 0;
    lastProbeLocation = p.packedPosition();
    // We now go forward to see what we can find
    boolean cacheSet = false;
    for (int i = startPos; i < allReads.reads.length; i++) {
        // Reads come in order, so we can stop when we've seen enough.
        if (SequenceRead.start(allReads.reads[i]) > p.end()) {
            break;
        }
        if (SequenceRead.overlaps(allReads.reads[i], p.packedPosition())) {
            // then update the cache
            if (!cacheSet) {
                lastIndex = i;
                cacheSet = true;
            }
            reads.add(allReads.reads[i]);
            counts.add(allReads.counts[i]);
        }
    }
    long[] returnReads = expandReadsAndCounts(reads.toArray(), counts.toArray());
    // SequenceRead.sort(returnReads);
    return returnReads;
}
Also used : LongVector(uk.ac.babraham.SeqMonk.Utilities.LongVector) IntVector(uk.ac.babraham.SeqMonk.Utilities.IntVector) ReadsWithCounts(uk.ac.babraham.SeqMonk.DataTypes.Sequence.ReadsWithCounts)

Example 2 with IntVector

use of uk.ac.babraham.SeqMonk.Utilities.IntVector in project SeqMonk by s-andrews.

the class ChromosomeDataTrack method assignSlots.

/**
 * Assign slots.
 */
private void assignSlots() {
    if (getHeight() == height && DisplayPreferences.getInstance().getReadDisplay() == lastSplitMode && thisReadDensity == lastReadDensity && drawProbes == lastDrawProbes) {
        // Nothing to do.
        return;
    }
    // Cache the values so we might be able to skip this next time.
    height = getHeight();
    lastReadDensity = thisReadDensity;
    lastDrawProbes = drawProbes;
    lastSplitMode = DisplayPreferences.getInstance().getReadDisplay();
    // Lets recalculate the slot values
    /* 
		 * Each slot is a shaded area of [readHeight]px separated by a
		 * blank [readSpace]px area.  This means there are [readHeight+readSpace]px between
		 * adjacent slots.  Because height might not be even
		 * we need to calculate for the smallest half (hence
		 * the divide by 2 and later multiply by 2.
		 *
		 * Finally we leave the top and bottom slots empty so
		 * we can distinguish between tracks (hence the -2 at
		 * the end.
		 * 
		 * I've changed the -2 to -1 since there should always be an odd
		 * number of slots (a central one and then pairs around it)
		 * 
		 * If we don't have much space for each lane then we can get
		 * negative slot counts, and we can't let that happen!
		 * 
		 * We also calculate differently depending on whether we have to
		 * draw probes as well.  If we're drawing probes we only
		 * have half of the lane to work with.  If we're just
		 * drawing reads we've got the whole space.
		 */
    // We'll only use half of the height if we're either drawing probes, or if
    // we're a HiC dataset where the bottom half will show interactions.
    // int halfHeightCorrection = (drawProbes ? 2 : 1);
    int halfHeightCorrection = 1;
    if (drawProbes || isHiC) {
        halfHeightCorrection = 2;
    }
    /*
		 * This gets a value of 2 if we're drawing probes as well and 1
		 * if we're not.
		 */
    int slotCount = (((height / (2 * halfHeightCorrection)) / (readHeight + readSpace)) * 2) - 1;
    if (slotCount < 1)
        slotCount = 1;
    slotYValues = new int[slotCount];
    // System.err.println("There will be "+slotYValues.length+" slots");
    int mid = height / (2 * halfHeightCorrection);
    for (int i = 0; i < slotYValues.length; i++) {
        if (i == 0) {
            slotYValues[i] = mid;
        } else if (i % 2 == 0) {
            // We're going down
            slotYValues[i] = mid + ((readHeight + readSpace) * (i / 2));
        } else {
            // We're going up
            slotYValues[i] = mid - ((readHeight + readSpace) * ((i + 1) / 2));
        }
    }
    // We now need to assign each probe to a slot
    // We're going to go back to the original source for the reads.  That way we only need to keep
    // hold of the ones which are assignable in this height of view which could save us a lot of
    // memory
    ReadsWithCounts rwc = data.getReadsForChromosome(DisplayPreferences.getInstance().getCurrentChromosome());
    // We'll start a temporary list of the reads which we can draw, and this will be what we put together.
    LongVector drawableReads = new LongVector();
    IntVector drawableSlotValues = new IntVector();
    // We can also make the array of cached positions to optimise drawing
    lastReadXEnds = new int[slotCount];
    // The lastBase array keeps track of the last
    // base to be drawn in each slot.
    int[] lastBase = new int[slotCount];
    for (int i = 0; i < lastBase.length; i++) {
        lastBase[i] = 0;
    }
    // fit them
    if (lastSplitMode == DisplayPreferences.READ_DISPLAY_COMBINED) {
        // To save doing a lot of processing we're going to cache the
        // next available position if we're off the end of the display
        // so we can quickly skip over reads which are never going to
        // fit
        int nextPossibleSlot = 0;
        for (int r = 0; r < rwc.reads.length; r++) {
            long read = rwc.reads[r];
            READ: for (int c = 0; c < rwc.counts[r]; c++) {
                if (nextPossibleSlot != 0) {
                    // See if we can quickly skip this read
                    if (nextPossibleSlot > SequenceRead.start(reads[r])) {
                        continue;
                    } else {
                        // Reset this as we're adding reads again.
                        nextPossibleSlot = 0;
                    }
                }
                for (int s = 0; s < slotCount; s++) {
                    if (lastBase[s] < SequenceRead.start(read)) {
                        drawableReads.add(read);
                        drawableSlotValues.add(s);
                        lastBase[s] = SequenceRead.end(read);
                        continue READ;
                    }
                }
                // skip stuff quickly in future
                for (int s = 0; s < slotCount; s++) {
                    if (lastBase[s] < nextPossibleSlot)
                        nextPossibleSlot = lastBase[s];
                }
            }
        }
    } else if (lastSplitMode == DisplayPreferences.READ_DISPLAY_SEPARATED) {
        // reads go below.
        for (int r = 0; r < rwc.reads.length; r++) {
            long read = rwc.reads[r];
            READ: for (int c = 0; c < rwc.counts[r]; c++) {
                int startSlot = 0;
                int interval = slotCount;
                if (SequenceRead.strand(read) == Location.FORWARD) {
                    startSlot = 1;
                    interval = 2;
                } else if (SequenceRead.strand(read) == Location.REVERSE) {
                    startSlot = 2;
                    interval = 2;
                }
                for (int s = startSlot; s < slotCount; s += interval) {
                    if (lastBase[s] < SequenceRead.start(read)) {
                        drawableSlotValues.add(s);
                        drawableReads.add(read);
                        lastBase[s] = SequenceRead.end(read);
                        continue READ;
                    }
                }
            // If we get here then we don't have enough
            // slots to draw the reads in this chromosome.
            // In this case we just don't draw them in this
            // display.  That just measns we don't add them
            // to anything.
            }
        }
    }
    reads = drawableReads.toArray();
    slotValues = drawableSlotValues.toArray();
}
Also used : LongVector(uk.ac.babraham.SeqMonk.Utilities.LongVector) IntVector(uk.ac.babraham.SeqMonk.Utilities.IntVector) ReadsWithCounts(uk.ac.babraham.SeqMonk.DataTypes.Sequence.ReadsWithCounts)

Example 3 with IntVector

use of uk.ac.babraham.SeqMonk.Utilities.IntVector in project SeqMonk by s-andrews.

the class DataSetEditor method actionPerformed.

/* (non-Javadoc)
	 * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
	 */
public void actionPerformed(ActionEvent ae) {
    String c = ae.getActionCommand();
    if (c.equals("rename_dataset")) {
        DataSet s = (DataSet) dataSetList.getSelectedValue();
        String dataSetName = null;
        while (true) {
            dataSetName = (String) JOptionPane.showInputDialog(this, "Enter DataSet name", "DataSet Name", JOptionPane.QUESTION_MESSAGE, null, null, s.name());
            // They cancelled
            if (dataSetName == null)
                return;
            if (dataSetName.length() > 0)
                break;
        }
        s.setName(dataSetName);
        dataSetModel.setElementAt(s, dataSetList.getSelectedIndex());
    } else if (c.equals("replace")) {
        Object[] o = dataSetList.getSelectedValues();
        DataSet[] ds = new DataSet[o.length];
        for (int i = 0; i < o.length; i++) {
            ds[i] = (DataSet) o[i];
        }
        String replaceWhat = null;
        while (true) {
            replaceWhat = (String) JOptionPane.showInputDialog(this, "Replace what", "Replace text", JOptionPane.QUESTION_MESSAGE, null, null, "");
            // They cancelled
            if (replaceWhat == null)
                return;
            if (replaceWhat.length() > 0)
                break;
        }
        String replaceWith = (String) JOptionPane.showInputDialog(this, "Replace with", "Replace text", JOptionPane.QUESTION_MESSAGE, null, null, "");
        // They cancelled
        if (replaceWith == null)
            return;
        // catch this so as to produce a nicer error message
        try {
            for (int s = 0; s < ds.length; s++) {
                String oldName = ds[s].name();
                String newName = oldName.replaceAll(replaceWhat, replaceWith);
                ds[s].setName(newName);
            }
            ListDataListener[] l = dataSetModel.getListDataListeners();
            for (int i = 0; i < l.length; i++) {
                l[i].contentsChanged(new ListDataEvent(dataSetModel, ListDataEvent.CONTENTS_CHANGED, 0, ds.length));
            }
        } catch (PatternSyntaxException pse) {
            JOptionPane.showMessageDialog(this, "<html>You used a regex in your search, but it contained a syntax error<br><br>" + pse.getLocalizedMessage(), "Pattern error", JOptionPane.ERROR_MESSAGE);
        }
    } else if (c.equals("reset")) {
        Object[] o = dataSetList.getSelectedValues();
        if (JOptionPane.showConfirmDialog(this, "Reset names to original file names?", "Reset names?", JOptionPane.YES_NO_OPTION) == JOptionPane.NO_OPTION)
            return;
        for (int i = 0; i < o.length; i++) {
            DataSet d = (DataSet) o[i];
            File f = new File(d.fileName());
            d.setName(f.getName());
        }
    } else if (c.equals("select_names")) {
        // We need to collect a list of names we want to keep
        CollectDataSetNamesDialog d = new CollectDataSetNamesDialog();
        String[] names = d.getNames();
        System.err.println("Got a list of " + names.length + " names");
        // They cancelled or didn't enter anything
        if (names.length == 0)
            return;
        IntVector iv = new IntVector();
        INDEX: for (int index = 0; index < dataSetModel.getSize(); index++) {
            for (int n = 0; n < names.length; n++) {
                if (names[n].equals(dataSetModel.elementAt(index).toString())) {
                    // System.err.println("It matches");
                    iv.add(index);
                    continue INDEX;
                }
            }
        }
        dataSetList.setSelectedIndices(iv.toArray());
    } else if (c.equals("close")) {
        setVisible(false);
        dispose();
    } else if (c.equals("delete_dataset")) {
        Object[] o = dataSetList.getSelectedValues();
        if (JOptionPane.showConfirmDialog(this, "Are you sure you want to delete " + o.length + " data sets?", "Really delete?", JOptionPane.YES_NO_OPTION) == JOptionPane.NO_OPTION)
            return;
        DataSet[] ds = new DataSet[o.length];
        for (int i = 0; i < o.length; i++) {
            ds[i] = (DataSet) o[i];
            dataSetModel.removeElement(o[i]);
        }
        collection.removeDataSets(ds);
    } else if (c.equals("close")) {
        setVisible(false);
        dispose();
    }
}
Also used : ListDataEvent(javax.swing.event.ListDataEvent) IntVector(uk.ac.babraham.SeqMonk.Utilities.IntVector) DataSet(uk.ac.babraham.SeqMonk.DataTypes.DataSet) File(java.io.File) PatternSyntaxException(java.util.regex.PatternSyntaxException)

Example 4 with IntVector

use of uk.ac.babraham.SeqMonk.Utilities.IntVector in project SeqMonk by s-andrews.

the class ContigProbeGenerator method getNonRedundantReads.

/**
 * Gets the non redundant reads.
 *
 * @param reads the reads
 * @return the non redundant reads
 */
private ReadsWithCounts getNonRedundantReads(ReadsWithCounts reads, int limitToStrand) {
    boolean limitStrand = false;
    if (limitToStrand == Location.FORWARD || limitToStrand == Location.REVERSE || limitToStrand == Location.UNKNOWN) {
        limitStrand = true;
    }
    if (!limitStrand) {
        // It's already done.
        return reads;
    }
    LongVector keptReads = new LongVector();
    IntVector keptCounts = new IntVector();
    for (int r = 0; r < reads.reads.length; r++) {
        if (!readStrandType.useRead(reads.reads[r])) {
            continue;
        }
        if (limitStrand && (SequenceRead.strand(reads.reads[r]) != limitToStrand))
            continue;
        keptReads.add(reads.reads[r]);
        keptCounts.add(reads.counts[r]);
    }
    return new ReadsWithCounts(keptReads.toArray(), keptCounts.toArray());
}
Also used : LongVector(uk.ac.babraham.SeqMonk.Utilities.LongVector) IntVector(uk.ac.babraham.SeqMonk.Utilities.IntVector) ReadsWithCounts(uk.ac.babraham.SeqMonk.DataTypes.Sequence.ReadsWithCounts)

Example 5 with IntVector

use of uk.ac.babraham.SeqMonk.Utilities.IntVector in project SeqMonk by s-andrews.

the class PCADataCalculator method getProbeList.

public ProbeList getProbeList(int pcaIndex, double rotationCutoff) {
    float[] rotations = getPCARotations(pcaIndex);
    IntVector higherIndices = new IntVector();
    IntVector lowerIndices = new IntVector();
    for (int i = 0; i < rotations.length; i++) {
        if (rotations[i] > rotationCutoff) {
            higherIndices.add(i);
        } else {
            lowerIndices.add(i);
        }
    }
    int[] indicesToAdd;
    String direction;
    if (higherIndices.length() > lowerIndices.length()) {
        indicesToAdd = lowerIndices.toArray();
        direction = "below";
    } else {
        indicesToAdd = higherIndices.toArray();
        direction = "above";
    }
    String descriptionDirection;
    if (direction.equals("below")) {
        descriptionDirection = "low";
    } else {
        descriptionDirection = "high";
    }
    ProbeList filteredList = new ProbeList(probeList, "PC" + (pcaIndex + 1) + " " + descriptionDirection + " rotation probes", "Probes from PC" + (pcaIndex + 1) + " with a rotation " + direction + " " + rotationCutoff, "Rotation");
    for (int i = 0; i < indicesToAdd.length; i++) {
        filteredList.addProbe(usedProbes[indicesToAdd[i]], rotations[indicesToAdd[i]]);
    }
    return filteredList;
}
Also used : IntVector(uk.ac.babraham.SeqMonk.Utilities.IntVector) ProbeList(uk.ac.babraham.SeqMonk.DataTypes.Probes.ProbeList)

Aggregations

IntVector (uk.ac.babraham.SeqMonk.Utilities.IntVector)5 ReadsWithCounts (uk.ac.babraham.SeqMonk.DataTypes.Sequence.ReadsWithCounts)3 LongVector (uk.ac.babraham.SeqMonk.Utilities.LongVector)3 File (java.io.File)1 PatternSyntaxException (java.util.regex.PatternSyntaxException)1 ListDataEvent (javax.swing.event.ListDataEvent)1 DataSet (uk.ac.babraham.SeqMonk.DataTypes.DataSet)1 ProbeList (uk.ac.babraham.SeqMonk.DataTypes.Probes.ProbeList)1