/** * Decode a single line in a SAM text file. * * @param line line to decode. * @return A SAMReadFeature modeling that line. */ @Override public SAMReadFeature decode(String line) { // we may be asked to process a header line; ignore it if (line.startsWith("@")) return null; String[] tokens = new String[expectedTokenCount]; // split the line int count = ParsingUtils.splitWhitespace(line, tokens); // check to see if we've parsed the string into the right number of tokens (expectedTokenCount) if (count != expectedTokenCount) throw new CodecLineParsingException( "the SAM read line didn't have the expected number of tokens " + "(expected = " + expectedTokenCount + ", saw = " + count + " on " + "line = " + line + ")"); final String readName = tokens[0]; final int flags = Integer.parseInt(tokens[1]); final String contigName = tokens[2]; final int alignmentStart = Integer.parseInt(tokens[3]); final int mapQ = Integer.parseInt(tokens[4]); final String cigarString = tokens[5]; final String mateContigName = tokens[6]; final int mateAlignmentStart = Integer.parseInt(tokens[7]); final int inferredInsertSize = Integer.parseInt(tokens[8]); final byte[] bases = StringUtil.stringToBytes(tokens[9]); final byte[] qualities = StringUtil.stringToBytes(tokens[10]); // Infer the alignment end. Cigar cigar = TextCigarCodec.decode(cigarString); int alignmentEnd = alignmentStart + cigar.getReferenceLength() - 1; // Remove printable character conversion from the qualities. for (byte quality : qualities) quality -= 33; return new SAMReadFeature( readName, flags, contigName, alignmentStart, alignmentEnd, mapQ, cigarString, mateContigName, mateAlignmentStart, inferredInsertSize, bases, qualities); }
@Test(expectedExceptions = {SAMException.class}) public void testUnmergeableSequenceDictionary() { final String sd1 = sq1 + sq2 + sq5; final String sd2 = sq2 + sq3 + sq4 + sq1; SAMFileReader reader1 = new SAMFileReader(new ByteArrayInputStream(StringUtil.stringToBytes(sd1))); SAMFileReader reader2 = new SAMFileReader(new ByteArrayInputStream(StringUtil.stringToBytes(sd2))); final List<SAMFileHeader> inputHeaders = Arrays.asList(reader1.getFileHeader(), reader2.getFileHeader()); new SamFileHeaderMerger(SAMFileHeader.SortOrder.coordinate, inputHeaders, true); }
@Test public void testSequenceDictionaryMerge() { final String sd1 = sq1 + sq2 + sq5; final String sd2 = sq2 + sq3 + sq4; SAMFileReader reader1 = new SAMFileReader(new ByteArrayInputStream(StringUtil.stringToBytes(sd1))); SAMFileReader reader2 = new SAMFileReader(new ByteArrayInputStream(StringUtil.stringToBytes(sd2))); final List<SAMFileHeader> inputHeaders = Arrays.asList(reader1.getFileHeader(), reader2.getFileHeader()); SamFileHeaderMerger merger = new SamFileHeaderMerger(SAMFileHeader.SortOrder.coordinate, inputHeaders, true); final SAMFileHeader mergedHeader = merger.getMergedHeader(); for (final SAMFileHeader inputHeader : inputHeaders) { int prevTargetIndex = -1; for (final SAMSequenceRecord sequenceRecord : inputHeader.getSequenceDictionary().getSequences()) { final int targetIndex = mergedHeader.getSequenceIndex(sequenceRecord.getSequenceName()); Assert.assertNotSame(targetIndex, -1); Assert.assertTrue(prevTargetIndex < targetIndex); prevTargetIndex = targetIndex; } } }