]> git.donarmstrong.com Git - mothur.git/blob - sharedlistvector.cpp
added logfile feature
[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                                 order->push_back(i, binSize, groupName);  //i represents what bin you are in
204                         }
205                         //get last name
206                         groupName = groupmap->getGroup(names);
207                         order->push_back(i, binSize, groupName);
208                 }
209
210                 random_shuffle(order->begin(), order->end());
211                 order->updateStats();
212                 
213                 return order;
214         }
215         catch(exception& e) {
216                 errorOut(e, "SharedListVector", "getSharedOrderVector");
217                 exit(1);
218         }
219 }
220 /***********************************************************************/
221 SharedRAbundVector SharedListVector::getSharedRAbundVector(string groupName) {
222         try {
223                 SharedRAbundVector rav(data.size());
224                 string group, names, name;
225                 
226                 for(int i=0;i<numBins;i++){
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                                 group = groupmap->getGroup(name);
232                                 if (group == groupName) { //this name is in the group you want the vector for.
233                                         rav.set(i, rav.getAbundance(i) + 1, group);  //i represents what bin you are in
234                                 }
235                         }
236                         
237                         //get last name
238                         groupName = groupmap->getGroup(names);
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                 rav.setLabel(label);
245                 rav.setGroup(groupName);
246
247                 return rav;
248                 
249         }
250         catch(exception& e) {
251                 errorOut(e, "SharedListVector", "getSharedRAbundVector");
252                 exit(1);
253         }
254 }
255 /***********************************************************************/
256 vector<SharedRAbundVector*> SharedListVector::getSharedRAbundVector() {
257         try {
258                 SharedUtil* util;
259                 util = new SharedUtil();
260                 vector<SharedRAbundVector*> lookup;
261                 map<string, SharedRAbundVector*> finder;
262                 string group, names, name;
263                 
264                 util->setGroups(globaldata->Groups, globaldata->gGroupmap->namesOfGroups);
265                 
266                 delete util;
267
268                 for (int i = 0; i < globaldata->Groups.size(); i++) {
269                         SharedRAbundVector* temp = new SharedRAbundVector(data.size());
270                         finder[globaldata->Groups[i]] = temp;
271                         finder[globaldata->Groups[i]]->setLabel(label);
272                         finder[globaldata->Groups[i]]->setGroup(globaldata->Groups[i]);
273                         //*temp = getSharedRAbundVector(globaldata->Groups[i]);
274                         lookup.push_back(finder[globaldata->Groups[i]]);
275                 }
276                 
277                 //fill vectors
278                 for(int i=0;i<numBins;i++){
279                         names = get(i);  
280                         while (names.find_first_of(',') != -1) { 
281                                 name = names.substr(0,names.find_first_of(','));
282                                 names = names.substr(names.find_first_of(',')+1, names.length());
283                                 group = groupmap->getGroup(name);
284                                 finder[group]->set(i, finder[group]->getAbundance(i) + 1, group);  //i represents what bin you are in
285                         }
286                         
287                         //get last name
288                         group = groupmap->getGroup(names);
289                         finder[group]->set(i, finder[group]->getAbundance(i) + 1, group);  //i represents what bin you are in
290                         
291                 }
292                 
293                 return lookup;
294         }
295         catch(exception& e) {
296                 errorOut(e, "SharedListVector", "getSharedRAbundVector");
297                 exit(1);
298         }
299 }
300
301 /***********************************************************************/
302 SharedSAbundVector SharedListVector::getSharedSAbundVector(string groupName) {
303         try { 
304                 SharedSAbundVector sav;
305                 SharedRAbundVector rav;
306                 
307                 rav = this->getSharedRAbundVector(groupName);
308                 sav = rav.getSharedSAbundVector();
309                 
310                 return sav;
311         }
312         catch(exception& e) {
313                 errorOut(e, "SharedListVector", "getSharedSAbundVector");
314                 exit(1);
315         }
316 }
317 /***********************************************************************/
318
319 OrderVector SharedListVector::getOrderVector(map<string,int>* orderMap = NULL){
320         
321         try {
322                 if(orderMap == NULL){
323                         OrderVector ov;
324                 
325                         for(int i=0;i<data.size();i++){
326                                 int binSize = getNumNames(data[i]);             
327                                 for(int j=0;j<binSize;j++){
328                                         ov.push_back(i);
329                                 }
330                         }
331                         random_shuffle(ov.begin(), ov.end());
332                         ov.setLabel(label);
333                         ov.getNumBins();
334                 
335                         return ov;
336                 
337                 }
338                 else{
339                         OrderVector ov(numSeqs);
340                 
341                         for(int i=0;i<data.size();i++){
342                                 string listOTU = data[i];
343                                 int length = listOTU.size();
344                                 
345                                 string seqName="";
346                         
347                                 for(int j=0;j<length;j++){
348                                 
349                                         if(listOTU[j] != ','){
350                                                 seqName += listOTU[j];
351                                         }
352                                         else{
353                                                 if(orderMap->count(seqName) == 0){
354                                                         mothurOut(seqName + " not found, check *.names file\n");
355                                                         exit(1);
356                                                 }
357                                         
358                                                 ov.set((*orderMap)[seqName], i);
359                                                 seqName = "";
360                                         }                                               
361                                 }
362                         
363                                 if(orderMap->count(seqName) == 0){
364                                         mothurOut(seqName + " not found, check *.names file\n");
365                                         exit(1);
366                                 }
367                                 ov.set((*orderMap)[seqName], i);        
368                         }
369                 
370                         ov.setLabel(label);
371                         ov.getNumBins();
372                 
373                         return ov;              
374                 }
375         }
376         catch(exception& e) {
377                 errorOut(e, "SharedListVector", "getOrderVector");
378                 exit(1);
379         }
380 }
381
382 /***********************************************************************/
383