]> git.donarmstrong.com Git - mothur.git/blob - parselistcommand.cpp
20f92f73b3e8107bd47a1e99f253779e67d2a423
[mothur.git] / parselistcommand.cpp
1 /*
2  *  parselistcommand.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 "parselistcommand.h"
11
12 //**********************************************************************************************************************
13 ParseListCommand::ParseListCommand(){
14         try {
15                 globaldata = GlobalData::getInstance();
16                 
17                 //read in group map info.
18                 groupMap = new GroupMap(globaldata->getGroupFile());
19                 groupMap->readMap();
20
21                 
22                 //fill filehandles with neccessary ofstreams
23                 int i;
24                 ofstream* temp;
25                 for (i=0; i<groupMap->getNumGroups(); i++) {
26                         temp = new ofstream;
27                         filehandles[groupMap->namesOfGroups[i]] = temp;
28                 }
29                 
30                 //set fileroot
31                 fileroot = getRootName(globaldata->getListFile());
32                 
33                 //open output list files
34                 for (i=0; i<groupMap->getNumGroups(); i++) {//opens an output file for each group
35                         openOutputFile(fileroot + groupMap->namesOfGroups[i] + ".list", *(filehandles[groupMap->namesOfGroups[i]]));
36                 }
37         }
38         catch(exception& e) {
39                 cout << "Standard Error: " << e.what() << " has occurred in the ParseListCommand class Function ParseListCommand. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
40                 exit(1);
41         }
42         catch(...) {
43                 cout << "An unknown error has occurred in the ParseListCommand class function ParseListCommand. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
44                 exit(1);
45         }
46 }
47 /***********************************************************************/
48 void ParseListCommand::parse(int index, SharedListVector* list) {
49         try {
50                 string prefix, suffix, groupsName;
51                 suffix = list->get(index);
52         
53                 while (suffix.find_first_of(',') != -1) {//while you still have sequences
54                         prefix = suffix.substr(0,suffix.find_first_of(','));
55                         if ((suffix.find_first_of(',')+1) <= suffix.length()) {  //checks to make sure you don't have comma at end of string
56                                 suffix = suffix.substr(suffix.find_first_of(',')+1, suffix.length());
57                         }
58                         
59                         groupsName = groupMap->getGroup(prefix);
60                         if (groupsName != "not found") {
61                                 listGroups[groupsName] = listGroups[groupsName] + "," + prefix; //adds prefix to the correct group.
62                         }else {
63                                 cerr << "Error: Sequence '" << prefix << "' was not found in the group file, please correct\n";
64                         }
65                 }
66                 
67                 //save last name after comma
68                 groupsName = groupMap->getGroup(suffix);
69                 if (groupsName != "not found") {
70                         listGroups[groupsName] = listGroups[groupsName] + "," + suffix; //adds prefix to the correct group.
71                 }else {
72                         cerr << "Error: Sequence '" << suffix << "' was not found in the group file, please correct\n";
73                 }
74         }
75         catch(exception& e) {
76                 cout << "Standard Error: " << e.what() << " has occurred in the ParseListCommand class Function parse. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
77                 exit(1);
78         }
79         catch(...) {
80                 cout << "An unknown error has occurred in the ParseListCommand class function parse. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
81                 exit(1);
82         }
83 }
84
85 //**********************************************************************************************************************
86
87 int ParseListCommand::execute(){
88         try{
89                         globaldata = GlobalData::getInstance();
90                         int count = 1;
91                         
92                         //read in listfile
93                         read = new ReadOTUFile(globaldata->inputFileName);      
94                         read->read(&*globaldata); 
95                         input = globaldata->ginput;
96                         list = globaldata->gSharedList;
97                         SharedListVector* lastList = list;
98                 
99                         //if the users enters label "0.06" and there is no "0.06" in their file use the next lowest label.
100                         set<string> processedLabels;
101                         set<string> userLabels = globaldata->labels;
102
103                         //read in group map info.
104                         groupMap = new GroupMap(globaldata->getGroupFile());
105                         groupMap->readMap();
106                         
107                         //create new list vectors to fill with parsed data
108                         for (int i=0; i<groupMap->getNumGroups(); i++) {
109                                 groupOfLists[groupMap->namesOfGroups[i]] = new SharedListVector();
110                         }
111                         
112                                                 
113                         //parses and sets each groups listvector
114                         //as long as you are not at the end of the file or done wih the lines you want
115                         while((list != NULL) && ((globaldata->allLines == 1) || (userLabels.size() != 0))) {
116                                                                 
117                                 if(globaldata->allLines == 1 || globaldata->lines.count(count) == 1 || globaldata->labels.count(list->getLabel()) == 1){
118                                         cout << list->getLabel() << '\t' << count << endl;
119                                         process(list);
120                                         
121                                         processedLabels.insert(list->getLabel());
122                                         userLabels.erase(list->getLabel());
123                                 }
124                                 
125                                 if ((anyLabelsToProcess(list->getLabel(), userLabels, "") == true) && (processedLabels.count(lastList->getLabel()) != 1)) {
126                                         cout << lastList->getLabel() << '\t' << count << endl;
127                                         process(lastList);
128                                         
129                                         processedLabels.insert(lastList->getLabel());
130                                         userLabels.erase(lastList->getLabel());
131                                 }
132
133                                 if (count != 1) { delete lastList; }
134                                 lastList = list;                        
135
136                                 list = input->getSharedListVector();
137                                 count++;
138                         }
139                         
140                         //output error messages about any remaining user labels
141                         set<string>::iterator it;
142                         bool needToRun = false;
143                         for (it = userLabels.begin(); it != userLabels.end(); it++) {  
144                                 cout << "Your file does not include the label "<< *it; 
145                                 if (processedLabels.count(lastList->getLabel()) != 1) {
146                                         cout << ". I will use " << lastList->getLabel() << "." << endl;
147                                         needToRun = true;
148                                 }else {
149                                         cout << ". Please refer to " << lastList->getLabel() << "." << endl;
150                                 }
151                         }
152                 
153                         //run last line if you need to
154                         if (needToRun == true)  {
155                                 cout << lastList->getLabel() << '\t' << count << endl;
156                                 process(lastList);
157                         }
158                         delete lastList;
159
160                         //set groupmap for .shared commands
161                         if (globaldata->gGroupmap != NULL) { delete globaldata->gGroupmap; }
162                         globaldata->gGroupmap = groupMap; 
163                         
164                         return 0;
165         }
166         catch(exception& e) {
167                 cout << "Standard Error: " << e.what() << " has occurred in the ParseListCommand class Function execute. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
168                 exit(1);
169         }
170         catch(...) {
171                 cout << "An unknown error has occurred in the ParseListCommand class function execute. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
172                 exit(1);
173         }
174
175 }
176 //**********************************************************************************************************************
177
178 ParseListCommand::~ParseListCommand(){
179         delete list;
180         delete input;
181         delete read;    
182 }
183 //**********************************************************************************************************************
184 void ParseListCommand::process(SharedListVector* thisList) {
185         try {
186                         string seq;
187
188                         for(int i=0; i<thisList->size(); i++) {
189                                 parse(i, thisList); //parses data[i] list of sequence names
190                                 for (it=listGroups.begin(); it != listGroups.end(); it++) {  //loop through map and set new list vectors
191                                         seq = it->second;
192                                         seq = seq.substr(1, seq.length()); //rips off extra comma
193                                         groupOfLists[it->first]->push_back(seq); //sets new listvector for each group
194                                 }
195                                 listGroups.clear();
196                         }
197                         //prints each new list file
198                         for (int i=0; i<groupMap->getNumGroups(); i++) {
199                                 groupOfLists[groupMap->namesOfGroups[i]]->setLabel(thisList->getLabel());
200                                 groupOfLists[groupMap->namesOfGroups[i]]->print(*(filehandles[groupMap->namesOfGroups[i]]));
201                                 groupOfLists[groupMap->namesOfGroups[i]]->clear();
202                         }
203
204         }
205         catch(exception& e) {
206                 cout << "Standard Error: " << e.what() << " has occurred in the ParseListCommand class Function process. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
207                 exit(1);
208         }
209         catch(...) {
210                 cout << "An unknown error has occurred in the ParseListCommand class function process. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
211                 exit(1);
212         }
213 }