Search in sources :

Example 21 with FastqRecord

use of htsjdk.samtools.fastq.FastqRecord in project jvarkit by lindenb.

the class FastqGrep method run.

private void run(final FastqReader r, final FastqWriter out) {
    long nRec = 0L;
    r.setValidationStringency(ValidationStringency.LENIENT);
    while (r.hasNext()) {
        FastqRecord fastq = r.next();
        boolean keep = false;
        String readName = getReadName(fastq);
        Integer count = readNames.get(readName);
        if (count != null) {
            keep = true;
        }
        if (inverse)
            keep = !keep;
        if (keep) {
            ++nRec;
            out.write(fastq);
        }
        if (n_before_remove != -1 && !inverse && keep) {
            count++;
            if (count >= n_before_remove) {
                readNames.remove(readName);
                if (readNames.isEmpty())
                    break;
            } else {
                readNames.put(readName, count);
            }
        }
    }
    LOG.info("Done. N-Reads:" + nRec);
}
Also used : FastqRecord(htsjdk.samtools.fastq.FastqRecord)

Example 22 with FastqRecord

use of htsjdk.samtools.fastq.FastqRecord in project gatk by broadinstitute.

the class SamToFastq method writeRecord.

private void writeRecord(final SAMRecord read, final Integer mateNumber, final FastqWriter writer, final int basesToTrim, final Integer maxBasesToWrite) {
    final String seqHeader = mateNumber == null ? read.getReadName() : read.getReadName() + "/" + mateNumber;
    String readString = read.getReadString();
    String baseQualities = read.getBaseQualityString();
    // If we're clipping, do the right thing to the bases or qualities
    if (CLIPPING_ATTRIBUTE != null) {
        final Integer clipPoint = (Integer) read.getAttribute(CLIPPING_ATTRIBUTE);
        if (clipPoint != null) {
            if (CLIPPING_ACTION.equalsIgnoreCase("X")) {
                readString = clip(readString, clipPoint, null, !read.getReadNegativeStrandFlag());
                baseQualities = clip(baseQualities, clipPoint, null, !read.getReadNegativeStrandFlag());
            } else if (CLIPPING_ACTION.equalsIgnoreCase("N")) {
                readString = clip(readString, clipPoint, 'N', !read.getReadNegativeStrandFlag());
            } else {
                final char newQual = SAMUtils.phredToFastq(new byte[] { (byte) Integer.parseInt(CLIPPING_ACTION) }).charAt(0);
                baseQualities = clip(baseQualities, clipPoint, newQual, !read.getReadNegativeStrandFlag());
            }
        }
    }
    if (RE_REVERSE && read.getReadNegativeStrandFlag()) {
        readString = SequenceUtil.reverseComplement(readString);
        baseQualities = StringUtil.reverseString(baseQualities);
    }
    if (basesToTrim > 0) {
        readString = readString.substring(basesToTrim);
        baseQualities = baseQualities.substring(basesToTrim);
    }
    if (maxBasesToWrite != null && maxBasesToWrite < readString.length()) {
        readString = readString.substring(0, maxBasesToWrite);
        baseQualities = baseQualities.substring(0, maxBasesToWrite);
    }
    writer.write(new FastqRecord(seqHeader, readString, "", baseQualities));
}
Also used : FastqRecord(htsjdk.samtools.fastq.FastqRecord)

Example 23 with FastqRecord

use of htsjdk.samtools.fastq.FastqRecord in project gatk by broadinstitute.

the class FastqToSam method doPaired.

/** More complicated method that takes two fastq files and builds pairing information in the SAM. */
protected int doPaired(final FastqReader freader1, final FastqReader freader2, final SAMFileWriter writer) {
    int readCount = 0;
    final ProgressLogger progress = new ProgressLogger(LOG);
    for (; freader1.hasNext() && freader2.hasNext(); readCount++) {
        final FastqRecord frec1 = freader1.next();
        final FastqRecord frec2 = freader2.next();
        final String frec1Name = getReadName(frec1.getReadName(), true);
        final String frec2Name = getReadName(frec2.getReadName(), true);
        final String baseName = getBaseName(frec1Name, frec2Name, freader1, freader2);
        final SAMRecord srec1 = createSamRecord(writer.getFileHeader(), baseName, frec1, true);
        srec1.setFirstOfPairFlag(true);
        srec1.setSecondOfPairFlag(false);
        writer.addAlignment(srec1);
        progress.record(srec1);
        final SAMRecord srec2 = createSamRecord(writer.getFileHeader(), baseName, frec2, true);
        srec2.setFirstOfPairFlag(false);
        srec2.setSecondOfPairFlag(true);
        writer.addAlignment(srec2);
        progress.record(srec2);
    }
    writer.close();
    if (freader1.hasNext() || freader2.hasNext()) {
        throw new UserException("Input paired fastq files must be the same length");
    }
    return readCount;
}
Also used : FastqRecord(htsjdk.samtools.fastq.FastqRecord) ProgressLogger(org.broadinstitute.hellbender.utils.runtime.ProgressLogger) UserException(org.broadinstitute.hellbender.exceptions.UserException)

Example 24 with FastqRecord

use of htsjdk.samtools.fastq.FastqRecord in project gatk by broadinstitute.

the class RunSGAViaProcessBuilderOnSparkUnitTest method extractNamesAndSeqFromFASTQ.

// utility function: for extracting read names and sequences from a fastq/fasta file
private static void extractNamesAndSeqFromFASTQ(final File FASTAFile, final boolean fastqFilesWellFormed, List<String> readNames, List<String> sequences) throws IOException {
    if (fastqFilesWellFormed) {
        try (final FastqReader reader = new FastqReader(FASTAFile)) {
            while (reader.hasNext()) {
                final FastqRecord record = reader.next();
                readNames.add(record.getReadName());
                sequences.add(record.getReadString());
            }
        }
    } else {
        try (final BufferedReader reader = new BufferedReader(new FileReader(FASTAFile))) {
            String line = "";
            int i = 0;
            while ((line = reader.readLine()) != null) {
                final int l = i % 4;
                if (0 == l) {
                    readNames.add(line);
                    ++i;
                } else {
                    sequences.add(line);
                    reader.readLine();
                    reader.readLine();
                    i += 3;
                }
            }
        }
    }
}
Also used : FastqReader(htsjdk.samtools.fastq.FastqReader) FastqRecord(htsjdk.samtools.fastq.FastqRecord) BufferedReader(java.io.BufferedReader) FileReader(java.io.FileReader)

Example 25 with FastqRecord

use of htsjdk.samtools.fastq.FastqRecord in project gatk by broadinstitute.

the class SamToFastqIntegrationTest method testClipping.

@Test(dataProvider = "clippingTests")
public void testClipping(final String clippingAction, final String bases1_1, final String quals1_1, final String bases1_2, final String quals1_2, final String bases2_1, final String quals2_1, final String bases2_2, final String quals2_2, final String testName) throws IOException {
    final File samFile = new File(TEST_DATA_DIR, CLIPPING_TEST_DATA);
    final File f1 = BaseTest.createTempFile("clippingtest1", "fastq");
    final File f2 = BaseTest.createTempFile("clippingtest2", "fastq");
    if (clippingAction != null) {
        convertFile(new String[] { "-input", samFile.getAbsolutePath(), "--FASTQ", f1.getAbsolutePath(), "--SECOND_END_FASTQ", f2.getAbsolutePath(), "--CLIPPING_ACTION", clippingAction, "--CLIPPING_ATTRIBUTE", "XT" });
    } else {
        convertFile(new String[] { "--input", samFile.getAbsolutePath(), "--FASTQ", f1.getAbsolutePath(), "--SECOND_END_FASTQ", f2.getAbsolutePath() });
    }
    Iterator<FastqRecord> it = new FastqReader(f1).iterator();
    FastqRecord first = it.next();
    Assert.assertEquals(first.getReadString(), bases1_1, testName);
    Assert.assertEquals(first.getBaseQualityString(), quals1_1, testName);
    FastqRecord second = it.next();
    Assert.assertEquals(second.getReadString(), bases1_2, testName);
    Assert.assertEquals(second.getBaseQualityString(), quals1_2, testName);
    it = new FastqReader(f2).iterator();
    first = it.next();
    Assert.assertEquals(first.getReadString(), bases2_1, testName);
    Assert.assertEquals(first.getBaseQualityString(), quals2_1, testName);
    second = it.next();
    Assert.assertEquals(second.getReadString(), bases2_2, testName);
    Assert.assertEquals(second.getBaseQualityString(), quals2_2, testName);
}
Also used : FastqReader(htsjdk.samtools.fastq.FastqReader) FastqRecord(htsjdk.samtools.fastq.FastqRecord) File(java.io.File) BaseTest(org.broadinstitute.hellbender.utils.test.BaseTest) Test(org.testng.annotations.Test) CommandLineProgramTest(org.broadinstitute.hellbender.CommandLineProgramTest)

Aggregations

FastqRecord (htsjdk.samtools.fastq.FastqRecord)40 SAMRecord (htsjdk.samtools.SAMRecord)16 Test (org.junit.Test)12 FastqReader (htsjdk.samtools.fastq.FastqReader)8 File (java.io.File)6 SAMFileHeader (htsjdk.samtools.SAMFileHeader)4 FastqReader (com.github.lindenb.jvarkit.util.picard.FastqReader)3 FourLinesFastqReader (com.github.lindenb.jvarkit.util.picard.FourLinesFastqReader)3 SAMFileWriter (htsjdk.samtools.SAMFileWriter)3 IOException (java.io.IOException)3 Test (org.testng.annotations.Test)3 BufferedReferenceSequenceFile (au.edu.wehi.idsv.picard.BufferedReferenceSequenceFile)2 ChimericAlignment (au.edu.wehi.idsv.sam.ChimericAlignment)2 SAMSequenceDictionaryProgress (com.github.lindenb.jvarkit.util.picard.SAMSequenceDictionaryProgress)2 Cigar (htsjdk.samtools.Cigar)2 CigarElement (htsjdk.samtools.CigarElement)2 SAMFileWriterFactory (htsjdk.samtools.SAMFileWriterFactory)2 SAMRecordIterator (htsjdk.samtools.SAMRecordIterator)2 CommandLineProgramTest (org.broadinstitute.hellbender.CommandLineProgramTest)2 ProgressLogger (org.broadinstitute.hellbender.utils.runtime.ProgressLogger)2