5 // Created by Sarah Westcott on 4/11/12.
6 // Copyright (c) 2012 Schloss Lab. All rights reserved.
9 #include "treereader.h"
12 /***********************************************************************/
14 TreeReader::TreeReader(string tf) : treefile(tf) {
16 m = MothurOut::getInstance();
22 m->errorOut(e, "TreeReader", "TreeReader");
26 /***********************************************************************/
28 TreeReader::TreeReader(string tf, string gf) : treefile(tf), groupfile(gf) {
30 m = MothurOut::getInstance();
35 m->errorOut(e, "TreeReader", "TreeReader");
39 /***********************************************************************/
40 TreeReader::TreeReader(string tf, string gf, string nf) : treefile(tf), groupfile(gf), namefile(nf) {
42 m = MothurOut::getInstance();
46 m->errorOut(e, "TreeReader", "TreeReader");
50 /***********************************************************************/
51 bool TreeReader::readTrees() {
55 if (groupfile != "") { tmap->readMap(groupfile); }
56 else{ //fake out by putting everyone in one group
57 Tree* tree = new Tree(treefile); delete tree; //extracts names from tree to make faked out groupmap
58 for (int i = 0; i < m->Treenames.size(); i++) { tmap->addSeq(m->Treenames[i], "Group1"); }
61 int numUniquesInName = 0;
62 if (namefile != "") { numUniquesInName = readNamesFile(); }
64 ReadTree* read = new ReadNewickTree(treefile);
65 int readOk = read->read(tmap);
67 if (readOk != 0) { m->mothurOut("Read Terminated."); m->mothurOutEndLine(); delete read; m->control_pressed=true; return 0; }
69 read->AssembleTrees(names);
70 trees = read->getTrees();
73 //make sure all files match
74 //if you provide a namefile we will use the numNames in the namefile as long as the number of unique match the tree names size.
77 if (numUniquesInName == m->Treenames.size()) { numNamesInTree = nameMap.size(); }
78 else { numNamesInTree = m->Treenames.size(); }
79 }else { numNamesInTree = m->Treenames.size(); }
82 //output any names that are in group file but not in tree
83 if (numNamesInTree < tmap->getNumSeqs()) {
84 for (int i = 0; i < tmap->namesOfSeqs.size(); i++) {
85 //is that name in the tree?
87 for (int j = 0; j < m->Treenames.size(); j++) {
88 if (tmap->namesOfSeqs[i] == m->Treenames[j]) { break; } //found it
92 if (m->control_pressed) { for (int i = 0; i < trees.size(); i++) { delete trees[i]; } return 0; }
94 //then you did not find it so report it
95 if (count == m->Treenames.size()) {
96 //if it is in your namefile then don't remove
97 map<string, string>::iterator it = nameMap.find(tmap->namesOfSeqs[i]);
99 if (it == nameMap.end()) {
100 m->mothurOut(tmap->namesOfSeqs[i] + " is in your groupfile and not in your tree. It will be disregarded."); m->mothurOutEndLine();
101 tmap->removeSeq(tmap->namesOfSeqs[i]);
102 i--; //need this because removeSeq removes name from namesOfSeqs
110 catch(exception& e) {
111 m->errorOut(e, "TreeReader", "readTrees");
115 /*****************************************************************/
116 int TreeReader::readNamesFile() {
120 int numUniquesInName = 0;
123 m->openInputFile(namefile, in);
125 string first, second;
126 map<string, string>::iterator itNames;
129 in >> first >> second; m->gobble(in);
133 itNames = nameMap.find(first);
134 if (itNames == nameMap.end()) {
135 names[first] = second;
137 //we need a list of names in your namefile to use above when removing extra seqs above so we don't remove them
138 vector<string> dupNames;
139 m->splitAtComma(second, dupNames);
141 for (int i = 0; i < dupNames.size(); i++) {
142 nameMap[dupNames[i]] = first;
143 if ((groupfile == "") && (i != 0)) { tmap->addSeq(dupNames[i], "Group1"); }
145 }else { m->mothurOut(first + " has already been seen in namefile, disregarding names file."); m->mothurOutEndLine(); in.close(); nameMap.clear(); names.clear(); namefile = ""; return 1; }
149 return numUniquesInName;
151 catch(exception& e) {
152 m->errorOut(e, "TreeReader", "readNamesFile");
156 /***********************************************************************/