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());
21 //fill filehandles with neccessary ofstreams
24 for (i=0; i<groupMap->getNumGroups(); i++) {
26 filehandles[groupMap->namesOfGroups[i]] = temp;
30 fileroot = getRootName(globaldata->getListFile());
32 //open output list files
33 for (i=0; i<groupMap->getNumGroups(); i++) {//opens an output file for each group
34 openOutputFile(fileroot + groupMap->namesOfGroups[i] + ".list", *(filehandles[groupMap->namesOfGroups[i]]));
38 cout << "Standard Error: " << e.what() << " has occurred in the ParseListCommand class Function ParseListCommand. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
42 cout << "An unknown error has occurred in the ParseListCommand class function ParseListCommand. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
46 /***********************************************************************/
47 void ParseListCommand::parse(int index) {
49 string prefix, suffix, groupsName;
50 suffix = list->get(index);
52 while (suffix.find_first_of(',') != -1) {//while you still have sequences
53 prefix = suffix.substr(0,suffix.find_first_of(','));
54 if ((suffix.find_first_of(',')+1) <= suffix.length()) { //checks to make sure you don't have comma at end of string
55 suffix = suffix.substr(suffix.find_first_of(',')+1, suffix.length());
58 groupsName = groupMap->getGroup(prefix);
59 if (groupsName != "not found") {
60 listGroups[groupsName] = listGroups[groupsName] + "," + prefix; //adds prefix to the correct group.
62 cerr << "Error: Sequence '" << prefix << "' was not found in the group file, please correct\n";
66 //save last name after comma
67 groupsName = groupMap->getGroup(suffix);
68 if (groupsName != "not found") {
69 listGroups[groupsName] = listGroups[groupsName] + "," + suffix; //adds prefix to the correct group.
71 cerr << "Error: Sequence '" << suffix << "' was not found in the group file, please correct\n";
75 cout << "Standard Error: " << e.what() << " has occurred in the ParseListCommand class Function parse. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
79 cout << "An unknown error has occurred in the ParseListCommand class function parse. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
84 //**********************************************************************************************************************
86 int ParseListCommand::execute(){
88 globaldata = GlobalData::getInstance();
92 read = new ReadOTUFile(globaldata->inputFileName);
93 read->read(&*globaldata);
94 input = globaldata->ginput;
95 list = globaldata->gSharedList;
97 //read in group map info.
98 groupMap = new GroupMap(globaldata->getGroupFile());
103 //create new list vectors to fill with parsed data
104 for (i=0; i<groupMap->getNumGroups(); i++) {
105 groupOfLists[groupMap->namesOfGroups[i]] = new SharedListVector();
108 //parses and sets each groups listvector
110 label = list->getLabel();
112 if(globaldata->allLines == 1 || globaldata->lines.count(count) == 1 || globaldata->labels.count(label) == 1){
114 for(i=0; i<list->size(); i++) {
115 parse(i); //parses data[i] list of sequence names
116 for (it=listGroups.begin(); it != listGroups.end(); it++) { //loop through map and set new list vectors
118 seq = seq.substr(1, seq.length()); //rips off extra comma
119 groupOfLists[it->first]->push_back(seq); //sets new listvector for each group
123 //prints each new list file
124 for (i=0; i<groupMap->getNumGroups(); i++) {
125 groupOfLists[groupMap->namesOfGroups[i]]->setLabel(label);
126 groupOfLists[groupMap->namesOfGroups[i]]->print(*(filehandles[groupMap->namesOfGroups[i]]));
127 groupOfLists[groupMap->namesOfGroups[i]]->clear();
130 cout << label << '\t' << count << endl;
133 list = input->getSharedListVector();
137 //set groupmap for .shared commands
138 //if (globaldata->gGroupmap != NULL) { delete globaldata->gGroupmap; }
139 globaldata->gGroupmap = groupMap;
143 catch(exception& e) {
144 cout << "Standard Error: " << e.what() << " has occurred in the ParseListCommand class Function execute. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
148 cout << "An unknown error has occurred in the ParseListCommand class function execute. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
153 //**********************************************************************************************************************
155 ParseListCommand::~ParseListCommand(){
160 //**********************************************************************************************************************