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