]> git.donarmstrong.com Git - mothur.git/blob - sharedlistvector.cpp
added multiple processors option for Windows users to align.seqs, dist.seqs, summary...
[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){ groupmap = NULL; }
20
21 /***********************************************************************/
22
23 SharedListVector::SharedListVector(int n):      DataVector(), data(n, "") , maxRank(0), numBins(0), numSeqs(0){ groupmap = NULL; }
24
25 /***********************************************************************/
26 SharedListVector::SharedListVector(ifstream& f) : DataVector(), maxRank(0), numBins(0), numSeqs(0) {
27         try {
28                 //set up groupmap for later.
29                 groupmap = new GroupMap(m->getGroupFile());
30                 groupmap->readMap(); 
31
32                 int hold;
33                 string inputData;
34                 f >> label >> hold;
35         
36                 data.assign(hold, "");
37                 
38                 for(int i=0;i<hold;i++){
39                         f >> inputData;
40                         set(i, inputData);
41                 }
42                 
43         }
44         catch(exception& e) {
45                 m->errorOut(e, "SharedListVector", "SharedListVector");
46                 exit(1);
47         }
48 }
49
50 /***********************************************************************/
51 void SharedListVector::set(int binNumber, string seqNames){
52         try {
53                 int nNames_old = m->getNumNames(data[binNumber]);
54                 data[binNumber] = seqNames;
55                 int nNames_new = m->getNumNames(seqNames);
56         
57                 if(nNames_old == 0)                     {       numBins++;                              }
58                 if(nNames_new == 0)                     {       numBins--;                              }
59                 if(nNames_new > maxRank)        {       maxRank = nNames_new;   }
60         
61                 numSeqs += (nNames_new - nNames_old);
62                 
63                          
64         }
65         catch(exception& e) {
66                 m->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 = m->getNumNames(seqNames);
83         
84                 numBins++;
85         
86                 if(nNames > maxRank)    {       maxRank = nNames;       }
87         
88                 numSeqs += nNames;
89         }
90         catch(exception& e) {
91                 m->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                 m->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 = m->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                 m->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 = m->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                 m->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 = m->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") {  m->mothurOut("Error: Sequence '" + name + "' was not found in the group file, please correct."); m->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") {  m->mothurOut("Error: Sequence '" + names + "' was not found in the group file, please correct."); m->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                 m->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") {      m->mothurOut("Error: Sequence '" + name + "' was not found in the group file, please correct."); m->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") {  m->mothurOut("Error: Sequence '" + names + "' was not found in the group file, please correct."); m->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                 m->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;  //contains just the groups the user selected
267                 map<string, SharedRAbundVector*> finder;  //contains all groups in groupmap
268                 string group, names, name;
269                 
270                 vector<string> Groups = m->getGroups();
271                 vector<string> allGroups = groupmap->getNamesOfGroups();
272                 util->setGroups(Groups, allGroups);
273                 m->setGroups(Groups);
274                 delete util;
275
276                 for (int i = 0; i < allGroups.size(); i++) {
277                         SharedRAbundVector* temp = new SharedRAbundVector(data.size());
278                         finder[allGroups[i]] = temp;
279                         finder[allGroups[i]]->setLabel(label);
280                         finder[allGroups[i]]->setGroup(allGroups[i]);
281                         if (m->inUsersGroups(allGroups[i], m->getGroups())) {  //if this group is in user groups
282                                 lookup.push_back(finder[allGroups[i]]);
283                         }
284                 }
285         
286                 //fill vectors
287                 for(int i=0;i<numBins;i++){
288                         names = get(i);  
289                         int nameLength = names.size();
290                         string seqName = "";
291                         
292                         for(int j=0;j<nameLength;j++){
293                                 if(names[j] == ','){
294                                         group = groupmap->getGroup(seqName);
295                                         if(group == "not found") {      m->mothurOut("Error: Sequence '" + seqName + "' was not found in the group file, please correct."); m->mothurOutEndLine();  exit(1); }
296                                         finder[group]->set(i, finder[group]->getAbundance(i) + 1, group);  //i represents what bin you are in
297                                         
298                                         seqName = "";
299                                 }
300                                 else{
301                                         seqName += names[j];
302                                 }
303                         }
304                         group = groupmap->getGroup(seqName);
305                         if(group == "not found") {      m->mothurOut("Error: Sequence '" + seqName + "' was not found in the group file, please correct."); m->mothurOutEndLine();  exit(1); }
306                         finder[group]->set(i, finder[group]->getAbundance(i) + 1, group);  //i represents what bin you are in
307                         
308                         
309                         
310 //                      while (names.find_first_of(',') != -1) { 
311 //                              name = names.substr(0,names.find_first_of(','));
312 //                              names = names.substr(names.find_first_of(',')+1, names.length());
313 //                              group = groupmap->getGroup(name);
314 //                              if(group == "not found") {      m->mothurOut("Error: Sequence '" + name + "' was not found in the group file, please correct."); m->mothurOutEndLine();  exit(1); }
315 //                              finder[group]->set(i, finder[group]->getAbundance(i) + 1, group);  //i represents what bin you are in
316 //                      }
317                         
318                         //get last name
319 //                      group = groupmap->getGroup(names);
320 //                      if(group == "not found") {      m->mothurOut("Error: Sequence '" + names + "' was not found in the group file, please correct."); m->mothurOutEndLine();  exit(1); }
321 //                      finder[group]->set(i, finder[group]->getAbundance(i) + 1, group);  //i represents what bin you are in
322                         
323                 }
324
325                 return lookup;
326         }
327         catch(exception& e) {
328                 m->errorOut(e, "SharedListVector", "getSharedRAbundVector");
329                 exit(1);
330         }
331 }
332
333 /***********************************************************************/
334 SharedSAbundVector SharedListVector::getSharedSAbundVector(string groupName) {
335         try { 
336                 SharedSAbundVector sav;
337                 SharedRAbundVector rav;
338                 
339                 rav = this->getSharedRAbundVector(groupName);
340                 sav = rav.getSharedSAbundVector();
341                 
342                 return sav;
343         }
344         catch(exception& e) {
345                 m->errorOut(e, "SharedListVector", "getSharedSAbundVector");
346                 exit(1);
347         }
348 }
349 /***********************************************************************/
350
351 OrderVector SharedListVector::getOrderVector(map<string,int>* orderMap = NULL){
352         
353         try {
354                 if(orderMap == NULL){
355                         OrderVector ov;
356                 
357                         for(int i=0;i<data.size();i++){
358                                 int binSize = m->getNumNames(data[i]);          
359                                 for(int j=0;j<binSize;j++){
360                                         ov.push_back(i);
361                                 }
362                         }
363                         random_shuffle(ov.begin(), ov.end());
364                         ov.setLabel(label);
365                         ov.getNumBins();
366                 
367                         return ov;
368                 
369                 }
370                 else{
371                         OrderVector ov(numSeqs);
372                 
373                         for(int i=0;i<data.size();i++){
374                                 string listOTU = data[i];
375                                 int length = listOTU.size();
376                                 
377                                 string seqName="";
378                         
379                                 for(int j=0;j<length;j++){
380                                 
381                                         if(listOTU[j] != ','){
382                                                 seqName += listOTU[j];
383                                         }
384                                         else{
385                                                 if(orderMap->count(seqName) == 0){
386                                                         m->mothurOut(seqName + " not found, check *.names file\n");
387                                                         exit(1);
388                                                 }
389                                         
390                                                 ov.set((*orderMap)[seqName], i);
391                                                 seqName = "";
392                                         }                                               
393                                 }
394                         
395                                 if(orderMap->count(seqName) == 0){
396                                         m->mothurOut(seqName + " not found, check *.names file\n");
397                                         exit(1);
398                                 }
399                                 ov.set((*orderMap)[seqName], i);        
400                         }
401                 
402                         ov.setLabel(label);
403                         ov.getNumBins();
404                 
405                         return ov;              
406                 }
407         }
408         catch(exception& e) {
409                 m->errorOut(e, "SharedListVector", "getOrderVector");
410                 exit(1);
411         }
412 }
413
414 /***********************************************************************/
415