From: westcott Date: Mon, 22 Jun 2009 18:47:04 +0000 (+0000) Subject: worked on parselist and shared commands, removing memory leaks and allowing any numbe... X-Git-Url: https://git.donarmstrong.com/?p=mothur.git;a=commitdiff_plain;h=301b61cb98aaa4641f8701f43cf830c0fa44fac1 worked on parselist and shared commands, removing memory leaks and allowing any number of groups to be used by parselist. --- diff --git a/Mothur.xcodeproj/project.pbxproj b/Mothur.xcodeproj/project.pbxproj index 1407154..9da05bc 100644 --- a/Mothur.xcodeproj/project.pbxproj +++ b/Mothur.xcodeproj/project.pbxproj @@ -117,7 +117,6 @@ 37D928840F21331F001D4494 /* sabundvector.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 37D9281A0F21331F001D4494 /* sabundvector.cpp */; }; 37D928850F21331F001D4494 /* sequence.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 37D9281C0F21331F001D4494 /* sequence.cpp */; }; 37D928860F21331F001D4494 /* shannon.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 37D9281E0F21331F001D4494 /* shannon.cpp */; }; - 37D928870F21331F001D4494 /* shared.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 37D928200F21331F001D4494 /* shared.cpp */; }; 37D928880F21331F001D4494 /* sharedace.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 37D928220F21331F001D4494 /* sharedace.cpp */; }; 37D928890F21331F001D4494 /* sharedchao1.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 37D928240F21331F001D4494 /* sharedchao1.cpp */; }; 37D9288A0F21331F001D4494 /* sharedcommand.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 37D928260F21331F001D4494 /* sharedcommand.cpp */; }; @@ -412,8 +411,6 @@ 37D9281D0F21331F001D4494 /* sequence.hpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.h; path = sequence.hpp; sourceTree = SOURCE_ROOT; }; 37D9281E0F21331F001D4494 /* shannon.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = shannon.cpp; sourceTree = SOURCE_ROOT; }; 37D9281F0F21331F001D4494 /* shannon.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = shannon.h; sourceTree = SOURCE_ROOT; }; - 37D928200F21331F001D4494 /* shared.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = shared.cpp; sourceTree = SOURCE_ROOT; }; - 37D928210F21331F001D4494 /* shared.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = shared.h; sourceTree = SOURCE_ROOT; }; 37D928220F21331F001D4494 /* sharedace.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = sharedace.cpp; sourceTree = SOURCE_ROOT; }; 37D928230F21331F001D4494 /* sharedace.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = sharedace.h; sourceTree = SOURCE_ROOT; }; 37D928240F21331F001D4494 /* sharedchao1.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = sharedchao1.cpp; sourceTree = SOURCE_ROOT; }; @@ -594,8 +591,6 @@ 37D928050F21331F001D4494 /* rarefact.cpp */, 37D928090F21331F001D4494 /* rarefactioncurvedata.h */, 3796441D0FB9B9650081FDB6 /* read */, - 37D928210F21331F001D4494 /* shared.h */, - 37D928200F21331F001D4494 /* shared.cpp */, 211C38310F961DD400FEE541 /* sharedutilities.h */, 211C38300F961DD400FEE541 /* sharedutilities.cpp */, 37D928420F21331F001D4494 /* singlelinkage.cpp */, @@ -1001,7 +996,6 @@ 37D928840F21331F001D4494 /* sabundvector.cpp in Sources */, 37D928850F21331F001D4494 /* sequence.cpp in Sources */, 37D928860F21331F001D4494 /* shannon.cpp in Sources */, - 37D928870F21331F001D4494 /* shared.cpp in Sources */, 37D928880F21331F001D4494 /* sharedace.cpp in Sources */, 37D928890F21331F001D4494 /* sharedchao1.cpp in Sources */, 37D9288A0F21331F001D4494 /* sharedcommand.cpp in Sources */, diff --git a/fileoutput.h b/fileoutput.h index a047a20..0c7999d 100644 --- a/fileoutput.h +++ b/fileoutput.h @@ -145,6 +145,6 @@ private: string iters; }; -/***********************************************************************/ + #endif diff --git a/parselistcommand.cpp b/parselistcommand.cpp index f040307..8173bdc 100644 --- a/parselistcommand.cpp +++ b/parselistcommand.cpp @@ -15,24 +15,31 @@ ParseListCommand::ParseListCommand(){ globaldata = GlobalData::getInstance(); //read in group map info. - groupMap = new GroupMap(globaldata->getGroupFile()); - groupMap->readMap(); + //groupMap = new GroupMap(globaldata->getGroupFile()); + //groupMap->readMap(); + groupMap = globaldata->gGroupmap; //fill filehandles with neccessary ofstreams int i; ofstream* temp; + SharedListVector* templist; for (i=0; igetNumGroups(); i++) { temp = new ofstream; + templist = new SharedListVector(); filehandles[groupMap->namesOfGroups[i]] = temp; + mapOfLists[groupMap->namesOfGroups[i]] = templist; } //set fileroot fileroot = getRootName(globaldata->getListFile()); - //open output list files - for (i=0; igetNumGroups(); i++) {//opens an output file for each group + //clears file before we start to write to it below + for (int i=0; igetNumGroups(); i++) { openOutputFile(fileroot + groupMap->namesOfGroups[i] + ".list", *(filehandles[groupMap->namesOfGroups[i]])); + (*(filehandles[groupMap->namesOfGroups[i]])).close(); } + + } catch(exception& e) { cout << "Standard Error: " << e.what() << " has occurred in the ParseListCommand class Function ParseListCommand. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n"; @@ -46,29 +53,29 @@ ParseListCommand::ParseListCommand(){ /***********************************************************************/ void ParseListCommand::parse(int index, SharedListVector* list) { try { - string prefix, suffix, groupsName; - suffix = list->get(index); - - while (suffix.find_first_of(',') != -1) {//while you still have sequences - prefix = suffix.substr(0,suffix.find_first_of(',')); - if ((suffix.find_first_of(',')+1) <= suffix.length()) { //checks to make sure you don't have comma at end of string - suffix = suffix.substr(suffix.find_first_of(',')+1, suffix.length()); + string member, bin, groupName; + bin = list->get(index); + + while (bin.find_first_of(',') != -1) {//while you still have sequences + member = bin.substr(0,bin.find_first_of(',')); + if ((bin.find_first_of(',')+1) <= bin.length()) { //checks to make sure you don't have comma at end of string + bin = bin.substr(bin.find_first_of(',')+1, bin.length()); } - groupsName = groupMap->getGroup(prefix); - if (groupsName != "not found") { - listGroups[groupsName] = listGroups[groupsName] + "," + prefix; //adds prefix to the correct group. + groupName = groupMap->getGroup(member); + if (groupName != "not found") { + listGroups[groupName] = listGroups[groupName] + "," + member; //adds prefix to the correct group. }else { - cerr << "Error: Sequence '" << prefix << "' was not found in the group file, please correct\n"; + cerr << "Error: Sequence '" << member << "' was not found in the group file, please correct\n"; } } //save last name after comma - groupsName = groupMap->getGroup(suffix); - if (groupsName != "not found") { - listGroups[groupsName] = listGroups[groupsName] + "," + suffix; //adds prefix to the correct group. + groupName = groupMap->getGroup(bin); + if (groupName != "not found") { + listGroups[groupName] = listGroups[groupName] + "," + bin; //adds prefix to the correct group. }else { - cerr << "Error: Sequence '" << suffix << "' was not found in the group file, please correct\n"; + cerr << "Error: Sequence '" << bin << "' was not found in the group file, please correct\n"; } } catch(exception& e) { @@ -85,6 +92,7 @@ void ParseListCommand::parse(int index, SharedListVector* list) { int ParseListCommand::execute(){ try{ + int count = 1; //read in listfile @@ -99,12 +107,6 @@ int ParseListCommand::execute(){ set userLabels = globaldata->labels; set userLines = globaldata->lines; - //create new list vectors to fill with parsed data - for (int i=0; igetNumGroups(); i++) { - groupOfLists[groupMap->namesOfGroups[i]] = new SharedListVector(); - } - - //parses and sets each groups listvector //as long as you are not at the end of the file or done wih the lines you want while((list != NULL) && ((globaldata->allLines == 1) || (userLabels.size() != 0) || (userLines.size() != 0))) { @@ -151,24 +153,20 @@ int ParseListCommand::execute(){ cout << lastList->getLabel() << '\t' << count << endl; process(lastList); } - delete lastList; - - //set groupmap for .shared commands - if (globaldata->gGroupmap != NULL) { delete globaldata->gGroupmap; } - globaldata->gGroupmap = groupMap; - - //close files - for (it3 = filehandles.begin(); it3 != filehandles.end(); it3++) { - ofstream* temp = it3->second; - (*temp).close(); - delete it3->second; - } + delete lastList; globaldata->gSharedList = NULL; //delete list vectors to fill with parsed data - for (it2 = groupOfLists.begin(); it2 != groupOfLists.end(); it2++) { + for (it2 = mapOfLists.begin(); it2 != mapOfLists.end(); it2++) { + delete it2->second; + } + for (it3 = filehandles.begin(); it3 != filehandles.end(); it3++) { delete it2->second; } + + delete input; globaldata->ginput = NULL; + delete read; + return 0; } catch(exception& e) { @@ -184,11 +182,8 @@ int ParseListCommand::execute(){ //********************************************************************************************************************** ParseListCommand::~ParseListCommand(){ - - globaldata->gSharedList = NULL; - delete input; globaldata->ginput = NULL; - delete read; - + + } //********************************************************************************************************************** void ParseListCommand::process(SharedListVector* thisList) { @@ -200,15 +195,17 @@ void ParseListCommand::process(SharedListVector* thisList) { for (it=listGroups.begin(); it != listGroups.end(); it++) { //loop through map and set new list vectors seq = it->second; seq = seq.substr(1, seq.length()); //rips off extra comma - groupOfLists[it->first]->push_back(seq); //sets new listvector for each group + mapOfLists[it->first]->push_back(seq); //sets new listvector for each group } listGroups.clear(); } //prints each new list file for (int i=0; igetNumGroups(); i++) { - groupOfLists[groupMap->namesOfGroups[i]]->setLabel(thisList->getLabel()); - groupOfLists[groupMap->namesOfGroups[i]]->print(*(filehandles[groupMap->namesOfGroups[i]])); - groupOfLists[groupMap->namesOfGroups[i]]->clear(); + openOutputFileAppend(fileroot + groupMap->namesOfGroups[i] + ".list", *(filehandles[groupMap->namesOfGroups[i]])); + mapOfLists[groupMap->namesOfGroups[i]]->setLabel(thisList->getLabel()); + mapOfLists[groupMap->namesOfGroups[i]]->print(*(filehandles[groupMap->namesOfGroups[i]])); + mapOfLists[groupMap->namesOfGroups[i]]->clear(); + (*(filehandles[groupMap->namesOfGroups[i]])).close(); } } diff --git a/parselistcommand.h b/parselistcommand.h index b3a2f9c..9e9e968 100644 --- a/parselistcommand.h +++ b/parselistcommand.h @@ -40,7 +40,7 @@ private: InputData* input; ReadOTUFile* read; map filehandles; - map groupOfLists; + map mapOfLists; SharedListVector* list; map listGroups; //maps group name to sequences from that group in a specific OTU map::iterator it; diff --git a/shared.cpp b/shared.cpp deleted file mode 100644 index b15ef93..0000000 --- a/shared.cpp +++ /dev/null @@ -1,74 +0,0 @@ -/* - * shared.cpp - * Dotur - * - * Created by Sarah Westcott on 12/5/08. - * Copyright 2008 Schloss Lab UMASS Amherst. All rights reserved. - * - */ - -#include "shared.h" - -/**************************************************************************************************/ - -Shared::Shared(){ - globaldata = GlobalData::getInstance(); -} - -/**************************************************************************************************/ -void Shared::getSharedVectors(SharedListVector* list) { - string label, group; - int i,j; - label = list->getLabel(); - - for (it = sharedGroups.begin(); it != sharedGroups.end(); it++) { delete it->second; } - sharedGroups.clear(); //removes old info. - - //initalize sharedGroups - for (j=0; jgGroupmap->getNumGroups(); j++) {//for each group - group = globaldata->gGroupmap->namesOfGroups[j]; - sharedGroups[group] = new SharedRAbundVector(); - sharedGroups[group]->setLabel(label); - for (i = 0; isize(); i++) { //for each otu - sharedGroups[group]->push_back(0, i, group); //initialize to 0. - } - } - - //fills sharedGroups - for (i = 0; isize(); i++) { - parse(i, list); - } - - //updates sharedVector - //sharedRAbund.push_back(sharedGroups); -} - - - -/***********************************************************************/ -void Shared::parse(int index, SharedListVector* list) { - - string prefix, suffix, groupsName; - suffix = list->get(index); - - while (suffix.find_first_of(',') != -1) {//while you still have sequences - prefix = suffix.substr(0,suffix.find_first_of(',')); - if ((suffix.find_first_of(',')+1) <= suffix.length()) { //checks to make sure you don't have comma at end of string - suffix = suffix.substr(suffix.find_first_of(',')+1, suffix.length()); - } - groupsName = globaldata->gGroupmap->getGroup(prefix); - if (groupsName != "not found") { - sharedGroups[groupsName]->set(index, (sharedGroups[groupsName]->getAbundance(index) + 1), groupsName); //increment shared vector for that group - }else { - cerr << "Error: Sequence '" << prefix << "' was not found in the group file, please correct\n"; - } - } - - //save last name after comma - groupsName = globaldata->gGroupmap->getGroup(suffix); - if (groupsName != "not found") { - sharedGroups[groupsName]->set(index, ((sharedGroups[groupsName]->getAbundance(index)) + 1), groupsName); //increment shared vector for that group - }else { - cerr << "Error: Sequence '" << suffix << "' was not found in the group file, please correct\n"; - } - } diff --git a/shared.h b/shared.h deleted file mode 100644 index 515c2a0..0000000 --- a/shared.h +++ /dev/null @@ -1,32 +0,0 @@ -#ifndef SHARED_H -#define SHARED_H - -/* - * shared.h - * Dotur - * - * Created by Sarah Westcott on 12/5/08. - * Copyright 2008 Schloss Lab UMASS Amherst. All rights reserved. - * - */ - -#include "mothur.h" -#include "sharedrabundvector.h" -#include "sharedlistvector.h" -#include "globaldata.hpp" - -class Shared { - public: - Shared(); - ~Shared() {}; - void getSharedVectors(SharedListVector*); - map sharedGroups; //string is groupname, SharedVector* is out info for that group - - private: - GlobalData* globaldata; - map::iterator it; - void parse(int, SharedListVector*); - //vector< map > sharedRAbund; //contains all the info needed to create the .shared file not sure if we will need -}; - -#endif diff --git a/sharedcommand.cpp b/sharedcommand.cpp index 42601f4..439cdf1 100644 --- a/sharedcommand.cpp +++ b/sharedcommand.cpp @@ -35,7 +35,8 @@ SharedCommand::SharedCommand(){ int SharedCommand::execute(){ try { - globaldata = GlobalData::getInstance(); + + cout << "creating sharedfile..."; //lookup.clear(); int count = 1; string errorOff = "no error"; @@ -47,23 +48,21 @@ int SharedCommand::execute(){ input = globaldata->ginput; SharedList = globaldata->gSharedList; SharedListVector* lastList = SharedList; - //lookup = SharedList->getSharedRAbundVector(); - //vector lastLookup = lookup; - + vector lookup; + //if the users enters label "0.06" and there is no "0.06" in their file use the next lowest label. set processedLabels; set userLabels = globaldata->labels; set userLines = globaldata->lines; - shared = new Shared(); while((SharedList != NULL) && ((globaldata->allLines == 1) || (userLabels.size() != 0) || (userLines.size() != 0))) { if(globaldata->allLines == 1 || globaldata->lines.count(count) == 1 || globaldata->labels.count(SharedList->getLabel()) == 1){ - - shared->getSharedVectors(SharedList); //fills sharedGroups with new info and updates sharedVector - printSharedData(); //prints info to the .shared file + lookup = SharedList->getSharedRAbundVector(); + printSharedData(lookup); //prints info to the .shared file + for (int i = 0; i < lookup.size(); i++) { delete lookup[i]; } processedLabels.insert(SharedList->getLabel()); userLabels.erase(SharedList->getLabel()); @@ -71,21 +70,20 @@ int SharedCommand::execute(){ } if ((anyLabelsToProcess(SharedList->getLabel(), userLabels, errorOff) == true) && (processedLabels.count(lastList->getLabel()) != 1)) { - shared->getSharedVectors(lastList); //fills sharedGroups with new info and updates sharedVector - printSharedData(); //prints info to the .shared file - + lookup = lastList->getSharedRAbundVector(); + printSharedData(lookup); //prints info to the .shared file + for (int i = 0; i < lookup.size(); i++) { delete lookup[i]; } + processedLabels.insert(lastList->getLabel()); userLabels.erase(lastList->getLabel()); } if (count != 1) { delete lastList; } - lastList = SharedList; + lastList = SharedList; + SharedList = input->getSharedListVector(); //get new list vector to process - count++; - //if (count != 1) { for (int i = 0; i < lastLookup.size(); i++) { delete lastLookup[i]; } } - //lastLookup = lookup; - //if (SharedList != NULL) { lookup = SharedList->getSharedRAbundVector(); } + count++; } //output error messages about any remaining user labels @@ -103,14 +101,18 @@ int SharedCommand::execute(){ //run last line if you need to if (needToRun == true) { - shared->getSharedVectors(lastList); //fills sharedGroups with new info and updates sharedVector - printSharedData(); //prints info to the .shared file + lookup = lastList->getSharedRAbundVector(); + printSharedData(lookup); //prints info to the .shared file + for (int i = 0; i < lookup.size(); i++) { delete lookup[i]; } + } delete lastList; globaldata->gSharedList = NULL; - delete shared; + delete read; + out.close(); + cout << "complete." << endl; return 0; } catch(exception& e) { @@ -124,19 +126,15 @@ int SharedCommand::execute(){ } //********************************************************************************************************************** -void SharedCommand::printSharedData() { +void SharedCommand::printSharedData(vector thislookup) { try { - - ///for (int i = 0; i < thislookup.size(); i++) { - // out << thislookup[i]->getLabel() << '\t' << thislookup[i]->getGroup() << '\t'; -//cout << thislookup[i]->getLabel() << '\t' << thislookup[i]->getGroup() << endl; - // thislookup[i]->print(out); - // } - //prints out horizontally - for (it = shared->sharedGroups.begin(); it != shared->sharedGroups.end(); it++) { - out << it->second->getLabel() << "\t" << it->first << "\t"; //prints out label and groupname - it->second->print(out); // prints sharedrabundvector + + //initialize bin values + for (int i = 0; i < thislookup.size(); i++) { + out << thislookup[i]->getLabel() << '\t' << thislookup[i]->getGroup() << '\t'; + thislookup[i]->print(out); } + } catch(exception& e) { cout << "Standard Error: " << e.what() << " has occurred in the SharedCommand class Function printSharedData. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n"; @@ -153,7 +151,7 @@ void SharedCommand::printSharedData() { SharedCommand::~SharedCommand(){ //delete list; - delete read; + } diff --git a/sharedcommand.h b/sharedcommand.h index e5be50a..ca0ddd8 100644 --- a/sharedcommand.h +++ b/sharedcommand.h @@ -12,7 +12,6 @@ #include "command.hpp" #include "sharedlistvector.h" #include "inputdata.h" -#include "shared.h" #include "readotu.h" /* The shared() command: @@ -33,16 +32,16 @@ public: void help() {} private: - void printSharedData(); + void printSharedData(vector); GlobalData* globaldata; ReadOTUFile* read; SharedListVector* SharedList; InputData* input; - Shared* shared; - map::iterator it; + //map::iterator it; //vector lookup; ofstream out; string filename; + bool firsttime; }; diff --git a/sharedlistvector.cpp b/sharedlistvector.cpp index 4774180..3cb3e9e 100644 --- a/sharedlistvector.cpp +++ b/sharedlistvector.cpp @@ -16,11 +16,11 @@ /***********************************************************************/ -SharedListVector::SharedListVector() : DataVector(), maxRank(0), numBins(0), numSeqs(0){globaldata = GlobalData::getInstance();} +SharedListVector::SharedListVector() : DataVector(), maxRank(0), numBins(0), numSeqs(0){globaldata = GlobalData::getInstance(); groupmap = NULL; } /***********************************************************************/ -SharedListVector::SharedListVector(int n): DataVector(), data(n, "") , maxRank(0), numBins(0), numSeqs(0){globaldata = GlobalData::getInstance();} +SharedListVector::SharedListVector(int n): DataVector(), data(n, "") , maxRank(0), numBins(0), numSeqs(0){globaldata = GlobalData::getInstance(); groupmap = NULL; } /***********************************************************************/ SharedListVector::SharedListVector(ifstream& f) : DataVector(), maxRank(0), numBins(0), numSeqs(0) { @@ -291,17 +291,38 @@ vector SharedListVector::getSharedRAbundVector() { SharedUtil* util; util = new SharedUtil(); vector lookup; + map finder; + string group, names, name; util->setGroups(globaldata->Groups, globaldata->gGroupmap->namesOfGroups); delete util; for (int i = 0; i < globaldata->Groups.size(); i++) { - SharedRAbundVector* temp = new SharedRAbundVector(); - *temp = getSharedRAbundVector(globaldata->Groups[i]); - lookup.push_back(temp); + SharedRAbundVector* temp = new SharedRAbundVector(data.size()); + finder[globaldata->Groups[i]] = temp; + finder[globaldata->Groups[i]]->setLabel(label); + finder[globaldata->Groups[i]]->setGroup(globaldata->Groups[i]); + //*temp = getSharedRAbundVector(globaldata->Groups[i]); + lookup.push_back(finder[globaldata->Groups[i]]); } - + + //fill vectors + for(int i=0;igetGroup(name); + finder[group]->set(i, finder[group]->getAbundance(i) + 1, group); //i represents what bin you are in + } + + //get last name + group = groupmap->getGroup(names); + finder[group]->set(i, finder[group]->getAbundance(i) + 1, group); //i represents what bin you are in + + } + return lookup; } catch(exception& e) { diff --git a/sharedlistvector.h b/sharedlistvector.h index 2c8d01b..0586dff 100644 --- a/sharedlistvector.h +++ b/sharedlistvector.h @@ -33,8 +33,8 @@ public: SharedListVector(); SharedListVector(int); SharedListVector(ifstream&); - SharedListVector(const SharedListVector& lv) : DataVector(lv.label), data(lv.data), maxRank(lv.maxRank), numBins(lv.numBins), numSeqs(lv.numSeqs){globaldata = GlobalData::getInstance();}; - ~SharedListVector(){}; + SharedListVector(const SharedListVector& lv) : DataVector(lv.label), data(lv.data), maxRank(lv.maxRank), numBins(lv.numBins), numSeqs(lv.numSeqs){globaldata = GlobalData::getInstance(); groupmap = NULL; }; + ~SharedListVector(){ if (groupmap != NULL) { delete groupmap; } }; int getNumBins() { return numBins; } int getNumSeqs() { return numSeqs; }