use of org.broadinstitute.hellbender.exceptions.GATKException in project gatk by broadinstitute.
the class TargetMetricsCollector method reflectiveCopy.
/**
* Since the targeted metrics (HsMetrics, TargetedPcrMetrics,...) share many of the same values as TargetMetrics, this copy will copy all public attributes in targetMetrics
* to the outputMetrics' attributes of the same name. If no matching attribute exists in the outputMetrics or the attribute of the target metrics class also is found
* in targetKeys then it's value is not copied. Further more, targetKeys and outputKeys are attribute name arrays synchronized by the index.
* For each target key, targetMetrics.<targetKeys[i]> is assigned to outputMetrics.<outputKeys[i]>
*
* @param targetMetrics A metric with values to be copied
* @param outputMetrics A metrics intended to receive values from targetMetrics
* @param targetKeys Specific names of attributes of targetMetrics to copy to outputMetrics, each key has a corresponding one in outputKeys
* @param outputKeys Specific names of the destination attributes of outputMetrics that will be filled with values of outputMetrics, each key has a corresponding one in targetKeys
* @param <MT> The type of metric of outputMetrics
*/
protected static <MT extends MetricBase> void reflectiveCopy(final TargetMetrics targetMetrics, final MT outputMetrics, final String[] targetKeys, final String[] outputKeys) {
if (targetKeys == null || outputKeys == null) {
if (outputKeys != null) {
throw new GATKException("Target keys is null but output keys == " + StringUtil.join(",", outputKeys));
}
if (targetKeys != null) {
throw new GATKException("Output keys is null but target keys == " + StringUtil.join(",", targetKeys));
}
} else {
if (targetKeys.length != outputKeys.length) {
throw new GATKException("Target keys and output keys do not have the same length: " + "targetKeys == (" + StringUtil.join(",", targetKeys) + ") " + "outputKeys == (" + StringUtil.join(",", outputKeys) + ")");
}
}
final Class<?> mtClass = outputMetrics.getClass();
final Set<Field> targetSet = CollectionUtil.makeSet(TargetMetrics.class.getFields());
for (final String targetKey : targetKeys) {
if (targetSet.contains(targetKey)) {
targetSet.remove(targetKey);
}
}
final Set<String> outputSet = new HashSet<>();
for (final Field field : outputMetrics.getClass().getFields()) {
outputSet.add(field.getName());
}
for (final Field field : targetSet) {
if (outputSet.contains(field.getName())) {
try {
final Field outputField = mtClass.getField(field.getName());
outputField.set(outputMetrics, field.get(targetMetrics));
} catch (Exception e) {
throw new GATKException("Exception while copying targetMetrics to " + outputMetrics.getClass().getName(), e);
}
}
}
for (int i = 0; i < targetKeys.length; i++) {
try {
Field targetMetricField = TargetMetrics.class.getField(targetKeys[i]);
Field outputMetricField = mtClass.getField(outputKeys[i]);
outputMetricField.set(outputMetrics, targetMetricField.get(targetMetrics));
} catch (final Exception exc) {
throw new GATKException("Exception while copying TargetMetrics." + targetKeys[i] + " to " + mtClass.getName() + "." + outputKeys[i], exc);
}
}
}
use of org.broadinstitute.hellbender.exceptions.GATKException in project gatk by broadinstitute.
the class BedToIntervalList method doWork.
@Override
protected Object doWork() {
IOUtil.assertFileIsReadable(INPUT);
IOUtil.assertFileIsReadable(SEQUENCE_DICTIONARY);
IOUtil.assertFileIsWritable(OUTPUT);
try {
final SAMFileHeader header = SamReaderFactory.makeDefault().referenceSequence(REFERENCE_SEQUENCE).getFileHeader(SEQUENCE_DICTIONARY);
final IntervalList intervalList = new IntervalList(header);
/**
* NB: BED is zero-based, but a BEDCodec by default (since it is returns tribble Features) has an offset of one,
* so it returns 1-based starts. Ugh. Set to zero.
*/
final FeatureReader<BEDFeature> bedReader = AbstractFeatureReader.getFeatureReader(INPUT.getAbsolutePath(), new BEDCodec(BEDCodec.StartOffset.ZERO), false);
final CloseableTribbleIterator<BEDFeature> iterator = bedReader.iterator();
final ProgressLogger progressLogger = new ProgressLogger(logger, (int) 1e6);
while (iterator.hasNext()) {
final BEDFeature bedFeature = iterator.next();
final String sequenceName = bedFeature.getContig();
/**
* NB: BED is zero-based, so we need to add one here to make it one-based. Please observe we set the start
* offset to zero when creating the BEDCodec.
*/
final int start = bedFeature.getStart() + 1;
/**
* NB: BED is 0-based OPEN (which, for the end is equivalent to 1-based closed).
*/
final int end = bedFeature.getEnd();
// NB: do not use an empty name within an interval
String name = bedFeature.getName();
if (name.isEmpty())
name = null;
final SAMSequenceRecord sequenceRecord = header.getSequenceDictionary().getSequence(sequenceName);
// Do some validation
if (null == sequenceRecord) {
throw new GATKException(String.format("Sequence '%s' was not found in the sequence dictionary", sequenceName));
} else if (start < 1) {
throw new GATKException(String.format("Start on sequence '%s' was less than one: %d", sequenceName, start));
} else if (sequenceRecord.getSequenceLength() < start) {
throw new GATKException(String.format("Start on sequence '%s' was past the end: %d < %d", sequenceName, sequenceRecord.getSequenceLength(), start));
} else if (end < 1) {
throw new GATKException(String.format("End on sequence '%s' was less than one: %d", sequenceName, end));
} else if (sequenceRecord.getSequenceLength() < end) {
throw new GATKException(String.format("End on sequence '%s' was past the end: %d < %d", sequenceName, sequenceRecord.getSequenceLength(), end));
} else if (end < start - 1) {
throw new GATKException(String.format("On sequence '%s', end < start-1: %d <= %d", sequenceName, end, start));
}
final Interval interval = new Interval(sequenceName, start, end, bedFeature.getStrand() == Strand.POSITIVE, name);
intervalList.add(interval);
progressLogger.record(sequenceName, start);
}
CloserUtil.close(bedReader);
// Sort and write the output
intervalList.uniqued().write(OUTPUT);
} catch (final IOException e) {
throw new RuntimeException(e);
}
return null;
}
use of org.broadinstitute.hellbender.exceptions.GATKException in project gatk by broadinstitute.
the class FindBadGenomicKmersSparkUnitTest method miniRefTest.
@Test(groups = "spark")
public void miniRefTest() throws IOException {
final JavaSparkContext ctx = SparkContextFactory.getTestSparkContext();
final ReferenceMultiSource ref = new ReferenceMultiSource((PipelineOptions) null, REFERENCE_FILE_NAME, ReferenceWindowFunctions.IDENTITY_FUNCTION);
final SAMSequenceDictionary dict = ref.getReferenceSequenceDictionary(null);
if (dict == null)
throw new GATKException("No reference dictionary available.");
final Map<SVKmer, Long> kmerMap = new LinkedHashMap<>();
for (final SAMSequenceRecord rec : dict.getSequences()) {
final SimpleInterval interval = new SimpleInterval(rec.getSequenceName(), 1, rec.getSequenceLength());
final byte[] bases = ref.getReferenceBases(null, interval).getBases();
final SVKmerizer kmerizer = new SVKmerizer(bases, KMER_SIZE, new SVKmerLong());
while (kmerizer.hasNext()) {
final SVKmer kmer = kmerizer.next().canonical(KMER_SIZE);
final Long currentCount = kmerMap.getOrDefault(kmer, 0L);
kmerMap.put(kmer, currentCount + 1);
}
}
final Iterator<Map.Entry<SVKmer, Long>> kmerIterator = kmerMap.entrySet().iterator();
while (kmerIterator.hasNext()) {
if (kmerIterator.next().getValue() <= FindBadGenomicKmersSpark.MAX_KMER_FREQ)
kmerIterator.remove();
}
final List<SVKmer> badKmers = FindBadGenomicKmersSpark.findBadGenomicKmers(ctx, KMER_SIZE, Integer.MAX_VALUE, ref, null, null);
final Set<SVKmer> badKmerSet = new HashSet<>(badKmers);
Assert.assertEquals(badKmers.size(), badKmerSet.size());
Assert.assertEquals(badKmerSet, kmerMap.keySet());
}
use of org.broadinstitute.hellbender.exceptions.GATKException in project gatk-protected by broadinstitute.
the class HDF5PCACoveragePoN method readMatrixAndCheckDimensions.
/**
* Reads a matrix from the underlying PoN file and check its dimensions.
* @param fullPath the target data-set full path within the HDF5 file.
* @param expectedRowCount a predicate that returns true iff its argument is an expected number of rows.
* @param expectedColumnCount a predicate that returns true iff its argument is an expected number of columns.
* @return GATKException if the result matrix dimensions do not match the expectations or
* any other cause as described in {@link #readMatrix(String)}.
*/
private RealMatrix readMatrixAndCheckDimensions(final String fullPath, final IntPredicate expectedRowCount, final IntPredicate expectedColumnCount) {
final RealMatrix result = readMatrix(fullPath);
if (expectedRowCount.test(result.getRowDimension()) && expectedColumnCount.test(result.getColumnDimension())) {
return result;
}
final RealMatrix transpose = result.transpose();
if (!expectedRowCount.test(transpose.getRowDimension())) {
throw new GATKException(String.format("wrong number of rows in '%s' matrix from file '%s': %d", fullPath, file.getFile(), result.getRowDimension()));
}
if (!expectedColumnCount.test(transpose.getColumnDimension())) {
throw new GATKException(String.format("wrong number of columns in '%s' from file '%s': %d", fullPath, file.getFile(), result.getColumnDimension()));
}
return transpose;
}
use of org.broadinstitute.hellbender.exceptions.GATKException in project gatk-protected by broadinstitute.
the class HDF5PCACoveragePoN method renderPoNTargets.
private static List<Target> renderPoNTargets(final String[][] values, final List<String> targetNamesToRender, final HDF5File reader) {
if (values.length != targetNamesToRender.size()) {
throw new GATKException(String.format("Wrong number of elements in the targets recovered " + "from file '%s': number of targets found in file (%d) != number of target names (%d)", reader.getFile(), values.length, targetNamesToRender.size()));
}
final int numTargetCols = (int) reader.readDouble(NUM_TARGET_COLUMNS_PATH);
final List<Target> result = new ArrayList<>(values.length);
for (int i = 0; i < values.length; i++) {
if (values[i].length != numTargetCols) {
throw new GATKException(String.format("Wrong number of column elements in the targets recovered " + "from file '%s': number of columns found in file (%d) != number of target columns (%d)", reader.getFile(), values[i].length, numTargetCols));
}
result.add(new Target(targetNamesToRender.get(i), new SimpleInterval(values[i][0], Integer.parseInt(values[i][1]), Integer.parseInt(values[i][2]))));
}
return result;
}
Aggregations