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