5 * Created by Sarah Westcott on 1/2/09.
6 * Copyright 2009 Schloss Lab UMASS Amherst. All rights reserved.
10 #include "parselistcommand.h"
12 //**********************************************************************************************************************
13 ParseListCommand::ParseListCommand(){
15 globaldata = GlobalData::getInstance();
17 //read in group map info.
18 groupMap = new GroupMap(globaldata->getGroupFile());
22 //fill filehandles with neccessary ofstreams
25 for (i=0; i<groupMap->getNumGroups(); i++) {
27 filehandles[groupMap->namesOfGroups[i]] = temp;
31 fileroot = getRootName(globaldata->getListFile());
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]]));
39 cout << "Standard Error: " << e.what() << " has occurred in the ParseListCommand class Function ParseListCommand. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
43 cout << "An unknown error has occurred in the ParseListCommand class function ParseListCommand. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
47 /***********************************************************************/
48 void ParseListCommand::parse(int index, SharedListVector* list) {
50 string prefix, suffix, groupsName;
51 suffix = list->get(index);
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());
59 groupsName = groupMap->getGroup(prefix);
60 if (groupsName != "not found") {
61 listGroups[groupsName] = listGroups[groupsName] + "," + prefix; //adds prefix to the correct group.
63 cerr << "Error: Sequence '" << prefix << "' was not found in the group file, please correct\n";
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.
72 cerr << "Error: Sequence '" << suffix << "' was not found in the group file, please correct\n";
76 cout << "Standard Error: " << e.what() << " has occurred in the ParseListCommand class Function parse. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
80 cout << "An unknown error has occurred in the ParseListCommand class function parse. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
85 //**********************************************************************************************************************
87 int ParseListCommand::execute(){
89 globaldata = GlobalData::getInstance();
93 read = new ReadOTUFile(globaldata->inputFileName);
94 read->read(&*globaldata);
95 input = globaldata->ginput;
96 list = globaldata->gSharedList;
97 SharedListVector* lastList = list;
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;
103 //read in group map info.
104 groupMap = new GroupMap(globaldata->getGroupFile());
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();
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))) {
117 if(globaldata->allLines == 1 || globaldata->lines.count(count) == 1 || globaldata->labels.count(list->getLabel()) == 1){
118 cout << list->getLabel() << '\t' << count << endl;
121 processedLabels.insert(list->getLabel());
122 userLabels.erase(list->getLabel());
125 if ((anyLabelsToProcess(list->getLabel(), userLabels, "") == true) && (processedLabels.count(lastList->getLabel()) != 1)) {
126 cout << lastList->getLabel() << '\t' << count << endl;
129 processedLabels.insert(lastList->getLabel());
130 userLabels.erase(lastList->getLabel());
133 if (count != 1) { delete lastList; }
136 list = input->getSharedListVector();
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;
149 cout << ". Please refer to " << lastList->getLabel() << "." << endl;
153 //run last line if you need to
154 if (needToRun == true) {
155 cout << lastList->getLabel() << '\t' << count << endl;
160 //set groupmap for .shared commands
161 if (globaldata->gGroupmap != NULL) { delete globaldata->gGroupmap; }
162 globaldata->gGroupmap = groupMap;
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";
171 cout << "An unknown error has occurred in the ParseListCommand class function execute. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
176 //**********************************************************************************************************************
178 ParseListCommand::~ParseListCommand(){
183 //**********************************************************************************************************************
184 void ParseListCommand::process(SharedListVector* thisList) {
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
192 seq = seq.substr(1, seq.length()); //rips off extra comma
193 groupOfLists[it->first]->push_back(seq); //sets new listvector for each group
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();
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";
210 cout << "An unknown error has occurred in the ParseListCommand class function process. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";