]> git.donarmstrong.com Git - mothur.git/blob - sharedlistvector.cpp
Merge branch 'master' of https://github.com/mothur/mothur.git into development
[mothur.git] / sharedlistvector.cpp
1 /*
2  *  sharedSharedListVector.cpp
3  *  Mothur
4  *
5  *  Created by Sarah Westcott on 1/22/09.
6  *  Copyright 2009 Schloss Lab UMASS Amherst. All rights reserved.
7  *
8  */
9
10 #include "sabundvector.hpp"
11 #include "rabundvector.hpp"
12 #include "ordervector.hpp"
13 #include "sharedlistvector.h"
14 #include "sharedordervector.h"
15 #include "sharedutilities.h"
16
17 /***********************************************************************/
18
19 SharedListVector::SharedListVector() : DataVector(), maxRank(0), numBins(0), numSeqs(0){ groupmap = NULL; countTable = NULL; }
20
21 /***********************************************************************/
22
23 SharedListVector::SharedListVector(int n):      DataVector(), data(n, "") , maxRank(0), numBins(0), numSeqs(0){ groupmap = NULL; countTable = NULL; }
24
25 /***********************************************************************/
26 SharedListVector::SharedListVector(ifstream& f) : DataVector(), maxRank(0), numBins(0), numSeqs(0) {
27         try {
28                 //set up groupmap for later.
29         if (m->groupMode == "group") {
30             groupmap = new GroupMap(m->getGroupFile());
31             groupmap->readMap(); 
32         }else {
33             countTable = new CountTable();
34             countTable->readTable(m->getCountTableFile());
35         }
36
37                 int hold;
38                 string inputData;
39                 f >> label >> hold;
40         
41                 data.assign(hold, "");
42                 
43                 for(int i=0;i<hold;i++){
44                         f >> inputData;
45                         set(i, inputData);
46                 }
47                 
48         }
49         catch(exception& e) {
50                 m->errorOut(e, "SharedListVector", "SharedListVector");
51                 exit(1);
52         }
53 }
54
55 /***********************************************************************/
56 void SharedListVector::set(int binNumber, string seqNames){
57         try {
58                 int nNames_old = m->getNumNames(data[binNumber]);
59                 data[binNumber] = seqNames;
60                 int nNames_new = m->getNumNames(seqNames);
61         
62                 if(nNames_old == 0)                     {       numBins++;                              }
63                 if(nNames_new == 0)                     {       numBins--;                              }
64                 if(nNames_new > maxRank)        {       maxRank = nNames_new;   }
65         
66                 numSeqs += (nNames_new - nNames_old);
67                 
68                          
69         }
70         catch(exception& e) {
71                 m->errorOut(e, "SharedListVector", "set");
72                 exit(1);
73         }
74 }
75
76 /***********************************************************************/
77
78 string SharedListVector::get(int index){
79         return data[index];
80 }
81
82 /***********************************************************************/
83
84 void SharedListVector::push_back(string seqNames){
85         try {
86                 data.push_back(seqNames);
87                 int nNames = m->getNumNames(seqNames);
88         
89                 numBins++;
90         
91                 if(nNames > maxRank)    {       maxRank = nNames;       }
92         
93                 numSeqs += nNames;
94         }
95         catch(exception& e) {
96                 m->errorOut(e, "SharedListVector", "push_back");
97                 exit(1);
98         }
99 }
100
101 /***********************************************************************/
102
103 void SharedListVector::resize(int size){
104         data.resize(size);              
105 }
106
107 /***********************************************************************/
108
109 int SharedListVector::size(){
110         return data.size();
111 }
112 /***********************************************************************/
113
114 void SharedListVector::clear(){
115         numBins = 0;
116         maxRank = 0;
117         numSeqs = 0;
118         return data.clear();
119         
120 }
121
122 /***********************************************************************/
123
124 void SharedListVector::print(ostream& output){
125         try {
126                 output << label << '\t' << numBins << '\t';
127         
128                 for(int i=0;i<data.size();i++){
129                         if(data[i] != ""){
130                                 output << data[i] << '\t';
131                         }
132                 }
133                 output << endl;
134         }
135         catch(exception& e) {
136                 m->errorOut(e, "SharedListVector", "print");
137                 exit(1);
138         }
139 }
140
141
142 /***********************************************************************/
143
144 RAbundVector SharedListVector::getRAbundVector(){
145         try {
146                 RAbundVector rav;
147         
148                 for(int i=0;i<data.size();i++){
149                         int binSize = m->getNumNames(data[i]);
150                         rav.push_back(binSize);
151                 }
152         
153         //  This was here before to output data in a nice format, but it screws up the name mapping steps
154         //      sort(rav.rbegin(), rav.rend());
155         //      
156         //      for(int i=data.size()-1;i>=0;i--){
157         //              if(rav.get(i) == 0){    rav.pop_back(); }
158         //              else{
159         //                      break;
160         //              }
161         //      }
162                 rav.setLabel(label);
163         
164                 return rav;
165         }
166         catch(exception& e) {
167                 m->errorOut(e, "SharedListVector", "getRAbundVector");
168                 exit(1);
169         }
170 }
171
172 /***********************************************************************/
173
174 SAbundVector SharedListVector::getSAbundVector(){
175         try {
176                 SAbundVector sav(maxRank+1);
177         
178                 for(int i=0;i<data.size();i++){
179                         int binSize = m->getNumNames(data[i]);  
180                         sav.set(binSize, sav.get(binSize) + 1); 
181                 }
182                 sav.set(0, 0);
183                 sav.setLabel(label);
184         
185                 return sav;
186         }
187         catch(exception& e) {
188                 m->errorOut(e, "SharedListVector", "getSAbundVector");
189                 exit(1);
190         }
191 }
192
193 /***********************************************************************/
194 SharedOrderVector* SharedListVector::getSharedOrderVector(){
195         try {
196                 SharedOrderVector* order = new SharedOrderVector();
197                 order->setLabel(label);
198         
199                 for(int i=0;i<numBins;i++){
200                         int binSize = m->getNumNames(get(i));   //find number of individual in given bin        
201                         string names = get(i);
202             vector<string> binNames;
203             m->splitAtComma(names, binNames);
204             if (m->groupMode != "group") {
205                 binSize = 0;
206                 for (int j = 0; j < binNames.size(); j++) {  binSize += countTable->getNumSeqs(binNames[i]);  }
207             }
208                         for (int j = 0; j < binNames.size(); j++) { 
209                 if (m->control_pressed) { return order; }
210                 if (m->groupMode == "group") {
211                     string groupName = groupmap->getGroup(binNames[i]);
212                     if(groupName == "not found") {      m->mothurOut("Error: Sequence '" + binNames[i] + "' was not found in the group file, please correct."); m->mothurOutEndLine();  exit(1); }
213                                 
214                     order->push_back(i, binSize, groupName);  //i represents what bin you are in
215                 }else {
216                     vector<int> groupAbundances = countTable->getGroupCounts(binNames[i]);
217                     vector<string> groupNames = countTable->getNamesOfGroups();
218                     for (int k = 0; k < groupAbundances.size(); k++) { //groupAbundances.size() == 0 if there is a file mismatch and m->control_pressed is true.
219                         if (m->control_pressed) { return order; }
220                         for (int l = 0; l < groupAbundances[k]; l++) {  order->push_back(i, binSize, groupNames[k]);  }
221                     }
222                 }
223                         }
224                 }
225
226                 random_shuffle(order->begin(), order->end());
227                 order->updateStats();
228                 
229                 return order;
230         }
231         catch(exception& e) {
232                 m->errorOut(e, "SharedListVector", "getSharedOrderVector");
233                 exit(1);
234         }
235 }
236 /***********************************************************************/
237 SharedRAbundVector SharedListVector::getSharedRAbundVector(string groupName) {
238         try {
239                 SharedRAbundVector rav(data.size());
240                 
241                 for(int i=0;i<numBins;i++){
242                         string names = get(i);
243             vector<string> binNames;
244             m->splitAtComma(names, binNames);
245             for (int j = 0; j < binNames.size(); j++) { 
246                                 if (m->control_pressed) { return rav; }
247                 if (m->groupMode == "group") {
248                     string group = groupmap->getGroup(binNames[j]);
249                     if(group == "not found") {  m->mothurOut("Error: Sequence '" + binNames[j] + "' was not found in the group file, please correct."); m->mothurOutEndLine();  exit(1); }
250                     if (group == groupName) { //this name is in the group you want the vector for.
251                         rav.set(i, rav.getAbundance(i) + 1, group);  //i represents what bin you are in
252                     }
253                 }else {
254                     int count = countTable->getGroupCount(binNames[j], groupName);
255                     rav.set(i, rav.getAbundance(i) + count, groupName);
256                 }
257                         }
258                 }
259                 
260                 rav.setLabel(label);
261                 rav.setGroup(groupName);
262
263                 return rav;
264                 
265         }
266         catch(exception& e) {
267                 m->errorOut(e, "SharedListVector", "getSharedRAbundVector");
268                 exit(1);
269         }
270 }
271 /***********************************************************************/
272 vector<SharedRAbundVector*> SharedListVector::getSharedRAbundVector() {
273         try {
274                 SharedUtil* util;
275                 util = new SharedUtil();
276                 vector<SharedRAbundVector*> lookup;  //contains just the groups the user selected
277         vector<SharedRAbundVector*> lookupDelete;
278                 map<string, SharedRAbundVector*> finder;  //contains all groups in groupmap
279                 
280                 vector<string> Groups = m->getGroups();
281         vector<string> allGroups;
282                 if (m->groupMode == "group") {  allGroups = groupmap->getNamesOfGroups();  }
283         else {  allGroups = countTable->getNamesOfGroups();  }
284                 util->setGroups(Groups, allGroups);
285                 m->setGroups(Groups);
286                 delete util;
287
288                 for (int i = 0; i < allGroups.size(); i++) {
289                         SharedRAbundVector* temp = new SharedRAbundVector(data.size());
290                         finder[allGroups[i]] = temp;
291                         finder[allGroups[i]]->setLabel(label);
292                         finder[allGroups[i]]->setGroup(allGroups[i]);
293                         if (m->inUsersGroups(allGroups[i], m->getGroups())) {  //if this group is in user groups
294                                 lookup.push_back(finder[allGroups[i]]);
295                         }else {
296                 lookupDelete.push_back(finder[allGroups[i]]);
297             }
298                 }
299         
300                 //fill vectors
301                 for(int i=0;i<numBins;i++){
302                         string names = get(i);  
303                         vector<string> binNames;
304             m->splitAtComma(names, binNames);
305             for (int j = 0; j < binNames.size(); j++) { 
306                 if (m->groupMode == "group") {
307                     string group = groupmap->getGroup(binNames[j]);
308                     if(group == "not found") {  m->mothurOut("Error: Sequence '" + binNames[j] + "' was not found in the group file, please correct."); m->mothurOutEndLine();  exit(1); }
309                     finder[group]->set(i, finder[group]->getAbundance(i) + 1, group);  //i represents what bin you are in       
310                 }else{
311                     vector<int> counts = countTable->getGroupCounts(binNames[j]);
312                     for (int k = 0; k < allGroups.size(); k++) {
313                         finder[allGroups[k]]->set(i, finder[allGroups[k]]->getAbundance(i) + counts[k], allGroups[k]);
314                     }
315                 }
316                         }
317                 }
318         
319         for (int j = 0; j < lookupDelete.size(); j++) {  delete lookupDelete[j];  }
320
321                 return lookup;
322         }
323         catch(exception& e) {
324                 m->errorOut(e, "SharedListVector", "getSharedRAbundVector");
325                 exit(1);
326         }
327 }
328
329 /***********************************************************************/
330 SharedSAbundVector SharedListVector::getSharedSAbundVector(string groupName) {
331         try { 
332                 SharedSAbundVector sav;
333                 SharedRAbundVector rav;
334                 
335                 rav = this->getSharedRAbundVector(groupName);
336                 sav = rav.getSharedSAbundVector();
337                 
338                 return sav;
339         }
340         catch(exception& e) {
341                 m->errorOut(e, "SharedListVector", "getSharedSAbundVector");
342                 exit(1);
343         }
344 }
345 /***********************************************************************/
346
347 OrderVector SharedListVector::getOrderVector(map<string,int>* orderMap = NULL){
348         
349         try {
350                 if(orderMap == NULL){
351                         OrderVector ov;
352                 
353                         for(int i=0;i<data.size();i++){
354                 string names = data[i];
355                 vector<string> binNames;
356                 m->splitAtComma(names, binNames);
357                                 int binSize = binNames.size();  
358                 if (m->groupMode != "group") {
359                     binSize = 0;
360                     for (int j = 0; j < binNames.size(); j++) {  binSize += countTable->getNumSeqs(binNames[i]);  }
361                 }
362                                 for(int j=0;j<binSize;j++){
363                                         ov.push_back(i);
364                                 }
365                         }
366                         random_shuffle(ov.begin(), ov.end());
367                         ov.setLabel(label);
368                         ov.getNumBins();
369                 
370                         return ov;
371                 
372                 }
373                 else{
374                         OrderVector ov(numSeqs);
375                 
376                         for(int i=0;i<data.size();i++){
377                                 string listOTU = data[i];
378                                 vector<string> binNames;
379                 m->splitAtComma(listOTU, binNames);
380                 for (int j = 0; j < binNames.size(); j++) { 
381                     if(orderMap->count(binNames[j]) == 0){
382                         m->mothurOut(binNames[j] + " not found, check *.names file\n");
383                         exit(1);
384                     }
385                     ov.set((*orderMap)[binNames[j]], i);
386                                 }
387                         }
388                 
389                         ov.setLabel(label);
390                         ov.getNumBins();
391                 
392                         return ov;              
393                 }
394         }
395         catch(exception& e) {
396                 m->errorOut(e, "SharedListVector", "getOrderVector");
397                 exit(1);
398         }
399 }
400
401 /***********************************************************************/
402