-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathL-INSIGHT-pred.cpp
192 lines (164 loc) · 5.21 KB
/
L-INSIGHT-pred.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
#include <cmath>
#include <cstdlib>
#include <cstdlib>
#include <cstring>
#include <fstream>
#include <iostream>
#include <vector>
#include <stdexcept>
#include <ctime>
#include "text_tools.hpp"
#include <fenv.h>
#include <iomanip>
#include <dirent.h>
#include <map>
#include <stdint.h>
#include "fitreg_util.hpp"
#define GOMPERTZ_SIGMOID
using namespace std;
using namespace hui;
vector<double> readParameters(string inFile);
inline vector<double> calculateSelectionVariables(const vector<double> ¶, const vector<double> &features, size_t featureSize, double rhoShift){
if (para.size() % 2 == 1){
cerr << "Error: The number of parameters is odd number!\n";
cerr << " Please check the parameter file.\n";
exit(1);
}
if (features.size() * 2 + 2 != para.size()){
cerr << "Error: The number of features does not match the number of weights!\n";
cerr << " Please check the input files.\n";
exit(1);
}
double rhoLatent = para[0];
unsigned long halfSize = para.size() / 2;
double gammaPrimeLatent = para[halfSize];
for (size_t i = 0; i < features.size(); i ++){
double f = features[i];
rhoLatent += para[i + 1] * f;
gammaPrimeLatent += para[i + 1 + halfSize] * f;
}
#ifdef GOMPERTZ_SIGMOID
// a = 1
// b = -3
// c = -1
// f(x) = a * exp(b * exp(c * x))
// rhoLatent = -1;
double rho = exp(rhoShift * exp(-rhoLatent));
// using logistic sigmoid for gamma
double gammaPrime = 1. / (1. + exp(-gammaPrimeLatent));
#else
double rho = 1. / (1. + exp(-rhoLatent));
double gammaPrime = 1. / (1. + exp(-gammaPrimeLatent));
#endif
vector<double> selection(2);
selection[0] = rho;
selection[1] = gammaPrime;
return selection;
}
int main(int argc, char *argv[]){
string featureFile;
string paraFile;
string outFile;
if (argc == 1 || (argc == 2 && (string(argv[1]) == "--help" || string(argv[1]) == "-h"))){
cout << "usage: LINSIGHT-score [-h] -f FEATURE_FILE -p PARAMETER_FILE -o OUTPUT_FILE\n";
exit(0);
}
if (argc % 2 != 1){
cerr << "Error: odd number of arguments provided.\n";
exit(1);
}
for (int i = 1; i < argc; i +=2){
string label(argv[i]);
string value(argv[i + 1]);
if (label == "--feature-file" || label == "-f"){
featureFile = value;
}
else if (label == "--parameter-file" || label == "-p"){
paraFile = value;
}
else if (label == "--output-file" || label == "-o"){
outFile = value;
}
else{
cerr << "ERROR: Unknown command line arugment: " << label << ".\n";
exit(1);
}
}
if (featureFile == ""){
cerr << "ERROR: Feature file is not provided.\n";
exit(1);
}
if (paraFile == ""){
cerr << "ERROR: Parameter file is not provided.\n";
exit(1);
}
if (outFile == ""){
cerr << "ERROR: Output file is not provided.\n";
exit(1);
}
// parameters for sigmoid neuron
double rhoShift = -3.;
vector<double> para = readParameters(paraFile);
// the number of features
size_t featureSize = 0;
string featureChr;
uint64_t featureStart;
uint64_t featureEnd;
vector<double> features;
// read features
ifstream featureHandle;
featureHandle.open(featureFile.c_str(), ios::in);
if (!featureHandle.is_open()){
cerr << "Error: cannot open feature file!\n";
exit(1);
}
string line;
ofstream out;
out.open(outFile.c_str(), ios::out);
if (!out.is_open()){
cerr << "Error: cannot open output file!\n";
exit(1);
}
// out << "#chr\tstart\tend\trho\tgamma\n";
// if the first time of reading file
while (readNextFeatureEntry(featureHandle, featureChr, featureStart, featureEnd, features, featureSize)){
if (featureSize == 0){
featureSize = features.size();
}
vector<double> selection = calculateSelectionVariables(para, features, featureSize, rhoShift);
// out << featureChr << "\t" << featureStart << "\t" << featureEnd << "\t" << selection[0] << "\t" << selection[1] << endl;
out << featureChr << "\t" << featureStart << "\t" << featureEnd << "\t" << selection[0] << endl;
}
}
vector<double> readParameters(string inFile){
ifstream file;
file.open(inFile.c_str());
if(!file.is_open()){
cerr << "Error: cannot open parameter file!\n";
exit(1);
}
file.seekg (0, file.end);
long length = file.tellg();// get file size
vector<double> para;
long i;
// loop backward over the file
for(i = length - 2; i > 0; i-- )
{
file.seekg(i);
char c = file.get();
if( c == '\r' || c == '\n' )//new line?
break;
}
// check whether we should start from the begining of the file
if (i == 0){
file.seekg(0);
}
string line;
getline(file, line);//read last line
vector<string> tmp = TextTools::split(line, '\t');
for (vector<string>::const_iterator it = tmp.begin(); it != tmp.end(); it ++){
para.push_back(TextTools::string2double(*it));
}
file.close();
return para;
}