use of org.broadinstitute.hellbender.exceptions.GATKException in project gatk by broadinstitute.
the class AlleleSubsettingUtils method getSACs.
/**
* Get the genotype SACs
*
* @param g the genotype
* @return an arrays of SACs
* @throws IllegalArgumentException if the genotype does not have an SAC attribute
* @throws GATKException if the type of the SACs is unexpected
*/
private static int[] getSACs(final Genotype g) {
if (!g.hasExtendedAttribute(GATKVCFConstants.STRAND_COUNT_BY_SAMPLE_KEY)) {
throw new IllegalArgumentException("Genotype must have SAC");
}
Class<?> clazz = g.getExtendedAttributes().get(GATKVCFConstants.STRAND_COUNT_BY_SAMPLE_KEY).getClass();
if (clazz.equals(String.class)) {
final String SACsString = (String) g.getExtendedAttributes().get(GATKVCFConstants.STRAND_COUNT_BY_SAMPLE_KEY);
String[] stringSACs = SACsString.split(",");
final int[] intSACs = new int[stringSACs.length];
int i = 0;
for (String sac : stringSACs) {
intSACs[i++] = Integer.parseInt(sac);
}
return intSACs;
} else if (clazz.equals(int[].class)) {
return (int[]) g.getExtendedAttributes().get(GATKVCFConstants.STRAND_COUNT_BY_SAMPLE_KEY);
} else {
throw new GATKException("Unexpected SAC type");
}
}
use of org.broadinstitute.hellbender.exceptions.GATKException in project gatk by broadinstitute.
the class NioBam method getReads.
/** 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);
}
}
use of org.broadinstitute.hellbender.exceptions.GATKException in project gatk by broadinstitute.
the class IOUtils method writeResource.
/**
* Writes the an embedded resource to a file.
* File is not scheduled for deletion and must be cleaned up by the caller.
* @param resource Embedded resource.
* @param file File path to write.
*/
public static void writeResource(Resource resource, File file) {
String path = resource.getPath();
InputStream inputStream = resource.getResourceContentsAsStream();
OutputStream outputStream = null;
try {
outputStream = FileUtils.openOutputStream(file);
org.apache.commons.io.IOUtils.copy(inputStream, outputStream);
} catch (IOException e) {
throw new GATKException(String.format("Unable to copy resource '%s' to '%s'", path, file), e);
} finally {
org.apache.commons.io.IOUtils.closeQuietly(inputStream);
org.apache.commons.io.IOUtils.closeQuietly(outputStream);
}
}
use of org.broadinstitute.hellbender.exceptions.GATKException in project gatk by broadinstitute.
the class IOUtils method createTempFile.
/**
* Creates a temp file that will be deleted on exit
*
* This will also mark the corresponding Tribble/Tabix/BAM indices matching the temp file for deletion.
* @param name Prefix of the file.
* @param extension Extension to concat to the end of the file.
* @return A file in the temporary directory starting with name, ending with extension, which will be deleted after the program exits.
*/
public static File createTempFile(String name, String extension) {
try {
final File file = File.createTempFile(name, extension);
file.deleteOnExit();
// Mark corresponding indices for deletion on exit as well just in case an index is created for the temp file:
new File(file.getAbsolutePath() + Tribble.STANDARD_INDEX_EXTENSION).deleteOnExit();
new File(file.getAbsolutePath() + TabixUtils.STANDARD_INDEX_EXTENSION).deleteOnExit();
new File(file.getAbsolutePath() + ".bai").deleteOnExit();
new File(file.getAbsolutePath().replaceAll(extension + "$", ".bai")).deleteOnExit();
return file;
} catch (IOException ex) {
throw new GATKException("Cannot create temp file: " + ex.getMessage(), ex);
}
}
use of org.broadinstitute.hellbender.exceptions.GATKException in project gatk by broadinstitute.
the class IOUtils method gunzip.
/**
* Un-gzips the input file to the output file.
*/
public static void gunzip(File input, File output) {
try {
try (GZIPInputStream in = new GZIPInputStream(new FileInputStream(input));
OutputStream out = new FileOutputStream(output)) {
byte[] buf = new byte[4096];
int len;
while ((len = in.read(buf)) > 0) {
out.write(buf, 0, len);
}
}
} catch (IOException e) {
throw new GATKException("Exception while unzipping a file:" + input + " to:" + output, e);
}
}
Aggregations