-
Notifications
You must be signed in to change notification settings - Fork 10
/
COMPASS.cpp
183 lines (171 loc) · 6.32 KB
/
COMPASS.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
#include <iostream>
#include <cmath>
#include <cfloat>
#include <random>
#include <fstream>
#include <string.h>
#include <stdexcept>
#include <omp.h>
#include "Inference.h"
#include "Tree.h"
#include "Scores.h"
#include "input.h"
int n_cells;
int n_loci;
int n_regions;
std::vector<Cell> cells;
Data data;
Params parameters;
int main(int argc, char* argv[]){
init_params();
parameters.verbose=false;
// Read command line arguments
std::string input_file{};
std::string regionweights_file{};
int n_chains=4;
int chain_length=5000;
int burn_in = -1;
double temperature=10;
double betabin_overdisp = parameters.omega_het;
bool use_CNA=true;
bool output_simplified = true;
std::string output{};
data.sex = "female";
//parameters.verbose=true;
for (int i=1;i<argc-1;i++){
std::string argument{argv[i]};
if (strcmp(argv[i],"-i")==0){
input_file = argv[i+1];
}
else if (strcmp(argv[i],"--regionweights")==0){
regionweights_file = argv[i+1];
}
else if (strcmp(argv[i],"--nchains")==0){
n_chains=atoi(argv[i+1]);
}
else if (strcmp(argv[i],"--chainlength")==0){
chain_length=atoi(argv[i+1]);
}
else if (strcmp(argv[i],"--burnin")==0){
burn_in=atoi(argv[i+1]);
}
else if (strcmp(argv[i],"--temperature")==0){
temperature=atoi(argv[i+1]);
}
else if (strcmp(argv[i],"--overdisp")==0){
betabin_overdisp=atof(argv[i+1]);
}
else if (strcmp(argv[i],"--doubletrate")==0){
parameters.doublet_rate=atof(argv[i+1]);
}
else if (strcmp(argv[i],"--dropoutrate")==0){ // mean of the prior dropout rate
parameters.prior_dropoutrate_mean=atof(argv[i+1]);
}
else if (strcmp(argv[i],"--dropoutrate_concentration")==0){ // concentration parameter for the beta binomial distribution for the dropout rates (higher values: dropout rates will be closer to the mean)
parameters.prior_dropoutrate_omega=atof(argv[i+1]);
}
else if (strcmp(argv[i],"--seqerror")==0){ // sequencing error rate
parameters.sequencing_error_rate=atof(argv[i+1]);
}
else if (strcmp(argv[i],"--nodecost")==0){ // Penalty for adding nodes in the tree
parameters.node_cost=atof(argv[i+1]);
}
else if (strcmp(argv[i],"--cnacost")==0){ // Penalty for adding CNA events in the tree
parameters.CNA_cost=atof(argv[i+1]);
}
else if (strcmp(argv[i],"--lohcost")==0){ // Penalty for adding loh events in the tree
parameters.LOH_cost=atof(argv[i+1]);
}
else if (strcmp(argv[i],"-o")==0){
output=argv[i+1];
}
else if (strcmp(argv[i],"-d")==0){
if (strcmp(argv[i+1],"0")==0) parameters.use_doublets=false;
}
else if (strcmp(argv[i],"--CNA")==0){
if (strcmp(argv[i+1],"0")==0) use_CNA=false;
}
else if (strcmp(argv[i],"--CNV")==0){
if (strcmp(argv[i+1],"0")==0) use_CNA=false;
}
else if (strcmp(argv[i],"--filterregions")==0){
if (strcmp(argv[i+1],"0")==0){
parameters.filter_regions=false;
parameters.filter_regions_CNLOH=false;
}
}
else if (strcmp(argv[i],"--filterregionsCNLOH")==0){
if (strcmp(argv[i+1],"0")==0){
parameters.filter_regions_CNLOH=false;
}
}
else if (strcmp(argv[i],"--verbose")==0){
if (strcmp(argv[i+1],"1")==0) parameters.verbose=true;
}
else if (strcmp(argv[i],"--sex")==0){
data.sex= std::string(argv[i+1]);
}
else if (strcmp(argv[i],"--prettyplot")==0){
if (strcmp(argv[i+1],"0")==0) output_simplified=false;
}
else if (argument.substr(0,1)=="-"){
std::cout<<" Unrecognized argument: " <<argv[i]<<std::endl;
throw std::invalid_argument("Invalid argument: "+ argument);
}
}
if (input_file.size()==0){
if (argc==2){
input_file = argv[1];
std::cout << "Will use "<<argv[1]<<" as input."<<std::endl;
}
else{
throw std::invalid_argument("No input was provided. Please provide one with the -i option.");
}
}
if (output.size()==0){
std::cout << "No output name was provided. COMPASS will use the same basename as the input for the output." <<std::endl;
}
if (burn_in==-1){
burn_in=chain_length/2;
}
load_CSV(input_file,regionweights_file,use_CNA);
parameters.omega_het = std::min(parameters.omega_het,betabin_overdisp);
parameters.omega_het_indel = std::min(parameters.omega_het_indel,betabin_overdisp);
// Get the name of the file, without directory
std::string input_name = input_file;
int name_start=0;
for (int i=0;i<input_file.size();i++){
if (input_file[i]=='/') name_start=i+1;
}
input_name=input_file.substr(name_start,input_file.size()-name_start);
std::vector<double> results{};
results.resize(n_chains);
std::vector<Tree> best_trees{};
best_trees.resize(n_chains);
if (n_chains<omp_get_num_procs()) omp_set_num_threads(n_chains);
else omp_set_num_threads(omp_get_num_procs());
std::cout<<"Starting "<<std::to_string(n_chains)<< " MCMC chains in parallel"<<std::endl;
#pragma omp parallel for
for (int i=0;i<n_chains;i++){
std::srand(i);
Inference infer{"",temperature,i};
best_trees[i] = infer.find_best_tree(use_CNA,chain_length,burn_in);
results[i]=best_trees[i].log_score;
}
double best_score=-DBL_MAX;
int best_score_index=-1;
for (int i=0;i<n_chains;i++){
if (best_score<results[i]){
best_score=results[i];
best_score_index = i;
}
}
if (output_simplified) best_trees[best_score_index].to_dot(output,true);
else best_trees[best_score_index].to_dot(output,false);
std::string gv_filename(output);
if ( output.size()<= 3 || (output.size()>3 && output.substr(output.size()-3)!=".gv")){
gv_filename = output + + "_tree.gv";
}
std::cout<<"Completed ! The output was written to "<<output<< ". You can visualize the tree by running: dot -Tpng "<<gv_filename<<" -o output.png"<<std::endl;
return 0;
}