本文整理汇总了Java中htsjdk.samtools.SamReaderFactory.open方法的典型用法代码示例。如果您正苦于以下问题:Java SamReaderFactory.open方法的具体用法?Java SamReaderFactory.open怎么用?Java SamReaderFactory.open使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类htsjdk.samtools.SamReaderFactory
的用法示例。
在下文中一共展示了SamReaderFactory.open方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: open
import htsjdk.samtools.SamReaderFactory; //导入方法依赖的package包/类
/**
* A method that creates a SamReader object that's passed on to the HTSJDK.
* Each time someone tries to open a SamReader on an URL,
* HTSJDK checks if there's a custom reader factory and if it's there, this method is called.
*
* @param url target file URL
* @return A SamReader object on a specified file URL
*/
@Override
public SamReader open(URL url) {
PerformanceMonitor.start();
AmazonS3URI amazonURI = new AmazonS3URI(url.toString());
S3Client client = new S3Client();
S3InputStreamFactory streamFactory = new S3InputStreamFactory(client);
//download index file if is possible, and then start download .bam file
final Optional<SeekableStream> indexStream;
try {
IndexLoader loader = new IndexLoader(client);
indexStream = loader.loadIndex(amazonURI);
} catch (IOException e) {
throw new RuntimeIOException(e.getMessage() + " failed to download index", e);
}
SeekableStream stream = new S3SeekableStream(amazonURI, client, streamFactory);
SamReaderFactory factory = SamReaderFactory.makeDefault();
SamInputResource inputResource = SamInputResource.of(stream);
indexStream.ifPresent(inputResource::index);
return factory.open(inputResource);
}
示例2: main
import htsjdk.samtools.SamReaderFactory; //导入方法依赖的package包/类
public static void main(String args[]) throws IOException, ParseException {
Options options = new Options();
options.addOption("u","uniquehits",false,"only output hits with a single mapping");
options.addOption("s","nosuboptimal",false,"do not include hits whose score is not equal to the best score for the read");
options.addOption("p","pairedend",false,"output paired-end hits");
options.addOption("j","junctions",false,"output junction mapping reads (reads with gaps)");
CommandLineParser parser = new GnuParser();
CommandLine cl = parser.parse( options, args, false );
uniqueOnly = cl.hasOption("uniquehits");
filterSubOpt = cl.hasOption("nosuboptimal");
inclPairedEnd = cl.hasOption("pairedend");
inclJunction = cl.hasOption("junctions");
SamReaderFactory factory =
SamReaderFactory.makeDefault()
.enable(SamReaderFactory.Option.INCLUDE_SOURCE_IN_RECORDS, SamReaderFactory.Option.VALIDATE_CRC_CHECKSUMS)
.validationStringency(ValidationStringency.SILENT);
SamReader reader = factory.open(SamInputResource.of(System.in));
CloseableIterator<SAMRecord> iter = reader.iterator();
while (iter.hasNext()) {
SAMRecord record = iter.next();
if (record.getReadUnmappedFlag()) {continue; }
processRecord(record);
}
iter.close();
reader.close();
}
示例3: fromBam
import htsjdk.samtools.SamReaderFactory; //导入方法依赖的package包/类
@NotNull
public Multimap<Chromosome, CobaltCount> fromBam(@NotNull final String referenceBam, @NotNull final String tumorBam)
throws IOException, ExecutionException, InterruptedException {
final File tumorFile = new File(tumorBam);
final File referenceFile = new File(referenceBam);
final SamReaderFactory readerFactory = SamReaderFactory.make();
final String chromosomeLengthFileName = ChromosomeLengthFile.generateFilename(outputDirectory, tumor);
final List<ChromosomeLength> lengths;
try (SamReader reader = readerFactory.open(tumorFile)) {
lengths = ChromosomeLengthFactory.create(reader.getFileHeader());
}
ChromosomeLengthFile.write(chromosomeLengthFileName, lengths);
LOGGER.info("Calculating Read Count from {}", tumorFile.toString());
final List<Future<ChromosomeReadCount>> tumorFutures = createFutures(readerFactory, tumorFile, lengths);
LOGGER.info("Calculating Read Count from {}", referenceFile.toString());
final List<Future<ChromosomeReadCount>> referenceFutures = createFutures(readerFactory, referenceFile, lengths);
final Multimap<String, ReadCount> tumorCounts = fromFutures(tumorFutures);
final Multimap<String, ReadCount> referenceCounts = fromFutures(referenceFutures);
LOGGER.info("Read Count Complete");
return CobaltCountFactory.merge(referenceCounts, tumorCounts);
}
示例4: setSamSeqDictFromBam
import htsjdk.samtools.SamReaderFactory; //导入方法依赖的package包/类
private boolean setSamSeqDictFromBam(String bamfile) {
/* ------------------------------------------------------ */
/* This chunk prepares SamReader from local bam */
SamReaderFactory srf=SamReaderFactory.make();
srf.validationStringency(ValidationStringency.SILENT);
SamReader samReader;
samReader= srf.open(new File(bamfile));
/* ------------------------------------------------------ */
SAMSequenceDictionary seqDict = samReader.getFileHeader().getSequenceDictionary();
if(seqDict != null && !seqDict.isEmpty()){
this.setSamSeqDictSource(new File(bamfile).getAbsolutePath());
this.setSamSeqDict(seqDict);
return true;
}
return false;
}
示例5: getAlignedReadCount
import htsjdk.samtools.SamReaderFactory; //导入方法依赖的package包/类
public static long getAlignedReadCount(String bam) throws IOException{
/* ------------------------------------------------------ */
/* This chunk prepares SamReader from local bam or URL bam */
UrlValidator urlValidator = new UrlValidator();
SamReaderFactory srf=SamReaderFactory.make();
srf.validationStringency(ValidationStringency.SILENT);
SamReader samReader;
if(urlValidator.isValid(bam)){
samReader = SamReaderFactory.makeDefault().open(
SamInputResource.of(new URL(bam)).index(new URL(bam + ".bai"))
);
} else {
samReader= srf.open(new File(bam));
}
/* ------------------------------------------------------ */
List<SAMSequenceRecord> sequences = samReader.getFileHeader().getSequenceDictionary().getSequences();
long alnCount= 0;
for(SAMSequenceRecord x : sequences){
alnCount += samReader.indexing().getIndex().getMetaData(x.getSequenceIndex()).getAlignedRecordCount();
}
samReader.close();
return alnCount;
}
示例6: bamHasIndex
import htsjdk.samtools.SamReaderFactory; //导入方法依赖的package包/类
public static boolean bamHasIndex(String bam) throws IOException{
/* ------------------------------------------------------ */
/* This chunk prepares SamReader from local bam or URL bam */
UrlValidator urlValidator = new UrlValidator();
SamReaderFactory srf=SamReaderFactory.make();
srf.validationStringency(ValidationStringency.SILENT);
SamReader samReader;
if(urlValidator.isValid(bam)){
samReader = SamReaderFactory.makeDefault().open(
SamInputResource.of(new URL(bam)).index(new URL(bam + ".bai"))
);
} else {
samReader= srf.open(new File(bam));
}
/* ------------------------------------------------------ */
// SamReaderFactory srf=SamReaderFactory.make();
// srf.validationStringency(ValidationStringency.SILENT);
// SamReader samReader = srf.open(new File(bam));
boolean hasIndex= samReader.hasIndex();
samReader.close();
return hasIndex;
}
示例7: openBAMReader
import htsjdk.samtools.SamReaderFactory; //导入方法依赖的package包/类
private static SamReader openBAMReader(SamInputResource resource, ValidationStringency stringency, boolean includeFileSource, long offset) throws IOException {
SamReaderFactory samReaderFactory = SamReaderFactory
.makeDefault()
.validationStringency(stringency)
.enable(SamReaderFactory.Option.CACHE_FILE_BASED_INDEXES);
if (includeFileSource) {
samReaderFactory.enable(SamReaderFactory.Option.INCLUDE_SOURCE_IN_RECORDS);
}
if (offset == 0) {
return samReaderFactory.open(resource);
}
LOG.info("Initializing seeking reader with the offset of " + offset);
SeekingBAMFileReader primitiveReader = new SeekingBAMFileReader(resource,
false,
stringency,
DefaultSAMRecordFactory.getInstance(),
offset);
final SeekingReaderAdapter reader =
new SeekingReaderAdapter(primitiveReader, resource);
samReaderFactory.reapplyOptions(reader);
return reader;
}
示例8: testReadsHeader
import htsjdk.samtools.SamReaderFactory; //导入方法依赖的package包/类
@Test
public void testReadsHeader() throws Exception {
final GATKTool tool = new TestGATKToolWithReads();
final CommandLineParser clp = new CommandLineArgumentParser(tool);
final File bamFile = new File(publicTestDir + "org/broadinstitute/hellbender/engine/reads_data_source_test1.bam");
final String[] args = {"-I", bamFile.getCanonicalPath()};
clp.parseArguments(System.out, args);
tool.onStartup();
final SAMFileHeader headerForReads = tool.getHeaderForReads();
final SamReaderFactory factory = SamReaderFactory.makeDefault() //read the file directly and compare headers
.validationStringency(ValidationStringency.SILENT);
try(SamReader samReader = factory.open(bamFile)) {
final SAMFileHeader samFileHeader = samReader.getFileHeader();
Assert.assertEquals(headerForReads, samFileHeader);
}
tool.doWork();
tool.onShutdown();
}
示例9: getDuplicateCountForBam
import htsjdk.samtools.SamReaderFactory; //导入方法依赖的package包/类
private static int getDuplicateCountForBam(final File bam, final File referenceFile) throws IOException {
int duplicateCount = 0;
final SamReaderFactory factory = SamReaderFactory.makeDefault();
if(referenceFile != null) {
factory.referenceSequence(referenceFile);
}
try ( final SamReader reader = factory.open(bam) ) {
for ( SAMRecord read : reader ) {
if ( read.getDuplicateReadFlag() ) {
++duplicateCount;
}
}
}
return duplicateCount;
}
示例10: reOpen
import htsjdk.samtools.SamReaderFactory; //导入方法依赖的package包/类
@Override
public BamFile reOpen() throws IOException {
final String url=this.getSource();
final SamReaderFactory srf = SamReaderFactory.makeDefault();
srf.validationStringency(ValidationStringency.LENIENT);
final SamInputResource sir;
if(IOUtil.isUrl(url))
{
sir = SamInputResource.of(new URL(url));
if(!this.indexFile.isPresent()) throw new IOException("Boum");
sir.index(this.indexFile.get());
}
else
{
sir = SamInputResource.of(new File(url));
}
final BamFile bf = new BamFile(url,srf.open(sir),this.indexFile);
bf.delete_index_on_close=false;
return bf;
}
示例11: SamReaderList
import htsjdk.samtools.SamReaderFactory; //导入方法依赖的package包/类
SamReaderList(final File bamFile,int n) {
final SamReaderFactory srf = SamReaderFactory.makeDefault().validationStringency(ValidationStringency.LENIENT);
this.bamFile=bamFile;
SamReader samReader1 = srf.open(bamFile);
if(!samReader1.hasIndex()) throw new RuntimeIOException("Bam "+bamFile+" is missing an index");
SAMFileHeader header= samReader1.getFileHeader();
this.dict = header.getSequenceDictionary();
if(this.dict==null) throw new RuntimeIOException("Bam "+bamFile+" is missing a sequence dictionary");
String sampleName=null;
for(final SAMReadGroupRecord srg : header.getReadGroups()) {
if(srg.getSample()==null) throw new RuntimeIOException("SamReadGroup in"+bamFile+" is missing a SAMPLE (SN)");
final String indi =srg.getSample();
if(sampleName!=null && !sampleName.equals(indi)) throw new RuntimeIOException("SamReadGroup in"+bamFile+" : more than one sample defined");
sampleName=indi;
}
if(sampleName==null) throw new RuntimeIOException("no SamReadGroup in "+bamFile+" with a SAMPLE (SN)");
this.sample = sampleName;
List<SamReader> readers = new ArrayList<>();
readers.add(samReader1);
while(n>1) {
readers.add(srf.open(bamFile));
--n;
}
this.samReaders = Collections.unmodifiableList(readers);
}
示例12: SAMStats
import htsjdk.samtools.SamReaderFactory; //导入方法依赖的package包/类
public SAMStats(boolean bowtie1, boolean bowtie2){
this.bowtie1 = bowtie1;
this.bowtie2 = bowtie2;
histo = new RealValuedHistogram(0, 1000, 100);
SamReaderFactory factory =
SamReaderFactory.makeDefault()
.enable(SamReaderFactory.Option.INCLUDE_SOURCE_IN_RECORDS, SamReaderFactory.Option.VALIDATE_CRC_CHECKSUMS)
.validationStringency(ValidationStringency.SILENT);
SamReader reader = factory.open(SamInputResource.of(System.in));
CloseableIterator<SAMRecord> iter = reader.iterator();
while (iter.hasNext()) {
SAMRecord record = iter.next();
if(readLen==-1)
readLen = record.getReadLength();
if(bowtie1)
processBT1SAMRecord(record);
else if(bowtie2)
processBT2SAMRecord(record);
else
processSAMRecord(record);
}
iter.close();
try {
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
示例13: main
import htsjdk.samtools.SamReaderFactory; //导入方法依赖的package包/类
public static void main(String args[]) throws IOException, ParseException {
Options options = new Options();
options.addOption("l","left",true,"filename of left side of read");
options.addOption("r","right",true,"filename of right side of read");
options.addOption("u","uniquehits",false,"only output hits with a single mapping");
options.addOption("s","nosuboptimal",false,"do not include hits whose score is not equal to the best score for the read");
options.addOption("D","debug",false,"enable debugging spew?");
CommandLineParser parser = new GnuParser();
CommandLine cl = parser.parse( options, args, false );
uniqueOnly = cl.hasOption("uniquehits");
filterSubOpt = cl.hasOption("nosuboptimal");
debug = cl.hasOption("debug");
String leftfile = cl.getOptionValue("left");
String rightfile = cl.getOptionValue("right");
SamReaderFactory factory =
SamReaderFactory.makeDefault()
.enable(SamReaderFactory.Option.INCLUDE_SOURCE_IN_RECORDS, SamReaderFactory.Option.VALIDATE_CRC_CHECKSUMS)
.validationStringency(ValidationStringency.SILENT);
SamReader leftreader = factory.open(SamInputResource.of(new FileInputStream(leftfile)));
SamReader rightreader = factory.open(SamInputResource.of(new FileInputStream(rightfile)));
leftiter = leftreader.iterator();
rightiter = rightreader.iterator();
leftbuffer = new ArrayList<SAMRecord>();
rightbuffer = new ArrayList<SAMRecord>();
makePairs();
leftreader.close();
rightreader.close();
}
示例14: createReader
import htsjdk.samtools.SamReaderFactory; //导入方法依赖的package包/类
public SamReader createReader() throws IOException, URISyntaxException {
URI indexURI = new URI(index);
String indexFilename = null;
if (indexURI.getScheme() != null && indexURI.getScheme().equals("file")) {
indexFilename = indexURI.getPath();
} else if (indexURI.getScheme() != null && indexURI.getScheme().equals("http")) {
DefaultHttpClient httpclient = new DefaultHttpClient();
HttpGet httpget = new HttpGet(indexURI);
HttpResponse response = httpclient.execute(httpget);
HttpEntity entity = response.getEntity();
indexFilename = File.createTempFile("bam","index").getAbsolutePath();
if (entity != null) {
FileOutputStream os = new FileOutputStream(indexFilename);
entity.writeTo(os);
os.close();
}
httpclient.getConnectionManager().shutdown();
} else {
indexFilename = index; // hope for the best here
}
SamReaderFactory factory =
SamReaderFactory.makeDefault()
.enable(SamReaderFactory.Option.INCLUDE_SOURCE_IN_RECORDS, SamReaderFactory.Option.VALIDATE_CRC_CHECKSUMS)
.validationStringency(ValidationStringency.SILENT);
File datafile = new File(data);
if (datafile.exists()) {
return factory.open(SamInputResource.of(datafile).index(new File(indexFilename)));
} else {
return factory.open(SamInputResource.of(new URL(data)).index(new File(indexFilename)));
}
}
示例15: countReadsInWindow
import htsjdk.samtools.SamReaderFactory; //导入方法依赖的package包/类
/**
* Count reads in interval using the given filters.
* @param bam
* @param gc
* @param filters List of filters to apply
* @return
* @throws MalformedURLException
*/
public static long countReadsInWindow(String bam, GenomicCoords gc, List<SamRecordFilter> filters) throws MalformedURLException {
/* ------------------------------------------------------ */
/* This chunk prepares SamReader from local bam or URL bam */
UrlValidator urlValidator = new UrlValidator();
SamReaderFactory srf=SamReaderFactory.make();
srf.validationStringency(ValidationStringency.SILENT);
SamReader samReader;
if(urlValidator.isValid(bam)){
samReader = srf.open(SamInputResource.of(new URL(bam)).index(new URL(bam + ".bai")));
} else {
samReader= srf.open(new File(bam));
}
/* ------------------------------------------------------ */
long cnt= 0;
Iterator<SAMRecord> sam= samReader.query(gc.getChrom(), gc.getFrom(), gc.getTo(), false);
AggregateFilter aggregateFilter= new AggregateFilter(filters);
while(sam.hasNext()){
SAMRecord rec= sam.next();
if( !aggregateFilter.filterOut(rec) ){
cnt++;
}
}
try {
samReader.close();
} catch (IOException e) {
e.printStackTrace();
}
return cnt;
}