htsjdk.samtools.util.RuntimeIOException Java Examples

The following examples show how to use htsjdk.samtools.util.RuntimeIOException. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: VcfSubset.java    From rtg-tools with BSD 2-Clause "Simplified" License 6 votes vote down vote up
@Override
protected Set<String> collectIds(String flag) {
  final Set<String> result = new LinkedHashSet<>();
  final TsvParser<Set<String>> p = new TsvParser<Set<String>>() {
    @Override
    protected void parseLine(String... parts) {
      result.add(parts[0]);
    }
  };
  for (final Object o : mFlags.getValues(flag)) {
    final String sampleOrFile = (String) o;
    if (sampleOrFile.length() > 0) {
      if (new File(sampleOrFile).exists()) {
        try {
          p.parse(new File(sampleOrFile));
        } catch (IOException e) {
          e.printStackTrace();
          throw new RuntimeIOException(e);
        }
      } else {
        result.add(sampleOrFile);
      }
    }
  }
  return result;
}
 
Example #2
Source File: HaplotypeCallerEngine.java    From gatk with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
/**
 * Shutdown this HC engine, closing resources as appropriate
 */
public void shutdown() {
    likelihoodCalculationEngine.close();
    aligner.close();
    if ( haplotypeBAMWriter.isPresent() ) {
        haplotypeBAMWriter.get().close();
    }
    if ( referenceReader != null){
        try {
            referenceReader.close();
        } catch (IOException e) {
            throw new RuntimeIOException(e);
        }
    }

    if (assemblyDebugOutStream != null) {
        assemblyDebugOutStream.close();
    }
    // Write assembly region debug output if present
    assemblyEngine.printDebugHistograms();

}
 
Example #3
Source File: BasicInputParser.java    From picard with MIT License 6 votes vote down vote up
/**
 * Workhorse method that reads the next line from the underlying reader
 *
 * @return  String or null if there is no next line
 */
protected byte[] readNextLine()
{
    try {
        final String line = reader.readLine();
        if (nextLine != null && !isComment(nextLine.getBytes())) {
            currentLineNumber = nextLineNumber;
            currentLine = nextLine;
        }
        if (line != null) {
            nextLineNumber++;
            nextLine = line;
            return line.getBytes();
        }
        if (!inputs.isEmpty()) {
            advanceFile();
            return readNextLine();
        }
        return null;
    }
    catch(RuntimeIOException ioe) {
        throw new PicardException("Error reading from file " + currentFileName, ioe);
    }
}
 
Example #4
Source File: CramContainer_OBA_Supplier.java    From cramtools with Apache License 2.0 6 votes vote down vote up
@Override
public OrderedByteArray get() {
	OrderedByteArray cb = new OrderedByteArray();
	Container containerHeader;
	try {
		containerHeader = ContainerIO.readContainerHeader(cramHeader.getVersion().major, is);
		log.debug("Read container: " + containerHeader.toString());
		if (containerHeader.isEOF()) {
			log.info("EOF container");
			return null;
		}

		ByteArrayOutputStream baos = new ByteArrayOutputStream(containerHeader.containerByteSize + 1024 * 10);

		ContainerIO.writeContainerHeader(cramHeader.getVersion().major, containerHeader, baos);
		byte[] blocks = InputStreamUtils.readFully(is, containerHeader.containerByteSize);
		baos.write(blocks);

		cb.bytes = baos.toByteArray();
		cb.order = order++;
		return cb;
	} catch (IOException e) {
		throw new RuntimeIOException(e);
	}
}
 
Example #5
Source File: MergeDgeSparse.java    From Drop-seq with MIT License 6 votes vote down vote up
private void writeDiscardedCellsFile(final List<SparseDge> dges) {
    if (DISCARDED_CELLS_FILE != null) {
        try {
            LOG.info("Writing " + DISCARDED_CELLS_FILE.getAbsolutePath());
            final BufferedWriter writer = IOUtil.openFileForBufferedWriting(DISCARDED_CELLS_FILE);
            for (final SparseDge dge : dges) {
                for (final String cell_barcode: dge.getDiscardedCells()) {
                    writer.write(cell_barcode);
                    writer.newLine();
                }
            }
            writer.close();
        } catch (IOException e) {
            throw new RuntimeIOException("Exception writing " + DISCARDED_CELLS_FILE.getAbsolutePath(), e);
        }
    }
}
 
Example #6
Source File: IntervalListToBed.java    From picard with MIT License 6 votes vote down vote up
@Override
protected int doWork() {
    IOUtil.assertFileIsReadable(INPUT);
    IOUtil.assertFileIsWritable(OUTPUT);

    IntervalList intervals = IntervalList.fromFile(INPUT);
    if (SORT) intervals = intervals.sorted();

    try {
        final BufferedWriter out = IOUtil.openFileForBufferedWriting(OUTPUT);
        for (final Interval i : intervals) {
            final String strand = i.isNegativeStrand() ? "-" : "+";
            final List<?> fields = CollectionUtil.makeList(i.getContig(), i.getStart()-1, i.getEnd(), i.getName(), SCORE, strand);
            out.append(fields.stream().map(String::valueOf).collect(Collectors.joining("\t")));
            out.newLine();
        }

        out.close();
    }
    catch (IOException ioe) {
        throw new RuntimeIOException(ioe);
    }

    return 0;
}
 
Example #7
Source File: CompareSAMs.java    From picard with MIT License 6 votes vote down vote up
/**
 * Do the work after command line has been parsed. RuntimeException may be
 * thrown by this method, and are reported appropriately.
 *
 * @return program exit status.
 */
@Override
protected int doWork() {
    final SamReaderFactory samReaderFactory = SamReaderFactory.makeDefault().referenceSequence(REFERENCE_SEQUENCE);

    try (final SamReader samReader1 = samReaderFactory.open(SAM_FILES.get(0));
         final SamReader samReader2 = samReaderFactory.open(SAM_FILES.get(1)))
    {
        final SamComparison comparison = new SamComparison(samReader1, samReader2,
                SAM_FILES.get(0).getAbsolutePath(), SAM_FILES.get(1).getAbsolutePath(), samComparisonArgumentCollection);
        if (OUTPUT != null) {
            comparison.writeReport(OUTPUT, getDefaultHeaders());
        }
        if (comparison.areEqual()) {
            System.out.println("SAM files match.");
        } else {
            System.out.println("SAM files differ.");
        }
        return comparison.areEqual() ? 0 : 1;
    } catch (IOException e) {
        throw new RuntimeIOException("Error opening file", e);
    }
}
 
Example #8
Source File: BclReader.java    From picard with MIT License 6 votes vote down vote up
public BclReader(final File bclFile, final BclQualityEvaluationStrategy bclQualityEvaluationStrategy, final boolean seekable) {
    super(new int[]{1}, bclQualityEvaluationStrategy);
    try {
        final ByteBuffer byteBuffer = ByteBuffer.allocate(HEADER_SIZE);
        final String filePath = bclFile.getName();
        final boolean isGzip = filePath.endsWith(".gz");
        final boolean isBgzf = filePath.endsWith(".bgzf");
        final InputStream stream = open(bclFile, seekable, isGzip, isBgzf);
        final int read = stream.read(byteBuffer.array());

        if (read != HEADER_SIZE) {
            throw new RuntimeIOException(String.format("BCL %s has invalid header structure.", bclFile.getAbsoluteFile()));
        }

        byteBuffer.order(ByteOrder.LITTLE_ENDIAN);
        this.numClustersPerCycle[0] = byteBuffer.getInt();
        if (!isBgzf && !isGzip) {
            assertProperFileStructure(bclFile, this.getNumClusters(), stream);
        }
        this.streams[0] = stream;
        this.streamFiles[0] = bclFile;
    } catch (final IOException ioe) {
        throw new PicardException("IOException opening file " + bclFile.getAbsoluteFile(), ioe);
    }
}
 
Example #9
Source File: MergeDgeSparse.java    From Drop-seq with MIT License 6 votes vote down vote up
private Set<String> loadSelectedCellsLists(final List<File> files) {
    final Set<String> ret = new HashSet<>();
    final Pattern comment = Pattern.compile("#");
    final Pattern whitespace = Pattern.compile("\\s");
    for (final File file : files) {
        final BufferedReader reader = IOUtil.openFileForBufferedReading(file);
        String line;
        try {
            while ((line = reader.readLine()) != null) {
                // Remove trailing comments
                String[] fields = comment.split(line, 2);
                if (!fields[0].isEmpty()) {
                    // Remove trailing whitespace
                    fields = whitespace.split(fields[0], 2);
                    if (!fields[0].isEmpty())
			ret.add(fields[0]);
                }
            }
        } catch (IOException e) {
            throw new RuntimeIOException("Exception reading " + file.getAbsolutePath(), e);
        }
    }
    return ret;
}
 
Example #10
Source File: TabixLineReader.java    From rtg-tools with BSD 2-Clause "Simplified" License 6 votes vote down vote up
protected void advanceSubIterator() {
  try {
    if (mCurrentOffset < mOffsets.size()) {
      if (mOffsets.start(mCurrentOffset) != mPreviousVirtualOffsetStart) {
        //Diagnostic.developerLog("After region " + mCurrentOffset + " " + mCurrentRegion + ", opening new reader at offset " + VirtualOffsets.offsetToString(mOffsets.start(mCurrentOffset)) + " for region " + mOffsets.region(mCurrentOffset));
        mReader.seek(mOffsets.start(mCurrentOffset));
        mBuffered = false;
      //} else {
      //  Diagnostic.developerLog("After region " + mCurrentOffset + " " + mCurrentRegion + ", re-using existing reader for region " + mOffsets.region(mCurrentOffset));
      }
      mCurrentRegion = mOffsets.region(mCurrentOffset);
      final int newTemplate = mSequenceLookup.get(mCurrentRegion.getSequenceName());
      if (newTemplate != mCurrentTemplate) {
        mPreviousAlignmentStart = Integer.MIN_VALUE;
      }
      mPreviousVirtualOffsetStart = mOffsets.start(mCurrentOffset);
      mCurrentTemplate = newTemplate;

    }
    ++mCurrentOffset;
  } catch (IOException e) {
    throw new RuntimeIOException(e.getMessage(), e);
  }
}
 
Example #11
Source File: SplitIntervals.java    From gatk-protected with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Override
public void onTraversalStart() {
    ParamUtils.isPositive(scatterCount, "scatter count must be > 0.");

    if (!outputDir.exists() && !outputDir.mkdir()) {
        throw new RuntimeIOException("Unable to create directory: " + outputDir.getAbsolutePath());
    }

    // in general dictionary will be from the reference, but using -I reads.bam or -F variants.vcf
    // to use the sequence dict from a bam or vcf is also supported
    final SAMSequenceDictionary sequenceDictionary = getBestAvailableSequenceDictionary();

    final List<SimpleInterval> intervals = hasIntervals() ? intervalArgumentCollection.getIntervals(sequenceDictionary)
            : IntervalUtils.getAllIntervalsForReference(sequenceDictionary);

    final IntervalList intervalList = new IntervalList(sequenceDictionary);
    intervals.stream().map(si -> new Interval(si.getContig(), si.getStart(), si.getEnd())).forEach(intervalList::add);
    final IntervalListScatterer scatterer = new IntervalListScatterer(subdivisionMode);
    final List<IntervalList> scattered = scatterer.scatter(intervalList, scatterCount, false);

    final DecimalFormat formatter = new DecimalFormat("0000");
    IntStream.range(0, scattered.size()).forEach(n -> scattered.get(n).write(new File(outputDir, formatter.format(n) + "-scattered.intervals")));
}
 
Example #12
Source File: BamReader.java    From rtg-tools with BSD 2-Clause "Simplified" License 6 votes vote down vote up
@Override
public SamBamRecord next() {
  mAttributeCheck = false;
  mVirtualOffset = mNextVirtualOffset;
  mCurrentAlignmentLength = mNextAlignmentLength;
  initAlignmentBlock(mCurrentAlignmentLength);
  try {
    readDataFully(mCurrentAlignment, 0, mCurrentAlignmentLength, mInput);
    updateVariableLengths();
    mState = true;
    populateHasNext();
  } catch (final IOException e) {
    throw new RuntimeIOException(e.getMessage(), e);
  }
  return mCurrentRecord;
}
 
Example #13
Source File: SequencesReaderReferenceSource.java    From rtg-tools with BSD 2-Clause "Simplified" License 6 votes vote down vote up
/**
 * Get the bases (in uppercase ASCII) for a sequence.  Implementation is cached so that
 * repeated requests for the same (or recently retrieved) sequences are likely to be
 * fast.
 * @param name name of the sequence
 * @return bases of the sequence in uppercase ASCII
 */
public synchronized byte[] getReferenceBases(final String name) {
  final byte[] cached = findInCache(name);
  if (cached != null) {
    return cached;
  }

  Diagnostic.developerLog("SequencesReaderReferenceSource get uncached reference bases for: " + name);
  try {
    final Long seqId = getNames().get(name);
    if (seqId == null) {
      return null;
    }

    final byte[] data = new byte[mReader.length(seqId)];
    mReader.read(seqId, data);
    // Convert from internal binary 0-4 encoding to the ASCII uppercase bases that htsjdk wants
    for (int i = 0; i < data.length; ++i) {
      data[i] = (byte) DnaUtils.getBase(data[i]);
    }
    mCache.put(name, new SoftReference<>(data));
    return data;
  } catch (IOException ioe) {
    throw new RuntimeIOException(ioe);
  }
}
 
Example #14
Source File: SparkUtils.java    From gatk with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * Private helper method for {@link #convertHeaderlessHadoopBamShardToBam} that takes a SAMFileHeader and writes it
 * to the provided `OutputStream`, correctly encoded for the BAM format and preceded by the BAM magic bytes.
 *
 * @param samFileHeader SAM header to write
 * @param outputStream stream to write the SAM header to
 */
private static void writeBAMHeaderToStream( final SAMFileHeader samFileHeader, final OutputStream outputStream ) {
    final BlockCompressedOutputStream blockCompressedOutputStream = new BlockCompressedOutputStream(outputStream, (File)null);
    final BinaryCodec outputBinaryCodec = new BinaryCodec(new DataOutputStream(blockCompressedOutputStream));

    final String headerString;
    final Writer stringWriter = new StringWriter();
    new SAMTextHeaderCodec().encode(stringWriter, samFileHeader, true);
    headerString = stringWriter.toString();

    outputBinaryCodec.writeBytes(ReadUtils.BAM_MAGIC);

    // calculate and write the length of the SAM file header text and the header text
    outputBinaryCodec.writeString(headerString, true, false);

    // write the sequences binarily.  This is redundant with the text header
    outputBinaryCodec.writeInt(samFileHeader.getSequenceDictionary().size());
    for (final SAMSequenceRecord sequenceRecord: samFileHeader.getSequenceDictionary().getSequences()) {
        outputBinaryCodec.writeString(sequenceRecord.getSequenceName(), true, true);
        outputBinaryCodec.writeInt(sequenceRecord.getSequenceLength());
    }

    try {
        blockCompressedOutputStream.flush();
    } catch (final IOException ioe) {
        throw new RuntimeIOException(ioe);
    }
}
 
Example #15
Source File: BclReader.java    From picard with MIT License 5 votes vote down vote up
void advance() {
    int totalCycleCount = 0;
    final BclData data = new BclData(outputLengths);
    for (int read = 0; read < outputLengths.length; read++) {
        for (int cycle = 0; cycle < outputLengths[read]; ++cycle) {
            try {

                final int readByte;
                try {
                    readByte = this.streams[totalCycleCount].read();
                } catch (IOException e) {
                    // when logging the error, increment cycle by 1, since totalCycleCount is zero-indexed but Illumina directories are 1-indexed.
                    throw new IOException(String.format("Error while reading from BCL file for cycle %d. Offending file on disk is %s",
                            (totalCycleCount+1), this.streamFiles[totalCycleCount].getAbsolutePath()), e);
                }

                if (readByte == -1) {
                    queue = null;
                    return;
                }

                decodeBasecall(data, read, cycle, readByte);
                totalCycleCount++;
            } catch (final IOException ioe) {
                throw new RuntimeIOException(ioe);
            }

        }
    }
    this.queue = data;
}
 
Example #16
Source File: CollectHsMetricsTest.java    From picard with MIT License 5 votes vote down vote up
/** Read back the first metrics record in an hs metrics file. */
private HsMetrics readMetrics(final File f) {
    try {
        final MetricsFile<HsMetrics, Comparable<?>> mFile = new MetricsFile<HsMetrics, Comparable<?>>();
        mFile.read(new FileReader(f));
        return mFile.getMetrics().get(0);
    }
    catch (IOException ioe) {
         throw new RuntimeIOException(ioe);
    }
}
 
Example #17
Source File: PedFile.java    From picard with MIT License 5 votes vote down vote up
/**
 * Writes a set of pedigrees out to disk.
 */
public void write(final File file) {
    IOUtil.assertFileIsWritable(file);
    final BufferedWriter out = IOUtil.openFileForBufferedWriting(file);

    try {
        for (final PedTrio trio : values()) {
            out.write(trio.getFamilyId());
            out.write("\t");
            out.write(trio.getIndividualId());
            out.write("\t");
            out.write(trio.getPaternalId());
            out.write("\t");
            out.write(trio.getMaternalId());
            out.write("\t");
            out.write(String.valueOf(trio.getSex().toCode()));
            out.write("\t");
            out.write(trio.getPhenotype().toString());
            out.newLine();
        }

        out.close();
    }
    catch (final IOException ioe) {
        throw new RuntimeIOException("IOException while writing to file " + file.getAbsolutePath(), ioe);
    }
}
 
Example #18
Source File: SequenceDictionaryUtils.java    From picard with MIT License 5 votes vote down vote up
public void encode(final String str) {
    try {
        dos.writeUTF(str);
    } catch (IOException e) {
        throw new RuntimeIOException(e);
    }
}
 
Example #19
Source File: SplitIntervals.java    From gatk with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Override
public void onTraversalStart() {
    ParamUtils.isPositive(scatterCount, "scatter-count must be > 0.");

    if (!outputDir.exists() && !outputDir.mkdir()) {
        throw new RuntimeIOException("Unable to create directory: " + outputDir.getAbsolutePath());
    }

    // in general dictionary will be from the reference, but using -I reads.bam or -F variants.vcf
    // to use the sequence dict from a bam or vcf is also supported
    final SAMSequenceDictionary sequenceDictionary = getBestAvailableSequenceDictionary();

    final List<SimpleInterval> intervals = hasUserSuppliedIntervals() ? intervalArgumentCollection.getIntervals(sequenceDictionary)
            : IntervalUtils.getAllIntervalsForReference(sequenceDictionary).stream()
            .filter(contig -> contig.getLengthOnReference() >= minContigSize)
            .collect(Collectors.toList());

    final IntervalList intervalList = new IntervalList(sequenceDictionary);
    intervals.stream().map(si -> new Interval(si.getContig(), si.getStart(), si.getEnd())).forEach(intervalList::add);
    final IntervalListScatterer scatterer = subdivisionMode.make();
    final List<IntervalList> scattered = scatterer.scatter(intervalList, scatterCount);

    // optionally split interval lists that contain intervals from multiple contigs
    final List<IntervalList> scatteredFinal = !dontMixContigs ? scattered :
            scattered.stream().flatMap(il -> il.getIntervals().stream()
                    .collect(Collectors.groupingBy(Interval::getContig)).entrySet().stream()    // group each interval list into sublists
                    .sorted(Comparator.comparingInt(entry -> sequenceDictionary.getSequenceIndex(entry.getKey())))  // sort entries by contig
                    .map(entry -> entry.getValue()) // discard the keys and just keep the lists of intervals
                    .map(list -> {
                        final IntervalList singleContigList = new IntervalList(sequenceDictionary);
                        singleContigList.addall(list);
                        return singleContigList;
                    })  // turn the intervals back into an IntervalList
            ).collect(Collectors.toList());

    final int maxNumberOfPlaces = Math.max((int)Math.floor(Math.log10(scatterCount-1))+1, DEFAULT_NUMBER_OF_DIGITS);
    final String formatString = "%0" + maxNumberOfPlaces + "d";
    IntStream.range(0, scatteredFinal.size()).forEach(n -> scatteredFinal.get(n).write(new File(outputDir, String.format(formatString, n) + extension)));
}
 
Example #20
Source File: AsynchOutputStreamTest.java    From rtg-tools with BSD 2-Clause "Simplified" License 5 votes vote down vote up
@Override
public void write(int b) {
  if (--mCount <= 0) {
    //emulating stupid picard exceptions
    throw new RuntimeIOException("Some stupid message", new IOException("Disk out of space."));
  }
}
 
Example #21
Source File: BlockCompressedLineReader.java    From rtg-tools with BSD 2-Clause "Simplified" License 5 votes vote down vote up
@Override
public int peek() {
  if (mPos >= mBufferUsed) {
    try {
      fillBuffer();
    } catch (IOException e) {
      throw new RuntimeIOException(e.getMessage(), e);
    }
  }
  if (mPos < mBufferUsed) {
    return mBuffer[mPos] & 0xff;
  }
  return -1;
}
 
Example #22
Source File: BlockCompressedLineReader.java    From rtg-tools with BSD 2-Clause "Simplified" License 5 votes vote down vote up
@Override
public void close() {
  try {
    mStream.close();
  } catch (IOException e) {
    throw new RuntimeIOException(e.getMessage(), e);
  }
}
 
Example #23
Source File: CreateSequenceDictionary.java    From varsim with BSD 2-Clause "Simplified" License 5 votes vote down vote up
public void encode(final String str) {
  try {
    dos.writeUTF(str);
  } catch (IOException e) {
    throw new RuntimeIOException(e);
  }
}
 
Example #24
Source File: AsyncFastqSequenceWriter.java    From rtg-tools with BSD 2-Clause "Simplified" License 5 votes vote down vote up
@Override
@SuppressWarnings("try")
protected void synchronouslyClose() {
  try {
    try (Closeable ignored = mWriter) {
      // Just for nice closing behaviour
    }
  } catch (IOException e) {
    throw new RuntimeIOException(e);
  }
}
 
Example #25
Source File: AsyncFastqSequenceWriter.java    From rtg-tools with BSD 2-Clause "Simplified" License 5 votes vote down vote up
@Override
protected void synchronouslyWrite(FastqSequence read) {
  try {
    mWriter.write(read);
  } catch (IOException e) {
    throw new RuntimeIOException(e);
  }
}
 
Example #26
Source File: FastqIterator.java    From rtg-tools with BSD 2-Clause "Simplified" License 5 votes vote down vote up
@Override
protected FastqSequence current() {
  try {
    return new FastqSequence(mFastqReader.name(), mFastqReader.sequenceData(), mFastqReader.qualityData(), mFastqReader.currentLength());
  } catch (IOException e) {
    throw new RuntimeIOException(e);
  }
}
 
Example #27
Source File: FastqIterator.java    From rtg-tools with BSD 2-Clause "Simplified" License 5 votes vote down vote up
@Override
protected final void step() {
  try {
    mAtEnd = !mFastqReader.nextSequence();
  } catch (IOException e) {
    throw new RuntimeIOException(e);
  }

}
 
Example #28
Source File: OBAWriteConsumer.java    From cramtools with Apache License 2.0 5 votes vote down vote up
@Override
public void accept(OrderedByteArray t) {
	try {
		os.write(t.bytes);
	} catch (IOException e) {
		throw new RuntimeIOException(e);
	}
}
 
Example #29
Source File: SAMFileHeader_Utils.java    From cramtools with Apache License 2.0 5 votes vote down vote up
protected static void writeHeader(final OutputStream outputStream, final SAMFileHeader samFileHeader) {
	final BlockCompressedOutputStream blockCompressedOutputStream = new BlockCompressedOutputStream(outputStream,
			null);
	final BinaryCodec outputBinaryCodec = new BinaryCodec(new DataOutputStream(blockCompressedOutputStream));
	writeHeader(outputBinaryCodec, samFileHeader);
	try {
		blockCompressedOutputStream.flush();
	} catch (final IOException ioe) {
		throw new RuntimeIOException(ioe);
	}
}
 
Example #30
Source File: UsageServer.java    From rtg-tools with BSD 2-Clause "Simplified" License 5 votes vote down vote up
@Override
protected void synchronouslyWrite(UsageMessage record) {
  try {
    recordMessage(record);
  } catch (IOException e) {
    throw new RuntimeIOException(e);
  }
}