Created
November 16, 2015 06:11
-
-
Save nh13/5783fe6e6c8d2fbd6952 to your computer and use it in GitHub Desktop.
AddDummyAlignmentInformation.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package picard.sam; | |
import htsjdk.samtools.SAMFileHeader; | |
import htsjdk.samtools.SAMFileWriter; | |
import htsjdk.samtools.SAMFileWriterFactory; | |
import htsjdk.samtools.SAMRecord; | |
import htsjdk.samtools.SAMSequenceRecord; | |
import htsjdk.samtools.SamReader; | |
import htsjdk.samtools.SamReaderFactory; | |
import htsjdk.samtools.util.CloserUtil; | |
import htsjdk.samtools.util.Log; | |
import htsjdk.samtools.util.ProgressLogger; | |
import picard.PicardException; | |
import picard.cmdline.CommandLineProgram; | |
import picard.cmdline.CommandLineProgramProperties; | |
import picard.cmdline.Option; | |
import picard.cmdline.StandardOptionDefinitions; | |
import picard.cmdline.programgroups.SamOrBam; | |
import java.io.File; | |
@CommandLineProgramProperties( | |
usage = "Adds dummy alignment information to a query-sorted unmapped SAM or BAM.", | |
usageShort = "Adds dummy alignment information to a query-sorted unmapped SAM or BAM.", | |
programGroup = SamOrBam.class | |
) | |
public class AddDummyAlignmentInformation extends CommandLineProgram { | |
private static final Log log = Log.getInstance(AddDummyAlignmentInformation.class); | |
@Option(shortName = StandardOptionDefinitions.INPUT_SHORT_NAME, doc = "The SAM or BAM file or GA4GH url to view.") | |
public File INPUT; | |
@Option(shortName = StandardOptionDefinitions.INPUT_SHORT_NAME, doc = "The BAM file to write.") | |
public File OUTPUT; | |
/** Stock main method. */ | |
public static void main(final String[] argv) { | |
System.exit(new AddDummyAlignmentInformation().instanceMain(argv)); | |
} | |
protected int doWork() { | |
final SamReader reader = SamReaderFactory.makeDefault().referenceSequence(REFERENCE_SEQUENCE).open(INPUT); | |
if (reader.getFileHeader().getSortOrder() != SAMFileHeader.SortOrder.queryname) { | |
throw new PicardException("INPUT must be in queryname order"); | |
} | |
final SAMFileHeader header = reader.getFileHeader(); | |
header.addSequence(new SAMSequenceRecord("DUMMY", Integer.MAX_VALUE)); | |
final SAMFileWriter writer = new SAMFileWriterFactory().setCreateIndex(true).makeBAMWriter(header, false, OUTPUT); | |
int recordIndex = 1; | |
SAMRecord prevRecord = null; | |
final ProgressLogger progress = new ProgressLogger(log); | |
for (final SAMRecord record : reader) { | |
// read | |
record.setReadUnmappedFlag(true); | |
record.setReferenceIndex(0); | |
record.setAlignmentStart(recordIndex); | |
// mate info | |
record.setMateUnmappedFlag(true); | |
record.setMateReferenceIndex(0); | |
record.setMateAlignmentStart(recordIndex); | |
writer.addAlignment(record); | |
progress.record(record); | |
if (recordIndex == Integer.MAX_VALUE) { | |
throw new PicardException(String.format("Too many records (>%d)", Integer.MAX_VALUE)); | |
} | |
if (null == prevRecord || record.getReadName().equals(prevRecord.getReadName())) { | |
prevRecord = record; | |
recordIndex++; | |
} | |
} | |
log.info(String.format("Wrote %d records.", recordIndex-1)); | |
writer.close(); | |
CloserUtil.close(reader); | |
return 0; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment