Debian Codegraph @

src/cxx/tools/orthoFromOldPrIME/test_GuestTreeModel.cc

raw
1 #include <fstream>
2
3 #include "AnError.hh"
4 #include "BirthDeathProbs.hh"
5 #include "BranchSwapping.hh"
6 #include "GuestTreeModel.hh"
7 #include "LambdaMap.hh"
8 #include "StrStrMap.hh"
9 #include "TreeIO.hh"
10
11
12 using namespace beep;
13 using namespace std;
14
15 std::vector<Node*> getDescendentNodes(Node* n);
16 std::vector<Node*> getDescendentNodeRecursive(Node* n);
17 void rescale_specie_tree(Tree *S);
18 bool isObligateDuplication(Node* node);
19 void populateGsMap(Tree gtree, StrStrMap &map);
20 std::vector<std::string> &split(const std::string &s, char delim, std::vector<std::string> &elems);
21 std::vector<std::string> split(const std::string &s, char delim);
22
23 int
24 main(int argc, char** argv) {
25
26 if (argc < 4 || argc > 6) {
27 cerr << "usage: test_GuestTreeModel <Guesttree> <HostTree> <birthrate> <deathrate> [<toptime>]\n";
28 exit(1);
29 }
30
31 //Get tree and Data
32 //---------------------------------------------
33 string guest(argv[1]);
34 TreeIO io = TreeIO::fromFile(guest);
35 //StrStrMap gs;
36 StrStrMap *gs = new StrStrMap();
37
38 Tree G = io.readBeepTree(0, gs);
39 G.setName("G");
40
41 string host(argv[2]);
42 TreeIO io2 = TreeIO::fromFile(host);
43 Tree S = io2.readHostTree(); // Reads times?
44 S.setName("S");
45 S.rescale_specie_tree();
46
47 cout << S << endl;
48
49 char filename[800] = {0};
50 strcat(filename, guest.data());
51 strcat(filename, ".gemcomputed");
52 ofstream outfile;
53 outfile.open(filename);
54
55 string gtname = "";
56
57 size_t pos = guest.find_last_of("/");
58 if (pos != std::string::npos)
59 gtname.assign(guest.begin() + pos + 1, guest.end());
60 cout << "Input gene tree name is " << gtname << endl;
61
62 ostringstream gsfile;
63 gsfile << argv[2] << "_" << gtname << ".gs" << endl;
64 string gsfilename = gsfile.str();
65
66 // trim any spaces in start or end of the filepath
67 gsfilename.erase(gsfilename.find_last_not_of(" \n\r\t") + 1);
68
69 //if (gs.size() == 0) {
70 // gs = TreeIO::readGeneSpeciesInfo(gsfilename);
71 //}
72 populateGsMap(G, *gs);
73
74 LambdaMap lambda(G, S, *gs);
75
76 Real brate = atof(argv[3]);
77 Real drate = atof(argv[4]);
78 Real topTime = 1.0; //atof(argv[5]);
79 if (argc == 6)
80 topTime = atof(argv[5]);
81 BirthDeathProbs bdp(S, brate, drate, &topTime);
82
83 GuestTreeModel a(G, *gs, bdp);
84
85 Probability prG = a.calculateDataProbability();
86 prG = 0;
87 ProbVector prOrtho(G);
88 GuestTreeModel g(a);
89
90 prG += g.calculateDataProbability();
91 for (unsigned i = 0; i < G.getNumberOfNodes(); i++) {
92 Node* u = G.getNode(i);
93 if (u->isLeaf() == false) { // && lambda[u]->isLeaf() == false) {
94 a.setOrthoNode(u);
95 prOrtho[u] += a.calculateDataProbability();
96 }
97 }
98
99 for (unsigned i = 0; i < G.getNumberOfNodes(); i++) {
100 Node* u = G.getNode(i);
101 if (!isObligateDuplication(u)) { //&& lambda[u]->isLeaf() == false) {
102 vector<Node*> lnodes = getDescendentNodes(u->getLeftChild());
103 vector<Node*> rnodes = getDescendentNodes(u->getRightChild());
104 ostringstream os;
105 os << "[" << lnodes[0]->getName();
106 for (unsigned i = 1; i < lnodes.size(); i++)
107 os << " " << lnodes[i]->getName();
108 os << ",";
109 for (unsigned i = 0; i < rnodes.size(); i++)
110 os << " " << rnodes[i]->getName();
111 os << "]";
112 //cout << os.str() << endl;
113 outfile << os.str() << "\t" << (prOrtho[u] / prG).val() << endl;
114 cout << os.str() << "\t" << (prOrtho[u] / prG).val() << endl;
115 }
116 }
117
118 outfile.close();
119 cout << "Done..." << endl;
120 cout << "Computed orthologies are written to " << filename << endl;
121
122 return (0);
123 }
124
125 bool isObligateDuplication(Node* node) {
126 if(node->isLeaf())
127 return true;
128 vector<Node*> leafNodes = getDescendentNodes(node);
129 string firstLeaf;
130 for (int i = 0; i < leafNodes.size(); i++) {
131 std::vector<std::string> elems = split(leafNodes[i]->getName(), '_');
132 if (i == 0)
133 firstLeaf = elems[1];
134 else {
135 if (firstLeaf != elems[1])
136 return false;
137 }
138 }
139 return true;
140 }
141
142 std::vector<Node*> getDescendentNodes(Node* n) {
143 return getDescendentNodeRecursive(n);
144 }
145
146 std::vector<Node*> getDescendentNodeRecursive(Node* n) {
147 std::vector<Node*> nodes; // = new std::vector<Node*>();
148 if (n->isLeaf()) {
149 nodes.push_back(n);
150 return nodes;
151 } else {
152 std::vector<Node*> lnodes = getDescendentNodeRecursive(n->getLeftChild());
153 std::vector<Node*> rnodes = getDescendentNodeRecursive(n->getRightChild());
154 lnodes.insert(lnodes.end(), rnodes.begin(), rnodes.end());
155 return lnodes;
156 }
157 }
158
159 /**
160 *
161 * @param gtree Gene Tree
162 * @param map Map object to be populated
163 */
164 void populateGsMap(Tree gtree, StrStrMap &map) {
165 std::vector<Node*> nodes = gtree.getAllNodes();
166
167 map.clearMap();
168 for (int i = 0; i < gtree.getNumberOfNodes(); i++) {
169 if (nodes[i]->isLeaf() && nodes[i] != NULL) {
170 std::vector<std::string> elems = split(nodes[i]->getName(), '_');
171 //cout << nodes[i]->getName() << " , " << elems[1] << endl;
172 map.insert(nodes[i]->getName(), elems[1]);
173 }
174 }
175 }
176
177 std::vector<std::string> &split(const std::string &s, char delim, std::vector<std::string> &elems) {
178 std::stringstream ss(s);
179 std::string item;
180 while (std::getline(ss, item, delim)) {
181 elems.push_back(item);
182 }
183 return elems;
184 }
185
186 std::vector<std::string> split(const std::string &s, char delim) {
187 std::vector<std::string> elems;
188 return split(s, delim, elems);
189 }
190
191 /**
192 * rescale_specie_tree
193 * Rescales a species tree to [0,1].
194 *
195 * @param S The specie tree
196 */
197 void rescale_specie_tree(Tree *S) {
198 Real sc = S->rootToLeafTime();
199 beep::RealVector* tms = new beep::RealVector(S->getTimes());
200 for (beep::RealVector::iterator it = tms->begin(); it != tms->end(); ++it) {
201 (*it) /= sc;
202 }
203 S->setTopTime(S->getTopTime() / sc);
204 S->setTimes(*tms, true);
205 cout << "Specie tree root-to-leaf span was rescaled from " << sc << " to 1.0.\n";
206 }