-
Notifications
You must be signed in to change notification settings - Fork 42
/
dropest.cpp
338 lines (305 loc) · 10.8 KB
/
dropest.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
#include <iostream>
#include <iterator>
#include <string>
#include <vector>
#include <getopt.h>
#include <ctime>
#include <boost/property_tree/ptree.hpp>
#include <boost/property_tree/xml_parser.hpp>
#include <Estimation/BamProcessing/BamController.h>
#include <Estimation/Merge/MergeStrategyFactory.h>
#include "Estimation/ResultsPrinter.h"
#include "Tools/Logs.h"
#include "Tools/ReadParameters.h"
#include "Tools/UtilFunctions.h"
using namespace std;
using namespace Estimation;
using namespace boost::property_tree;
static const std::string SCRIPT_NAME = "dropest";
struct Params
{
bool bam_output = false;
bool cant_parse = false;
bool filled_bam = false;
bool filtered_bam_output = false;
bool merge_tags = false;
bool merge_tags_precise = false;
bool pseudoaligner = false;
bool quiet = false;
bool reads_output = false;
bool stats_for_validation = false;
bool umi_merge = false;
bool velocyto_matrices = false;
bool write_matrix = false;
string config_file_name = "";
string genes_filename = "";
string log_prefix = "";
string output_name = "";
string read_params_filenames = "";
std::string gene_match_level = UMI::Mark::DEFAULT_CODE;
int max_cell_number = -1;
int min_genes_after_merge = -1;
};
static void check_files_existence(const Params ¶ms, const vector<string> &bam_files)
{
if (!params.config_file_name.empty() && !std::ifstream(params.config_file_name))
throw std::runtime_error("Can't open config file '" + params.config_file_name + "'");
if (!params.genes_filename.empty() && !std::ifstream(params.genes_filename))
throw std::runtime_error("Can't open genes file '" + params.genes_filename + "'");
for (auto const &file : bam_files)
{
if (!std::ifstream(file))
throw std::runtime_error("Can't open BAM file '" + file + "'");
}
}
static void usage()
{
cerr << SCRIPT_NAME <<": estimate molecular counts per cell\n";
cerr << "Version: " << VERSION << "\n\n";
cerr << "SYNOPSIS\n";
cerr << "\t" << SCRIPT_NAME << " [options] -c config.xml file_1.bam [..., file_n.bam]\n";
cerr << "OPTIONS:\n";
cerr << "\t-b, --bam-output: print tagged bam files\n";
cerr << "\t-c, --config filename: xml file with estimation parameters\n";
cerr << "\t-C, --cells num: maximal number of output cells\n";
cerr << "\t-f, --filled-bam: bam file already contains genes/barcodes tags\n";
cerr << "\t-F, --filtered-bam: print tagged bam file after the merge and filtration\n";
cerr << "\t-g, --genes filename: file with genes annotations (.bed or .gtf)\n";
cerr << "\t-G, --genes-min num: minimal number of genes in output cells\n";
cerr << "\t-h, --help: show this info\n";
cerr << "\t-l, --log-prefix : logs prefix\n";
cerr << "\t-L, --gene-match-level :\n"
"\t\te: count UMIs with exonic reads only;\n"
"\t\ti: count UMIs with intronic reads only;\n"
"\t\tE: count UMIs, which have both exonic and not annotated reads;\n"
"\t\tI: count UMIs, which have both intronic and not annotated reads;\n"
"\t\tB: count UMIs, which have both exonic and intronic reads;\n"
"\t\tA: count UMIs, which have exonic, intronic and not annotated reads.\n"
"\t\tDefault: -L " << Params().gene_match_level << "." << endl;
cerr << "\t-m, --merge-barcodes : merge linked cell tags" << endl;
cerr << "\t-M, --merge-barcodes-precise : use precise merge strategy (can be slow), recommended to use when the list of real barcodes is not available\n";
cerr << "\t-o, --output-file filename : output file name\n";
cerr << "\t-P, --pseudoaligner: use chromosome name as a source of gene id\n";
cerr << "\t-q, --quiet : disable logs\n";
cerr << "\t-r, --read-params filenames: file or files with serialized params from tags search step. If there are several files"
<< ", they should be provided in quotes, separated by space: \"file1.params.gz file2.params.gz file3.params.gz\"" << endl;
cerr << "\t-R, --reads-output: print count matrix for reads and don't use UMI statistics\n";
cerr << "\t-u, --merge-umi: apply 'directional' correction of UMI errors. This option prevents output of 'reads_per_umi_per_cell'. If you want to apply more advanced UMI correction, don’t use '-u', but use follow up R analysis.\n";
cerr << "\t-V, --velocyto : save separate count matrices for exons, introns and exon/intron spanning reads\n";
cerr << "\t-w, --write-mtx : write out matrix in MatrixMarket format\n";
}
static Params parse_cmd_params(int argc, char **argv)
{
Params params{};
int option_index = 0;
int c;
static struct option long_options[] = {
{"bam-output", no_argument, 0, 'b'},
{"config", required_argument, 0, 'c'},
{"cells", required_argument, 0, 'C'},
{"filled-bam", no_argument, 0, 'f'},
{"filtered-bam", no_argument, 0, 'F'},
{"genes", required_argument, 0, 'g'},
{"genes-min", required_argument, 0, 'G'},
{"help", no_argument, 0, 'h'},
{"log-prefix", required_argument, 0, 'l'},
{"gene-match-level", required_argument, 0, 'L'},
{"merge-barcodes", no_argument, 0, 'm'},
{"merge-barcodes-precise", no_argument, 0, 'M'},
{"merge-umi", no_argument, 0, 'u'},
{"not-filtered", no_argument, 0, 'n'},
{"output-file", required_argument, 0, 'o'},
{"read-params", required_argument, 0, 'r'},
{"pseudoaligner", no_argument, 0, 'P'},
{"quiet", no_argument, 0, 'q'},
{"reads-output", no_argument, 0, 'R'},
{"validation-stats", no_argument, 0, 'S'},
{"velocyto", no_argument, 0, 'V'},
{"write-mtx", no_argument, 0, 'w'},
{0, 0, 0, 0}
};
while ((c = getopt_long(argc, argv, "bc:C:fFg:G:hl:L:mMno:r:PqRSuVw", long_options, &option_index)) != -1)
{
switch (c)
{
case 'b':
params.bam_output = true;
break;
case 'c' :
params.config_file_name = string(optarg);
break;
case 'C' :
params.max_cell_number = int(strtol(optarg, nullptr, 10));
break;
case 'f' :
params.filled_bam = true;
break;
case 'F' :
params.filtered_bam_output = true;
break;
case 'g' :
params.genes_filename = string(optarg);
break;
case 'G' :
params.min_genes_after_merge = int(strtol(optarg, nullptr, 10));
break;
case 'h' :
usage();
exit(0);
case 'l' :
params.log_prefix = string(optarg);
break;
case 'L' :
params.gene_match_level = string(optarg);
break;
case 'm' :
params.merge_tags = true;
break;
case 'M' :
params.merge_tags = true;
params.merge_tags_precise = true;
break;
case 'o' :
params.output_name = string(optarg);
break;
case 'r' :
params.read_params_filenames = string(optarg);
break;
case 'P' :
params.pseudoaligner = true;
break;
case 'R' :
params.reads_output = true;
break;
case 'q' :
params.quiet = true;
break;
case 'S' :
params.stats_for_validation = true;
break;
case 'V' :
params.velocyto_matrices = true;
break;
case 'u' :
params.umi_merge = true;
break;
case 'w' :
params.write_matrix = true;
break;
default:
cerr << SCRIPT_NAME << ": unknown arguments passed: '" << (char)c <<"'" << endl;
params.cant_parse = true;
return params;
}
}
if (optind > argc - 1)
{
cerr << SCRIPT_NAME << ": at least one bam file must be supplied" << endl;
params.cant_parse = true;
}
if (params.config_file_name.empty())
{
cerr << SCRIPT_NAME << ": config file must be supplied" << endl;
params.cant_parse = true;
}
if (params.filled_bam && params.read_params_filenames != "")
{
cerr << SCRIPT_NAME << ": only one genes source must be provided (you can't use both -r and -f at the same time)" << endl;
params.cant_parse = true;
}
if (params.genes_filename.empty() && params.gene_match_level.find_first_of("eE") == std::string::npos)
{
cerr << SCRIPT_NAME << ": you should provide genes file (-g option) to use intron annotations" << endl;
params.cant_parse = true;
}
if (params.output_name.empty())
{
params.output_name = "cell.counts.rds";
}
return params;
}
CellsDataContainer get_cells_container(const vector<string> &files, const Params ¶ms,
const ptree &est_config, const BamProcessing::BamController &bam_controller)
{
auto match_levels = UMI::Mark::get_by_code(params.gene_match_level);
Merge::MergeStrategyFactory merge_factory(est_config, params.config_file_name, params.min_genes_after_merge);
CellsDataContainer container(merge_factory.get_cb_strat(params.merge_tags, params.merge_tags_precise),
merge_factory.get_umi(params.umi_merge), match_levels, params.filtered_bam_output,
params.max_cell_number);
bam_controller.parse_bam_files(files, params.bam_output, container);
container.set_initialized();
container.merge_and_filter();
return container;
}
int main(int argc, char **argv)
{
std::string command_line;
for (int i = 0; i < argc; ++i)
{
command_line += argv[i];
command_line += " ";
}
Params params = parse_cmd_params(argc, argv);
if (params.cant_parse)
{
usage();
return 1;
}
if (params.log_prefix.length() != 0)
{
params.log_prefix += "_";
}
Tools::init_log(!params.quiet, false, params.log_prefix + "est_main.log", params.log_prefix + "est_debug.log");
Tools::copy_file(params.config_file_name, params.log_prefix + "est_config.dump.xml");
L_TRACE << command_line;
L_TRACE << "Version: " << VERSION << ".";
vector<string> files;
Tools::init_r();
while (optind < argc)
{
files.emplace_back(argv[optind++]);
}
try
{
check_files_existence(params, files);
Tools::trace_time("Run", true);
ptree estimation_config;
if (!params.config_file_name.empty())
{
ptree pt;
read_xml(params.config_file_name, pt);
estimation_config = pt.get_child("config.Estimation", ptree());
}
BamProcessing::BamController bam_controller(BamProcessing::BamTags(estimation_config), params.filled_bam,
params.read_params_filenames, params.genes_filename, params.pseudoaligner,
Tools::ReadParameters::quality_to_phred(estimation_config.get<int>("Other.min_barcode_quality", 0)));
CellsDataContainer container = get_cells_container(files, params, estimation_config, bam_controller);
if (params.filtered_bam_output)
{
bam_controller.write_filtered_bam_files(files, container);
}
ResultsPrinter printer(params.write_matrix, params.reads_output, params.stats_for_validation, !params.umi_merge);
Tools::trace_time("Done");
printer.save_results(container, params.output_name);
if (params.velocyto_matrices)
{
printer.save_intron_exon_matrices(container, params.output_name);
}
}
catch (std::runtime_error &err)
{
L_ERR << err.what();
return 1;
}
catch (std::logic_error &err)
{
L_ERR << err.what();
return 1;
}
catch (std::exception &err)
{
L_ERR << err.what();
return 1;
}
Tools::trace_time("All done");
}