@Test public void testGetSAMString() { SAMProgramRecord r = new SAMProgramRecord("SW-eIV"); r.setProgramName("telnet"); r.setProgramVersion("0.17-40"); r.setCommandLine("telnet towel.blinkenlights.nl"); Assert.assertEquals("@PG\tID:SW-eIV\tPN:telnet\tVN:0.17-40\tCL:telnet towel.blinkenlights.nl", r.getSAMString()); } }
/** * Chains <code>program</code> in front of the first "head" item in the list of * SAMProgramRecords in <code>header</code>. This method should not be used * when there are multiple chains of program groups in a header, only when * it can safely be assumed that there is only one chain. It correctly handles * the case where <code>program</code> has already been added to the header, so * it can be used whether creating a SAMProgramRecord with a constructor or when * calling SAMFileHeader.createProgramRecord(). */ public static void chainSAMProgramRecord(final SAMFileHeader header, final SAMProgramRecord program) { final List<SAMProgramRecord> pgs = header.getProgramRecords(); if (!pgs.isEmpty()) { final List<String> referencedIds = new ArrayList<>(); for (final SAMProgramRecord pg : pgs) { if (pg.getPreviousProgramGroupId() != null) { referencedIds.add(pg.getPreviousProgramGroupId()); } } for (final SAMProgramRecord pg : pgs) { // if record being chained has already been added, ignore it if (pg.getProgramGroupId().equals(program.getProgramGroupId())) { continue; } if (!referencedIds.contains(pg.getProgramGroupId())) { program.setPreviousProgramGroupId(pg.getProgramGroupId()); break; } } } }
/** * Generate the list of program records seen in the SAM file, approximating this with those in the header that were not * themselves mentioned elsewhere. */ private void setPGIdsSeen(final SAMFileHeader header) { final Set<String> pgIdsSeenAsPrevious = new HashSet<String>(); // get all program record ids that are mentioned as previously seen for (final SAMProgramRecord samProgramRecord : header.getProgramRecords()) { final String previousProgramGroupID = samProgramRecord.getPreviousProgramGroupId(); if (null != previousProgramGroupID) pgIdsSeenAsPrevious.add(previousProgramGroupID); } // ignore those that were previously seen for (final SAMProgramRecord samProgramRecord : header.getProgramRecords()) { final String pgId = samProgramRecord.getId(); if (!pgIdsSeenAsPrevious.contains(pgId)) this.pgIdsSeen.add(pgId); } } }
public void addPG(SAMFileHeader header, String program, String cmd, String version) { SAMProgramRecord programRecord = header.createProgramRecord(); programRecord.setCommandLine(cmd); programRecord.setProgramName(program); programRecord.setProgramVersion(version); }
/** * Creates a program record (@PG) tag * * @param toolkit the engine * @param walker the walker object (so we can extract the command line) * @param PROGRAM_RECORD_NAME the name for the PG tag * @return a program record for the tool */ public static SAMProgramRecord createProgramRecord(GenomeAnalysisEngine toolkit, Object walker, String PROGRAM_RECORD_NAME) { final SAMProgramRecord programRecord = new SAMProgramRecord(PROGRAM_RECORD_NAME); try { programRecord.setProgramVersion(CommandLineProgram.getVersionNumber()); } catch (MissingResourceException e) { // couldn't care less if the resource is missing... } programRecord.setCommandLine(toolkit.createApproximateCommandLineArgumentString(toolkit, walker)); return programRecord; }
for (final SAMProgramRecord programGroup : header.getProgramRecords()) { if (!idsThatAreAlreadyTaken.add(programGroup.getId())) throw new SAMException("Input file: " + header + " contains more than one PG with the same id (" + programGroup.getId() + ")"); for (final Iterator<HeaderRecordAndFileHeader<SAMProgramRecord>> programGroupsLeftToProcessIterator = programGroupsLeftToProcess.iterator(); programGroupsLeftToProcessIterator.hasNext(); ) { final HeaderRecordAndFileHeader<SAMProgramRecord> pair = programGroupsLeftToProcessIterator.next(); if (pair.getHeaderRecord().getAttribute(SAMProgramRecord.PREVIOUS_PROGRAM_GROUP_ID_TAG) == null) { programGroupsLeftToProcessIterator.remove(); currentProgramGroups.add(pair); for (final Iterator<HeaderRecordAndFileHeader<SAMProgramRecord>> programGroupsLeftToProcessIterator = programGroupsLeftToProcess.iterator(); programGroupsLeftToProcessIterator.hasNext(); ) { final HeaderRecordAndFileHeader<SAMProgramRecord> pairLeftToProcess = programGroupsLeftToProcessIterator.next(); final Object ppIdOfRecordLeftToProcess = pairLeftToProcess.getHeaderRecord().getAttribute(SAMProgramRecord.PREVIOUS_PROGRAM_GROUP_ID_TAG); final String idJustProcessed = justProcessedPair.getHeaderRecord().getId(); if (pairLeftToProcess.getFileHeader() == justProcessedPair.getFileHeader() && ppIdOfRecordLeftToProcess.equals(idJustProcessed)) { programGroupsLeftToProcessIterator.remove(); for (final HeaderRecordAndFileHeader<SAMProgramRecord> pair : programGroupsLeftToProcess) { final SAMProgramRecord record = pair.getHeaderRecord(); errorMsg.append("@PG ID:" + record.getProgramGroupId() + " PN:" + record.getProgramName() + " PP:" + record.getPreviousProgramGroupId() + "\n");
for (final HeaderRecordAndFileHeader<SAMProgramRecord> pair : programGroups) { final SAMProgramRecord record = pair.getHeaderRecord(); final String id = record.getProgramGroupId(); final String ppId = (String) record.getAttribute(SAMProgramRecord.PREVIOUS_PROGRAM_GROUP_ID_TAG); translatedRecord = new SAMProgramRecord(translatedId, record); translatedRecord.setAttribute(SAMProgramRecord.PREVIOUS_PROGRAM_GROUP_ID_TAG, translatedPpId); } else if (needToTranslateId) { translatedRecord = new SAMProgramRecord(translatedId, record); } else if (needToTranslatePpId) { translatedRecord = new SAMProgramRecord(id, record); translatedRecord.setAttribute(SAMProgramRecord.PREVIOUS_PROGRAM_GROUP_ID_TAG, translatedPpId);
for (final SAMProgramRecord pg : header.getProgramRecords()) pgs.add(pg.getId()); for (int i = 1; i < Integer.MAX_VALUE; ++i) { if (!pgs.contains(String.valueOf(i))) { final SAMProgramRecord pg = new SAMProgramRecord(String.valueOf(i)); pg.setCommandLine(getCommandLine()); pg.setProgramName(getClass().getSimpleName()); header.addProgramRecord(pg); break;
@Override public String getId() { return getProgramGroupId(); }
public void testMergingProgramGroupsWithThreeReaders() { final SAMRecordSetBuilder builder1 = new SAMRecordSetBuilder(false, SAMFileHeader.SortOrder.queryname); final SAMProgramRecord program1 = new SAMProgramRecord("0"); program1.setCommandLine("Hi, Mom!"); program1.setProgramVersion("123"); builder1.setProgramRecord(program1); builder1.addFrag("read1", 20, 28833, false); final SAMProgramRecord program2 = new SAMProgramRecord("0"); program2.setCommandLine("Hi, Mom!"); program2.setProgramVersion("123"); builder2.setProgramRecord(program2); builder2.addFrag("read2", 19, 28833, false); final SAMProgramRecord program3 = new SAMProgramRecord("0"); program3.setCommandLine("Hi, Dad!"); builder3.setProgramRecord(program3); builder3.addFrag("read3", 19, 28833, false); final List<SAMProgramRecord> outputProgramGroups = headerMerger.getMergedHeader().getProgramRecords(); Assert.assertEquals(outputProgramGroups.size(), 2); Assert.assertTrue(outputProgramGroups.get(0).equivalent(program1)); Assert.assertTrue(outputProgramGroups.get(1).equivalent(program3));
@Override public SAMProgramRecord createRecord(final String id, final SAMProgramRecord srcProgramRecord) { return new SAMProgramRecord(id, srcProgramRecord); } };
private boolean compareProgramRecord(final SAMProgramRecord programRecord1, final SAMProgramRecord programRecord2) { if (programRecord1 == null && programRecord2 == null) { return true; } if (programRecord1 == null) { reportDifference("null", programRecord2.getProgramGroupId(), "Program Record"); return false; } if (programRecord2 == null) { reportDifference(programRecord1.getProgramGroupId(), "null", "Program Record"); return false; } boolean ret = compareValues(programRecord1.getProgramGroupId(), programRecord2.getProgramGroupId(), "Program Name"); final String[] attributes = {"VN", "CL"}; for (final String attribute : attributes) { ret = compareValues(programRecord1.getAttribute(attribute), programRecord2.getAttribute(attribute), attribute + " Program Record attribute") && ret; } return ret; }
@Test public void testChainProgramRecord() { SAMFileHeader header = new SAMFileHeader(); SAMProgramRecord first = header.createProgramRecord(); SAMUtils.chainSAMProgramRecord(header, first); Assert.assertEquals(header.getProgramRecords().size(), 1); Assert.assertNull(first.getPreviousProgramGroupId()); SAMProgramRecord second = header.createProgramRecord(); SAMUtils.chainSAMProgramRecord(header, second); Assert.assertEquals(header.getProgramRecords().size(), 2); Assert.assertNull(first.getPreviousProgramGroupId()); Assert.assertEquals(second.getPreviousProgramGroupId(), first.getProgramGroupId()); SAMProgramRecord third = new SAMProgramRecord("3"); SAMUtils.chainSAMProgramRecord(header, third); header.addProgramRecord(third); Assert.assertEquals(header.getProgramRecords().size(), 3); Assert.assertNull(first.getPreviousProgramGroupId()); Assert.assertEquals(second.getPreviousProgramGroupId(), first.getProgramGroupId()); Assert.assertEquals(third.getPreviousProgramGroupId(), second.getProgramGroupId()); } }
public String getPreviousProgramGroupId() { return (String)getAttribute(PREVIOUS_PROGRAM_GROUP_ID_TAG); }
protected String getPGLine(final SAMProgramRecord programRecord) { final String[] fields = new String[2 + programRecord.getAttributes().size()]; fields[0] = HEADER_LINE_START + HeaderRecordType.PG; fields[1] = SAMProgramRecord.PROGRAM_GROUP_ID_TAG + TAG_KEY_VALUE_SEPARATOR + programRecord.getProgramGroupId(); encodeTags(programRecord, fields, 2); return StringUtil.join(FIELD_SEPARATOR, fields); }
/** * Checks whether the provided SAM header if from a reduced bam file. * @param header the SAM header for a given file * @throws UserException if the header is from a reduced bam */ private void checkForUnsupportedBamFile(final SAMFileHeader header) { for ( final SAMProgramRecord PGrecord : header.getProgramRecords() ) { if ( unsupportedPGs.containsKey(PGrecord.getId()) ) throw new UserException("The GATK no longer supports running off of BAMs produced by " + unsupportedPGs.get(PGrecord.getId())); } }
"Number of sequences did not match"); SAMProgramRecord pg = result.getFileHeader().getProgramRecords().get(0); Assert.assertEquals(pg.getProgramGroupId(), "0"); Assert.assertEquals(pg.getProgramVersion(), "1.0"); Assert.assertEquals(pg.getCommandLine(), "align!"); Assert.assertEquals(pg.getProgramName(), "myAligner"); Assert.assertEquals(pg.getProgramGroupId(), "1", "Program group ID not picked up correctly from aligned BAM"); Assert.assertEquals(pg.getProgramVersion(), "2.0", "Program version not picked up correctly from aligned BAM"); Assert.assertNull(pg.getCommandLine(), "Program command line not picked up correctly from aligned BAM"); Assert.assertEquals(pg.getProgramName(), "Hey!", "Program name not picked up correctly from aligned BAM");
public static SAMFileHeader buildSAMFileHeader(AlignmentHeader alignmentHeader){ SAMFileHeader samFileHeader = new SAMFileHeader(); for(Map.Entry<String, String> entry : alignmentHeader.getAttributes().entrySet()) { samFileHeader.setAttribute(entry.getKey(), entry.getValue()); } for(AlignmentHeader.SequenceRecord sq : alignmentHeader.getSequenceDiccionary()){ SAMSequenceRecord samSequenceRecord = new SAMSequenceRecord(sq.getSequenceName(), sq.getSequenceLength()); for(Map.Entry<String, String> entry : sq.getAttributes().entrySet()) samSequenceRecord.setAttribute(entry.getKey(), entry.getValue()); samFileHeader.addSequence(samSequenceRecord); } for(AlignmentHeader.ReadGroup rg : alignmentHeader.getReadGroups()){ SAMReadGroupRecord samReadGroupRecord = new SAMReadGroupRecord(rg.getId()); for(Map.Entry<String, String> entry : rg.getAttributes().entrySet()){ samReadGroupRecord.setAttribute(entry.getKey(), entry.getValue()); } samFileHeader.addReadGroup(samReadGroupRecord); } for(AlignmentHeader.ProgramRecord pg : alignmentHeader.getProgramRecords()){ SAMProgramRecord samProgramRecord = new SAMProgramRecord(pg.getId()); for(Map.Entry<String, String> entry : pg.getAttributes().entrySet()){ samProgramRecord.setAttribute(entry.getKey(), entry.getValue()); } samFileHeader.addProgramRecord(samProgramRecord); } samFileHeader.setComments(alignmentHeader.getComments()); return samFileHeader; } public static AlignmentHeader buildAlignmentHeader(SAMFileHeader samHeader, String studyName){
for (final ExpectedPnAndVn expected : expectedList) { final SAMProgramRecord programRecord = header.getProgramRecord(pgId); if (expected.expectedPn != null) Assert.assertEquals(programRecord.getProgramName(), expected.expectedPn); if (expected.expectedVn != null) Assert.assertEquals(programRecord.getProgramVersion(), expected.expectedVn); pgId = programRecord.getPreviousProgramGroupId();
for (Map.Entry<String,String> entry :pg.getAttributes()){ attr.put(entry.getKey(), entry.getValue()); alignmentHeader.addProgramRecords(pg.getId(), attr);