]> git.donarmstrong.com Git - mothur.git/blob - treemap.cpp
added count.groups command and paralellized align.seqs for windows
[mothur.git] / treemap.cpp
1 /*
2  *  treemap.cpp
3  *  Mothur
4  *
5  *  Created by Sarah Westcott on 1/26/09.
6  *  Copyright 2009 Schloss Lab UMASS Amherst. All rights reserved.
7  *
8  */
9
10 #include "treemap.h"
11
12 /************************************************************/
13
14  TreeMap::TreeMap(string filename) {
15         m = MothurOut::getInstance();
16         groupFileName = filename;
17         m->openInputFile(filename, fileHandle);
18 }
19
20 /************************************************************/
21  TreeMap::~TreeMap(){}
22
23 /************************************************************/
24 int TreeMap::readMap() {
25                 string seqName, seqGroup;
26                 int error = 0;
27                 
28                 while(fileHandle){
29                         fileHandle >> seqName;          //read from first column
30                         fileHandle >> seqGroup;                 //read from second column
31                         
32                         if (m->control_pressed) {  fileHandle.close();  return 1; }
33                         
34                         setNamesOfGroups(seqGroup);
35                                         
36                         map<string, GroupIndex>::iterator itCheck = treemap.find(seqName);
37                         if (itCheck != treemap.end()) { error = 1; m->mothurOut("[WARNING]: Your groupfile contains more than 1 sequence named " + seqName + ", sequence names must be unique. Please correct."); m->mothurOutEndLine();  }
38                         else {
39                                 namesOfSeqs.push_back(seqName);
40                                 treemap[seqName].groupname = seqGroup;  //store data in map
41                                 
42                                 it2 = seqsPerGroup.find(seqGroup);
43                                 if (it2 == seqsPerGroup.end()) { //if it's a new group
44                                         seqsPerGroup[seqGroup] = 1;
45                                 }else {//it's a group we already have
46                                         seqsPerGroup[seqGroup]++;
47                                 }                               
48                         }
49                         
50                         m->gobble(fileHandle);
51                 }
52                 fileHandle.close();
53         
54
55                 return error;
56 }
57 /************************************************************/
58 void TreeMap::addSeq(string seqName, string seqGroup) {
59         
60                 namesOfSeqs.push_back(seqName);
61                 setNamesOfGroups(seqGroup);
62                                         
63                 treemap[seqName].groupname = seqGroup;  //store data in map
64                         
65                 it2 = seqsPerGroup.find(seqGroup);
66                 if (it2 == seqsPerGroup.end()) { //if it's a new group
67                         seqsPerGroup[seqGroup] = 1;
68                 }else {//it's a group we already have
69                         seqsPerGroup[seqGroup]++;
70                 }
71 }
72 /************************************************************/
73 void TreeMap::removeSeq(string seqName) {
74         
75         //erase name from namesOfSeqs
76         for (int i = 0; i < namesOfSeqs.size(); i++) {
77                 if (namesOfSeqs[i] == seqName)  {
78                         namesOfSeqs.erase(namesOfSeqs.begin()+i);
79                         break;
80                 }
81         }
82         
83         //decrement sequences in this group
84         string group = treemap[seqName].groupname;
85         seqsPerGroup[group]--;
86         
87         //remove seq from treemap
88         it = treemap.find(seqName);
89         treemap.erase(it);
90 }
91 /************************************************************/
92
93 int TreeMap::getNumGroups() {
94                         
95         return namesOfGroups.size();    
96                 
97 }
98 /************************************************************/
99
100 int TreeMap::getNumSeqs() {
101                         
102         return namesOfSeqs.size();      
103                 
104 }
105
106 /************************************************************/
107
108 string TreeMap::getGroup(string sequenceName) {
109                         
110         it = treemap.find(sequenceName);
111         if (it != treemap.end()) { //sequence name was in group file
112                 return it->second.groupname;    
113         }else {
114                 return "not found";
115         }
116                 
117 }
118 /************************************************************/
119 void TreeMap::setIndex(string seq, int index) {
120         it = treemap.find(seq);
121         if (it != treemap.end()) { //sequence name was in group file
122                 treemap[seq].vectorIndex = index;       
123         }else {
124                 treemap[seq].vectorIndex = index;
125                 treemap[seq].groupname = "not found";
126         }
127 }
128 /************************************************************/
129 int TreeMap::getIndex(string seq) {
130         
131         it = treemap.find(seq);
132         // if it is a valid sequence name then return index
133         if (it != treemap.end()) { return treemap[seq].vectorIndex; }
134         // if not return error code
135         else { return -1; }
136         
137 }
138 /************************************************************/
139
140 void TreeMap::setNamesOfGroups(string seqGroup) {
141                         int i, count;
142                         count = 0;
143                         for (i=0; i<namesOfGroups.size(); i++) {
144                                 if (namesOfGroups[i] != seqGroup) {
145                                         count++; //you have not found this group
146                                 }else {
147                                         break; //you already have it
148                                 }
149                         }
150                         if (count == namesOfGroups.size()) {
151                                 namesOfGroups.push_back(seqGroup); //new group
152                         }
153 }
154 /************************************************************/
155 bool TreeMap::isValidGroup(string groupname) {
156         try {
157                 for (int i = 0; i < namesOfGroups.size(); i++) {
158                         if (groupname == namesOfGroups[i]) { return true; }
159                 }
160                 
161                 return false;
162         }
163         catch(exception& e) {
164                 m->errorOut(e, "TreeMap", "isValidGroup");
165                 exit(1);
166         }
167 }
168 /***********************************************************************/
169
170 void TreeMap::print(ostream& output){
171         try {
172                 
173                 for(it = treemap.begin(); it != treemap.end(); it++){
174                         output << it->first << '\t' << it->second.groupname << '\t' << it->second.vectorIndex << endl;
175                 }
176         }
177         catch(exception& e) {
178                 m->errorOut(e, "TreeMap", "print");
179                 exit(1);
180         }
181 }
182
183 /************************************************************/
184 void TreeMap::makeSim(vector<string> ThisnamesOfGroups) {
185         try {
186                 //set names of groups
187                 namesOfGroups = ThisnamesOfGroups;
188                 
189                 //set names of seqs to names of groups
190                 namesOfSeqs = ThisnamesOfGroups;
191                 
192                 // make map where key and value are both the group name since that what the tree.shared command wants
193                 for (int i = 0; i < namesOfGroups.size(); i++) {
194                         treemap[namesOfGroups[i]].groupname = namesOfGroups[i];
195                         seqsPerGroup[namesOfGroups[i]] = 1;
196                 }
197                 
198                 numGroups = namesOfGroups.size();
199                 
200         }
201         catch(exception& e) {
202                 m->errorOut(e, "TreeMap", "makeSim");
203                 exit(1);
204         }
205 }
206 /************************************************************/
207 void TreeMap::makeSim(ListVector* list) {
208         try {
209                 //set names of groups
210                 namesOfGroups.clear();
211                 for(int i = 0; i < list->size(); i++) {
212                         namesOfGroups.push_back(list->get(i));
213                 }
214                 
215                 //set names of seqs to names of groups
216                 namesOfSeqs = namesOfGroups;
217                 
218                 // make map where key and value are both the group name since that what the tree.shared command wants
219                 for (int i = 0; i < namesOfGroups.size(); i++) {
220                         treemap[namesOfGroups[i]].groupname = namesOfGroups[i];
221                         seqsPerGroup[namesOfGroups[i]] = 1;
222                 }
223                 
224                 numGroups = namesOfGroups.size();
225                 
226         }
227         catch(exception& e) {
228                 m->errorOut(e, "TreeMap", "makeSim");
229                 exit(1);
230         }
231 }
232
233 /************************************************************/
234