3131#include " htslib_wrapper.hpp"
3232#include " sam_record.hpp"
3333#include " cigar_utils.hpp"
34+ #include " dnmt_error.hpp"
3435
3536#include " bam_record_utils.hpp"
3637
@@ -205,12 +206,15 @@ main_methstates(int argc, const char **argv) {
205206
206207 string chrom_file;
207208 string outfile;
209+ int n_threads = 1 ;
208210
209211 /* ***************** COMMAND LINE OPTIONS ********************/
210212 OptionParser opt_parse (argv[0 ], description, " <sam-file>" );
211213 opt_parse.add_opt (" output" , ' o' , " output file name" , false , outfile);
212214 opt_parse.add_opt (" chrom" , ' c' , " fasta format reference genome file" ,
213215 true , chrom_file);
216+ opt_parse.add_opt (" threads" , ' t' , " threads to use for reading input" ,
217+ false , n_threads);
214218 opt_parse.add_opt (" verbose" , ' v' , " print more run info" , false , VERBOSE);
215219
216220 vector<string> leftover_args;
@@ -235,6 +239,9 @@ main_methstates(int argc, const char **argv) {
235239 const string mapped_reads_file = leftover_args.front ();
236240 /* ***************** END COMMAND LINE OPTIONS *****************/
237241
242+ if (n_threads < 0 )
243+ throw dnmt_error (" thread count cannot be negative" );
244+
238245 /* first load in all the chromosome sequences and names, and make
239246 a map from chromosome name to the location of the chromosome
240247 itself */
@@ -251,12 +258,16 @@ main_methstates(int argc, const char **argv) {
251258 if (VERBOSE)
252259 cerr << " n_chroms: " << all_chroms.size () << endl;
253260
261+ bamxx::bam_tpool tp (n_threads); // declared first; destroyed last
262+
254263 bamxx::bam_in in (mapped_reads_file);
255- if (!in)
256- throw runtime_error (" cannot open input file " + mapped_reads_file);
264+ if (!in) throw dnmt_error (" cannot open input file " + mapped_reads_file);
257265 bamxx::bam_header hdr (in);
258- if (!hdr)
259- throw runtime_error (" cannot read heade" + mapped_reads_file);
266+ if (!hdr) throw dnmt_error (" cannot read heade" + mapped_reads_file);
267+
268+ /* set the threads for the input file decompression */
269+ if (n_threads > 1 )
270+ tp.set_io (in);
260271
261272 std::ofstream of;
262273 if (!outfile.empty ()) of.open (outfile.c_str ());
0 commit comments