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