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