File: treereader.cpp

package info (click to toggle)
mothur 1.48.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 13,692 kB
  • sloc: cpp: 161,866; makefile: 122; sh: 31
file content (129 lines) | stat: -rwxr-xr-x 4,330 bytes parent folder | download | duplicates (3)
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
//
//  treereader.cpp
//  Mothur
//
//  Created by Sarah Westcott on 4/11/12.
//  Copyright (c) 2012 Schloss Lab. All rights reserved.
//

#include "treereader.h"
#include "readtree.h"
#include "groupmap.h"

/***********************************************************************/
TreeReader::TreeReader(string tf, string cf) : treefile(tf), countfile(cf)  { 
    try {
        m = MothurOut::getInstance();
        ct = new CountTable();
        ct->readTable(cf, true, false);
        Utils util;
        Treenames = util.parseTreeFile(treefile); //fills treenames
        
        //if no groupinfo in count file we need to add it
        if (!ct->hasGroupInfo()) {
            ct->addGroup("Group1");
            vector<string> namesOfSeqs = ct->getNamesOfSeqs();
            for (int i = 0; i < namesOfSeqs.size(); i++) { 
                ct->setAbund(namesOfSeqs[i], "Group1", ct->getNumSeqs(namesOfSeqs[i]));
            }
        }
        namefile = "";
        groupfile = "";
        
        readTrees();
    }
	catch(exception& e) {
		m->errorOut(e, "TreeReader", "TreeReader");
		exit(1);
	}
}
/***********************************************************************/
TreeReader::TreeReader(string tf, string gf, string nf) : treefile(tf),  groupfile(gf), namefile(nf)  { 
    try {
        m = MothurOut::getInstance();
        Utils util;
        Treenames = util.parseTreeFile(treefile); //fills treenames
        countfile = "";
        ct = new CountTable();
        if (namefile != "") { ct->createTable(namefile, groupfile, nullVector, true); }
        else {
            set<string> nameMap;
            map<string, string> groupMap;
            set<string> gps;
            for (int i = 0; i < Treenames.size(); i++) { nameMap.insert(Treenames[i]);  }
            if (groupfile == "") { gps.insert("Group1"); for (int i = 0; i < Treenames.size(); i++) { groupMap[Treenames[i]] = "Group1"; } }
            else {
                GroupMap g(groupfile); 
                g.readMap();
                vector<string> seqs = g.getNamesSeqs();
                for (int i = 0; i < seqs.size(); i++) {  
                    string group = g.getGroup(seqs[i]);
                    groupMap[seqs[i]] = group;
                    gps.insert(group);
                }
            }
            ct->createTable(nameMap, groupMap, gps);
        }

        readTrees();
    }
	catch(exception& e) {
		m->errorOut(e, "TreeReader", "TreeReader");
		exit(1);
	}
}
/***********************************************************************/
bool TreeReader::readTrees()  { 
    try {
        
        int numUniquesInName = ct->getNumUniqueSeqs();
		
		ReadTree* read = new ReadNewickTree(treefile, Treenames);
		int readOk = read->read(ct); 
		
		if (readOk != 0) { m->mothurOut("Read Terminated.\n");  delete read; m->setControl_pressed(true); return 0; }
		
		read->AssembleTrees();
		trees = read->getTrees();
		delete read;
        
		//make sure all files match
		//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.
		int numNamesInTree;
		if (namefile != "")  {  
			if (numUniquesInName == Treenames.size()) {  numNamesInTree = ct->getNumSeqs();  }
			else {   numNamesInTree = Treenames.size();  }
		}else {  numNamesInTree = Treenames.size();  }
		
		
		//output any names that are in group file but not in tree
		if (numNamesInTree < ct->getNumSeqs()) {
            vector<string> namesSeqsCt = ct->getNamesOfSeqs();
			for (int i = 0; i < namesSeqsCt.size(); i++) {
				//is that name in the tree?
				int count = 0;
				for (int j = 0; j < Treenames.size(); j++) {
					if (namesSeqsCt[i] == Treenames[j]) { break; } //found it
					count++;
				}
				
				if (m->getControl_pressed()) { for (int i = 0; i < trees.size(); i++) { delete trees[i]; } return 0; }
				
				//then you did not find it so report it 
				if (count == Treenames.size()) {
                    m->mothurOut(namesSeqsCt[i] + " is in your name or group file and not in your tree. It will be disregarded.\n");
                    ct->remove(namesSeqsCt[i]);
				}
			}
		}
        
        return true;
    }
	catch(exception& e) {
		m->errorOut(e, "TreeReader", "readTrees");
		exit(1);
	}
}
/***********************************************************************/