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);
}
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));
}
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;
}
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;
}
}
}
}
}
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);
}
Aggregations