-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
237 lines (195 loc) · 6 KB
/
main.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
/*
* MinMaxMirnaGene - A program for computing optimal miRNA-gene covers.
* Copyright (C) 2016 Daniel Stöckel <[email protected]>
*
* MinMaxMirnaGene is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* MinMaxMirnaGene is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with MinMaxMirnaGene. If not, see <http://www.gnu.org/licenses/>.
*/
#include "CPLEXException.h"
#include "MaxGeneProblem.h"
#include "MinMaxProblem.h"
#include "TargetMappings.h"
#include <cstring>
#include <fstream>
#include <iostream>
TargetMappings readMappings(const std::string& path)
{
TargetMappings mappings;
std::ifstream input(path);
if(!input) {
std::cerr << "Could not open file '" << path << "' for reading.\n";
exit(-1);
}
std::string mirna;
std::string gene;
input >> mirna >> gene;
while(input) {
mappings.add(mirna, gene);
input >> mirna >> gene;
}
return mappings;
}
void writeList(const std::string& path, const std::vector<std::string>& items)
{
std::ofstream output(path);
if(!output) {
std::cerr << "Could not open file '" << path << "' for writing.\n";
exit(-8);
}
for(const auto& item : items) {
output << item << '\n';
}
}
const char* commandList()
{
return "\tminmax\n\tmaxgene\n\tmaxgene-curve\n\tminmirna\n\tminmirna-curve";
}
void printILPStatistics(const ILPProblem& problem)
{
std::cout << "Created ILP formulation with " << problem.numVariables()
<< " variables, " << problem.numConstraints()
<< " constraints, and " << problem.numNonZero()
<< " non-zero entries.\n";
}
void solveProblem(ILPProblem& problem, const std::string& mpath,
const std::string& gpath)
{
printILPStatistics(problem);
std::vector<std::string> mirnas;
std::vector<std::string> genes;
std::tie(mirnas, genes) = problem.solve();
std::cout << "Solution contains " << mirnas.size() << " miRNAs and "
<< genes.size() << " genes.\n";
writeList(mpath, mirnas);
writeList(gpath, genes);
}
int minMax(int argc, char* argv[], TargetMappings& mappings)
{
if(argc <= 6) {
std::cerr << "Not enough arguments supplied. Usage:\n\t" << argv[0]
<< " minmax mappings.txt mirna_weight gene_weight mirnas.out "
"genes.out\n";
return -3;
}
double mirnaWeight = 0.0;
double geneWeight = 0.0;
try {
mirnaWeight = std::stof(argv[3]);
geneWeight = std::stof(argv[4]);
} catch(const std::exception& e) {
std::cerr << "Error converting argument to a number\n";
return -4;
}
MinMaxProblem problem(mappings, mirnaWeight, geneWeight);
solveProblem(problem, argv[5], argv[6]);
return 0;
}
int maxGene(int argc, char* argv[], TargetMappings& mappings)
{
if(argc <= 5) {
std::cerr << "Not enough arguments supplied. Usage:\n\t" << argv[0]
<< " minmax mappings.txt num_mirna mirnas.out genes.out\n";
return -3;
}
int num_mirnas = 0;
try {
num_mirnas = std::stoi(argv[3]);
} catch(const std::exception& e) {
std::cerr << "Could not convert " << argv[3] << " to a number\n";
return -6;
}
MaxGeneProblem problem(mappings, num_mirnas);
solveProblem(problem, argv[4], argv[5]);
return 0;
}
int maxGeneCurve(int argc, char* argv[], TargetMappings& mappings)
{
std::ofstream curve(argv[3]);
if(!curve) {
std::cerr << "Could not open file '" << argv[3] << "' for writing!\n";
return -9;
}
MaxGeneProblem problem(mappings, 0);
printILPStatistics(problem);
curve << "0\t0\n";
std::size_t i = 1;
for(; i <= mappings.numMirnas(); ++i) {
std::cout << "\rProcessing " << i << "/" << mappings.numMirnas();
std::cout.flush();
problem.setNumMirna(i);
auto result = problem.solve();
curve << i << '\t' << result.second.size() << '\n';
if(result.second.size() == mappings.numGenes() &&
i != mappings.numMirnas()) {
std::cout << "\nCovered all available target genes. Exiting early.";
break;
}
}
std::cout << '\n';
for(++i; i <= mappings.numMirnas(); ++i) {
curve << i << '\t' << mappings.numGenes() << '\n';
}
return 0;
}
int minMirna(int argc, char* argv[], TargetMappings& mappings)
{
std::cerr << "Not yet implemented!\n";
return 0;
}
int minMirnaCurve(int argc, char* argv[], TargetMappings& mappings)
{
std::cerr << "Not yet implemented!\n";
return 0;
}
int dispatchCLIArguments(int argc, char* argv[], TargetMappings& mappings)
{
if(strcmp(argv[1], "minmax") == 0) {
return minMax(argc, argv, mappings);
} else if(strcmp(argv[1], "maxgene") == 0) {
return maxGene(argc, argv, mappings);
} else if(strcmp(argv[1], "maxgene-curve") == 0) {
return maxGeneCurve(argc, argv, mappings);
} else if(strcmp(argv[1], "minmirna") == 0) {
return minMirna(argc, argv, mappings);
} else if(strcmp(argv[1], "minmirna-curve") == 0) {
return minMirnaCurve(argc, argv, mappings);
} else {
std::cerr << "Unknown command '" << argv[1]
<< "'.\n\nAvailable commands:\n" << commandList() << '\n';
return -2;
}
}
void printMappingStatistics(const TargetMappings& mappings)
{
std::cout << "Read " << mappings.numMappings() << " mappings between "
<< mappings.numMirnas() << " miRNAs and " << mappings.numGenes()
<< " genes.\n";
}
int main(int argc, char* argv[])
{
if(argc <= 2) {
std::cerr << "Not enough arguments supplied. Usage:\n\n\t" << argv[0]
<< " [command] mappings.txt [...]\n\nAvailable commands:\n"
<< commandList() << '\n';
return -1;
}
auto mappings = readMappings(argv[2]);
printMappingStatistics(mappings);
try {
return dispatchCLIArguments(argc, argv, mappings);
} catch(const CPLEXException& e) {
std::cerr << "Error during ILP computation: " << e.what() << std::endl;
return -7;
}
return 0;
}