]> git.donarmstrong.com Git - mothur.git/blob - sharedcommand.cpp
modified read.otu and shared command
[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                 int count = 1;
55                 string errorOff = "no error";
56                         
57                 //read in listfile
58                 read = new ReadOTUFile(globaldata->inputFileName);      
59                 read->read(&*globaldata); 
60
61                 input = globaldata->ginput;
62                 SharedList = globaldata->gSharedList;
63                 string lastLabel = SharedList->getLabel();
64                 vector<SharedRAbundVector*> lookup; 
65                                 
66                 //if the users enters label "0.06" and there is no "0.06" in their file use the next lowest label.
67                 set<string> processedLabels;
68                 set<string> userLabels = globaldata->labels;
69                 set<int> userLines = globaldata->lines;
70                 
71                 
72                 while((SharedList != NULL) && ((globaldata->allLines == 1) || (userLabels.size() != 0) || (userLines.size() != 0))) {
73                         
74
75                         if(globaldata->allLines == 1 || globaldata->lines.count(count) == 1 || globaldata->labels.count(SharedList->getLabel()) == 1){
76                                         
77                                         lookup = SharedList->getSharedRAbundVector();
78                                         mothurOut(lookup[0]->getLabel() + "\t" + toString(count)); mothurOutEndLine();
79                                         
80                                         printSharedData(lookup); //prints info to the .shared file
81                                         for (int i = 0; i < lookup.size(); i++) {  delete lookup[i];  }
82                                 
83                                         processedLabels.insert(SharedList->getLabel());
84                                         userLabels.erase(SharedList->getLabel());
85                                         userLines.erase(count);
86                         }
87                         
88                         if ((anyLabelsToProcess(SharedList->getLabel(), userLabels, errorOff) == true) && (processedLabels.count(lastLabel) != 1)) {
89                                         delete SharedList;
90                                         SharedList = input->getSharedListVector(lastLabel); //get new list vector to process
91                                         
92                                         lookup = SharedList->getSharedRAbundVector();
93                                         mothurOut(lookup[0]->getLabel() + "\t" + toString(count)); mothurOutEndLine();
94                                         
95                                         printSharedData(lookup); //prints info to the .shared file
96                                         for (int i = 0; i < lookup.size(); i++) {  delete lookup[i];  }
97                                         
98                                         processedLabels.insert(SharedList->getLabel());
99                                         userLabels.erase(SharedList->getLabel());
100                         }
101                         
102                 
103                         lastLabel = SharedList->getLabel();
104                                 
105                         delete SharedList;
106                         SharedList = input->getSharedListVector(); //get new list vector to process
107                         
108                         count++;                
109                 }
110                 
111                 //output error messages about any remaining user labels
112                 set<string>::iterator it;
113                 bool needToRun = false;
114                 for (it = userLabels.begin(); it != userLabels.end(); it++) {  
115                         if (processedLabels.count(lastLabel) != 1) {
116                                 needToRun = true;
117                         }
118                 }
119                 
120                 //run last line if you need to
121                 if (needToRun == true)  {
122                         delete SharedList;
123                         SharedList = input->getSharedListVector(lastLabel); //get new list vector to process
124                                         
125                         lookup = SharedList->getSharedRAbundVector();
126                         mothurOut(lookup[0]->getLabel() + "\t" + toString(count)); mothurOutEndLine();
127                         
128                         printSharedData(lookup); //prints info to the .shared file
129                         for (int i = 0; i < lookup.size(); i++) {  delete lookup[i];  }
130                         delete SharedList;
131                 }
132                 
133                 globaldata->gSharedList = NULL;
134                 delete read;
135                 
136                 out.close();
137                 
138                 for (it3 = filehandles.begin(); it3 != filehandles.end(); it3++) {
139                         delete it3->second;
140                 }
141
142                 
143                 return 0;
144         }
145         catch(exception& e) {
146                 errorOut(e, "SharedCommand", "execute");
147                 exit(1);
148         }
149 }
150 //**********************************************************************************************************************
151 void SharedCommand::printSharedData(vector<SharedRAbundVector*> thislookup) {
152         try {
153                 
154                 //initialize bin values
155                 for (int i = 0; i < thislookup.size(); i++) {
156                         out << thislookup[i]->getLabel() << '\t' << thislookup[i]->getGroup() << '\t';
157                         thislookup[i]->print(out);
158                         
159                         openOutputFileAppend(fileroot + thislookup[i]->getGroup() + ".rabund", *(filehandles[thislookup[i]->getGroup()]));
160                         (*filehandles[thislookup[i]->getGroup()]) << thislookup[i]->getLabel()  << '\t' << thislookup[i]->getGroup()  << '\t';
161                         thislookup[i]->print(*(filehandles[thislookup[i]->getGroup()]));
162                         (*(filehandles[thislookup[i]->getGroup()])).close();
163                 }
164  
165         }
166         catch(exception& e) {
167                 errorOut(e, "SharedCommand", "printSharedData");
168                 exit(1);
169         }
170 }
171
172 //**********************************************************************************************************************
173
174 SharedCommand::~SharedCommand(){
175         //delete list;
176         
177         
178 }
179
180 //**********************************************************************************************************************