Search in sources :

Example 1 with NonThreadSafeIntCounter

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

the class PairedDataSet method addData.

/**
 * This method is used to add data to the paired data set and should be called
 * by all parsers which create a new set.  Pairs of reads should be added
 * sequentially to the data set and will be paired by it internally.
 *
 * Only the SeqMonk parser should ever set the noReverse parameter to true.  Specifying
 * this for all data lets the dataset skip the sorting step when caching which is
 * otherwise really slow, but if this is incorrectly skipped then subsequent results
 * returned by the data set will be wrong.
 *
 * @param c
 * @param read
 * @param noReverse
 */
public void addData(Chromosome c, long read, boolean skipSorting) {
    if (!skipSorting)
        needToSort = true;
    if (lastChromosome == null) {
        // We'll just store this read for now until we can pair it with
        // the next read which is submitted
        lastRead = read;
        lastChromosome = c;
    } else if (lastChromosome != c && ignoreTrans) {
        // Skip this all together.
        lastRead = 0;
        lastChromosome = null;
    } else // Skip cis reads which are too close together
    if (filterOnMinDistance && c == lastChromosome && SequenceRead.fragmentLength(lastRead, read) < minDistance) {
        lastRead = 0;
        lastChromosome = null;
    } else {
        // We're actually going to add this pair
        int increment = 2;
        if (skipSorting)
            increment = 1;
        if (lastChromosome != c) {
            // Increment the trans counts for each chromosome
            transCount += increment;
            if (transChromosomeCounts.containsKey(lastChromosome)) {
                transChromosomeCounts.get(lastChromosome).increment();
            } else {
                transChromosomeCounts.put(lastChromosome, new NonThreadSafeIntCounter());
                transChromosomeCounts.get(lastChromosome).increment();
            }
            if (!skipSorting) {
                if (transChromosomeCounts.containsKey(c)) {
                    transChromosomeCounts.get(c).increment();
                } else {
                    transChromosomeCounts.put(c, new NonThreadSafeIntCounter());
                    transChromosomeCounts.get(c).increment();
                }
            }
        } else {
            // Increment the cis counts for each chromosome
            cisCount += increment;
            if (cisChromosomeCounts.containsKey(c)) {
                cisChromosomeCounts.get(lastChromosome).increment();
            } else {
                cisChromosomeCounts.put(c, new NonThreadSafeIntCounter());
                cisChromosomeCounts.get(lastChromosome).increment();
            }
        }
        try {
            if (isFinalised) {
                throw new SeqMonkException("This data set is finalised.  No more data can be added");
            }
            // Add the forward pair.
            if (!readData.containsKey(lastChromosome)) {
                ChromosomeDataStore cds = new ChromosomeDataStore(lastChromosome);
                readData.put(lastChromosome, cds);
            }
            readData.get(lastChromosome).hitCollection.addHit(c.name(), lastRead, read);
            if (!skipSorting) {
                // Add the reverse pair.
                if (!readData.containsKey(c)) {
                    ChromosomeDataStore cds = new ChromosomeDataStore(c);
                    readData.put(c, cds);
                }
                readData.get(c).hitCollection.addHit(lastChromosome.name(), read, lastRead);
            }
        } catch (SeqMonkException sme) {
            throw new IllegalStateException(sme);
        }
        lastRead = 0;
        lastChromosome = null;
    }
}
Also used : NonThreadSafeIntCounter(uk.ac.babraham.SeqMonk.Utilities.NonThreadSafeIntCounter) SeqMonkException(uk.ac.babraham.SeqMonk.SeqMonkException)

Aggregations

SeqMonkException (uk.ac.babraham.SeqMonk.SeqMonkException)1 NonThreadSafeIntCounter (uk.ac.babraham.SeqMonk.Utilities.NonThreadSafeIntCounter)1