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