1 module dhtslib.fastq; 2 import dhtslib.bgzf; 3 import std.range.interfaces : InputRange, inputRangeObject; 4 5 /* 6 * Represents a complete fastq read record. 7 */ 8 struct FastqRecord 9 { 10 string id; 11 string sequence; 12 string extra; 13 string qscores; 14 } 15 16 /* 17 * Parses a bgzipped, gzipped, or plain-text fastq file 18 * into a range of FastqRecords. 19 */ 20 struct FastqFile 21 { 22 BGZFile f; 23 InputRange!string lines; 24 FastqRecord rec; 25 bool last; 26 27 this(string fn) 28 { 29 f = BGZFile(fn); 30 lines = f.byLineCopy.inputRangeObject; 31 popFront; 32 } 33 34 FastqRecord front() 35 { 36 return rec; 37 } 38 39 void popFront() 40 { 41 //get id line 42 rec.id = lines.front; 43 lines.popFront; 44 45 //get seq line 46 rec.sequence = lines.front; 47 lines.popFront; 48 49 //get extra line 50 rec.extra = lines.front; 51 lines.popFront; 52 53 //get qscore line 54 rec.qscores = lines.front; 55 if(!lines.empty) 56 lines.popFront; 57 else 58 last = true; 59 } 60 61 bool empty() 62 { 63 return lines.empty && last; 64 } 65 66 } 67 68 /// 69 debug(dhtslib_unittest) unittest 70 { 71 import std.stdio; 72 import htslib.hts_log; 73 import std.algorithm : map; 74 import std.array : array; 75 import std.path : buildPath,dirName; 76 hts_set_log_level(htsLogLevel.HTS_LOG_INFO); 77 hts_log_info(__FUNCTION__, "Testing FastqFile"); 78 hts_log_info(__FUNCTION__, "Loading test file"); 79 80 auto fqs = FastqFile(buildPath(dirName(dirName(dirName(__FILE__))),"htslib","test","fastqs.fq")); 81 assert(fqs.array.length == 125); 82 // assert(bg.array == ["122333444455555"]); 83 }