forked from sbenz/Paradigm
-
Notifications
You must be signed in to change notification settings - Fork 5
/
evidencesource.cpp
232 lines (202 loc) · 6.19 KB
/
evidencesource.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
/********************************************************************************/
/* Copyright 2009-2011 -- The Regents of the University of California */
/* This code is provided for research purposes to scientists at non-profit */
/* organizations. All other use is strictly prohibited. For further */
/* details please contact University of California, Santa Cruz or */
/* Five3 Genomics, LLC (http://five3genomics.com). */
/********************************************************************************/
#include <fstream>
#include "common.h"
#include "evidencesource.h"
#define THROW(msg) throw std::runtime_error(msg)
EvidenceFactorGen::EvidenceFactorGen(const PropertySet& p) : _params()
{
_params.reserve(9);
if (p.hasKey("factorParams")) {
vector<string> paramsStr;
Tokenize(p.getStringAs<string>("factorParams"),paramsStr,";");
if (paramsStr.size() != 9) {
THROW("must have 9 elements in factorParams");
}
for(size_t i = 0; i < paramsStr.size(); i++)
{
_params.push_back(atof(paramsStr[i].c_str()));
}
return;
}
if (!p.hasKey("epsilon") || !p.hasKey("epsilon0")) {
cout << "woops" << endl;
cout << p << endl;
THROW("must specify either factorParams, or epsilon and epsilon0");
}
double epsilon = p.getStringAs<double>("epsilon");
double epsilon0 = p.getStringAs<double>("epsilon0");
Real major = 1 - epsilon;
Real minor = epsilon / 2;
Real major0 = 1 - epsilon0;
Real minor0 = epsilon0 / 2;
bool flip = false;
if (p.hasKey("reverse")) {
if (p.getStringAs<string>("reverse") == "true") {
flip = true;
} else if (p.getStringAs<string>("reverse") == "false") {
flip = false;
} else {
THROW("The 'reverse' option for evidence must be 'true' or 'false'");
}
}
if (flip) {
_params.push_back(minor);
_params.push_back(minor0);
_params.push_back(major);
_params.push_back(minor);
_params.push_back(major0);
_params.push_back(minor);
_params.push_back(major);
_params.push_back(minor0);
_params.push_back(minor);
} else {
_params.push_back(major);
_params.push_back(minor0);
_params.push_back(minor);
_params.push_back(minor);
_params.push_back(major0);
_params.push_back(minor);
_params.push_back(minor);
_params.push_back(minor0);
_params.push_back(major);
}
}
void EvidenceFactorGen::generateValues(const vector< string >& edge_types,
vector< Real >& outVals) const
{
assert(edge_types.size() == 1);
for (size_t i = 0; i < _params.size(); i++) {
outVals.push_back(_params[i]);
}
}
EvidenceSource::EvidenceSource(PropertySet &p, string base) :
cutoffs(),
options(p),
attachPoint(),
_evidenceFile()
{
if (p.hasKey("disc"))
setCutoffs(p.getAs<string>("disc"));
else
setCutoffs("-1.3,1.3");
if (p.hasKey("node"))
attachPoint = p.getAs<string>("node");
else
THROW("EvidenceSource conf. is missing the required property \"node\"");
if (p.hasKey("suffix")) {
_suffix = p.getAs<string>("suffix");
_evidenceFile = base + _suffix;
} else {
THROW("EvidenceSource conf. is missing the required property \"suffix\"");
}
}
void EvidenceSource::setCutoffs(string discLimits)
{
vector<string> cutoffsStr;
Tokenize(discLimits,cutoffsStr,";");
for(size_t i = 0; i < cutoffsStr.size(); i++)
{
if(VERBOSE)
cerr << "Added cutoff " << atof(cutoffsStr[i].c_str()) << endl;
cutoffs.push_back(atof(cutoffsStr[i].c_str()));
}
}
int EvidenceSource::discCutoffs (float x)
{
if(cutoffs.size() == 0)
return 0;
if(x < cutoffs[0])
return 0;
size_t i = 1;
while (i < cutoffs.size())
{
if (x < cutoffs[i])
return i;
i++;
}
return i;
}
double stringToDouble(const string& s) {
stringstream ss(s);
double result;
if (!(ss >> result).eof()) {
THROW("String " + s + " can not be converted to double.");
}
return result;
}
void EvidenceSource::loadFromFile(PathwayTab& p,
map<string, size_t>& sampleMap,
vector<Evidence::Observation>& sampleData)
{
ifstream infile;
infile.open( _evidenceFile.c_str() );
if( infile.is_open() ) {
string line;
if(!getline(infile,line))
return;
vector<string> header;
Tokenize(line,header,"\t");
header.erase(header.begin());
vector<Var> vars;
vars.reserve(header.size());
for (size_t h = 0; h < header.size(); h++) {
if(p.getEntityType(header[h]) == "protein") // skip adding evidence if it's not in the pathway
{
vars.push_back(p.addObservationNode(header[h], attachPoint, _suffix));
}
}
FactorGenerator* fgen = new EvidenceFactorGen(options);
p.addFactorGenerator("protein", _suffix, fgen);
while(getline(infile,line)) {
vector<string> vals;
Tokenize(line,vals,"\t");
if (vals.size() > header.size() + 1) {
THROW("Entries in evidence line does not match header length");
}
string sample = vals[0];
_sampleNames.push_back(sample);
vals.erase(vals.begin());
for(size_t i = 0, var_idx = 0; i < vals.size(); i++) {
if(p.getEntityType(header[i]) != "protein") // skip adding evidence if it's not in the pathway
continue;
if(strcmp(vals[i].c_str(),"NA")==0) {
++var_idx;
continue;
}
double evidence = stringToDouble(vals[i]);
if (sampleMap.count(sample) == 0) {
sampleMap[sample] = sampleData.size();
sampleData.push_back(Evidence::Observation());
}
size_t sample_idx = sampleMap[sample];
sampleData[sample_idx][vars[var_idx++]] = discCutoffs(evidence);
}
}
infile.close();
}
return;
}
void Tokenize(const string& str,
vector<string>& tokens,
const string& delimiters)
{
// Skip delimiters at beginning.
string::size_type lastPos = str.find_first_not_of(delimiters, 0);
// Find first "non-delimiter".
string::size_type pos = str.find_first_of(delimiters, lastPos);
while (string::npos != pos || string::npos != lastPos)
{
// Found a token, add it to the vector.
tokens.push_back(str.substr(lastPos, pos - lastPos));
// Skip delimiters. Note the "not_of"
lastPos = str.find_first_not_of(delimiters, pos);
// Find next "non-delimiter"
pos = str.find_first_of(delimiters, lastPos);
}
}