Java Code Examples for htsjdk.samtools.util.StringLineReader

The following examples show how to use htsjdk.samtools.util.StringLineReader. These examples are extracted from open source projects. 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 Project: dataflow-java   Source File: WriteBAMTransform.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public HeaderInfo apply(String str) throws Exception {
  int newLinePos = str.indexOf("\n");
  String contigStr = str.substring(0, newLinePos);
  String headerStr = str.substring(newLinePos + 1);
  return new HeaderInfo(
      SAM_HEADER_CODEC.decode(new StringLineReader(headerStr),
          "HEADER_INFO_CODER"),
      Contig.parseContigsFromCommandLine(contigStr).iterator().next());
}
 
Example 2
Source Project: cramtools   Source File: CRAMFileWriter.java    License: Apache License 2.0 4 votes vote down vote up
@Override
protected void writeHeader(final String textHeader) {
	cramContainerStream.writeHeader(new SAMTextHeaderCodec().decode(new StringLineReader(textHeader),
			fileName != null ? fileName : null));
}
 
Example 3
Source Project: cramtools   Source File: SAMFileHeader_Utils.java    License: Apache License 2.0 4 votes vote down vote up
static SAMFileHeader readHeader(final BinaryCodec stream, final ValidationStringency validationStringency,
		final String source) throws IOException {

	final byte[] buffer = new byte[4];
	stream.readBytes(buffer);
	if (!Arrays.equals(buffer, "BAM\1".getBytes())) {
		throw new IOException("Invalid BAM file header");
	}

	final int headerTextLength = stream.readInt();
	final String textHeader = stream.readString(headerTextLength);
	final SAMTextHeaderCodec headerCodec = new SAMTextHeaderCodec();
	headerCodec.setValidationStringency(validationStringency);
	final SAMFileHeader samFileHeader = headerCodec.decode(new StringLineReader(textHeader), source);

	final int sequenceCount = stream.readInt();
	if (samFileHeader.getSequenceDictionary().size() > 0) {
		// It is allowed to have binary sequences but no text sequences, so
		// only validate if both are present
		if (sequenceCount != samFileHeader.getSequenceDictionary().size()) {
			throw new SAMFormatException("Number of sequences in text header ("
					+ samFileHeader.getSequenceDictionary().size() + ") != number of sequences in binary header ("
					+ sequenceCount + ") for file " + source);
		}
		for (int i = 0; i < sequenceCount; i++) {
			final SAMSequenceRecord binarySequenceRecord = readSequenceRecord(stream, source);
			final SAMSequenceRecord sequenceRecord = samFileHeader.getSequence(i);
			if (!sequenceRecord.getSequenceName().equals(binarySequenceRecord.getSequenceName())) {
				throw new SAMFormatException("For sequence " + i
						+ ", text and binary have different names in file " + source);
			}
			if (sequenceRecord.getSequenceLength() != binarySequenceRecord.getSequenceLength()) {
				throw new SAMFormatException("For sequence " + i
						+ ", text and binary have different lengths in file " + source);
			}
		}
	} else {
		// If only binary sequences are present, copy them into
		// samFileHeader
		final List<SAMSequenceRecord> sequences = new ArrayList<SAMSequenceRecord>(sequenceCount);
		for (int i = 0; i < sequenceCount; i++) {
			sequences.add(readSequenceRecord(stream, source));
		}
		samFileHeader.setSequenceDictionary(new SAMSequenceDictionary(sequences));
	}

	return samFileHeader;
}