Search in sources :

Example 11 with GATKException

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");
    }
}
Also used : GATKException(org.broadinstitute.hellbender.exceptions.GATKException)

Example 12 with GATKException

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);
    }
}
Also used : Path(java.nio.file.Path) SamReader(htsjdk.samtools.SamReader) ByteArraySeekableStream(htsjdk.samtools.seekablestream.ByteArraySeekableStream) ByteArraySeekableStream(htsjdk.samtools.seekablestream.ByteArraySeekableStream) SeekableStream(htsjdk.samtools.seekablestream.SeekableStream) QueryInterval(htsjdk.samtools.QueryInterval) IOException(java.io.IOException) GATKException(org.broadinstitute.hellbender.exceptions.GATKException)

Example 13 with GATKException

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);
    }
}
Also used : GZIPInputStream(java.util.zip.GZIPInputStream) BufferedInputStream(java.io.BufferedInputStream) BlockCompressedInputStream(htsjdk.samtools.util.BlockCompressedInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) GZIPOutputStream(java.util.zip.GZIPOutputStream) IOException(java.io.IOException) GATKException(org.broadinstitute.hellbender.exceptions.GATKException)

Example 14 with GATKException

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);
    }
}
Also used : IOException(java.io.IOException) GATKException(org.broadinstitute.hellbender.exceptions.GATKException) File(java.io.File)

Example 15 with GATKException

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);
    }
}
Also used : GZIPInputStream(java.util.zip.GZIPInputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) GZIPOutputStream(java.util.zip.GZIPOutputStream) FileOutputStream(java.io.FileOutputStream) IOException(java.io.IOException) GATKException(org.broadinstitute.hellbender.exceptions.GATKException) FileInputStream(java.io.FileInputStream)

Aggregations

GATKException (org.broadinstitute.hellbender.exceptions.GATKException)96 IOException (java.io.IOException)19 SimpleInterval (org.broadinstitute.hellbender.utils.SimpleInterval)13 CigarElement (htsjdk.samtools.CigarElement)12 ArrayList (java.util.ArrayList)10 UserException (org.broadinstitute.hellbender.exceptions.UserException)10 SAMSequenceRecord (htsjdk.samtools.SAMSequenceRecord)8 Cigar (htsjdk.samtools.Cigar)7 File (java.io.File)6 SAMFileHeader (htsjdk.samtools.SAMFileHeader)5 OutputStream (java.io.OutputStream)5 GATKRead (org.broadinstitute.hellbender.utils.read.GATKRead)5 VisibleForTesting (com.google.common.annotations.VisibleForTesting)4 Utils (org.broadinstitute.hellbender.utils.Utils)4 Tuple2 (scala.Tuple2)4 SAMSequenceDictionary (htsjdk.samtools.SAMSequenceDictionary)3 BufferedOutputStream (java.io.BufferedOutputStream)3 InputStream (java.io.InputStream)3 BigInteger (java.math.BigInteger)3 java.util (java.util)3