]> git.donarmstrong.com Git - mothur.git/blob - sharedrabundvector.cpp
added list parameter to get.seqs and remove.seqs and added readline library for inter...
[mothur.git] / sharedrabundvector.cpp
1 /*
2  *  sharedvector.cpp
3  *  Dotur
4  *
5  *  Created by Sarah Westcott on 12/5/08.
6  *  Copyright 2008 Schloss Lab UMASS Amherst. All rights reserved.
7  *
8  */
9
10 #include "sharedrabundvector.h" 
11 #include "sabundvector.hpp"
12 #include "ordervector.hpp"
13 #include "sharedutilities.h"
14
15
16 /***********************************************************************/
17
18 SharedRAbundVector::SharedRAbundVector() : DataVector(), maxRank(0), numBins(0), numSeqs(0) {globaldata = GlobalData::getInstance();}
19 /***********************************************************************/
20
21 SharedRAbundVector::~SharedRAbundVector() {
22         //for (int i = 0; i < lookup.size(); i++) {  delete lookup[i];  }
23
24 }
25
26 /***********************************************************************/
27
28 SharedRAbundVector::SharedRAbundVector(int n) : DataVector(), maxRank(0), numBins(n), numSeqs(0) {
29                 globaldata = GlobalData::getInstance();
30                 individual newGuy;
31                 //initialize data
32                 for (int i=0; i< n; i++) {
33                         newGuy.bin = i;
34                         newGuy.abundance = 0;
35                         data.push_back(newGuy);
36                 }
37 }
38
39 /***********************************************************************
40
41 SharedRAbundVector::SharedRAbundVector(string id, vector<individual> rav) : DataVector(id), data(rav) {
42         try {
43                 numBins = 0;
44                 maxRank = 0;
45                 numSeqs = 0;
46                 
47                 for(int i=0;i<data.size();i++){
48                         if(data[i].abundance != 0)              {       numBins = i+1;          }
49                         if(data[i].abundance > maxRank) {       maxRank = data[i].abundance;    }
50                         numSeqs += data[i].abundance;
51                 }
52         }
53         catch(exception& e) {
54                 errorOut(e, "SharedRAbundVector", "SharedRAbundVector");
55                 exit(1);
56         }
57 }
58
59
60 /***********************************************************************/
61 //reads a shared file
62 SharedRAbundVector::SharedRAbundVector(ifstream& f) : DataVector(), maxRank(0), numBins(0), numSeqs(0) {
63         try {
64                 globaldata = GlobalData::getInstance();
65                 
66                 if (globaldata->gGroupmap == NULL) {  groupmap = new GroupMap(); }
67                 
68                 int num, inputData, count;
69                 count = 0;  
70                 string holdLabel, nextLabel, groupN;
71                 individual newguy;
72                 
73                 for (int i = 0; i < lookup.size(); i++) {  delete lookup[i];  }
74                 lookup.clear();
75                 
76                 //read in first row since you know there is at least 1 group.
77                 f >> label >> groupN >> num;
78                 holdLabel = label;
79                 
80                 //add new vector to lookup
81                 SharedRAbundVector* temp = new SharedRAbundVector();
82                 lookup.push_back(temp);
83                 lookup[0]->setLabel(label);
84                 lookup[0]->setGroup(groupN);
85                 
86                 if (globaldata->gGroupmap == NULL) { 
87                         //save group in groupmap
88                         groupmap->namesOfGroups.push_back(groupN);
89                         groupmap->groupIndex[groupN] = 0;
90                 }
91                 
92                 //fill vector.  data = first sharedrabund in file
93                 for(int i=0;i<num;i++){
94                         f >> inputData;
95                         
96                         lookup[0]->push_back(inputData, groupN); //abundance, bin, group
97                         push_back(inputData, groupN);
98                         numSeqs += inputData;
99                         numBins++;
100                         if (inputData > maxRank) { maxRank = inputData; }
101                         
102                 }
103                 
104                 gobble(f);
105                 
106                 if (f.eof() != true) { f >> nextLabel; }
107                 
108                 //read the rest of the groups info in
109                 while ((nextLabel == holdLabel) && (f.eof() != true)) {
110                         f >> groupN >> num;
111                         count++;
112                         
113                         if (globaldata->gGroupmap == NULL) { 
114                                 //save group in groupmap
115                                 groupmap->namesOfGroups.push_back(groupN);
116                                 groupmap->groupIndex[groupN] = count;
117                         }
118                         
119                         //add new vector to lookup
120                         temp = new SharedRAbundVector();
121                         lookup.push_back(temp);
122                         lookup[count]->setLabel(label);
123                         lookup[count]->setGroup(groupN);
124
125                         //fill vector.  
126                         for(int i=0;i<num;i++){
127                                 f >> inputData;
128                                 lookup[count]->push_back(inputData, groupN); //abundance, bin, group
129                         }
130                         
131                         gobble(f);
132                                 
133                         if (f.eof() != true) { f >> nextLabel; }
134                 }
135                 
136                 //put file pointer back since you are now at a new distance label
137                 for (int i = 0; i < nextLabel.length(); i++) { f.unget();  }
138         
139                 if (globaldata->gGroupmap == NULL) { globaldata->gGroupmap = groupmap;  }
140                 
141         }
142         catch(exception& e) {
143                 errorOut(e, "SharedRAbundVector", "SharedRAbundVector");
144                 exit(1);
145         }
146 }
147
148 /***********************************************************************/
149
150 void SharedRAbundVector::set(int binNumber, int newBinSize, string groupname){
151         try {
152                 int oldBinSize = data[binNumber].abundance;
153                 data[binNumber].abundance = newBinSize;
154                 data[binNumber].group = groupname;
155         
156                 if(newBinSize > maxRank)        {       maxRank = newBinSize;   }
157         
158                 numSeqs += (newBinSize - oldBinSize);
159         }
160         catch(exception& e) {
161                 errorOut(e, "SharedRAbundVector", "set");
162                 exit(1);
163         }
164 }
165 /***********************************************************************/
166
167 void SharedRAbundVector::setData(vector <individual> newData){
168         data = newData;
169 }
170
171 /***********************************************************************/
172
173 int SharedRAbundVector::getAbundance(int index){
174         return data[index].abundance;
175         
176 }
177 /***********************************************************************/
178
179 int SharedRAbundVector::numNZ(){
180         int sum = 0;
181         for(int i = 1; i < numBins; i++)
182                 if(data[i].abundance > 0)
183                         sum++;
184         return sum;
185 }
186 /***********************************************************************/
187
188 void SharedRAbundVector::sortD(){
189         struct individual indObj;
190         sort(data.begin()+1, data.end(), indObj);
191 }
192 /***********************************************************************/
193
194 individual SharedRAbundVector::get(int index){
195         return data[index];
196         
197 }
198 /***********************************************************************/
199
200 vector <individual> SharedRAbundVector::getData(){
201         return data;
202 }
203 /***********************************************************************/
204
205 void SharedRAbundVector::push_back(int binSize, string groupName){
206         try {
207                 individual newGuy;
208                 newGuy.abundance = binSize;
209                 newGuy.group = groupName;
210                 newGuy.bin = data.size();
211                 
212                 data.push_back(newGuy);
213                 numBins++;
214         
215                 if(binSize > maxRank){
216                         maxRank = binSize;
217                 }
218         
219                 numSeqs += binSize;
220         }
221         catch(exception& e) {
222                 errorOut(e, "SharedRAbundVector", "push_back");
223                 exit(1);
224         }
225 }
226
227 /***********************************************************************/
228
229 void SharedRAbundVector::insert(int binSize, int otu, string groupName){
230         try {
231                 individual newGuy;
232                 newGuy.abundance = binSize;
233                 newGuy.group = groupName;
234                 newGuy.bin = otu;
235                 
236                 data.insert(data.begin()+otu, newGuy);
237                 numBins++;
238         
239                 if(binSize > maxRank){
240                         maxRank = binSize;
241                 }
242         
243                 numSeqs += binSize;
244         }
245         catch(exception& e) {
246                 errorOut(e, "SharedRAbundVector", "insert");
247                 exit(1);
248         }
249 }
250
251 /***********************************************************************/
252
253 void SharedRAbundVector::push_front(int binSize, int otu, string groupName){
254         try {
255                 individual newGuy;
256                 newGuy.abundance = binSize;
257                 newGuy.group = groupName;
258                 newGuy.bin = otu;
259                 
260                 data.insert(data.begin(), newGuy);
261                 numBins++;
262         
263                 if(binSize > maxRank){
264                         maxRank = binSize;
265                 }
266         
267                 numSeqs += binSize;
268         }
269         catch(exception& e) {
270                 errorOut(e, "SharedRAbundVector", "push_front");
271                 exit(1);
272         }
273 }
274
275 /***********************************************************************/
276 void SharedRAbundVector::pop_back(){
277         numSeqs -= data[data.size()-1].abundance;
278         numBins--;
279         return data.pop_back();
280 }
281
282 /***********************************************************************/
283
284
285 vector<individual>::reverse_iterator SharedRAbundVector::rbegin(){
286         return data.rbegin();                           
287 }
288
289 /***********************************************************************/
290
291 vector<individual>::reverse_iterator SharedRAbundVector::rend(){
292         return data.rend();                                     
293 }
294
295 /***********************************************************************/
296 void SharedRAbundVector::resize(int size){
297         
298         data.resize(size);
299 }
300
301 /***********************************************************************/
302
303 int SharedRAbundVector::size(){
304         return data.size();
305 }
306
307 /***********************************************************************/
308 void SharedRAbundVector::print(ostream& output){
309         try {
310                 output << numBins << '\t';
311         
312                 for(int i=0;i<data.size();i++){         output << data[i].abundance << '\t';            }
313                 output << endl;
314         }
315         catch(exception& e) {
316                 errorOut(e, "SharedRAbundVector", "print");
317                 exit(1);
318         }
319 }
320 /***********************************************************************/
321 string SharedRAbundVector::getGroup(){
322         return group;
323 }
324
325 /***********************************************************************/
326
327 void SharedRAbundVector::setGroup(string groupName){
328         group = groupName;
329 }
330 /***********************************************************************/
331 int SharedRAbundVector::getGroupIndex()  { return index; }
332 /***********************************************************************/
333 void SharedRAbundVector::setGroupIndex(int vIndex)      { index = vIndex; }
334 /***********************************************************************/
335 int SharedRAbundVector::getNumBins(){
336         return numBins;
337 }
338
339 /***********************************************************************/
340
341 int SharedRAbundVector::getNumSeqs(){
342         return numSeqs;
343 }
344
345 /***********************************************************************/
346
347 int SharedRAbundVector::getMaxRank(){
348         return maxRank;
349 }
350 /***********************************************************************/
351
352 SharedRAbundVector SharedRAbundVector::getSharedRAbundVector(){
353         return *this;                   
354 }
355 /***********************************************************************/
356 vector<SharedRAbundVector*> SharedRAbundVector::getSharedRAbundVectors(){
357         try {
358                 SharedUtil* util;
359                 util = new SharedUtil();
360                 
361                 util->setGroups(globaldata->Groups, globaldata->gGroupmap->namesOfGroups);
362
363                 for (int i = 0; i < lookup.size(); i++) {
364                         //if this sharedrabund is not from a group the user wants then delete it.
365                         if (util->isValidGroup(lookup[i]->getGroup(), globaldata->Groups) == false) { 
366                                 delete lookup[i]; 
367                                 lookup.erase(lookup.begin()+i); 
368                                 i--; 
369                         }
370                 }
371                 
372                 delete util;
373         
374                 return lookup;
375         }
376         catch(exception& e) {
377                 errorOut(e, "SharedRAbundVector", "getSharedRAbundVectors");
378                 exit(1);
379         }
380 }
381 /***********************************************************************/
382
383 RAbundVector SharedRAbundVector::getRAbundVector() {
384         try {
385                 RAbundVector rav;
386                 
387                 for (int i = 0; i < data.size(); i++) {
388                         if(data[i].abundance != 0) {
389                                 rav.push_back(data[i].abundance);
390                         }
391                 }
392                 
393                 rav.setLabel(label);
394                 return rav;
395         }
396         catch(exception& e) {
397                 errorOut(e, "SharedRAbundVector", "getRAbundVector");
398                 exit(1);
399         }
400 }
401 /***********************************************************************/
402
403 RAbundVector SharedRAbundVector::getRAbundVector2() {
404         try {
405                 RAbundVector rav;
406                 for(int i = 0; i < numBins; i++)
407                         if(data[i].abundance != 0)
408                                 rav.push_back(data[i].abundance-1);
409                 return rav;
410         }
411         catch(exception& e) {
412                 errorOut(e, "SharedRAbundVector", "getRAbundVector2");
413                 exit(1);
414         }
415 }
416 /***********************************************************************/
417
418 SharedSAbundVector SharedRAbundVector::getSharedSAbundVector(){
419         try {
420                 SharedSAbundVector sav(maxRank+1);
421                 
422                 for(int i=0;i<data.size();i++){
423                         int abund = data[i].abundance;
424                         sav.set(abund, sav.getAbundance(abund) + 1, group);
425                 }
426                 
427                 sav.set(0, 0, group);
428                 sav.setLabel(label);
429                 sav.setGroup(group);
430                 
431                 return sav;
432         }
433         catch(exception& e) {
434                 errorOut(e, "SharedRAbundVector", "getSharedSAbundVector");
435                 exit(1);
436         }
437 }
438 /***********************************************************************/
439
440 SAbundVector SharedRAbundVector::getSAbundVector() {
441         try {
442                 SAbundVector sav(maxRank+1);
443                 
444                 for(int i=0;i<data.size();i++){
445                         int abund = data[i].abundance;
446                         sav.set(abund, sav.get(abund) + 1);
447                 }
448                 sav.set(0, 0);
449                 sav.setLabel(label);
450                 return sav;
451         }
452         catch(exception& e) {
453                 errorOut(e, "SharedRAbundVector", "getSAbundVector");           
454                 exit(1);
455         }
456 }
457
458 /***********************************************************************/
459
460 SharedOrderVector SharedRAbundVector::getSharedOrderVector() {
461         try {
462                 SharedOrderVector ov;
463         
464                 for(int i=0;i<data.size();i++){
465                         for(int j=0;j<data[i].abundance;j++){
466                                 ov.push_back(data[i].bin, data[i].abundance, data[i].group);
467                         }
468                 }
469                 random_shuffle(ov.begin(), ov.end());
470
471                 ov.setLabel(label);     
472                 ov.updateStats();
473                 
474                 return ov;
475         }
476         catch(exception& e) {
477                 errorOut(e, "SharedRAbundVector", "getSharedOrderVector");
478                 exit(1);
479         }
480 }
481 /***********************************************************************/
482
483 OrderVector SharedRAbundVector::getOrderVector(map<string,int>* nameMap = NULL) {
484         try {
485                 OrderVector ov;
486         
487                 for(int i=0;i<data.size();i++){
488                         for(int j=0;j<data[i].abundance;j++){
489                                 ov.push_back(i);
490                         }
491                 }
492                 random_shuffle(ov.begin(), ov.end());
493
494                 ov.setLabel(label);     
495                 return ov;
496         }
497         catch(exception& e) {
498                 errorOut(e, "SharedRAbundVector", "getOrderVector");
499                 exit(1);
500         }
501 }
502
503 /***********************************************************************/
504