]> git.donarmstrong.com Git - mothur.git/blob - sharedcommand.cpp
added smart distance feature and optimized all commands using line by line processing
[mothur.git] / sharedcommand.cpp
1 /*
2  *  sharedcommand.cpp
3  *  Dotur
4  *
5  *  Created by Sarah Westcott on 1/2/09.
6  *  Copyright 2009 Schloss Lab UMASS Amherst. All rights reserved.
7  *
8  */
9
10 #include "sharedcommand.h"
11
12 //**********************************************************************************************************************
13
14 SharedCommand::SharedCommand(){
15         try {
16                 globaldata = GlobalData::getInstance();
17                 
18                 //getting output filename
19                 filename = globaldata->inputFileName;
20                 filename = getRootName(filename);
21                 filename = filename + "shared";
22                 openOutputFile(filename, out);
23         }
24         catch(exception& e) {
25                 cout << "Standard Error: " << e.what() << " has occurred in the SharedCommand class Function SharedCommand. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
26                 exit(1);
27         }
28         catch(...) {
29                 cout << "An unknown error has occurred in the SharedCommand class function SharedCommand. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
30                 exit(1);
31         }
32
33 }
34 //**********************************************************************************************************************
35
36 int SharedCommand::execute(){
37         try {
38                 globaldata = GlobalData::getInstance();
39                 int count = 1;
40                 string errorOff = "no error";
41                         
42                 //read in listfile
43                 read = new ReadOTUFile(globaldata->inputFileName);      
44                 read->read(&*globaldata); 
45                 input = globaldata->ginput;
46                 SharedList = globaldata->gSharedList;
47                 SharedListVector* lastList = SharedList;
48                 
49                 //if the users enters label "0.06" and there is no "0.06" in their file use the next lowest label.
50                 set<string> processedLabels;
51                 set<string> userLabels = globaldata->labels;
52                 
53                 shared = new Shared();
54                 
55                 while((SharedList != NULL) && ((globaldata->allLines == 1) || (userLabels.size() != 0))) {
56                         
57                                                 
58                         if(globaldata->allLines == 1 || globaldata->lines.count(count) == 1 || globaldata->labels.count(SharedList->getLabel()) == 1){
59                         
60                                         shared->getSharedVectors(SharedList); //fills sharedGroups with new info and updates sharedVector
61                                         printSharedData(); //prints info to the .shared file
62                                 
63                                         processedLabels.insert(SharedList->getLabel());
64                                         userLabels.erase(SharedList->getLabel());
65                         }
66                         
67                         if ((anyLabelsToProcess(SharedList->getLabel(), userLabels, errorOff) == true) && (processedLabels.count(lastList->getLabel()) != 1)) {
68                                         shared->getSharedVectors(lastList); //fills sharedGroups with new info and updates sharedVector
69                                         printSharedData(); //prints info to the .shared file
70
71                                         processedLabels.insert(lastList->getLabel());
72                                         userLabels.erase(lastList->getLabel());
73                         }
74                         
75                         if (count != 1) { delete lastList; }
76                         lastList = SharedList;                  
77
78                         SharedList = input->getSharedListVector(); //get new list vector to process
79                 }
80                 
81                 //output error messages about any remaining user labels
82                 set<string>::iterator it;
83                 bool needToRun = false;
84                 for (it = userLabels.begin(); it != userLabels.end(); it++) {  
85                         //cout << "Your file does not include the label "<< *it; 
86                         if (processedLabels.count(lastList->getLabel()) != 1) {
87                                 //cout << ". I will use " << lastList->getLabel() << "." << endl;
88                                 needToRun = true;
89                         }else {
90                                 //cout << ". Please refer to " << lastList->getLabel() << "." << endl;
91                         }
92                 }
93                 
94                 //run last line if you need to
95                 if (needToRun == true)  {
96                         shared->getSharedVectors(lastList); //fills sharedGroups with new info and updates sharedVector
97                         printSharedData(); //prints info to the .shared file
98                 }
99                 
100                 delete lastList;
101                 delete shared;
102                 
103                 return 0;
104         }
105         catch(exception& e) {
106                 cout << "Standard Error: " << e.what() << " has occurred in the SharedCommand class Function execute. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
107                 exit(1);
108         }
109         catch(...) {
110                 cout << "An unknown error has occurred in the SharedCommand class function execute. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
111                 exit(1);
112         }
113
114 }
115 //**********************************************************************************************************************
116 void SharedCommand::printSharedData() {
117         try {
118                 //prints out horizontally
119                 for (it = shared->sharedGroups.begin(); it != shared->sharedGroups.end(); it++) {
120                         out << it->second->getLabel() << "\t" << it->first << "\t"; //prints out label and groupname
121                         it->second->print(out); // prints sharedrabundvector
122                 }
123         }
124         catch(exception& e) {
125                 cout << "Standard Error: " << e.what() << " has occurred in the SharedCommand class Function printSharedData. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
126                 exit(1);
127         }
128         catch(...) {
129                 cout << "An unknown error has occurred in the SharedCommand class function printSharedData. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
130                 exit(1);
131         }
132         
133 }
134
135 //**********************************************************************************************************************
136
137 SharedCommand::~SharedCommand(){
138         //delete list;
139         delete read;
140         
141 }
142
143 //**********************************************************************************************************************