本文整理汇总了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();
}
示例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;
}
示例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();
}
示例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;
}
示例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));
}
示例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();
}
示例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;
}
示例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);
}
}
示例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;
}
示例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);
}
示例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;
}
示例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);
}
示例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);
}
示例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;
}
示例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);
}
}