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