You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

42 lines
1.7 KiB

  1. import bio.std.hts.bam.read;
  2. import bio.std.hts.bam.referenceinfo;
  3. import bio.std.hts.sam.header;
  4. import bio.std.hts.bam.reader;
  5. import bio.std.hts.bam.writer;
  6. import contrib.undead.stream;
  7. import std.stdio;
  8. import bio.std.hts.bam.cigar;
  9. void main() {
  10. auto header = new SamHeader(); // First, create SAM header
  11. RgLine rg; // and fill it with information.
  12. rg.identifier = "RG007";
  13. rg.platform = "ILLUMINA"; // Of course, you can modify header
  14. header.read_groups.add(rg); // provided by BamReader object.
  15. auto reference = ReferenceSequenceInfo("contig123", 4321);
  16. auto stream = new MemoryStream();
  17. auto writer = new BamWriter(stream);
  18. writer.writeSamHeader(header); // start writing BAM from header
  19. writer.writeReferenceSequenceInfo([reference]); // and reference sequence info
  20. auto read = BamRead("readName001", "ACTGATGAAC",
  21. [CigarOperation(7, 'M'), CigarOperation(1, 'I'), CigarOperation(2, 'S')]);
  22. read.base_qualities = [38, 34, 33, 35, 28, 39, 25, 19, 21, 17];
  23. read.ref_id = 0;
  24. read.mapping_quality = 46;
  25. read.is_unmapped = false;
  26. read["RG"] = "RG007";
  27. // ... set many more fields, flags, and tags
  28. read.position = 2345; // 0-based, in SAM output you will see 2346
  29. read.strand = '-'; // same as read.is_reverse_strand = true
  30. writer.writeRecord(read); // BGZF blocks are seamlessly compressed in parallel
  31. writer.flush(); // in practice, one would call finish() method
  32. stream.seekSet(0); // but here we will read from the stream
  33. auto reader = new BamReader(stream);
  34. write(reader.header.text); // serialized header already contains newline
  35. writefln("%j", reader.reads.front); // prints record in JSON format
  36. }