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 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399
|
/***************************************************************************
* Copyright (C) 2006 by BUI Quang Minh, Steffen Klaere, Arndt von Haeseler *
* minh.bui@univie.ac.at *
* *
* This program 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 2 of the License, or *
* (at your option) any later version. *
* *
* This program 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 this program; if not, write to the *
* Free Software Foundation, Inc., *
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
***************************************************************************/
#include "ncl/ncl.h"
#include "utils/tools.h"
#include "pdtree.h"
#include "nclextra/myreader.h"
/*********************************************
class PDTree
*********************************************/
PDTree::PDTree(Params ¶ms)
{
init(params);
}
void PDTree::init(Params ¶ms) {
MTree::init(params.user_file, params.is_rooted);
if (params.is_rooted) {
params.sub_size++;
params.min_size++;
}
if (params.is_rooted && params.root != NULL) {
outError(ERR_CONFLICT_ROOT);
}
if (params.sub_size > leafNum) {
ostringstream err;
err << "Subset size k = " << params.sub_size-params.is_rooted <<
" is greater than the number of taxa = " << leafNum-params.is_rooted;
outError(err.str());
}
if (params.is_rooted) {
initialset.push_back(root);
}
// read the parameter file
if (params.param_file != NULL) {
readParams(params);
}
// identify the root
if (params.root != NULL)
readRootNode(params.root);
// read the initial set of taxa, incoporate info into the split system
if (params.initial_file != NULL) {
readInitialSet(params);
}
}
/**
constructor
*/
PDTree::PDTree(PDTree &tree)
{
init(tree);
}
void PDTree::init(PDTree &tree) {
MTree::init(tree);
//subsize = tree.subsize;
initialset = tree.initialset;
}
void PDTree::buildLeafMapName(LeafMapName &lsn, Node *node, Node* dad) {
if (!node) node = root;
if (node->isLeaf()) {
if (lsn.find(node->name) != lsn.end())
outError(ERR_DUPLICATED_TAXA);
lsn[node->name] = node;
}
//for (NeighborVec::iterator it = node->neighbors.begin(); it != node->neighbors.end(); it++)
//if ((*it)->node != dad)
FOR_NEIGHBOR_IT(node, dad, it)
buildLeafMapName(lsn, (*it)->node, node);
}
/*
Node *PDTree::findNode(char *name, Node *node, Node *dad) {
if (!node) node = root;
// check the name if a leaf
if (node->isLeaf()) {
if (node->name == name)
return node;
}
// recursive search
//for (NeighborVec::iterator it = node->neighbors.begin(); it != node->neighbors.end(); it++)
//if ((*it)->node != dad) {
FOR_NEIGHBOR_IT(node, dad, it) {
Node *res = findNode(name, (*it)->node, node);
if (res != NULL)
return res;
}
return NULL;
}
*/
void PDTree::readRootNode(const char *root_name) {
string name = root_name;
Node *node = findNodeName(name);
if (node == NULL)
outError(ERR_NO_ROOT, root_name);
initialset.push_back(node);
}
/**
read the initial set of taxa to be included into PD-tree
*/
void PDTree::readInitialSet(Params ¶ms) {
LeafMapName lsn;
buildLeafMapName(lsn);
int ntaxa = leafNum - params.is_rooted;
StrVector tax_name;
readInitTaxaFile(params, ntaxa, tax_name);
for (StrVector::iterator it = tax_name.begin(); it != tax_name.end(); it++) {
LeafMapName::iterator nameit = lsn.find((*it));
if (nameit == lsn.end()) {
Node *node = findNodeName(*it);
if (!node)
cout << "Find no taxon with name " << *it << endl;
else {
Node *taxon;
int distance = findNearestTaxon(taxon, node);
cout << "Replace internal node " << node->name << " by taxon "
<< taxon->name << " (" << distance << " branches away)" << endl;
initialset.push_back(taxon);
}
} else
initialset.push_back((*nameit).second);
}
cout << initialset.size() - rooted << " initial taxa" << endl;
}
void PDTree::readParams(Params ¶ms) {
int ntaxa = leafNum - params.is_rooted;
// read parameters from file
double scale;
StrVector tax_name;
DoubleVector ori_weight, tax_weight;
readWeightFile(params, ntaxa, scale, tax_name, ori_weight);
// now convert the weights
LeafMapName lsn;
buildLeafMapName(lsn);
tax_weight.resize((unsigned long) ntaxa, 0);
for (int i = 0; i < tax_name.size(); i++) {
LeafMapName::iterator nameit = lsn.find(tax_name[i]);
if (nameit == lsn.end())
outError(ERR_NO_TAXON, tax_name[i]);
tax_weight[(*nameit).second->id] = ori_weight[i];
}
if (params.scaling_factor >= 0) {
if (params.scaling_factor > 1) outError("Scaling factor must be between 0 and 1");
cout << "Rescaling branch lengths with " << params.scaling_factor <<
" and taxa weights with " << 1 - params.scaling_factor << endl;
scale = params.scaling_factor;
for (DoubleVector::iterator it = tax_weight.begin(); it != tax_weight.end(); it++)
(*it) *= (1 - scale);
}
// incoporate them into the tree
incoporateParams(scale, tax_weight);
}
void PDTree::incoporateParams(double &scale, DoubleVector &tax_weight, Node* node, Node* dad) {
if (!node) node = root;
FOR_NEIGHBOR_DECLARE(node, NULL, it) {
double newlen;
newlen = (*it)->length * scale;
if (node->isLeaf())
newlen += tax_weight[node->id];
else if ((*it)->node->isLeaf())
newlen += tax_weight[(*it)->node->id];
(*it)->length = newlen;
}
FOR_NEIGHBOR(node, dad, it)
incoporateParams(scale, tax_weight, (*it)->node, node);
}
void PDTree::computePD(Params ¶ms, vector<PDTaxaSet> &taxa_set, PDRelatedMeasures &pd_more) {
LeafMapName lsn;
buildLeafMapName(lsn);
MSetsBlock *sets;
TaxaSetNameVector *allsets;
sets = new MSetsBlock();
cout << "Reading taxa sets in file " << params.pdtaxa_file << "..." << endl;
bool nexus_formated = (detectInputFile(params.pdtaxa_file) == IN_NEXUS);
if (nexus_formated) {
MyReader nexus(params.pdtaxa_file);
nexus.Add(sets);
MyToken token(nexus.inf);
nexus.Execute(token);
} else {
readTaxaSets(params.pdtaxa_file, sets);
}
allsets = sets->getSets();
//sets->Report(cout);
taxa_set.resize((unsigned long) sets->getNSets());
vector<PDTaxaSet>::iterator it_ts;
TaxaSetNameVector::iterator i;
for (i = allsets->begin(), it_ts = taxa_set.begin(); i != allsets->end(); i++, it_ts++) {
set<string> taxa_name;
for (NodeVector::iterator it = initialset.begin(); it != initialset.end(); it++)
taxa_name.insert((*it)->name);
for (vector<string>::iterator it2 = (*i)->taxlist.begin(); it2 != (*i)->taxlist.end(); it2++) {
LeafMapName::iterator nameit = lsn.find(*it2);
if (nameit == lsn.end())
outError(ERR_NO_TAXON, *it2);
taxa_name.insert(*it2);
}
Split id_set;
makeTaxaSet(taxa_name, *it_ts);
(*it_ts).makeIDSet(leafNum, id_set);
if (params.exclusive_pd) {
calcExclusivePD(id_set);
pd_more.exclusivePD.push_back(id_set.getWeight());
}
calcPD(id_set);
(*it_ts).score = id_set.getWeight();
(*it_ts).name = (*i)->name;
pd_more.PDScore.push_back(id_set.getWeight());
pd_more.setName.push_back((*i)->name);
}
delete sets;
}
void PDTree::makeTaxaSet(set<string> &taxa_name, PDTaxaSet &taxa_set, Node *node, Node *dad) {
if (!node) node = root;
if (node->isLeaf() && taxa_name.find(node->name) != taxa_name.end()) {
taxa_set.push_back(node);
}
FOR_NEIGHBOR_IT(node, dad, it) {
makeTaxaSet(taxa_name, taxa_set, (*it)->node, node);
}
}
bool PDTree::calcPD(Split &id_set, double cur_len, Node *node, Node *dad) {
if (!node) {
node = root;
id_set.weight = 0.0;
if (!rooted && !id_set.containTaxon(node->id)) {
int id = id_set.firstTaxon();
if (id < 0) return false;
node = findNodeID(id);
}
}
bool resval = false;
if (node->isLeaf() && id_set.containTaxon(node->id)) {
id_set.weight += cur_len;
resval = true;
}
FOR_NEIGHBOR_IT(node, dad, it) {
if (calcPD(id_set, cur_len + (*it)->length, (*it)->node, node)) {
cur_len = 0.0;
resval = true;
}
}
return resval;
}
void PDTree::calcExclusivePD(Split &id_set) {
id_set.invert();
calcPD(id_set);
id_set.invert();
id_set.weight = treeLength() - id_set.weight;
}
void PDTree::calcPDEndemism(vector<PDTaxaSet> &area_set, DoubleVector &pd_endem) {
vector<Split> id_sets;
vector<Split>::iterator it_s;
vector<PDTaxaSet>::iterator it_a;
// convert taxa set to id set
id_sets.resize(area_set.size());
for (it_a = area_set.begin(), it_s = id_sets.begin(); it_a != area_set.end(); it_a++, it_s++)
(*it_a).makeIDSet(leafNum, *it_s);
// make union of all id_sets
Split id_union(leafNum);
for (it_s = id_sets.begin(); it_s != id_sets.end(); it_s++)
id_union += *it_s;
// calculate PD of union
calcPD(id_union);
// now calculate PD endemism
pd_endem.clear();
for (it_s = id_sets.begin(); it_s != id_sets.end(); it_s++) {
// make union of all other set
Split id_other(leafNum);
for (vector<Split>::iterator it_s2 = id_sets.begin(); it_s2 != id_sets.end(); it_s2++)
if (it_s2 != it_s) id_other += *it_s2;
// calculate PD of all other sets
calcPD(id_other);
// calc PD endemism
pd_endem.push_back(id_union.weight - id_other.weight);
}
}
void PDTree::calcPDComplementarity(vector<PDTaxaSet> &area_set, char *area_names, DoubleVector &pd_comp) {
set<string> given_areas;
parseAreaName(area_names, given_areas);
/*
for (set<string>::iterator it = given_areas.begin(); it != given_areas.end(); it++)
cout << (*it) << "!";
cout << endl;
*/
vector<Split> id_sets;
vector<Split>::iterator it_s;
vector<PDTaxaSet>::iterator it_a;
Split given_id(leafNum);
// convert taxa set to id set
id_sets.resize(area_set.size());
for (it_a = area_set.begin(), it_s = id_sets.begin(); it_a != area_set.end(); it_a++, it_s++) {
(*it_a).makeIDSet(leafNum, *it_s);
if (given_areas.find((*it_a).name) != given_areas.end())
given_id += *it_s;
}
if (given_id.countTaxa() == 0)
outError("Complementary area name(s) not correct");
calcPD(given_id);
// now calculate PD complementarity
pd_comp.clear();
for (it_s = id_sets.begin(); it_s != id_sets.end(); it_s++) {
// make union the two sets
Split id_both(*it_s);
id_both += given_id;
// calculate PD of both sets
calcPD(id_both);
// calc PD complementarity
pd_comp.push_back(id_both.weight - given_id.weight);
}
}
int PDTree::findNearestTaxon(Node* &taxon, Node *node, Node *dad) {
if (node->isLeaf()) {
taxon = node;
return 0;
}
int distance = 10000000;
taxon = NULL;
FOR_NEIGHBOR_IT(node, dad, it) {
Node *mytaxon;
int mydistance = findNearestTaxon(mytaxon, (*it)->node, node);
if (mydistance < distance) {
distance = mydistance;
taxon = mytaxon;
}
}
return distance+1;
}
|