本文整理汇总了Java中htsjdk.samtools.SamInputResource类的典型用法代码示例。如果您正苦于以下问题:Java SamInputResource类的具体用法?Java SamInputResource怎么用?Java SamInputResource使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
SamInputResource类属于htsjdk.samtools包,在下文中一共展示了SamInputResource类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: loadIndex
import htsjdk.samtools.SamInputResource; //导入依赖的package包/类
private SamInputResource loadIndex(final SamInputResource samInputResource, final BiologicalDataItem indexFile)
throws IOException {
SamInputResource resource;
switch (indexFile.getType()) {
case FILE:
resource = samInputResource.index(new File(indexFile.getPath()));
break;
case URL:
resource = samInputResource.index(new URL(indexFile.getPath()));
break;
case S3:
resource = getS3Index(samInputResource, indexFile);
break;
case HDFS:
resource = getHDFSIndex(samInputResource, indexFile);
break;
default:
throw new IllegalArgumentException(getMessage(MessagesConstants.ERROR_INVALID_PARAM));
}
return resource;
}
示例2: loadFile
import htsjdk.samtools.SamInputResource; //导入依赖的package包/类
private SamInputResource loadFile(final BamFile bamFile)
throws IOException {
SamInputResource resource;
switch (bamFile.getType()) {
case FILE:
case URL:
resource = SamInputResource.of(bamFile.getPath());
break;
case S3:
resource = getS3SamInputResource(bamFile);
break;
case HDFS:
resource= getHDFSSamInputResource(bamFile);
break;
default:
throw new IllegalArgumentException(getMessage(MessagesConstants.ERROR_INVALID_PARAM));
}
return resource;
}
示例3: open
import htsjdk.samtools.SamInputResource; //导入依赖的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);
}
示例4: main
import htsjdk.samtools.SamInputResource; //导入依赖的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();
}
示例5: sliceFromS3
import htsjdk.samtools.SamInputResource; //导入依赖的package包/类
private static void sliceFromS3(@NotNull final CommandLine cmd) throws IOException, EmptyFileException {
final OkHttpClient httpClient = SbpS3Client.create(Integer.parseInt(cmd.getOptionValue(MAX_CONCURRENT_REQUESTS)));
final URL bamUrl = SbpS3UrlGenerator.generateUrl(cmd.getOptionValue(BUCKET), cmd.getOptionValue(INPUT));
final URL indexUrl = SbpS3UrlGenerator.generateUrl(cmd.getOptionValue(BUCKET), cmd.getOptionValue(INDEX));
final String outputPath = cmd.getOptionValue(OUTPUT);
final String bedPath = cmd.getOptionValue(BED);
final int maxBufferSize = readMaxBufferSize(cmd);
final File indexFile = downloadIndex(indexUrl);
final SamReader reader = SamReaderFactory.makeDefault().open(SamInputResource.of(bamUrl).index(indexFile));
LOGGER.info("Generating query intervals from BED file: {}", bedPath);
final QueryInterval[] intervals = getIntervalsFromBED(bedPath, reader.getFileHeader());
final BAMFileSpan span = bamSpanForIntervals(indexFile, reader.getFileHeader(), intervals);
final List<Chunk> expandedChunks = expandChunks(span.getChunks());
LOGGER.info("Generated {} query intervals which map to {} bam chunks", intervals.length, expandedChunks.size());
final SamInputResource bamResource =
SamInputResource.of(new CachingSeekableHTTPStream(httpClient, bamUrl, expandedChunks, maxBufferSize)).index(indexFile);
final SamReader cachingReader = SamReaderFactory.makeDefault().open(bamResource);
LOGGER.info("Slicing bam...");
final CloseableIterator<SAMRecord> iterator = getIterator(cachingReader, intervals, span.toCoordinateArray());
writeToSlice(outputPath, cachingReader.getFileHeader(), iterator);
cachingReader.close();
reader.close();
indexFile.deleteOnExit();
}
示例6: getAlignedReadCount
import htsjdk.samtools.SamInputResource; //导入依赖的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;
}
示例7: bamHasIndex
import htsjdk.samtools.SamInputResource; //导入依赖的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;
}
示例8: readSAMHeaderFrom
import htsjdk.samtools.SamInputResource; //导入依赖的package包/类
/** Does not close the stream. */
public static SAMFileHeader readSAMHeaderFrom(
final InputStream in, final Configuration conf)
{
final ValidationStringency
stringency = getValidationStringency(conf);
SamReaderFactory readerFactory = SamReaderFactory.makeDefault()
.setOption(SamReaderFactory.Option.EAGERLY_DECODE, false)
.setUseAsyncIo(false);
if (stringency != null) {
readerFactory.validationStringency(stringency);
}
final ReferenceSource refSource = getReferenceSource(conf);
if (null != refSource) {
readerFactory.referenceSource(refSource);
}
return readerFactory.open(SamInputResource.of(in)).getFileHeader();
}
示例9: openBAMReader
import htsjdk.samtools.SamInputResource; //导入依赖的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;
}
示例10: getReads
import htsjdk.samtools.SamInputResource; //导入依赖的package包/类
/** Parses the BAM file into SAMRecords. Will be distributed onto at least 'numPartitions' partitions. **/
public JavaRDD<SAMRecord> getReads(JavaSparkContext ctx, int numPartitions) {
try {
Path bamPath = IOUtils.getPath(bam);
ChannelAsSeekableStream bamOverNIO = new ChannelAsSeekableStream(Files.newByteChannel(bamPath), bamPath.toString());
final byte[] index = getIndex();
SeekableStream indexInMemory = new ByteArraySeekableStream(index);
SamReader bam3 = SamReaderFactory.makeDefault()
.validationStringency(ValidationStringency.LENIENT)
.enable(SamReaderFactory.Option.CACHE_FILE_BASED_INDEXES)
.open(SamInputResource.of(bamOverNIO).index(indexInMemory));
List<QueryInterval> chunks = getAllChunksBalanced(bam3, numPartitions);
// Ideally we'd get exactly the number of chunks the user is asking for, but until then...
logger.debug("We got: " + chunks.size() + " chunks.");
return ctx.parallelize(chunks, chunks.size()).flatMap(qi -> new ReadsIterable(bam, index, qi).iterator());
}
catch ( IOException e ) {
throw new GATKException("I/O error loading reads", e);
}
}
示例11: ReadsIterator
import htsjdk.samtools.SamInputResource; //导入依赖的package包/类
public ReadsIterator() throws IOException {
Path fpath = IOUtils.getPath(path);
byte[] indexData = index;
SeekableStream indexInMemory = new ByteArraySeekableStream(indexData);
// set high-level retries to deal with servers that might be temporarily overloaded
// while we're reading a very long file from them.
SeekableByteChannelPrefetcher chan = new SeekableByteChannelPrefetcher(
Files.newByteChannel(fpath), BUFSIZE);
ChannelAsSeekableStream bamOverNIO = new ChannelAsSeekableStream(chan, path);
bam = SamReaderFactory.makeDefault()
.validationStringency(ValidationStringency.LENIENT)
.enable(SamReaderFactory.Option.CACHE_FILE_BASED_INDEXES)
.open(SamInputResource.of(bamOverNIO).index(indexInMemory));
QueryInterval[] array = new QueryInterval[1];
array[0] = interval;
query = bam.query(array, false);
}
示例12: createIndex
import htsjdk.samtools.SamInputResource; //导入依赖的package包/类
/**
* Create the BAI index.
* @param conf the Hadoop configuration
* @param bamFile the BAM file
* @param indexFile the BAI file
* @throws IOException if an error occurs while creating the index
*/
static void createIndex(final Configuration conf, final Path bamFile,
final Path indexFile) throws IOException {
final InputStream in = FileSystem.get(conf).open(bamFile);
final SamReader reader = SamReaderFactory.makeDefault()
.enable(SamReaderFactory.Option.INCLUDE_SOURCE_IN_RECORDS)
.validationStringency(ValidationStringency.DEFAULT_STRINGENCY)
.open(SamInputResource.of(in));
final BAMIndexer indexer =
new BAMIndexer(indexFile.getFileSystem(conf).create(indexFile),
reader.getFileHeader());
for (SAMRecord rec : reader) {
indexer.processAlignment(rec);
}
indexer.finish();
}
示例13: readSAMHeader
import htsjdk.samtools.SamInputResource; //导入依赖的package包/类
/**
* Read the SAM header of a SAM file.
* @param is input stream
* @return a String with the SAM header
*/
public static String readSAMHeader(final InputStream is) {
if (is == null) {
throw new NullPointerException("The input stream is null.");
}
// Read SAM file header
final SamReader reader =
SamReaderFactory.makeDefault().open(SamInputResource.of(is));
final SAMFileHeader header = reader.getFileHeader();
// Close reader
// reader.close();
final StringWriter headerTextBuffer = new StringWriter();
new SAMTextHeaderCodec().encode(headerTextBuffer, header);
return headerTextBuffer.toString();
}
示例14: buildBloomFilter
import htsjdk.samtools.SamInputResource; //导入依赖的package包/类
@Override
protected EnhancedBloomFilter buildBloomFilter(final InputStream is)
throws IOException {
// Create filter
final EnhancedBloomFilter filter =
initBloomFilter(getExpectedNumberOfElements());
// Parse BAM file
try (final SamReader bamReader =
SamReaderFactory.makeDefault().open(SamInputResource.of(is))) {
for (SAMRecord aBamReader : bamReader) {
// Convert in line in SAM and save in filter
filter.put(aBamReader.getSAMString());
}
} catch (final Exception e) {
throw new IOException("Fail read BAM file exception: " + e.getMessage());
}
return filter;
}
示例15: reOpen
import htsjdk.samtools.SamInputResource; //导入依赖的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;
}