public static Cigar invertCigar (Cigar cigar) { Stack<CigarElement> cigarStack = new Stack<CigarElement>(); for (CigarElement cigarElement : cigar.getCigarElements()) cigarStack.push(cigarElement); Cigar invertedCigar = new Cigar(); while (!cigarStack.isEmpty()) invertedCigar.add(cigarStack.pop()); return invertedCigar; }
/** * Convert CIGAR from disk representation to object. * @param binaryCigar ByteArray that is assumed to have byte order set appropriately for extracting ints. */ static Cigar decode(final ByteBuffer binaryCigar) { final Cigar ret = new Cigar(); while (binaryCigar.hasRemaining()) { final int cigarette = binaryCigar.getInt(); ret.add(binaryCigarToCigarElement(cigarette)); } return ret; }
/** * Convert CIGAR from disk representation to object. * @param binaryCigar ByteArray that is assumed to have byte order set appropriately for extracting ints. */ Cigar decode(final ByteBuffer binaryCigar) { final Cigar ret = new Cigar(); while (binaryCigar.hasRemaining()) { final int cigarette = binaryCigar.getInt(); ret.add(binaryCigarToCigarElement(cigarette)); } return ret; }
/** * Convert CIGAR from disk representation to object. * @param binaryCigar ByteArray that is assumed to have byte order set appropriately for extracting ints. */ static Cigar decode(final ByteBuffer binaryCigar) { final Cigar ret = new Cigar(); while (binaryCigar.hasRemaining()) { final int cigarette = binaryCigar.getInt(); ret.add(binaryCigarToCigarElement(cigarette)); } return ret; }
/** * Convert CIGAR from disk representation to object. * @param binaryCigar Array of unsigned ints, one for each CIGAR element. */ Cigar decode(final int[] binaryCigar) { final Cigar ret = new Cigar(); for (final int cigarette : binaryCigar) { ret.add(binaryCigarToCigarElement(cigarette)); } return ret; }
/** * Convert CIGAR from disk representation to object. * @param binaryCigar Array of unsigned ints, one for each CIGAR element. */ static Cigar decode(final int[] binaryCigar) { final Cigar ret = new Cigar(); for (final int cigarette : binaryCigar) { ret.add(binaryCigarToCigarElement(cigarette)); } return ret; }
/** * Convert CIGAR from disk representation to object. * @param binaryCigar Array of unsigned ints, one for each CIGAR element. */ static Cigar decode(final int[] binaryCigar) { final Cigar ret = new Cigar(); for (final int cigarette : binaryCigar) { ret.add(binaryCigarToCigarElement(cigarette)); } return ret; }
public Cigar convertToCigar(boolean negativeStrand) { Cigar cigar = new Cigar(); Iterator<AlignmentMatchSequenceEntry> iterator = negativeStrand ? entries.descendingIterator() : entries.iterator(); while( iterator.hasNext() ) { AlignmentMatchSequenceEntry entry = iterator.next(); CigarOperator operator; switch( entry.getAlignmentState() ) { case MATCH_MISMATCH: operator = CigarOperator.MATCH_OR_MISMATCH; break; case INSERTION: operator = CigarOperator.INSERTION; break; case DELETION: operator = CigarOperator.DELETION; break; default: throw new ReviewedGATKException("convertToCigar: cannot process state: " + entry.getAlignmentState()); } cigar.add( new CigarElement(entry.count,operator) ); } return cigar; }
private static Cigar createCigarFromCombination(byte[] cigarCombination, CigarElement[] cigarElements) { Cigar cigar = new Cigar(); for (byte i : cigarCombination) { cigar.add(cigarElements[i]); } return cigar; }
/** * Gets the cigar for this alignment. * @return sam-jdk formatted alignment. */ public Cigar getCigar() { Cigar cigar = new Cigar(); for(int i = 0; i < cigarOperators.length; i++) { CigarOperator operator = CigarOperator.characterToEnum(cigarOperators[i]); cigar.add(new CigarElement(cigarLengths[i],operator)); } return cigar; }
/** * Get the haplotype cigar extended by padSize M at the tail, consolidated into a clean cigar * * @param padSize how many additional Ms should be appended to the end of this cigar. Must be >= 0 * @return a newly allocated Cigar that consolidate(getCigar + padSize + M) */ public Cigar getConsolidatedPaddedCigar(final int padSize) { if ( padSize < 0 ) throw new IllegalArgumentException("padSize must be >= 0 but got " + padSize); final Cigar extendedHaplotypeCigar = new Cigar(getCigar().getCigarElements()); if ( padSize > 0 ) extendedHaplotypeCigar.add(new CigarElement(padSize, CigarOperator.M)); return AlignmentUtils.consolidateCigar(extendedHaplotypeCigar); }
protected Cigar shiftCigarLeft(Cigar cigar, int positionsToShift) { Cigar newCigar = new Cigar(); for (int i=0; i<cigar.getCigarElements().size(); i++) { CigarElement elem = cigar.getCigarElement(i); if (isFirstNonSoftClippedElem(i, cigar)) { int newLen = elem.getLength() - positionsToShift; if (newLen > 0) { CigarElement newElem = new CigarElement(newLen, elem.getOperator()); newCigar.add(newElem); } } else if (isLastNonSoftClippedElem(i, cigar)) { if (elem.getOperator() == CigarOperator.M) { CigarElement newElem = new CigarElement(elem.getLength() + positionsToShift, CigarOperator.M); newCigar.add(newElem); } else { CigarElement newElem = new CigarElement(positionsToShift, CigarOperator.M); newCigar.add(elem); newCigar.add(newElem); } } else { newCigar.add(elem); } } return newCigar; }
/** * Convert from String CIGAR representation to Cigar class representation. Does not * do validation beyond the most basic CIGAR string well-formedness, i.e. each operator is * valid, and preceded by a decimal length. * @param textCigar CIGAR in String form ala SAM text file. "*" means empty CIGAR. * @throws RuntimeException if textCigar is invalid at the most basic level. * @return cigar in Cigar class format */ public Cigar decode(final String textCigar) { if (SAMRecord.NO_ALIGNMENT_CIGAR.equals(textCigar)) { return new Cigar(); } final Cigar ret = new Cigar(); final byte[] cigarBytes = StringUtil.stringToBytes(textCigar); for (int i = 0; i < cigarBytes.length; ++i) { if (!isDigit(cigarBytes[i])) { throw new IllegalArgumentException("Malformed CIGAR string: " + textCigar); } int length = (cigarBytes[i] - ZERO_BYTE); for (++i; isDigit(cigarBytes[i]); ++i) { length = (length * 10) + cigarBytes[i] - ZERO_BYTE; } final CigarOperator operator = CigarOperator.characterToEnum(cigarBytes[i]); ret.add(new CigarElement(length, operator)); } return ret; }
protected Cigar refactorNDNtoN(final Cigar originalCigar) { final Cigar refactoredCigar = new Cigar(); final int cigarLength = originalCigar.numCigarElements(); for(int i = 0; i < cigarLength; i++){ final CigarElement element = originalCigar.getCigarElement(i); if(element.getOperator() == CigarOperator.N && thereAreAtLeast2MoreElements(i,cigarLength)){ final CigarElement nextElement = originalCigar.getCigarElement(i+1); final CigarElement nextNextElement = originalCigar.getCigarElement(i+2); // if it is N-D-N replace with N (with the total length) otherwise just add the first N. if(nextElement.getOperator() == CigarOperator.D && nextNextElement.getOperator() == CigarOperator.N){ final int threeElementsLength = element.getLength() + nextElement.getLength() + nextNextElement.getLength(); final CigarElement refactoredElement = new CigarElement(threeElementsLength,CigarOperator.N); refactoredCigar.add(refactoredElement); i += 2; //skip the elements that were refactored } else refactoredCigar.add(element); // add only the first N } else refactoredCigar.add(element); // add any non-N element } return refactoredCigar; }
/** * Create a reference haplotype for an active region * * @param activeRegion the active region * @param refBases the ref bases * @param paddedReferenceLoc the location spanning of the refBases -- can be longer than activeRegion.getLocation() * @return a reference haplotype */ public static Haplotype createReferenceHaplotype(final ActiveRegion activeRegion, final byte[] refBases, final GenomeLoc paddedReferenceLoc) { final Haplotype refHaplotype = new Haplotype(refBases, true); final int alignmentStart = activeRegion.getExtendedLoc().getStart() - paddedReferenceLoc.getStart(); if ( alignmentStart < 0 ) throw new IllegalStateException("Bad alignment start in createReferenceHaplotype " + alignmentStart); refHaplotype.setAlignmentStartHapwrtRef(alignmentStart); final Cigar c = new Cigar(); c.add(new CigarElement(refHaplotype.getBases().length, CigarOperator.M)); refHaplotype.setCigar(c); return refHaplotype; } }
private SAMRecord makeRead(final SAMFileHeader alignedHeader, final SAMRecord unmappedRec, final HitSpec hitSpec, final int hitIndex) { final SAMRecord rec = new SAMRecord(alignedHeader); rec.setReadName(unmappedRec.getReadName()); rec.setReadBases(unmappedRec.getReadBases()); rec.setBaseQualities(unmappedRec.getBaseQualities()); rec.setMappingQuality(hitSpec.mapq); if (!hitSpec.primary) rec.setNotPrimaryAlignmentFlag(true); final Cigar cigar = new Cigar(); final int readLength = rec.getReadLength(); if (hitSpec.filtered) { // Add two insertions so alignment is filtered. cigar.add(new CigarElement(readLength-4, CigarOperator.M)); cigar.add(new CigarElement(1, CigarOperator.I)); cigar.add(new CigarElement(1, CigarOperator.M)); cigar.add(new CigarElement(1, CigarOperator.I)); cigar.add(new CigarElement(1, CigarOperator.M)); } else { cigar.add(new CigarElement(readLength, CigarOperator.M)); } rec.setCigar(cigar); rec.setReferenceName(bigSequenceName); rec.setAttribute(SAMTag.HI.name(), hitIndex); rec.setAlignmentStart(hitIndex + 1); return rec; }
private List<Haplotype> assemble(final ReadThreadingAssembler assembler, final byte[] refBases, final GenomeLoc loc, final List<GATKSAMRecord> reads) { final Haplotype refHaplotype = new Haplotype(refBases, true); final Cigar c = new Cigar(); c.add(new CigarElement(refHaplotype.getBases().length, CigarOperator.M)); refHaplotype.setCigar(c); final ActiveRegion activeRegion = new ActiveRegion(loc, null, true, genomeLocParser, 0); activeRegion.addAll(reads); // logger.warn("Assembling " + activeRegion + " with " + engine); final AssemblyResultSet assemblyResultSet = assembler.runLocalAssembly(activeRegion, refHaplotype, refBases, loc, Collections.<VariantContext>emptyList(), null); return assemblyResultSet.getHaplotypeList(); }
protected GATKSAMRecord buildSAMRecord(String readName, String contig, int alignmentStart, int alignmentEnd) { SAMFileHeader header = ArtificialSAMUtils.createDefaultReadGroup(new SAMFileHeader(), "test", "test"); header.setSequenceDictionary(dictionary); header.setSortOrder(SAMFileHeader.SortOrder.coordinate); GATKSAMRecord record = new GATKSAMRecord(header); record.setReadName(readName); record.setReferenceIndex(dictionary.getSequenceIndex(contig)); record.setAlignmentStart(alignmentStart); Cigar cigar = new Cigar(); int len = alignmentEnd - alignmentStart + 1; cigar.add(new CigarElement(len, CigarOperator.M)); record.setCigar(cigar); record.setReadString(new String(new char[len]).replace("\0", "A")); record.setBaseQualities(new byte[len]); record.setReadGroup(new GATKSAMReadGroupRecord(header.getReadGroup("test"))); return record; }
/** * Build a SAM record featuring the absolute minimum required dataset. * TODO: Blatantly copied from LocusViewTemplate. Refactor these into a set of tools. * @param contig Contig to populate. * @param alignmentStart start of alignment * @param alignmentEnd end of alignment * @return New SAM Record */ protected SAMRecord buildSAMRecord( String contig, int alignmentStart, int alignmentEnd ) { SAMFileHeader header = new SAMFileHeader(); header.setSequenceDictionary(sequenceFile.getSequenceDictionary()); SAMRecord record = new SAMRecord(header); record.setReferenceIndex(sequenceFile.getSequenceDictionary().getSequenceIndex(contig)); record.setAlignmentStart(alignmentStart); Cigar cigar = new Cigar(); cigar.add(new CigarElement(alignmentEnd-alignmentStart+1, CigarOperator.M)); record.setCigar(cigar); return record; }
/** * Build a SAM record featuring the absolute minimum required dataset. * * @param contig Contig to populate. * @param alignmentStart start of alignment * @param alignmentEnd end of alignment * * @return New SAM Record */ protected GATKSAMRecord buildSAMRecord(String readName, String contig, int alignmentStart, int alignmentEnd) { SAMFileHeader header = new SAMFileHeader(); header.setSequenceDictionary(sequenceSourceFile.getSequenceDictionary()); GATKSAMRecord record = new GATKSAMRecord(header); record.setReadName(readName); record.setReferenceIndex(sequenceSourceFile.getSequenceDictionary().getSequenceIndex(contig)); record.setAlignmentStart(alignmentStart); Cigar cigar = new Cigar(); int len = alignmentEnd - alignmentStart + 1; cigar.add(new CigarElement(len, CigarOperator.M)); record.setCigar(cigar); record.setReadBases(new byte[len]); record.setBaseQualities(new byte[len]); return record; }