当前位置: 首页>>代码示例>>Java>>正文


Java SAMFileWriterFactory类代码示例

本文整理汇总了Java中htsjdk.samtools.SAMFileWriterFactory的典型用法代码示例。如果您正苦于以下问题:Java SAMFileWriterFactory类的具体用法?Java SAMFileWriterFactory怎么用?Java SAMFileWriterFactory使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。


SAMFileWriterFactory类属于htsjdk.samtools包,在下文中一共展示了SAMFileWriterFactory类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: writeToSlice

import htsjdk.samtools.SAMFileWriterFactory; //导入依赖的package包/类
private static void writeToSlice(@NotNull final String path, @NotNull final SAMFileHeader header,
        @NotNull final CloseableIterator<SAMRecord> iterator) {
    final File outputBAM = new File(path);
    final SAMFileWriter writer = new SAMFileWriterFactory().setCreateIndex(true).makeBAMWriter(header, true, outputBAM);
    String contig = "";
    while (iterator.hasNext()) {
        final SAMRecord record = iterator.next();
        if (record.getContig() != null && !contig.equals(record.getContig())) {
            contig = record.getContig();
            LOGGER.info("Reading contig: {}", contig);
        }
        writer.addAlignment(record);
    }
    iterator.close();
    writer.close();
}
 
开发者ID:hartwigmedical,项目名称:hmftools,代码行数:17,代码来源:BamSlicerApplication.java

示例2: queryNameSortedBAM

import htsjdk.samtools.SAMFileWriterFactory; //导入依赖的package包/类
private static File queryNameSortedBAM(final SamReader reader, final QueryInterval[] intervals, final String name) throws IOException {

        final SAMFileHeader header = reader.getFileHeader().clone();
        header.setSortOrder(SAMFileHeader.SortOrder.queryname);

        final File file = File.createTempFile(name, ".bam");
        final SAMFileWriter writer = new SAMFileWriterFactory().makeSAMOrBAMWriter(header, false, file);

        final SAMRecordIterator iterator = reader.queryOverlapping(intervals);
        while (iterator.hasNext()) {
            writer.addAlignment(iterator.next());
        }

        iterator.close();
        writer.close();

        return file;
    }
 
开发者ID:hartwigmedical,项目名称:hmftools,代码行数:19,代码来源:Analysis.java

示例3: main

import htsjdk.samtools.SAMFileWriterFactory; //导入依赖的package包/类
public static void main(String[] args) throws IOException {
	File bamFile = new File(
			"./data/miniCaviar_IDT_NEB.runA.NA12878.bwa.bam");
	File newBamFile = new File(
			"./data/miniCaviar_IDT_NEB.runA.NA12878.bwa.chrom1.bam");
	SAMFileWriter outputSam = null;
	final SamReader reader = SamReaderFactory.makeDefault()
			.validationStringency(ValidationStringency.SILENT)
			.open(bamFile);

	outputSam = new SAMFileWriterFactory().makeBAMWriter(
			reader.getFileHeader(), true, newBamFile);

	int currentReads = 0;
	for (final SAMRecord samRecord : reader) {
		if (samRecord.getReferenceIndex() == 0)
			writeBam(samRecord, outputSam);

	}

	reader.close();
	outputSam.close();
}
 
开发者ID:acs6610987,项目名称:secram,代码行数:24,代码来源:DownSamplingBAM.java

示例4: callElPrep

import htsjdk.samtools.SAMFileWriterFactory; //导入依赖的package包/类
public int callElPrep(String input, String output, String rg, int threads, 
        SAMRecordIterator SAMit,
        SAMFileHeader header, String dictFile, boolean updateRG, boolean keepDups, String RGID) throws InterruptedException, QualityException {
    
    SAMRecord sam;
    SAMFileWriterFactory factory = new SAMFileWriterFactory();
    SAMFileWriter Swriter = factory.makeSAMWriter(header, true, new File(input));
    
    int reads = 0;
    while(SAMit.hasNext()) {
        sam = SAMit.next();
        if(updateRG)
            sam.setAttribute(SAMTag.RG.name(), RGID);
        Swriter.addAlignment(sam);
        reads++;
    }
    Swriter.close();
    
    String customArgs = HalvadeConf.getCustomArgs(context.getConfiguration(), "elprep", "");  
    String[] command = CommandGenerator.elPrep(bin, input, output, threads, true, rg, null, !keepDups, customArgs);
    long estimatedTime = runProcessAndWait("elPrep", command);
    if(context != null)
        context.getCounter(HalvadeCounters.TIME_ELPREP).increment(estimatedTime);
    
    return reads;
}
 
开发者ID:biointec,项目名称:halvade,代码行数:27,代码来源:PreprocessingTools.java

示例5: buildSamFileWriter

import htsjdk.samtools.SAMFileWriterFactory; //导入依赖的package包/类
/**
 * Build a SamFileWriter that will write its contents to the output file.
 *
 * @param output           The file to which to write
 * @param sampleAlias      The sample alias set in the read group header
 * @param libraryName      The name of the library to which this read group belongs
 * @param headerParameters Header parameters that will be added to the RG header for this SamFile
 * @return A SAMFileWriter
 */
private SAMFileWriterWrapper buildSamFileWriter(final File output, final String sampleAlias,
                                                final String libraryName, final Map<String, String> headerParameters,
                                                final boolean presorted) {
    IOUtil.assertFileIsWritable(output);
    final SAMReadGroupRecord rg = new SAMReadGroupRecord(READ_GROUP_ID);
    rg.setSample(sampleAlias);

    if (libraryName != null) rg.setLibrary(libraryName);
    for (final Map.Entry<String, String> tagNameToValue : headerParameters.entrySet()) {
        if (tagNameToValue.getValue() != null) {
            rg.setAttribute(tagNameToValue.getKey(), tagNameToValue.getValue());
        }
    }

    final SAMFileHeader header = new SAMFileHeader();

    header.setSortOrder(SAMFileHeader.SortOrder.queryname);
    header.addReadGroup(rg);
    return new SAMFileWriterWrapper(new SAMFileWriterFactory().makeSAMOrBAMWriter(header, presorted, output));
}
 
开发者ID:broadinstitute,项目名称:picard,代码行数:30,代码来源:IlluminaBasecallsToSam.java

示例6: gatherNormally

import htsjdk.samtools.SAMFileWriterFactory; //导入依赖的package包/类
/**
 * Simple implementation of a gather operations that uses SAMFileReaders and Writers in order to concatenate
 * multiple BAM files.
 */
private static void gatherNormally(final List<File> inputs, final File output, final boolean createIndex, final boolean createMd5,
                                   final File referenceFasta) {
    final SAMFileHeader header;
    {
        header = SamReaderFactory.makeDefault().referenceSequence(referenceFasta).getFileHeader(inputs.get(0));
    }

    final SAMFileWriter out = new SAMFileWriterFactory().setCreateIndex(createIndex).setCreateMd5File(createMd5).makeSAMOrBAMWriter(header, true, output);

    for (final File f : inputs) {
        log.info("Gathering " + f.getAbsolutePath());
        final SamReader in = SamReaderFactory.makeDefault().referenceSequence(referenceFasta).open(f);
        for (final SAMRecord rec : in) out.addAlignment(rec);
        CloserUtil.close(in);
    }

    out.close();
}
 
开发者ID:broadinstitute,项目名称:picard,代码行数:23,代码来源:GatherBamFiles.java

示例7: doWork

import htsjdk.samtools.SAMFileWriterFactory; //导入依赖的package包/类
protected int doWork() {
    IOUtil.assertFileIsReadable(INPUT);
    IOUtil.assertFileIsWritable(OUTPUT);
    final SamReader reader = SamReaderFactory.makeDefault().referenceSequence(REFERENCE_SEQUENCE).open(INPUT);
    final SAMFileWriter writer = new SAMFileWriterFactory().makeWriter(reader.getFileHeader(), true, OUTPUT, REFERENCE_SEQUENCE);

    if (CREATE_INDEX && writer.getFileHeader().getSortOrder() != SAMFileHeader.SortOrder.coordinate) {
        throw new PicardException("Can't CREATE_INDEX unless sort order is coordinate");
    }

    final ProgressLogger progress = new ProgressLogger(Log.getInstance(SamFormatConverter.class));
    for (final SAMRecord rec : reader) {
        writer.addAlignment(rec);
        progress.record(rec);
    }
    CloserUtil.close(reader);
    writer.close();
    return 0;
}
 
开发者ID:broadinstitute,项目名称:picard,代码行数:20,代码来源:SamFormatConverter.java

示例8: RevertSamWriter

import htsjdk.samtools.SAMFileWriterFactory; //导入依赖的package包/类
RevertSamWriter(
        final boolean outputByReadGroup,
        final Map<String, SAMFileHeader> headerMap,
        final Map<String, File> outputMap,
        final SAMFileHeader singleOutHeader,
        final File singleOutput,
        final boolean presorted,
        final SAMFileWriterFactory factory,
        final File referenceFasta) {

    this.outputByReadGroup = outputByReadGroup;
    if (outputByReadGroup) {
        singleWriter = null;
        for (final Map.Entry<String, File> outputMapEntry : outputMap.entrySet()) {
            final String readGroupId = outputMapEntry.getKey();
            final File output = outputMapEntry.getValue();
            final SAMFileHeader header = headerMap.get(readGroupId);
            final SAMFileWriter writer = factory.makeWriter(header, presorted, output, referenceFasta);
            writerMap.put(readGroupId, writer);
        }
    } else {
        singleWriter = factory.makeWriter(singleOutHeader, presorted, singleOutput, referenceFasta);
    }
}
 
开发者ID:broadinstitute,项目名称:picard,代码行数:25,代码来源:RevertSam.java

示例9: doWork

import htsjdk.samtools.SAMFileWriterFactory; //导入依赖的package包/类
protected int doWork() {
    IOUtil.assertFileIsReadable(INPUT);
    IOUtil.assertFileIsWritable(OUTPUT);
    final SamReader reader = SamReaderFactory.makeDefault().referenceSequence(REFERENCE_SEQUENCE).open(INPUT);
    ;
    reader.getFileHeader().setSortOrder(SORT_ORDER.getSortOrder());
    final SAMFileWriter writer = new SAMFileWriterFactory().makeSAMOrBAMWriter(reader.getFileHeader(), false, OUTPUT);
    writer.setProgressLogger(
            new ProgressLogger(log, (int) 1e7, "Wrote", "records from a sorting collection"));

    final ProgressLogger progress = new ProgressLogger(log, (int) 1e7, "Read");
    for (final SAMRecord rec : reader) {
        writer.addAlignment(rec);
        progress.record(rec);
    }

    log.info("Finished reading inputs, merging and writing to output now.");

    CloserUtil.close(reader);
    writer.close();
    return 0;
}
 
开发者ID:broadinstitute,项目名称:picard,代码行数:23,代码来源:SortSam.java

示例10: standardReheader

import htsjdk.samtools.SAMFileWriterFactory; //导入依赖的package包/类
private void standardReheader(final SAMFileHeader replacementHeader) {
    final SamReader recordReader = SamReaderFactory.makeDefault().referenceSequence(REFERENCE_SEQUENCE).validationStringency(ValidationStringency.SILENT).open(INPUT);
    if (replacementHeader.getSortOrder() != recordReader.getFileHeader().getSortOrder()) {
        throw new PicardException("Sort orders of INPUT (" + recordReader.getFileHeader().getSortOrder().name() +
                ") and HEADER (" + replacementHeader.getSortOrder().name() + ") do not agree.");
    }
    final SAMFileWriter writer = new SAMFileWriterFactory().makeSAMOrBAMWriter(replacementHeader, true, OUTPUT);

    final ProgressLogger progress = new ProgressLogger(Log.getInstance(ReplaceSamHeader.class));
    for (final SAMRecord rec : recordReader) {
        rec.setHeader(replacementHeader);
        writer.addAlignment(rec);
        progress.record(rec);
    }
    writer.close();
    CloserUtil.close(recordReader);
}
 
开发者ID:broadinstitute,项目名称:picard,代码行数:18,代码来源:ReplaceSamHeader.java

示例11: createIndexedBam

import htsjdk.samtools.SAMFileWriterFactory; //导入依赖的package包/类
/**
 * Useful test method.  Creates a (temporary) indexed BAM so that we can store a sam in the testdata set.
 *
 * @param samFile the sam file to convert to bam and index
 * @return File a (temporary) bam file (index file is created in same path).
 */
public static File createIndexedBam(final File samFile, final File tempFilePrefix) throws IOException {
    final File output = File.createTempFile(tempFilePrefix.getAbsolutePath(), ".bam");
    output.deleteOnExit();
    final File indexFile = new File(output.getAbsolutePath() + ".bai");
    indexFile.deleteOnExit();

    final SamReader in = SamReaderFactory.makeDefault().open(samFile);
    SAMFileWriter out = new SAMFileWriterFactory().setCreateIndex(true).makeBAMWriter(in.getFileHeader(), true, output);

    in.iterator().stream().forEach(out::addAlignment);
    out.close();
    in.close();

    return output;
}
 
开发者ID:broadinstitute,项目名称:picard,代码行数:22,代码来源:SamTestUtils.java

示例12: createCommonSAMWriter

import htsjdk.samtools.SAMFileWriterFactory; //导入依赖的package包/类
/**
 * Create a common SAMFileWriter for use with GATK tools.
 *
 * @param outputPath - if this file has a .cram extension then a reference is required. Can not be null.
 * @param referenceFile - the reference source to use. Can not be null if a output file has a .cram extension.
 * @param header - header to be used for the output writer
 * @param preSorted - if true then the records must already be sorted to match the header sort order
 * @param createOutputBamIndex - if true an index will be created for .BAM and .CRAM files
 * @param createMD5 - if true an MD5 file will be created
 *
 * @return SAMFileWriter
 */
public static SAMFileWriter createCommonSAMWriter(
    final Path outputPath,
    final File referenceFile,
    final SAMFileHeader header,
    final boolean preSorted,
    boolean createOutputBamIndex,
    final boolean createMD5)
{
    Utils.nonNull(outputPath);
    Utils.nonNull(header);

    if (createOutputBamIndex && header.getSortOrder() != SAMFileHeader.SortOrder.coordinate) {
        logger.warn("Skipping index file creation for: " +
            outputPath +  ". Index file creation requires reads in coordinate sorted order.");
        createOutputBamIndex = false;
    }

    final SAMFileWriterFactory factory = new SAMFileWriterFactory().setCreateIndex(createOutputBamIndex).setCreateMd5File(createMD5);
    return ReadUtils.createCommonSAMWriterFromFactory(factory, outputPath, referenceFile, header, preSorted);
}
 
开发者ID:broadinstitute,项目名称:gatk,代码行数:33,代码来源:ReadUtils.java

示例13: createCommonSAMWriterFromFactory

import htsjdk.samtools.SAMFileWriterFactory; //导入依赖的package包/类
/**
 * Create a common SAMFileWriter from a factory for use with GATK tools. Assumes that if the factory has been set
 * to create an index, the header must be set to coordinate sorted.
 *
 * @param outputPath if this file has a .cram extension then a reference is required. Can not be null.
 * @param referenceFile the reference source to use. Can not be null if a output file has a .cram extension.
 * @param header header to be used for the output writer
 * @param preSorted if true then records must already be sorted to match the header sort order
 * @param factory SAMFileWriterFactory factory to use
 * @param openOptions (optional) NIO options specifying how to open the file
 * @return SAMFileWriter
 */
public static SAMFileWriter createCommonSAMWriterFromFactory(
    final SAMFileWriterFactory factory,
    final Path outputPath,
    final File referenceFile,
    final SAMFileHeader header,
    final boolean preSorted,
    OpenOption... openOptions)
{
    Utils.nonNull(outputPath);
    Utils.nonNull(header);

    if (null == referenceFile && outputPath.toString().endsWith(CramIO.CRAM_FILE_EXTENSION)) {
        throw new UserException("A reference file is required for writing CRAM files");
    }

    return factory.makeWriter(header.clone(), preSorted, outputPath, referenceFile);
}
 
开发者ID:broadinstitute,项目名称:gatk,代码行数:30,代码来源:ReadUtils.java

示例14: createWriters

import htsjdk.samtools.SAMFileWriterFactory; //导入依赖的package包/类
/**
 * Creates SAMFileWriter instances for the reader splitters based on the input file.
 * @param splitters Reader splitters.
 * @return A map of file name keys to SAMFileWriter.
 */
private Map<String, SAMFileGATKReadWriter> createWriters(final List<ReaderSplitter<?>> splitters) {
    final Map<String, SAMFileGATKReadWriter> outs = new LinkedHashMap<>();

    final SAMFileWriterFactory samFileWriterFactory = new SAMFileWriterFactory();
    final SAMFileHeader samFileHeaderIn = getHeaderForReads();

    // Build up a list of key options at each level.
    final List<List<?>> splitKeys = splitters.stream()
            .map(splitter -> splitter.getSplitsBy(samFileHeaderIn))
            .collect(Collectors.toList());

    // For every combination of keys, add a SAMFileWriter.
    addKey(splitKeys, 0, "", key -> {
        outs.put(key, prepareSAMFileWriter(samFileWriterFactory, samFileHeaderIn, key));
    });

    return outs;
}
 
开发者ID:broadinstitute,项目名称:gatk,代码行数:24,代码来源:SplitReads.java

示例15: sortBamFile

import htsjdk.samtools.SAMFileWriterFactory; //导入依赖的package包/类
public static void sortBamFile(File inputBamFile, File outputBamFile, File tempDirectory, final Comparator<SAMRecord> comparator) {
	try (SamReader currentReader = SamReaderFactory.makeDefault().open(inputBamFile)) {
		SAMFileHeader header = currentReader.getFileHeader();
		Iterator<SAMRecord> iter = currentReader.iterator();
		try (CloseableAndIterableIterator<SAMRecord> sortedIter = getSortedBamIterator(iter, header, tempDirectory, comparator)) {

			if (comparator.getClass().equals(SAMRecordCoordinateComparator.class)) {
				header.setSortOrder(SortOrder.coordinate);
			} else if (comparator.getClass().equals(SAMRecordQueryNameComparator.class)) {
				header.setSortOrder(SortOrder.queryname);
			} else if (comparator.getClass().equals(SAMRecordDuplicateComparator.class)) {
				header.setSortOrder(SortOrder.duplicate);
			}
			try (SAMFileWriter writer = new SAMFileWriterFactory().makeBAMWriter(header, true, outputBamFile, BAM_COMPRESSION_LEVEL)) {
				while (sortedIter.hasNext()) {
					SAMRecord nextRecord = sortedIter.next();
					writer.addAlignment(nextRecord);
				}
			}
		}
	} catch (IOException e) {
		throw new PicardException(e.getMessage(), e);
	}

}
 
开发者ID:NimbleGen,项目名称:bioinformatics,代码行数:26,代码来源:BamSorter.java


注:本文中的htsjdk.samtools.SAMFileWriterFactory类示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。