本文整理汇总了Java中htsjdk.samtools.SAMRecordIterator.next方法的典型用法代码示例。如果您正苦于以下问题:Java SAMRecordIterator.next方法的具体用法?Java SAMRecordIterator.next怎么用?Java SAMRecordIterator.next使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类htsjdk.samtools.SAMRecordIterator
的用法示例。
在下文中一共展示了SAMRecordIterator.next方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: getReadFromBamFile
import htsjdk.samtools.SAMRecordIterator; //导入方法依赖的package包/类
@Nullable
private Read getReadFromBamFile(ReadQuery query, Chromosome chromosome, BamFile bamFile) throws IOException {
try (SamReader reader = bamHelper.makeSamReader(bamFile, Collections.singletonList(chromosome),
chromosome.getReferenceId())) {
String chromosomeName = chromosome.getName();
if (reader.getFileHeader().getSequence(chromosomeName) == null) {
chromosomeName = Utils.changeChromosomeName(chromosomeName);
}
SAMRecordIterator iterator = reader.query(chromosomeName, query.getStartIndex(), query.getEndIndex(),
true);
while (iterator.hasNext()) {
final SAMRecord samRecord = iterator.next();
if (samRecord.getReadName().equals(query.getName())) {
BamQueryOption option = new BamQueryOption();
option.setRefID(chromosome.getReferenceId());
option.setChromosomeName(chromosome.getName());
SAMRecordHandler recordHandler = new SAMRecordHandler(query.getStartIndex(), query.getEndIndex(),
referenceManager, null, option);
List<BasePosition> diffBase = recordHandler.computeDifferentBase(samRecord);
return BamUtil.createExtendedRead(samRecord, diffBase);
}
}
}
return null;
}
示例2: countJunctions
import htsjdk.samtools.SAMRecordIterator; //导入方法依赖的package包/类
/**
* Given a SamReader, find all reads within a region (ref:start-end) that span a splice junction and tally the number of times
* each junction is spanned. Optionally tally the average values of "tallyTagName" tags (eg. NM).
*
* @param reader
* @param ref
* @param start
* @param end
* @param orient
* @param minOverlap
* @param tallyTagName
* @param separateReadCounts
* @return
*/
public static SortedMap<GenomeSpan, MappedReadCounter> countJunctions(SamReader reader, String ref, int start, int end, Orientation orient, int minOverlap, String tallyTagName) {
SAMRecordIterator it = reader.query(ref, start, end, true);
SortedMap<GenomeSpan, MappedReadCounter> counters = new TreeMap<GenomeSpan, MappedReadCounter>();
while (it.hasNext()) {
SAMRecord read = it.next();
if (read.isSecondaryOrSupplementary() || read.getDuplicateReadFlag() || read.getNotPrimaryAlignmentFlag() || read.getReadUnmappedFlag() || read.getSupplementaryAlignmentFlag()) {
// skip all secondary / duplicate / unmapped reads
continue;
}
if (isJunctionSpanning(read)) {
for (GenomeSpan junction: getJunctionsForRead(read, orient, minOverlap)) {
if (!counters.containsKey(junction)) {
counters.put(junction, new MappedReadCounter(tallyTagName));
}
counters.get(junction).addRead(read);
}
}
}
it.close();
return counters;
}
示例3: convertFileAndVerifyRecordCount
import htsjdk.samtools.SAMRecordIterator; //导入方法依赖的package包/类
private void convertFileAndVerifyRecordCount(final int expectedCount,
final String fastqFilename1,
final String fastqFilename2,
final FastqQualityFormat version,
final boolean permissiveFormat,
final boolean useSequentialFastqs) throws IOException {
final File samFile = convertFile(fastqFilename1, fastqFilename2, version, permissiveFormat, useSequentialFastqs);
final SamReader samReader = SamReaderFactory.makeDefault().open(samFile);
final SAMRecordIterator iterator = samReader.iterator();
int actualCount = 0;
while (iterator.hasNext()) {
iterator.next();
actualCount++;
}
samReader.close();
Assert.assertEquals(actualCount, expectedCount);
}
示例4: compareReads
import htsjdk.samtools.SAMRecordIterator; //导入方法依赖的package包/类
private static String compareReads(final File actualSam, final File expectedSam, final ValidationStringency validation, final File reference) throws IOException {
try(final SamReader reader1 = getReader(actualSam, validation, reference);
final SamReader reader2 = getReader(expectedSam, validation, reference)) {
final SAMRecordIterator it1 = reader1.iterator();
final SAMRecordIterator it2 = reader2.iterator();
while (it1.hasNext() && it2.hasNext()) {
final SAMRecord read1 = it1.next();
final SAMRecord read2 = it2.next();
final String eqMessage = readsEqualAllowAddingAttributes(read1, read2);
if (eqMessage != null){
return eqMessage;
}
}
if (it1.hasNext() || it2.hasNext()) {
//at least one has no more records (because the while loop is done) and at least one does have records. So we're not equal.
return "Not the same number of reads";
}
return null;
}
}
示例5: scanPureJava
import htsjdk.samtools.SAMRecordIterator; //导入方法依赖的package包/类
private void scanPureJava() throws Exception
{
long start=System.currentTimeMillis();
final MessageDigest md = MessageDigest.getInstance("MD5");
for(int N=0;N< N_LOOP;++N)
{
htsjdk.samtools.SamReader r=htsjdk.samtools.SamReaderFactory.makeDefault().
validationStringency(ValidationStringency.DEFAULT_STRINGENCY).
open(bamFile);
SAMRecordIterator iter=r.iterator();
while(iter.hasNext())
{
SAMRecord rec=iter.next();
md.digest((rec.getReadName()+rec.getReadString()+rec.getBaseQualityString()+rec.getAlignmentStart()).getBytes());
}
r.close();
}
System.out.println("PureJava\t\t\tmd5="+digest(md)+" time="+(System.currentTimeMillis()-start));
}
示例6: getNextRecordToReturn
import htsjdk.samtools.SAMRecordIterator; //导入方法依赖的package包/类
private SAMRecord getNextRecordToReturn() {
SAMRecord nextRecord = null;
if (currentSortedRecords.size() > 0) {
SamRecordAndChunkIndex currentRecordAndReaderIndex = currentSortedRecords.pollFirst();
nextRecord = currentRecordAndReaderIndex.getRecord();
int chunkIndex = currentRecordAndReaderIndex.getChunkIndex();
SAMRecordIterator currentIter = iters[chunkIndex];
if (currentIter.hasNext()) {
SAMRecord nextChunkRecord = currentIter.next();
if (!currentSortedRecords.add(new SamRecordAndChunkIndex(nextChunkRecord, chunkIndex))) {
// add this point we know there is a problem just collecting more info before reporting the problem.
SamRecordAndChunkIndex alreadyContainedRecord = null;
for (SamRecordAndChunkIndex samRecordAndChunk : currentSortedRecords) {
if (comparator.compare(samRecordAndChunk.getRecord(), nextChunkRecord) == 0) {
alreadyContainedRecord = samRecordAndChunk;
}
}
throw new IllegalStateException("Provided comparator does not uniquely identify the records. Comparator failed on sam record[" + nextChunkRecord.getReadName() + " isReadOne:"
+ nextChunkRecord.getFirstOfPairFlag() + "] in chunk[" + chunkIndex + "] which matches [" + alreadyContainedRecord.getRecord().getReadName() + " isReadOne:"
+ alreadyContainedRecord.getRecord().getFirstOfPairFlag() + "] in chunk[" + alreadyContainedRecord.getChunkIndex() + "].");
}
}
}
return nextRecord;
}
示例7: scan
import htsjdk.samtools.SAMRecordIterator; //导入方法依赖的package包/类
private void scan(final SamReader in) throws IOException
{
SAMRecordIterator iter=in.iterator();
while(iter.hasNext())
{
final SAMRecord rec = iter.next();
String sampleName= this.partition.getPartion(rec);
if(sampleName==null || sampleName.isEmpty()) sampleName="__UNDEFINED_"+this.partition.name().toUpperCase()+"_";
Counter<Integer> counter = this.lengths.get(sampleName);
if(counter==null) {
counter = new Counter<>();
this.lengths.put(sampleName,counter);
}
final int len = rec.getReadLength();
counter.incr(len);
this.max_length=Math.max(max_length, len);
}
iter.close();
}
示例8: getReadsFromFile
import htsjdk.samtools.SAMRecordIterator; //导入方法依赖的package包/类
private Map<Sequence, List<SAMRecord>> getReadsFromFile(final Chromosome chromosome, final Track<Sequence> track,
final BamFile bamFile, final List<Sequence> blocks)
throws IOException {
final Map<Sequence, List<SAMRecord>> records = new HashMap<>();
try (SamReader reader = makeSamReader(bamFile, Collections.singletonList(chromosome),
chromosome.getReferenceId())) {
LOG.debug(getMessage(MessagesConstants.DEBUG_FILE_OPENING, bamFile.getPath()));
String chromosomeName = chromosome.getName();
final int startIndex = track.getStartIndex();
final int endIndex = track.getEndIndex();
if (reader.getFileHeader().getSequence(chromosomeName) == null) {
chromosomeName = Utils.changeChromosomeName(chromosomeName);
}
final SAMRecordIterator iterator = reader.queryOverlapping(chromosomeName, startIndex, endIndex);
LOG.debug(getMessage(MessagesConstants.DEBUG_GET_ITERATOR_QUERY, iterator.toString()));
while (iterator.hasNext()) {
final SAMRecord samRecord = iterator.next();
//if read unmapped
if (!samRecord.getSAMFlags().contains(SAMFlag.READ_UNMAPPED) && !samRecord.getCigar().isEmpty() &&
samRecord.getEnd() > samRecord.getStart()) {
addRecordToBlock(blocks, records, samRecord);
}
}
}
return records;
}
示例9: bufferRecord
import htsjdk.samtools.SAMRecordIterator; //导入方法依赖的package包/类
private static SAMRecord bufferRecord(final SAMRecordIterator iter,
final SAMRecord sam_record,
final SAMRecord[] buff_record,
final float[] as,
final boolean[] aln,
final String record_id) {
// TODO Auto-generated method stub
final Set<SAMRecord> buffered_records = new HashSet<SAMRecord>();
buffered_records.add(sam_record);
SAMRecord tmp_record;
while( (tmp_record=iter.hasNext()?iter.next():null)!=null &&
tmp_record.getReadName().equals(record_id)) {
buffered_records.add(tmp_record);
}
Arrays.fill(buff_record, null);
Arrays.fill(as, -1.0f);
Arrays.fill(aln, false);
for(SAMRecord r : buffered_records) {
if( r.getFirstOfPairFlag() &&
!r.getSupplementaryAlignmentFlag() &&
!r.getNotPrimaryAlignmentFlag() ) {
buff_record[0] = r;
as[0] = r.getFloatAttribute("AS");
aln[0] = !r.getReadUnmappedFlag();
}
if( r.getSecondOfPairFlag() &&
!r.getSupplementaryAlignmentFlag() &&
!r.getNotPrimaryAlignmentFlag() ) {
buff_record[1] = r;
as[1] = r.getFloatAttribute("AS");
aln[1] = !r.getReadUnmappedFlag();
}
}
return tmp_record;
}
示例10: getReadsFromFile
import htsjdk.samtools.SAMRecordIterator; //导入方法依赖的package包/类
private Map<Sequence, List<SAMRecord>> getReadsFromFile(
final Chromosome chromosome, final Track<Sequence> track, final BamFile bamFile,
final List<Sequence> blocks) throws IOException {
final Map<Sequence, List<SAMRecord>> records = new HashMap<>();
try (SamReader reader = makeSamReader(bamFile, Collections.singletonList(chromosome),
chromosome.getReferenceId())) {
LOG.debug(getMessage(MessagesConstants.DEBUG_FILE_OPENING, bamFile.getPath()));
String chromosomeName = chromosome.getName();
final int startIndex = track.getStartIndex();
final int endIndex = track.getEndIndex();
if (reader.getFileHeader().getSequence(chromosomeName) == null) {
chromosomeName = Utils.changeChromosomeName(chromosomeName);
}
final SAMRecordIterator iterator = reader.queryOverlapping(chromosomeName, startIndex, endIndex);
LOG.debug(getMessage(MessagesConstants.DEBUG_GET_ITERATOR_QUERY, iterator.toString()));
while (iterator.hasNext()) {
final SAMRecord samRecord = iterator.next();
//if read unmapped
if (!samRecord.getSAMFlags().contains(SAMFlag.READ_UNMAPPED) && !samRecord.getCigar().isEmpty() &&
samRecord.getEnd() > samRecord.getStart()) {
addRecordToBlock(blocks, records, samRecord);
}
}
}
return records;
}
示例11: getSamReadLength
import htsjdk.samtools.SAMRecordIterator; //导入方法依赖的package包/类
public static int getSamReadLength(SamReader reader, int recordsToScan) {
int i = 0;
int size = 0;
SAMRecordIterator it = reader.iterator();
while (it.hasNext() && i < recordsToScan) {
SAMRecord read = it.next();
size = Math.max(size, read.getReadLength());
i++;
}
it.close();
return size;
}
示例12: findOverlappingReads
import htsjdk.samtools.SAMRecordIterator; //导入方法依赖的package包/类
/**
* Find reads that overlap a given genomic region
* @param reader
* @param pos
* @param orient
* @param readLength
* @param minOverlap
* @param allowGaps
* @return
*/
public static List<SAMRecord> findOverlappingReads(SamReader reader, GenomeSpan pos, Orientation orient, int readLength, int minOverlap, boolean allowGaps) {
List<SAMRecord> out = new ArrayList<SAMRecord>();
SAMRecordIterator it = reader.query(pos.ref, pos.start - readLength + minOverlap, pos.start + readLength - minOverlap, true);
while (it.hasNext()) {
SAMRecord read = it.next();
if (read.isSecondaryOrSupplementary() || read.getDuplicateReadFlag() || read.getNotPrimaryAlignmentFlag() || read.getReadUnmappedFlag() || read.getSupplementaryAlignmentFlag()) {
// skip all secondary / duplicate / unmapped reads
continue;
}
if (!allowGaps) {
// skip all reads with gaps
for (CigarElement el: read.getCigar().getCigarElements()) {
if (el.getOperator() == CigarOperator.N) {
continue;
}
}
}
if (ReadUtils.getFragmentEffectiveStrand(read, orient) != pos.strand) {
continue;
}
for (GenomeSpan region: ReadUtils.getJunctionFlankingRegions(read, orient, minOverlap)) {
if (region.start <= (pos.start - minOverlap) && region.end >= (pos.start + minOverlap)) {
out.add(read);
break;
}
}
}
it.close();
return out;
}
示例13: writeReads
import htsjdk.samtools.SAMRecordIterator; //导入方法依赖的package包/类
/**
* Helper function that writes reads from iterator it into writer out, updating each SAMRecord along the way
* according to the newOrder mapping from dictionary index -> index. Name is used for printing only.
*/
private void writeReads(final SAMFileWriter out,
final SAMRecordIterator it,
final Map<Integer, Integer> newOrder,
final String name) {
long counter = 0;
log.info(" Processing " + name);
while (it.hasNext()) {
counter++;
final SAMRecord read = it.next();
final int oldRefIndex = read.getReferenceIndex();
final int oldMateIndex = read.getMateReferenceIndex();
final int newRefIndex = newOrderIndex(read, oldRefIndex, newOrder);
read.setHeader(out.getFileHeader());
read.setReferenceIndex(newRefIndex);
final int newMateIndex = newOrderIndex(read, oldMateIndex, newOrder);
if (oldMateIndex != -1 && newMateIndex == -1) { // becoming unmapped
read.setMateAlignmentStart(0);
read.setMateUnmappedFlag(true);
read.setAttribute(SAMTag.MC.name(), null); // Set the Mate Cigar String to null
}
read.setMateReferenceIndex(newMateIndex);
out.addAlignment(read);
}
it.close();
log.info("Wrote " + counter + " reads");
}
示例14: validateUq
import htsjdk.samtools.SAMRecordIterator; //导入方法依赖的package包/类
private void validateUq(final File input, final File reference) {
final SamReader reader = SamReaderFactory.makeDefault().referenceSequence(reference).open(input);
final SAMRecordIterator iterator = reader.iterator();
while (iterator.hasNext()){
SAMRecord rec = iterator.next();
if (!rec.getReadUnmappedFlag())
Assert.assertNotNull(rec.getAttribute("UQ"));
}
}
示例15: test
import htsjdk.samtools.SAMRecordIterator; //导入方法依赖的package包/类
protected void test() {
try {
final SamFileValidator validator = new SamFileValidator(new PrintWriter(System.out), 8000);
// Validate it has the expected cigar
validator.setIgnoreWarnings(true);
validator.setVerbose(true, 1000);
validator.setErrorsToIgnore(Arrays.asList(SAMValidationError.Type.MISSING_READ_GROUP));
SamReaderFactory factory = SamReaderFactory.makeDefault().validationStringency(ValidationStringency.LENIENT);
SamReader samReader = factory.open(getOutput());
final SAMRecordIterator iterator = samReader.iterator();
while (iterator.hasNext()) {
final SAMRecord rec = iterator.next();
Assert.assertEquals(rec.getCigarString(), expectedCigar);
if (SAMUtils.hasMateCigar(rec)) {
Assert.assertEquals(SAMUtils.getMateCigarString(rec), expectedCigar);
}
}
CloserUtil.close(samReader);
// Run validation on the output file
samReader = factory.open(getOutput());
final boolean validated = validator.validateSamFileVerbose(samReader, null);
CloserUtil.close(samReader);
Assert.assertTrue(validated, "ValidateSamFile failed");
} finally {
TestUtil.recursiveDelete(getOutputDir());
}
}