htsjdk.samtools.SAMValidationError Java Examples

The following examples show how to use htsjdk.samtools.SAMValidationError. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: CleanSamTest.java    From picard with MIT License 6 votes vote down vote up
@Test(dataProvider = "testCleanSamDataProvider")
public void testCleanSam(final String samFile, final String expectedCigar) throws IOException {
    final File cleanedFile = File.createTempFile(samFile + ".", ".sam");
    cleanedFile.deleteOnExit();
    final String[] args = new String[]{
            "INPUT=" + new File(TEST_DATA_DIR, samFile).getAbsolutePath(),
            "OUTPUT=" + cleanedFile.getAbsolutePath()
    };
    Assert.assertEquals(runPicardCommandLine(args), 0);

    final SamFileValidator validator = new SamFileValidator(new PrintWriter(System.out), 8000);
    validator.setIgnoreWarnings(true);
    validator.setVerbose(true, 1000);
    validator.setErrorsToIgnore(Arrays.asList(SAMValidationError.Type.MISSING_READ_GROUP));
    SamReader samReader = SamReaderFactory.makeDefault().validationStringency(ValidationStringency.LENIENT).open(cleanedFile);
    final SAMRecord rec = samReader.iterator().next();
    samReader.close();
    Assert.assertEquals(rec.getCigarString(), expectedCigar);
    samReader = SamReaderFactory.makeDefault().validationStringency(ValidationStringency.LENIENT).open(cleanedFile);
    final boolean validated = validator.validateSamFileVerbose(samReader, null);
    samReader.close();
    Assert.assertTrue(validated, "ValidateSamFile failed");
}
 
Example #2
Source File: CleanSamTester.java    From picard with MIT License 5 votes vote down vote up
protected void test() {
    try {
        final SamFileValidator validator = new SamFileValidator(new PrintWriter(System.out), 8000);

        // Validate it has the expected cigar
        validator.setIgnoreWarnings(true);
        validator.setVerbose(true, 1000);
        validator.setErrorsToIgnore(Arrays.asList(SAMValidationError.Type.MISSING_READ_GROUP));
        SamReaderFactory factory = SamReaderFactory.makeDefault().validationStringency(ValidationStringency.LENIENT);
        SamReader samReader = factory.open(getOutput());
        final SAMRecordIterator iterator = samReader.iterator();
        while (iterator.hasNext()) {
            final SAMRecord rec = iterator.next();
            Assert.assertEquals(rec.getCigarString(), expectedCigar);
            if (SAMUtils.hasMateCigar(rec)) {
                Assert.assertEquals(SAMUtils.getMateCigarString(rec), expectedCigar);
            }
        }
        CloserUtil.close(samReader);

        // Run validation on the output file
        samReader = factory.open(getOutput());
        final boolean validated = validator.validateSamFileVerbose(samReader, null);
        CloserUtil.close(samReader);

        Assert.assertTrue(validated, "ValidateSamFile failed");
    } finally {
        IOUtil.recursiveDelete(getOutputDir().toPath());
    }
}
 
Example #3
Source File: SamToFastq.java    From picard with MIT License 4 votes vote down vote up
protected int doWork() {
    IOUtil.assertFileIsReadable(INPUT);
    final SamReader reader = SamReaderFactory.makeDefault().referenceSequence(REFERENCE_SEQUENCE).open(INPUT);
    final Map<String, SAMRecord> firstSeenMates = new HashMap<>();
    final FastqWriterFactory factory = new FastqWriterFactory();
    factory.setCreateMd5(CREATE_MD5_FILE);

    initializeAdditionalWriters();
    final Map<SAMReadGroupRecord, FastqWriters> writers = generateWriters(reader.getFileHeader().getReadGroups(),
            factory);
    final Map<SAMReadGroupRecord, List<FastqWriter>> additionalWriters = generateAdditionalWriters(reader.getFileHeader().getReadGroups(), factory);
    if (writers.isEmpty()) {
        final String msgBase = INPUT + " does not contain Read Groups";
        final String msg = OUTPUT_PER_RG ? msgBase + ", consider not using the OUTPUT_PER_RG option" : msgBase;
        throw new PicardException(msg);
    }

    final ProgressLogger progress = new ProgressLogger(log);

    for (final SAMRecord currentRecord : reader) {
        handleRecord(currentRecord, writers, additionalWriters, firstSeenMates);
        progress.record(currentRecord);
    }

    CloserUtil.close(reader);

    // Close all the fastq writers being careful to close each one only once!
    for (final FastqWriters writerMapping : new HashSet<>(writers.values())) {
        writerMapping.closeAll();
    }

    // close all `additionalWriters` only once
    final Set<FastqWriter> additionalWriterSet = new HashSet<>();
    additionalWriters.values().forEach(additionalWriterSet::addAll);
    for (final FastqWriter fastqWriter : additionalWriterSet) {
        fastqWriter.close();
    }

    if (!firstSeenMates.isEmpty()) {
        SAMUtils.processValidationError(new SAMValidationError(SAMValidationError.Type.MATE_NOT_FOUND,
                "Found " + firstSeenMates.size() + " unpaired mates", null), VALIDATION_STRINGENCY);
    }

    return 0;
}
 
Example #4
Source File: ValidateSamTester.java    From picard with MIT License 4 votes vote down vote up
public void setIgnoreError(final Collection<SAMValidationError.Type> errorsToIgnore){
    ignoreErrors = new ArrayList<>(errorsToIgnore);
}
 
Example #5
Source File: ValidateCramFile.java    From cramtools with Apache License 2.0 4 votes vote down vote up
public static void main(String[] args) throws IOException, IllegalArgumentException, IllegalAccessException {
	Params params = new Params();
	JCommander jc = new JCommander(params);
	try {
		jc.parse(args);
	} catch (Exception e) {
		System.out.println("Failed to parse parameteres, detailed message below: ");
		System.out.println(e.getMessage());
		System.out.println();
		System.out.println("See usage: -h");
		System.exit(1);
	}

	if (args.length == 0 || params.help) {
		printUsage(jc);
		System.exit(1);
	}

	if (params.reference == null) {
		System.out.println("A reference fasta file is required.");
		System.exit(1);
	}

	if (params.cramFile == null) {
		System.out.println("A CRAM input file is required. ");
		System.exit(1);
	}

	Log.setGlobalLogLevel(Log.LogLevel.INFO);

	ReferenceSequenceFile referenceSequenceFile = ReferenceSequenceFileFactory
			.getReferenceSequenceFile(params.reference);

	FileInputStream fis = new FileInputStream(params.cramFile);
	BufferedInputStream bis = new BufferedInputStream(fis);

	CRAMIterator iterator = new CRAMIterator(bis, new ReferenceSource(params.reference),
			ValidationStringency.STRICT);
	CramHeader cramHeader = iterator.getCramHeader();

	iterator.close();

	ProgressLogger progress = new ProgressLogger(log, 100000, "Validated Read");
	SamFileValidator v = new SamFileValidator(new PrintWriter(System.out), 1);
	final SamReader reader = SamReaderFactory.make().referenceSequence(params.reference).open(params.cramFile);
	List<SAMValidationError.Type> errors = new ArrayList<SAMValidationError.Type>();
	errors.add(SAMValidationError.Type.MATE_NOT_FOUND);
	// errors.add(Type.MISSING_TAG_NM);
	v.setErrorsToIgnore(errors);
	v.validateSamFileSummary(reader, ReferenceSequenceFileFactory.getReferenceSequenceFile(params.reference));
	log.info("Elapsed seconds: " + progress.getElapsedSeconds());
}