]> git.donarmstrong.com Git - mothur.git/blob - sharedcommand.cpp
removed line option
[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                 groupMap = globaldata->gGroupmap;
25                 
26                 //fill filehandles with neccessary ofstreams
27                 int i;
28                 ofstream* temp;
29                 for (i=0; i<groupMap->getNumGroups(); i++) {
30                         temp = new ofstream;
31                         filehandles[groupMap->namesOfGroups[i]] = temp;
32                 }
33                 
34                 //set fileroot
35                 fileroot = getRootName(globaldata->getListFile());
36                 
37                 //clears file before we start to write to it below
38                 for (int i=0; i<groupMap->getNumGroups(); i++) {
39                         remove((fileroot + groupMap->namesOfGroups[i] + ".rabund").c_str());
40                 }
41
42         }
43         catch(exception& e) {
44                 errorOut(e, "SharedCommand", "SharedCommand");
45                 exit(1);
46         }
47 }
48 //**********************************************************************************************************************
49
50 int SharedCommand::execute(){
51         try {
52                 
53                 //lookup.clear();
54                 string errorOff = "no error";
55                         
56                 //read in listfile
57                 read = new ReadOTUFile(globaldata->inputFileName);      
58                 read->read(&*globaldata); 
59                 delete read;
60
61                 input = globaldata->ginput;
62                 SharedList = globaldata->gSharedList;
63                 string lastLabel = SharedList->getLabel();
64                 vector<SharedRAbundVector*> lookup; 
65                 
66                 if (SharedList->getNumSeqs() != groupMap->getNumSeqs()) {  
67                         mothurOut("Your group file contains " + toString(groupMap->getNumSeqs()) + " sequences and list file contains " + toString(SharedList->getNumSeqs()) + " sequences. Please correct."); mothurOutEndLine(); 
68                         
69                         createMisMatchFile();
70                         
71                         //delete memory
72                         for (it3 = filehandles.begin(); it3 != filehandles.end(); it3++) {
73                                 delete it3->second;
74                         }
75                         delete SharedList;
76                         globaldata->gSharedList = NULL;
77                         
78                         return 1; 
79                 }
80                 
81                 //if the users enters label "0.06" and there is no "0.06" in their file use the next lowest label.
82                 set<string> processedLabels;
83                 set<string> userLabels = globaldata->labels;
84                 
85                 
86                 while((SharedList != NULL) && ((globaldata->allLines == 1) || (userLabels.size() != 0))) {
87                         
88
89                         if(globaldata->allLines == 1 || globaldata->labels.count(SharedList->getLabel()) == 1){
90                                         
91                                         lookup = SharedList->getSharedRAbundVector();
92                                         mothurOut(lookup[0]->getLabel()); mothurOutEndLine();
93                                         
94                                         printSharedData(lookup); //prints info to the .shared file
95                                         for (int i = 0; i < lookup.size(); i++) {  delete lookup[i];  }
96                                 
97                                         processedLabels.insert(SharedList->getLabel());
98                                         userLabels.erase(SharedList->getLabel());
99                         }
100                         
101                         if ((anyLabelsToProcess(SharedList->getLabel(), userLabels, errorOff) == true) && (processedLabels.count(lastLabel) != 1)) {
102                                         delete SharedList;
103                                         SharedList = input->getSharedListVector(lastLabel); //get new list vector to process
104                                         
105                                         lookup = SharedList->getSharedRAbundVector();
106                                         mothurOut(lookup[0]->getLabel()); mothurOutEndLine();
107                                         
108                                         printSharedData(lookup); //prints info to the .shared file
109                                         for (int i = 0; i < lookup.size(); i++) {  delete lookup[i];  }
110                                         
111                                         processedLabels.insert(SharedList->getLabel());
112                                         userLabels.erase(SharedList->getLabel());
113                         }
114                         
115                 
116                         lastLabel = SharedList->getLabel();
117                                 
118                         delete SharedList;
119                         SharedList = input->getSharedListVector(); //get new list vector to process
120                 }
121                 
122                 //output error messages about any remaining user labels
123                 set<string>::iterator it;
124                 bool needToRun = false;
125                 for (it = userLabels.begin(); it != userLabels.end(); it++) {  
126                         if (processedLabels.count(lastLabel) != 1) {
127                                 needToRun = true;
128                         }
129                 }
130                 
131                 //run last label if you need to
132                 if (needToRun == true)  {
133                         if (SharedList != NULL) {       delete SharedList;      }
134                         SharedList = input->getSharedListVector(lastLabel); //get new list vector to process
135                                         
136                         lookup = SharedList->getSharedRAbundVector();
137                         mothurOut(lookup[0]->getLabel()); mothurOutEndLine();
138                         
139                         printSharedData(lookup); //prints info to the .shared file
140                         for (int i = 0; i < lookup.size(); i++) {  delete lookup[i];  }
141                         delete SharedList;
142                 }
143                 
144                 globaldata->gSharedList = NULL;
145                 
146                 out.close();
147                 
148                 for (it3 = filehandles.begin(); it3 != filehandles.end(); it3++) {
149                         delete it3->second;
150                 }
151
152                 
153                 return 0;
154         }
155         catch(exception& e) {
156                 errorOut(e, "SharedCommand", "execute");
157                 exit(1);
158         }
159 }
160 //**********************************************************************************************************************
161 void SharedCommand::printSharedData(vector<SharedRAbundVector*> thislookup) {
162         try {
163                 
164                 //initialize bin values
165                 for (int i = 0; i < thislookup.size(); i++) {
166                         out << thislookup[i]->getLabel() << '\t' << thislookup[i]->getGroup() << '\t';
167                         thislookup[i]->print(out);
168                         
169                         RAbundVector rav = thislookup[i]->getRAbundVector();
170                         openOutputFileAppend(fileroot + thislookup[i]->getGroup() + ".rabund", *(filehandles[thislookup[i]->getGroup()]));
171                         rav.print(*(filehandles[thislookup[i]->getGroup()]));
172                         (*(filehandles[thislookup[i]->getGroup()])).close();
173                 }
174  
175         }
176         catch(exception& e) {
177                 errorOut(e, "SharedCommand", "printSharedData");
178                 exit(1);
179         }
180 }
181 //**********************************************************************************************************************
182 void SharedCommand::createMisMatchFile() {
183         try {
184                 ofstream outMisMatch;
185                 string outputMisMatchName = getRootName(globaldata->inputFileName);
186                 
187                 //you have sequences in your list file that are not in your group file
188                 if (SharedList->getNumSeqs() > groupMap->getNumSeqs()) { 
189                         outputMisMatchName += "missing.group";
190                         mothurOut("For a list of names that are in your list file and not in your group file, please refer to " + outputMisMatchName + "."); mothurOutEndLine();
191                         
192                         openOutputFile(outputMisMatchName, outMisMatch);
193                         
194                         //go through list and if group returns "not found" output it
195                         for (int i = 0; i < SharedList->getNumBins(); i++) {
196                         
197                                 string names = SharedList->get(i); 
198                                 
199                                 while (names.find_first_of(',') != -1) { 
200                                         string name = names.substr(0,names.find_first_of(','));
201                                         names = names.substr(names.find_first_of(',')+1, names.length());
202                                         string group = groupMap->getGroup(name);
203                                         
204                                         if(group == "not found") {      outMisMatch << name << endl;  }
205                                 }
206                                 
207                                 //get last name
208                                 string group = groupMap->getGroup(names);
209                                 if(group == "not found") {      outMisMatch << names << endl;  }                                
210                         }
211                         
212                         outMisMatch.close();
213                         
214                 
215                 }else {//you have sequences in your group file that are not in you list file
216                         
217                         outputMisMatchName += "missing.name";
218                         mothurOut("For a list of names that are in your group file and not in your list file, please refer to " + outputMisMatchName + "."); mothurOutEndLine();
219                         
220                         map<string, string> namesInList;
221                         
222                         //go through listfile and get names
223                         for (int i = 0; i < SharedList->getNumSeqs(); i++) {
224                                 
225                                 string names = SharedList->get(i); 
226                                 
227                                 while (names.find_first_of(',') != -1) { 
228                                         string name = names.substr(0,names.find_first_of(','));
229                                         names = names.substr(names.find_first_of(',')+1, names.length());
230                                         
231                                         namesInList[name] = name;
232                                 }
233                                 
234                                 //get last name
235                                 namesInList[names] = names;                             
236                         }
237                         
238                         //get names of sequences in groupfile
239                         vector<string> seqNames = groupMap->getNamesSeqs();
240                 
241                         map<string, string>::iterator itMatch;
242                         
243                         openOutputFile(outputMisMatchName, outMisMatch);
244                         
245                         //loop through names in seqNames and if they aren't in namesIn list output them
246                         for (int i = 0; i < seqNames.size(); i++) {
247                                 
248                                 itMatch = namesInList.find(seqNames[i]);
249                                 
250                                 if (itMatch == namesInList.end()) {
251                                 
252                                         outMisMatch << seqNames[i] << endl; 
253                                 }
254                         }               
255                         outMisMatch.close();
256                 }
257  
258         }
259         catch(exception& e) {
260                 errorOut(e, "SharedCommand", "createMisMatchFile");
261                 exit(1);
262         }
263 }
264
265 //**********************************************************************************************************************
266
267 SharedCommand::~SharedCommand(){
268         //delete list;
269         
270         
271 }
272
273 //**********************************************************************************************************************