1 module test.samreader;
2 
3 import std.stdio;
4 import std..string;
5 
6 import dhtslib.sam;
7 import dhtslib.htslib.sam;
8 import dhtslib.htslib.hts_log;
9 
10 int main()
11 {
12     debug(dhtslib_debug)
13     {
14         writeln("enabling debug logging");
15         hts_set_log_level(htsLogLevel.HTS_LOG_TRACE);
16 
17         // Test log levels
18         hts_log_info(__FUNCTION__, "Testing log levels: expect I(nfo) D(ebug) W(arning) E(rror) T(race)");
19         hts_log_debug(__FUNCTION__, "Test debug");
20         hts_log_warning(__FUNCTION__, "Test warning");
21         hts_log_error(__FUNCTION__, "Test error");
22         hts_log_trace(__FUNCTION__, "Test trace (after which will reset to Debug level)");
23 
24         hts_set_log_level(htsLogLevel.HTS_LOG_DEBUG);
25     }
26 
27 
28     //auto sf = SAMFile("/Users/james/Documents/Development/blachlylab/funmap/wgEncodeUwRepliSeqBg02esG1bAlnRep1.bam");
29     auto sf = SAMFile("/Users/james/Documents/Development/blachlylab/funmap/ENCFF399AWI.bam");
30 
31     writeln("Basic SAM/BAM/CRAM data\n-----------------------");
32     writefln("Input file: %s", sf.filename);
33     writefln("N targets: %d", sf.n_targets);
34     writefln("Length of contig 0: %d", sf.target_len(0));
35     writefln("Length of all targets: %s", sf.target_lens);
36     writefln("Target names array: %s", sf.target_names);
37     writefln("Lookup by name: \"chr5\" is the %d'th target (0-indexed).", sf.target_id("chr5"));
38 
39     writeln("Now testing default class constructor");
40     for(int j; j<100_000; j++)
41     {
42         auto x = new SAMRecord();
43     }
44 
45     writeln("Query c raw htslib");
46     {
47         int j = 0;
48         auto fn = toStringz("/Users/james/Documents/Development/blachlylab/funmap/ENCFF399AWI.bam");
49         import dhtslib.htslib.hts;
50         auto fp = hts_open(fn, cast(immutable(char)*)"r".ptr);
51         auto idx= sam_index_load(fp, fn);
52         bam1_t *b = bam_init1();
53         hts_itr_t *iter;
54         int r;
55         if ((iter = sam_itr_queryi(idx, 0, 1_000_000, 2_000_000)) == null) {
56             hts_log_error(__FUNCTION__, "Failed to parse region");
57             return 1;
58         }
59         writefln("iter == %x", iter);
60         
61         while ((r = sam_itr_next(fp, iter, b)) >= 0) {
62             j++;
63         }
64 
65         writefln("Processed %d records with raw iter", j);
66 
67         hts_itr_destroy(iter);
68         bam_destroy1(b);
69         hts_close(fp);
70     }
71 
72     writeln("Testing query with D wrapper");
73     int j;
74     //auto qr = sf.query("chr1:1000000-2000000");
75     auto qr = sf.query(0, 1_000_000, 2_000_000);
76     foreach(r; qr) {
77         j++;
78     }
79     writefln("%d records", j);
80 
81     writeln("Testing query with expected no results");
82     j = 0;
83     qr = sf.query(0, 1, 2);
84     foreach(r; qr) {
85         j++;
86     }
87     writefln("%d records", j);
88 
89     writeln("Now testing AllRecordsRange");
90     int i;
91     auto x = sf.all_records;
92     foreach(r; x) {
93         i++;
94         //writeln(i);
95         //writeln(fromStringz(r.sequence));
96     }
97     writefln("%d records", i);
98 
99     writeln("SAMFile going out of scope?");
100  
101     return 0;
102 }