| 1 |
/* |
| 2 |
* File: OrthoUtils.cc |
| 3 |
* Author: ikramu |
| 4 |
* |
| 5 |
* Created on October 22, 2012, 1:19 PM |
| 6 |
*/ |
| 7 |
|
| 8 |
#include "OrthoUtils.hh" |
| 9 |
|
| 10 |
OrthoUtils::OrthoUtils() { |
| 11 |
} |
| 12 |
|
| 13 |
OrthoUtils::OrthoUtils(const OrthoUtils& orig) { |
| 14 |
} |
| 15 |
|
| 16 |
OrthoUtils::~OrthoUtils() { |
| 17 |
} |
| 18 |
|
| 19 |
void OrthoUtils::estimate_orthologies(string posterior_file, string true_tree, string specie_tree, |
| 20 |
vector<string> leaves, vector<map<string, double> > &ortho_list) { |
| 21 |
//vector<map<string, double> > ortho_list; |
| 22 |
string gene_tree; //Gene tree |
| 23 |
ifstream sample_file; //File object for samples |
| 24 |
string curr_line; //Current line when reading samples |
| 25 |
int burn_in = 1; |
| 26 |
string header = ""; |
| 27 |
bool endOfHeader = false; |
| 28 |
int hl; |
| 29 |
double mean, var; |
| 30 |
double birth, death; |
| 31 |
|
| 32 |
/*Read samples and create states*/ |
| 33 |
sample_file.open(posterior_file.c_str()); |
| 34 |
if (sample_file.is_open()) { |
| 35 |
try { |
| 36 |
while (!sample_file.eof()) { |
| 37 |
//Skip lines of comments |
| 38 |
if ((sample_file.peek() == '#') && !endOfHeader) { |
| 39 |
sample_file.ignore(SAMPLE_LENGTH, '\n'); |
| 40 |
getline(sample_file, header); |
| 41 |
|
| 42 |
Tokenizer tok(";"); |
| 43 |
tok.setString(header); |
| 44 |
hl = 0; |
| 45 |
while (tok.hasMoreTokens()) { |
| 46 |
tok.getNextToken(); |
| 47 |
hl++; |
| 48 |
} |
| 49 |
|
| 50 |
continue; |
| 51 |
} else if ((sample_file.peek() == '#') && endOfHeader) { |
| 52 |
// don't do anything, this is the summary at |
| 53 |
// the end of prime output. |
| 54 |
sample_file.ignore(SAMPLE_LENGTH, '\n'); |
| 55 |
continue; |
| 56 |
} |
| 57 |
if (burn_in <= BURN_IN) { |
| 58 |
sample_file.ignore(SAMPLE_LENGTH, '\n'); |
| 59 |
burn_in++; |
| 60 |
continue; |
| 61 |
} |
| 62 |
|
| 63 |
// set the endOfHeader to true, if not done already |
| 64 |
if (!endOfHeader) |
| 65 |
endOfHeader = true; |
| 66 |
|
| 67 |
//Get next line from file |
| 68 |
curr_line = ""; |
| 69 |
getline(sample_file, curr_line); |
| 70 |
|
| 71 |
//Skip empty lines |
| 72 |
if (curr_line == "") { |
| 73 |
cout << "line is empty..." << endl; |
| 74 |
continue; |
| 75 |
} |
| 76 |
|
| 77 |
get_data_from_sample(curr_line, gene_tree, mean, var, birth, death); |
| 78 |
|
| 79 |
DLRSOrthoCalculator *dos = new DLRSOrthoCalculator(gene_tree, specie_tree, mean, var, birth, death, true); |
| 80 |
map<string, double> ortho_prob = dos->getOrthoEstimates(); |
| 81 |
ortho_list.push_back(ortho_prob); |
| 82 |
// only take the samples after burn-in |
| 83 |
//cout << "sample number: " << burn_in << endl; |
| 84 |
cout << "read gene tree from sample no: " << burn_in << endl; |
| 85 |
//cout << G << endl; |
| 86 |
burn_in++; |
| 87 |
|
| 88 |
//find_vertices_single(vertices, gsr, ID, inv_ID); |
| 89 |
//find_vertices_single(vertices, G, ID, inv_ID); |
| 90 |
delete(dos); |
| 91 |
} |
| 92 |
} catch (exception &ex) { |
| 93 |
cout << ex.what() << endl; |
| 94 |
} |
| 95 |
sample_file.close(); |
| 96 |
//num_states = (burn_in - 1) - BURN_IN; |
| 97 |
//cout << "Total samples in hp_vertices are " << num_states << endl; |
| 98 |
//find_high_post_vertices(vertices, hp_vertices, numStates * posterior_threshold); |
| 99 |
} else { |
| 100 |
cerr << "Could not open file: " << sample_file << endl; |
| 101 |
exit(EXIT_FAILURE); |
| 102 |
} |
| 103 |
} |
| 104 |
|
| 105 |
void OrthoUtils::get_data_from_sample(string curr_line, string &gene_tree, double &mean, double &var, double &birth, double &death) { |
| 106 |
//TreeIO tree_reader; // used for parsing prime trees |
| 107 |
Tokenizer tokenizer(";"); //String tokenizer |
| 108 |
|
| 109 |
/* |
| 110 |
* Information in each line is separated by ';'. The tokenizer |
| 111 |
* splits the line at each ';' |
| 112 |
*/ |
| 113 |
//Set tokenizer |
| 114 |
tokenizer.setString(curr_line); |
| 115 |
|
| 116 |
//Skip info about likelihoods and number of iterations |
| 117 |
//cout << tokenizer.getNextToken() << endl; |
| 118 |
//cout << tokenizer.getNextToken() << endl; |
| 119 |
tokenizer.getNextToken(); |
| 120 |
tokenizer.getNextToken(); |
| 121 |
|
| 122 |
//Read gene tree |
| 123 |
//tree_reader.setSourceString(tokenizer.getNextToken()); |
| 124 |
|
| 125 |
gene_tree = tokenizer.getNextToken(); |
| 126 |
mean = atof(tokenizer.getNextToken().c_str()); |
| 127 |
var = atof(tokenizer.getNextToken().c_str()); |
| 128 |
tokenizer.getNextToken(); |
| 129 |
birth = atof(tokenizer.getNextToken().c_str()); |
| 130 |
death = atof(tokenizer.getNextToken().c_str()); |
| 131 |
} |
| 132 |
|
| 133 |
map<string, double> OrthoUtils::compute_ortho_single(string gene_tree, string specie_tree, double mean, |
| 134 |
double var, double birth, double death, vector<string> leaves) { |
| 135 |
//double mean = 0.0545746; |
| 136 |
//double var = 0.000356028; |
| 137 |
//double birth = 3.54539; |
| 138 |
//double death = 3.6247; |
| 139 |
|
| 140 |
//string gene_tree = "./data/gene.tree"; |
| 141 |
//string specie_tree = "/home/ikramu/PrIME/trunk/src/cxx/tools/ortho_true_vertices/data/mhcLasse.stree"; |
| 142 |
//string true_tree = "/home/ikramu/PrIME/trunk/src/cxx/tools/ortho_true_vertices/data/g95.true"; |
| 143 |
|
| 144 |
//vector<string> leaves = get_leaves_from_true_file(true_tree); |
| 145 |
|
| 146 |
DLRSOrthoCalculator dos(gene_tree, specie_tree, mean, var, birth, death); |
| 147 |
map<string, double> ortho_estimates = dos.getOrthoEstimates(); |
| 148 |
|
| 149 |
return ortho_estimates; |
| 150 |
} |
| 151 |
|
| 152 |
vector<string> OrthoUtils::get_leaves_from_true_file(string true_file) { |
| 153 |
vector<string> leaves; |
| 154 |
string tree; |
| 155 |
//ifstream tf(true_file); |
| 156 |
std::fstream ifs(true_file.c_str()); |
| 157 |
if (ifs) { |
| 158 |
std::stringstream oss; |
| 159 |
oss << ifs.rdbuf(); |
| 160 |
Tokenizer tk(";"); |
| 161 |
tree = oss.str(); |
| 162 |
tk.setString(tree); |
| 163 |
//string tree; |
| 164 |
while (tk.hasMoreTokens()) { |
| 165 |
string s = tk.getNextToken(); |
| 166 |
if (s != "\n") { |
| 167 |
trim(s); |
| 168 |
tree = s; |
| 169 |
} |
| 170 |
|
| 171 |
} |
| 172 |
} |
| 173 |
|
| 174 |
TreeIO tio = TreeIO::fromString(tree); |
| 175 |
Tree g = tio.readBeepTree(NULL, NULL); |
| 176 |
|
| 177 |
vector<Node*> all_nodes = g.getAllNodes(); |
| 178 |
|
| 179 |
//cout << g << endl; |
| 180 |
|
| 181 |
for (vector<Node*>::iterator i = all_nodes.begin(); i != all_nodes.end(); i++) { |
| 182 |
//cout << (*i)->getNumber() << endl; |
| 183 |
if ((*i) != NULL && !((*i)->isLeaf())) { |
| 184 |
vector<Node*> dnodes = g.getDescendentNodes(*i); |
| 185 |
stringstream strstream; |
| 186 |
for (unsigned int i = 0; i < dnodes.size(); i++) |
| 187 |
strstream << dnodes[i]->getName() << " "; |
| 188 |
|
| 189 |
string list = strstream.str(); |
| 190 |
trim(list); |
| 191 |
leaves.push_back(list); |
| 192 |
//cout << "For " << (*i)->getNumber() << " the leaves are: [" << list << "]" << endl; |
| 193 |
} |
| 194 |
} |
| 195 |
return leaves; |
| 196 |
} |
| 197 |
|
| 198 |
vector<string> OrthoUtils::get_gene_pair_from_true_tree(string true_file) { |
| 199 |
vector<string> leaves; |
| 200 |
string tree; |
| 201 |
|
| 202 |
std::fstream ifs(true_file.c_str()); |
| 203 |
if (ifs) { |
| 204 |
std::stringstream oss; |
| 205 |
oss << ifs.rdbuf(); |
| 206 |
Tokenizer tk(";"); |
| 207 |
tree = oss.str(); |
| 208 |
tk.setString(tree); |
| 209 |
//string tree; |
| 210 |
while (tk.hasMoreTokens()) { |
| 211 |
string s = tk.getNextToken(); |
| 212 |
if (s != "\n") { |
| 213 |
trim(s); |
| 214 |
tree = s; |
| 215 |
} |
| 216 |
|
| 217 |
} |
| 218 |
} |
| 219 |
|
| 220 |
TreeIO tio = TreeIO::fromString(tree); |
| 221 |
Tree g = tio.readBeepTree(NULL, NULL); |
| 222 |
cout << g << endl; |
| 223 |
vector<Node*> all_nodes = g.getAllNodes(); |
| 224 |
|
| 225 |
for (vector<Node*>::iterator iter = all_nodes.begin(); iter != all_nodes.end(); iter++) { |
| 226 |
if ((*iter != NULL) && !(*iter)->isLeaf()) { |
| 227 |
vector<Node*> lNodes = g.getDescendentNodes((*iter)->getLeftChild()); |
| 228 |
vector<Node*> rNodes = g.getDescendentNodes((*iter)->getRightChild()); |
| 229 |
for (unsigned int i = 0; i < lNodes.size(); i++) { |
| 230 |
for (unsigned int j = 0; j < rNodes.size(); j++) { |
| 231 |
if (not_same_specie(lNodes[i]->getName(), rNodes[j]->getName())) { |
| 232 |
vector<string> vp; |
| 233 |
vp.push_back(lNodes[i]->getName()); |
| 234 |
vp.push_back(rNodes[j]->getName()); |
| 235 |
sort(vp.begin(), vp.end()); |
| 236 |
|
| 237 |
string cpair = vp[0] + (string)" " + vp[1]; |
| 238 |
leaves.push_back(cpair); |
| 239 |
} |
| 240 |
} |
| 241 |
} |
| 242 |
} |
| 243 |
} |
| 244 |
|
| 245 |
return leaves; |
| 246 |
} |
| 247 |
|
| 248 |
void OrthoUtils::print_map(map<string, double> tmap) { |
| 249 |
|
| 250 |
for (map<string, double>::iterator iter = tmap.begin(); iter != tmap.end(); iter++) { |
| 251 |
cout << iter->first << ":" << iter->second << endl; |
| 252 |
} |
| 253 |
} |
| 254 |
|
| 255 |
bool OrthoUtils::not_same_specie(string left_gene, string right_gene) { |
| 256 |
if(get_specie_from_gene_name(left_gene) == get_specie_from_gene_name(right_gene)) |
| 257 |
return false; |
| 258 |
else |
| 259 |
return true; |
| 260 |
} |
| 261 |
|
| 262 |
string OrthoUtils::get_specie_from_gene_name(string gene) { |
| 263 |
Tokenizer tk("_"); |
| 264 |
tk.setString(gene); |
| 265 |
|
| 266 |
// example would be like gene_mus_1 or gene_homo_2 |
| 267 |
// first is the word "gene" so skip |
| 268 |
tk.getNextToken(); |
| 269 |
|
| 270 |
// return the next one which is specie name |
| 271 |
return tk.getNextToken(); |
| 272 |
} |
| 273 |
|
| 274 |
map<string, double> OrthoUtils::normalize_orthologies(vector<map<string, double> > ortho_list) { |
| 275 |
map<string, double> norm_ortho = ortho_list[0]; |
| 276 |
|
| 277 |
for (map<string, double>::iterator iter = norm_ortho.begin(); iter != norm_ortho.end(); iter++) { |
| 278 |
iter->second = 0.0; |
| 279 |
} |
| 280 |
|
| 281 |
for (unsigned int i = 0; i < ortho_list.size(); i++) { |
| 282 |
map<string, double> sample = ortho_list[i]; |
| 283 |
for (map<string, double>::iterator j = norm_ortho.begin(); j != norm_ortho.end(); j++) { |
| 284 |
//if(sample) |
| 285 |
if (sample.find(j->first) != sample.end()) |
| 286 |
j->second += sample.at(j->first); |
| 287 |
} |
| 288 |
} |
| 289 |
|
| 290 |
for (map<string, double>::iterator iter = norm_ortho.begin(); iter != norm_ortho.end(); iter++) { |
| 291 |
iter->second /= ortho_list.size(); |
| 292 |
cout << iter->first << ":" << iter->second << endl; |
| 293 |
} |
| 294 |
|
| 295 |
return norm_ortho; |
| 296 |
} |
| 297 |
|
| 298 |
void OrthoUtils::write_orthologies(map<string, double> orthologies, string posterior_file) { |
| 299 |
string outfile = posterior_file + std::string(".computed"); |
| 300 |
ofstream out; |
| 301 |
out.open(outfile.c_str()); |
| 302 |
|
| 303 |
if (out.is_open()) { |
| 304 |
for (map<string, double>::iterator iter = orthologies.begin(); iter != orthologies.end(); iter++) { |
| 305 |
//if(iter != NULL) |
| 306 |
out << iter->first << "\t" << iter->second << endl; |
| 307 |
//cout << iter->first << ":" << iter->second << endl; |
| 308 |
} |
| 309 |
} else { |
| 310 |
cerr << "error: can't open " << outfile << " for writing" << endl; |
| 311 |
} |
| 312 |
out.close(); |
| 313 |
} |
| 314 |
|
| 315 |
void OrthoUtils::print_vector(vector<string> vec) { |
| 316 |
for (vector<string>::iterator i = vec.begin(); i != vec.end(); i++) |
| 317 |
cout << *i << endl; |
| 318 |
} |
| 319 |
|
| 320 |
void OrthoUtils::set_orthology_map(vector<string> pairs){ |
| 321 |
orthology_map.clear(); |
| 322 |
for(vector<string>::iterator iter = pairs.begin(); iter != pairs.end(); iter++){ |
| 323 |
orthology_map.insert(pair<string, double> (*iter, 0.0)); |
| 324 |
} |
| 325 |
} |