本文整理汇总了Java中htsjdk.samtools.SAMFileWriter类的典型用法代码示例。如果您正苦于以下问题:Java SAMFileWriter类的具体用法?Java SAMFileWriter怎么用?Java SAMFileWriter使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
SAMFileWriter类属于htsjdk.samtools包,在下文中一共展示了SAMFileWriter类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: writeToSlice
import htsjdk.samtools.SAMFileWriter; //导入依赖的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.SAMFileWriter; //导入依赖的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.SAMFileWriter; //导入依赖的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.SAMFileWriter; //导入依赖的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: gatherNormally
import htsjdk.samtools.SAMFileWriter; //导入依赖的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();
}
示例6: doWork
import htsjdk.samtools.SAMFileWriter; //导入依赖的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;
}
示例7: RevertSamWriter
import htsjdk.samtools.SAMFileWriter; //导入依赖的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);
}
}
示例8: doWork
import htsjdk.samtools.SAMFileWriter; //导入依赖的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;
}
示例9: standardReheader
import htsjdk.samtools.SAMFileWriter; //导入依赖的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);
}
示例10: createIndexedBam
import htsjdk.samtools.SAMFileWriter; //导入依赖的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;
}
示例11: createCommonSAMWriter
import htsjdk.samtools.SAMFileWriter; //导入依赖的package包/类
/**
* Create a common SAMFileWriter for use with GATK tools.
*
* @param outputFile - 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 File outputFile,
final File referenceFile,
final SAMFileHeader header,
final boolean preSorted,
boolean createOutputBamIndex,
final boolean createMD5)
{
return createCommonSAMWriter(
(null == outputFile ? null : outputFile.toPath()),
referenceFile,
header,
preSorted,
createOutputBamIndex,
createMD5);
}
示例12: createCommonSAMWriterFromFactory
import htsjdk.samtools.SAMFileWriter; //导入依赖的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);
}
示例13: sortBamFile
import htsjdk.samtools.SAMFileWriter; //导入依赖的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);
}
}
示例14: ReadToProbeAssigner
import htsjdk.samtools.SAMFileWriter; //导入依赖的package包/类
public ReadToProbeAssigner(String sequenceName, File samFile, File samIndexFile, Map<String, IRangeMap<Probe>> positveStrandProbesRangesBySequenceName,
Map<String, IRangeMap<Probe>> negativeStrandProbesRangesBySequenceName, TallyMap<Integer> readNamesThatAreTheSameForMultiplePairs,
ReadToProbeAssignmentResults readToProbeAssignmentResults, Map<Integer, Set<Probe>> unpairedReadNamesToAssignedProbes, SAMFileWriter alternativeHitsSamWriter,
Map<Integer, SAMRecord> unpairedReadNamesToSamRecord) {
super();
this.sequenceName = sequenceName;
this.samFile = samFile;
this.samIndexFile = samIndexFile;
this.positiveStrandProbesRangesBySequenceName = positveStrandProbesRangesBySequenceName;
this.negativeStrandProbesRangesBySequenceName = negativeStrandProbesRangesBySequenceName;
this.readNamesThatAreTheSameForMultiplePairs = readNamesThatAreTheSameForMultiplePairs;
this.readToProbeAssignmentResults = readToProbeAssignmentResults;
this.unpairedReadNamesToAssignedProbes = unpairedReadNamesToAssignedProbes;
this.alternativeHitsSamWriter = alternativeHitsSamWriter;
this.unpairedReadNamesToSamRecord = unpairedReadNamesToSamRecord;
}
开发者ID:NimbleGen,项目名称:bioinformatics,代码行数:17,代码来源:PrimerReadExtensionAndPcrDuplicateIdentification.java
示例15: sortSamBam
import htsjdk.samtools.SAMFileWriter; //导入依赖的package包/类
public static void sortSamBam(File samBamFile, File sortedBamFile) {
SAMFileReader.setDefaultValidationStringency(ValidationStringency.SILENT);
SAMFileReader reader = new SAMFileReader(IOUtil.openFileForReading(samBamFile));
SAMFileWriter writer = null;
try {
reader.getFileHeader().setSortOrder(SAMFileHeader.SortOrder.coordinate);
writer = new SAMFileWriterFactory().makeBAMWriter(reader.getFileHeader(), false, sortedBamFile);
Iterator<SAMRecord> iterator = reader.iterator();
while (iterator.hasNext()) {
writer.addAlignment(iterator.next());
}
} finally {
closeIfPossible(reader);
closeIfPossible(writer);
}
}