use of org.broadinstitute.hellbender.exceptions.GATKException in project gatk by broadinstitute.
the class ReadEndsForMarkDuplicatesCodec method decode.
@Override
public ReadEndsForMarkDuplicates decode() {
final ReadEndsForMarkDuplicates read = new ReadEndsForMarkDuplicates();
try {
// If the first read results in an EOF we've exhausted the stream
try {
read.score = this.in.readShort();
} catch (final EOFException eof) {
return null;
}
read.libraryId = this.in.readShort();
read.orientation = this.in.readByte();
read.read1ReferenceIndex = this.in.readInt();
read.read1Coordinate = this.in.readInt();
read.read1IndexInFile = this.in.readLong();
read.read2ReferenceIndex = this.in.readInt();
if (read.orientation > ReadEnds.R) {
read.read2Coordinate = this.in.readInt();
read.read2IndexInFile = this.in.readLong();
}
read.readGroup = this.in.readShort();
read.tile = this.in.readShort();
read.x = this.in.readShort();
read.y = this.in.readShort();
read.orientationForOpticalDuplicates = this.in.readByte();
return read;
} catch (final IOException ioe) {
throw new GATKException("Exception writing ReadEnds to file.", ioe);
}
}
use of org.broadinstitute.hellbender.exceptions.GATKException in project gatk by broadinstitute.
the class SWPairwiseAlignment method printAlignment.
public void printAlignment(final byte[] ref, final byte[] read, final int width) {
final StringBuilder bread = new StringBuilder();
final StringBuilder bref = new StringBuilder();
final StringBuilder match = new StringBuilder();
int i = 0;
int j = 0;
final int offset = getAlignmentStart2wrt1();
Cigar cigar = getCigar();
if (overhangStrategy != OverhangStrategy.SOFTCLIP) {
// otherwise offset is never negative
if (offset < 0) {
for (; j < (-offset); j++) {
bread.append((char) read[j]);
bref.append(' ');
match.append(' ');
}
// at negative offsets, our cigar's first element carries overhanging bases
// that we have just printed above. Tweak the first element to
// exclude those bases. Here we create a new list of cigar elements, so the original
// list/original cigar are unchanged (they are unmodifiable anyway!)
final List<CigarElement> tweaked = new ArrayList<>();
tweaked.addAll(cigar.getCigarElements());
tweaked.set(0, new CigarElement(cigar.getCigarElement(0).getLength() + offset, cigar.getCigarElement(0).getOperator()));
cigar = new Cigar(tweaked);
}
}
if (offset > 0) {
// note: the way this implementation works, cigar will ever start from S *only* if read starts before the ref, i.e. offset = 0
for (; i < getAlignmentStart2wrt1(); i++) {
bref.append((char) ref[i]);
bread.append(' ');
match.append(' ');
}
}
for (final CigarElement e : cigar.getCigarElements()) {
switch(e.getOperator()) {
case M:
for (int z = 0; z < e.getLength(); z++, i++, j++) {
bref.append((i < ref.length) ? (char) ref[i] : ' ');
bread.append((j < read.length) ? (char) read[j] : ' ');
match.append((i < ref.length && j < read.length) ? (ref[i] == read[j] ? '.' : '*') : ' ');
}
break;
case I:
for (int z = 0; z < e.getLength(); z++, j++) {
bref.append('-');
bread.append((char) read[j]);
match.append('I');
}
break;
case S:
for (int z = 0; z < e.getLength(); z++, j++) {
bref.append(' ');
bread.append((char) read[j]);
match.append('S');
}
break;
case D:
for (int z = 0; z < e.getLength(); z++, i++) {
bref.append((char) ref[i]);
bread.append('-');
match.append('D');
}
break;
default:
throw new GATKException("Unexpected Cigar element:" + e.getOperator());
}
}
for (; i < ref.length; i++) bref.append((char) ref[i]);
for (; j < read.length; j++) bread.append((char) read[j]);
int pos = 0;
final int maxlength = Math.max(match.length(), Math.max(bread.length(), bref.length()));
while (pos < maxlength) {
print_cautiously(match, pos, width);
print_cautiously(bread, pos, width);
print_cautiously(bref, pos, width);
System.out.println();
pos += width;
}
}
use of org.broadinstitute.hellbender.exceptions.GATKException in project gatk by broadinstitute.
the class AlignmentUtils method readToAlignmentByteArray.
/**
* Generate an array of bases for just those that are aligned to the reference (i.e. no clips or insertions)
*
* @param cigar the read's CIGAR -- cannot be null
* @param read the read's base array
* @return a non-null array of bases (bytes)
*/
@SuppressWarnings("fallthrough")
public static byte[] readToAlignmentByteArray(final Cigar cigar, final byte[] read) {
Utils.nonNull(cigar);
Utils.nonNull(read);
final int alignmentLength = cigar.getReferenceLength();
final byte[] alignment = new byte[alignmentLength];
int alignPos = 0;
int readPos = 0;
for (int iii = 0; iii < cigar.numCigarElements(); iii++) {
final CigarElement ce = cigar.getCigarElement(iii);
final int elementLength = ce.getLength();
switch(ce.getOperator()) {
case I:
if (alignPos > 0) {
final int prevPos = alignPos - 1;
if (alignment[prevPos] == BaseUtils.Base.A.base) {
alignment[prevPos] = PileupElement.A_FOLLOWED_BY_INSERTION_BASE;
} else if (alignment[prevPos] == BaseUtils.Base.C.base) {
alignment[prevPos] = PileupElement.C_FOLLOWED_BY_INSERTION_BASE;
} else if (alignment[prevPos] == BaseUtils.Base.T.base) {
alignment[prevPos] = PileupElement.T_FOLLOWED_BY_INSERTION_BASE;
} else if (alignment[prevPos] == BaseUtils.Base.G.base) {
alignment[prevPos] = PileupElement.G_FOLLOWED_BY_INSERTION_BASE;
}
}
case S:
readPos += elementLength;
break;
case D:
case N:
for (int jjj = 0; jjj < elementLength; jjj++) {
alignment[alignPos++] = PileupElement.DELETION_BASE;
}
break;
case M:
case EQ:
case X:
for (int jjj = 0; jjj < elementLength; jjj++) {
alignment[alignPos++] = read[readPos++];
}
break;
case H:
case P:
break;
default:
throw new GATKException("Unsupported cigar operator: " + ce.getOperator());
}
}
return alignment;
}
use of org.broadinstitute.hellbender.exceptions.GATKException in project gatk by broadinstitute.
the class ArtificialReadQueryIterator method initialize.
/**
* initialize the query iterator
*
* @param contig the contig
* @param start the start position
* @param stop the stop postition
*/
private void initialize(String contig, int start, int stop) {
// throw away data from the previous invocation, if one exists.
ensureUntouched();
reset();
finalPos = stop;
startPos = start;
if (finalPos < 0) {
finalPos = Integer.MAX_VALUE;
}
// sanity check that we have the contig
contigIndex = -1;
List<SAMSequenceRecord> list = header.getSequenceDictionary().getSequences();
for (SAMSequenceRecord rec : list) {
if (rec.getSequenceName().equals(contig)) {
contigIndex = rec.getSequenceIndex();
}
}
if (contigIndex < 0) {
throw new IllegalArgumentException("ArtificialContig" + contig + " doesn't exist");
}
while (super.hasNext() && ReadUtils.getReferenceIndex(this.peek(), header) < contigIndex) {
super.next();
}
if (!super.hasNext()) {
throw new GATKException("Unable to find the target chromosome");
}
while (super.hasNext() && this.peek().getStart() < start) {
super.next();
}
// sanity check that we have an actual matching read next
GATKRead rec = this.peek();
if (!matches(rec)) {
throw new GATKException("The next read doesn't match");
}
// set the seeked variable to true
seeked = true;
}
use of org.broadinstitute.hellbender.exceptions.GATKException in project gatk by broadinstitute.
the class CigarConversionUtils method convertSAMCigarElementToCigarUnit.
/**
* Converts an individual SAM CigarElement to an equivalent GA4GH-style CigarUnit
*
* @param cigarElement SAM CigarElement to convert to CigarUnit
* @return equivalent CigarUnit
*/
public static CigarUnit convertSAMCigarElementToCigarUnit(final CigarElement cigarElement) {
if (cigarElement == null) {
throw new IllegalArgumentException("Cannot convert a null CigarElement");
}
final String convertedOperation = samToGA4GHOperatorTable.get(cigarElement.getOperator());
if (convertedOperation == null) {
throw new GATKException("Unable to convert CigarElement to CigarUnit: unknown operator in CigarElement: " + cigarElement.getOperator());
}
if (cigarElement.getLength() < 0) {
throw new GATKException("Unable to convert CigarElement to CigarUnit: negative operation length in CigarElement: " + cigarElement.getLength());
}
final CigarUnit cigarUnit = new CigarUnit();
cigarUnit.setOperationLength((long) cigarElement.getLength());
cigarUnit.setOperation(convertedOperation);
return cigarUnit;
}
Aggregations