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