From: westcott Date: Tue, 22 Jun 2010 12:40:41 +0000 (+0000) Subject: added mpi code to cluster.split command X-Git-Url: https://git.donarmstrong.com/?p=mothur.git;a=commitdiff_plain;h=4ee9d3c973fb056f3105c26306da3b5d7dfb8108 added mpi code to cluster.split command --- diff --git a/degapseqscommand.cpp b/degapseqscommand.cpp new file mode 100644 index 0000000..c6c4b55 --- /dev/null +++ b/degapseqscommand.cpp @@ -0,0 +1,160 @@ +/* + * degapseqscommand.cpp + * Mothur + * + * Created by westcott on 6/21/10. + * Copyright 2010 Schloss Lab. All rights reserved. + * + */ + +#include "degapseqscommand.h" +#include "sequence.hpp" + +//*************************************************************************************************************** + +DegapSeqsCommand::DegapSeqsCommand(string option) { + try { + abort = false; + + //allow user to run help + if(option == "help") { help(); abort = true; } + + else { + //valid paramters for this command + string Array[] = {"fasta", "outputdir","inputdir"}; + vector myArray (Array, Array+(sizeof(Array)/sizeof(string))); + + OptionParser parser(option); + map parameters = parser.getParameters(); + + ValidParameters validParameter; + map::iterator it; + + //check to make sure all parameters are valid for command + for (it = parameters.begin(); it != parameters.end(); it++) { + if (validParameter.isValidParameter(it->first, myArray, it->second) != true) { abort = true; } + } + + //if the user changes the input directory command factory will send this info to us in the output parameter + string inputDir = validParameter.validFile(parameters, "inputdir", false); + if (inputDir == "not found"){ inputDir = ""; } + + //check for required parameters + fastafile = validParameter.validFile(parameters, "fasta", false); + if (fastafile == "not found") { fastafile = ""; m->mothurOut("fasta is a required parameter for the degap.seqs command."); m->mothurOutEndLine(); abort = true; } + else { + splitAtDash(fastafile, fastaFileNames); + + //go through files and make sure they are good, if not, then disregard them + for (int i = 0; i < fastaFileNames.size(); i++) { + if (inputDir != "") { + string path = hasPath(fastaFileNames[i]); + //if the user has not given a path then, add inputdir. else leave path alone. + if (path == "") { fastaFileNames[i] = inputDir + fastaFileNames[i]; } + } + + ifstream in; + int ableToOpen = openInputFile(fastaFileNames[i], in); + in.close(); + + if (ableToOpen == 1) { + m->mothurOut(fastaFileNames[i] + " will be disregarded."); m->mothurOutEndLine(); + //erase from file list + fastaFileNames.erase(fastaFileNames.begin()+i); + i--; + } + } + + //make sure there is at least one valid file left + if (fastaFileNames.size() == 0) { m->mothurOut("no valid files."); m->mothurOutEndLine(); abort = true; } + } + + + //if the user changes the output directory command factory will send this info to us in the output parameter + outputDir = validParameter.validFile(parameters, "outputdir", false); if (outputDir == "not found"){ + outputDir = ""; + outputDir += hasPath(fastafile); //if user entered a file with a path then preserve it + } + + } + } + catch(exception& e) { + m->errorOut(e, "DegapSeqsCommand", "DegapSeqsCommand"); + exit(1); + } +} +//********************************************************************************************************************** + +void DegapSeqsCommand::help(){ + try { + m->mothurOut("The degap.seqs command reads a fastafile and removes all gap characters.\n"); + m->mothurOut("The degap.seqs command parameter is fasta.\n"); + m->mothurOut("The fasta parameter allows you to enter the fasta file containing your sequences, and is required. \n"); + m->mothurOut("You may enter multiple fasta files by separating their names with dashes. ie. fasta=abrecovery.fasta-amzon.fasta \n"); + m->mothurOut("The degap.seqs command should be in the following format: \n"); + m->mothurOut("degap.seqs(fasta=yourFastaFile) \n"); + m->mothurOut("Example: degap.seqs(fasta=abrecovery.align) \n"); + m->mothurOut("Note: No spaces between parameter labels (i.e. fasta), '=' and parameters (i.e.yourFastaFile).\n\n"); + } + catch(exception& e) { + m->errorOut(e, "DegapSeqsCommand", "help"); + exit(1); + } +} + +//*************************************************************************************************************** + +DegapSeqsCommand::~DegapSeqsCommand(){ /* do nothing */ } + +//*************************************************************************************************************** + + +int DegapSeqsCommand::execute(){ + try{ + + if (abort == true) { return 0; } + + for (int s = 0; s < fastaFileNames.size(); s++) { + + m->mothurOut("Degapping sequences from " + fastaFileNames[s] + " ..." ); m->mothurOutEndLine(); + ifstream inFASTA; + openInputFile(fastaFileNames[s], inFASTA); + + ofstream outFASTA; + string degapFile = outputDir + getRootName(getSimpleName(fastaFileNames[s])) + "ng.fasta"; + openOutputFile(degapFile, outFASTA); + + while(!inFASTA.eof()){ + if (m->control_pressed) { inFASTA.close(); outFASTA.close(); remove(degapFile.c_str()); for (int j = 0; j < outputNames.size(); j++) { remove(outputNames[j].c_str()); } return 0; } + + Sequence currSeq(inFASTA); gobble(inFASTA); + if (currSeq.getName() != "") { + outFASTA << ">" << currSeq.getName() << endl; + outFASTA << currSeq.getUnaligned() << endl; + } + } + inFASTA.close(); + outFASTA.close(); + + outputNames.push_back(degapFile); + + if (m->control_pressed) { remove(degapFile.c_str()); for (int j = 0; j < outputNames.size(); j++) { remove(outputNames[j].c_str()); } return 0; } + } + + m->mothurOutEndLine(); + m->mothurOut("Output File Name: "); m->mothurOutEndLine(); + for (int i = 0; i < outputNames.size(); i++) { m->mothurOut(outputNames[i]); m->mothurOutEndLine(); } + m->mothurOutEndLine(); + + + return 0; + + } + catch(exception& e) { + m->errorOut(e, "DegapSeqsCommand", "execute"); + exit(1); + } +} + +//*************************************************************************************************************** + diff --git a/degapseqscommand.h b/degapseqscommand.h new file mode 100644 index 0000000..192d6eb --- /dev/null +++ b/degapseqscommand.h @@ -0,0 +1,33 @@ +#ifndef DEGAPSEQSCOMMAND_H +#define DEGAPSEQSCOMMAND_H + +/* + * degapseqscommand.h + * Mothur + * + * Created by westcott on 6/21/10. + * Copyright 2010 Schloss Lab. All rights reserved. + * + */ + + +#include "command.hpp" + +class DegapSeqsCommand : public Command { +public: + DegapSeqsCommand(string); + ~DegapSeqsCommand(); + int execute(); + void help(); + +private: + + bool abort; + string fastafile, outputDir; + vector outputNames; + vector fastaFileNames; + +}; + +#endif + diff --git a/getrelabundcommand.cpp b/getrelabundcommand.cpp new file mode 100644 index 0000000..4fd95b2 --- /dev/null +++ b/getrelabundcommand.cpp @@ -0,0 +1,233 @@ +/* + * getrelabundcommand.cpp + * Mothur + * + * Created by westcott on 6/21/10. + * Copyright 2010 Schloss Lab. All rights reserved. + * + */ + +#include "getrelabundcommand.h" + +//********************************************************************************************************************** + +GetRelAbundCommand::GetRelAbundCommand(string option) { + try { + globaldata = GlobalData::getInstance(); + abort = false; + allLines = 1; + labels.clear(); + + //allow user to run help + if(option == "help") { help(); abort = true; } + + else { + //valid paramters for this command + string AlignArray[] = {"groups","label","scale","outputdir","inputdir"}; + vector myArray (AlignArray, AlignArray+(sizeof(AlignArray)/sizeof(string))); + + OptionParser parser(option); + map parameters = parser.getParameters(); + + ValidParameters validParameter; + + //check to make sure all parameters are valid for command + for (map::iterator it = parameters.begin(); it != parameters.end(); it++) { + if (validParameter.isValidParameter(it->first, myArray, it->second) != true) { abort = true; } + } + + //if the user changes the output directory command factory will send this info to us in the output parameter + outputDir = validParameter.validFile(parameters, "outputdir", false); if (outputDir == "not found"){ + outputDir = ""; + outputDir += hasPath(globaldata->inputFileName); //if user entered a file with a path then preserve it + } + + //make sure the user has already run the read.otu command + if ((globaldata->getSharedFile() == "")) { + m->mothurOut("You must read a list and a group, or a shared file before you can use the get.relabund command."); m->mothurOutEndLine(); abort = true; + } + + //check for optional parameter and set defaults + // ...at some point should added some additional type checking... + label = validParameter.validFile(parameters, "label", false); + if (label == "not found") { label = ""; } + else { + if(label != "all") { splitAtDash(label, labels); allLines = 0; } + else { allLines = 1; } + } + + //if the user has not specified any labels use the ones from read.otu + if (label == "") { + allLines = globaldata->allLines; + labels = globaldata->labels; + } + + groups = validParameter.validFile(parameters, "groups", false); + if (groups == "not found") { groups = ""; } + else { + splitAtDash(groups, Groups); + globaldata->Groups = Groups; + } + + scale = validParameter.validFile(parameters, "scale", false); if (scale == "not found") { scale = "totalgroup"; } + + if ((scale != "totalgroup") && (scale != "totalgroup") && (scale != "totalgroup") && (scale != "totalgroup")) { + m->mothurOut(scale + " is not a valid scaling option for the get.relabund command. Choices are totalgroup, totalotu, averagegroup, averageotu."); m->mothurOutEndLine(); abort = true; + } + } + + } + catch(exception& e) { + m->errorOut(e, "GetRelAbundCommand", "GetRelAbundCommand"); + exit(1); + } +} + +//********************************************************************************************************************** + +void GetRelAbundCommand::help(){ + try { + m->mothurOut("The get.relabund command can only be executed after a successful read.otu command of a list and group or shared file.\n"); + m->mothurOut("The get.relabund command parameters are groups, scale and label. No parameters are required.\n"); + m->mothurOut("The groups parameter allows you to specify which of the groups in your groupfile you would like included. The group names are separated by dashes.\n"); + m->mothurOut("The label parameter allows you to select what distance levels you would like, and are also separated by dashes.\n"); + m->mothurOut("The scale parameter allows you to select what scale you would like to use. Choices are totalgroup, totalotu, averagegroup, averageotu, default is totalgroup.\n"); + m->mothurOut("The get.relabund command should be in the following format: get.relabund(groups=yourGroups, label=yourLabels).\n"); + m->mothurOut("Example get.relabund(groups=A-B-C, scale=log10).\n"); + m->mothurOut("The default value for groups is all the groups in your groupfile, and all labels in your inputfile will be used.\n"); + m->mothurOut("The get.relabund command outputs a .relabund file.\n"); + m->mothurOut("Note: No spaces between parameter labels (i.e. groups), '=' and parameters (i.e.yourGroups).\n\n"); + + } + catch(exception& e) { + m->errorOut(e, "GetRelAbundCommand", "help"); + exit(1); + } +} + +//********************************************************************************************************************** + +GetRelAbundCommand::~GetRelAbundCommand(){ +} + +//********************************************************************************************************************** + +int GetRelAbundCommand::execute(){ + try { + + if (abort == true) { return 0; } + + string outputFileName = outputDir + getRootName(getSimpleName(globaldata->inputFileName)) + "relabund"; + ofstream out; + openOutputFile(outputFileName, out); + + read = new ReadOTUFile(globaldata->inputFileName); + read->read(&*globaldata); + input = globaldata->ginput; + lookup = input->getSharedRAbundVectors(); + string lastLabel = lookup[0]->getLabel(); + + //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 = labels; + + //as long as you are not at the end of the file or done wih the lines you want + while((lookup[0] != NULL) && ((allLines == 1) || (userLabels.size() != 0))) { + + if (m->control_pressed) { for (int i = 0; i < lookup.size(); i++) { delete lookup[i]; } globaldata->Groups.clear(); delete read; out.close(); remove(outputFileName.c_str()); return 0; } + + if(allLines == 1 || labels.count(lookup[0]->getLabel()) == 1){ + + m->mothurOut(lookup[0]->getLabel()); m->mothurOutEndLine(); + getRelAbundance(lookup, out); + + processedLabels.insert(lookup[0]->getLabel()); + userLabels.erase(lookup[0]->getLabel()); + } + + if ((anyLabelsToProcess(lookup[0]->getLabel(), userLabels, "") == true) && (processedLabels.count(lastLabel) != 1)) { + string saveLabel = lookup[0]->getLabel(); + + for (int i = 0; i < lookup.size(); i++) { delete lookup[i]; } + lookup = input->getSharedRAbundVectors(lastLabel); + m->mothurOut(lookup[0]->getLabel()); m->mothurOutEndLine(); + + getRelAbundance(lookup, out); + + processedLabels.insert(lookup[0]->getLabel()); + userLabels.erase(lookup[0]->getLabel()); + + //restore real lastlabel to save below + lookup[0]->setLabel(saveLabel); + } + + lastLabel = lookup[0]->getLabel(); + //prevent memory leak + for (int i = 0; i < lookup.size(); i++) { delete lookup[i]; lookup[i] = NULL; } + + //get next line to process + lookup = input->getSharedRAbundVectors(); + } + + if (m->control_pressed) { globaldata->Groups.clear(); delete read; out.close(); remove(outputFileName.c_str()); return 0; } + + //output error messages about any remaining user labels + set::iterator it; + bool needToRun = false; + for (it = userLabels.begin(); it != userLabels.end(); it++) { + m->mothurOut("Your file does not include the label " + *it); + if (processedLabels.count(lastLabel) != 1) { + m->mothurOut(". I will use " + lastLabel + "."); m->mothurOutEndLine(); + needToRun = true; + }else { + m->mothurOut(". Please refer to " + lastLabel + "."); m->mothurOutEndLine(); + } + } + + //run last label if you need to + if (needToRun == true) { + for (int i = 0; i < lookup.size(); i++) { if (lookup[i] != NULL) { delete lookup[i]; } } + lookup = input->getSharedRAbundVectors(lastLabel); + + m->mothurOut(lookup[0]->getLabel()); m->mothurOutEndLine(); + + getRelAbundance(lookup, out); + + for (int i = 0; i < lookup.size(); i++) { delete lookup[i]; } + } + + //reset groups parameter + globaldata->Groups.clear(); + delete input; globaldata->ginput = NULL; + delete read; + out.close(); + + if (m->control_pressed) { remove(outputFileName.c_str()); return 0;} + + m->mothurOutEndLine(); + m->mothurOut("Output File Names: "); m->mothurOutEndLine(); + m->mothurOut(outputFileName); m->mothurOutEndLine(); + m->mothurOutEndLine(); + + return 0; + } + catch(exception& e) { + m->errorOut(e, "GetRelAbundCommand", "execute"); + exit(1); + } +} +//********************************************************************************************************************** + +int GetRelAbundCommand::getRelAbundance(vector thisLookUp, ofstream& out){ + try { + + + } + catch(exception& e) { + m->errorOut(e, "GetRelAbundCommand", "getRelAbundance"); + exit(1); + } +} +//********************************************************************************************************************** + + diff --git a/getrelabundcommand.h b/getrelabundcommand.h new file mode 100644 index 0000000..aa123a4 --- /dev/null +++ b/getrelabundcommand.h @@ -0,0 +1,44 @@ +#ifndef GETRELABUNDCOMMAND_H +#define GETRELABUNDCOMMAND_H + +/* + * getrelabundcommand.h + * Mothur + * + * Created by westcott on 6/21/10. + * Copyright 2010 Schloss Lab. All rights reserved. + * + */ + +#include "command.hpp" +#include "inputdata.h" +#include "readotu.h" +#include "sharedrabundvector.h" + +class GlobalData; + +class GetRelAbundCommand : public Command { + +public: + GetRelAbundCommand(string); + ~GetRelAbundCommand(); + int execute(); + void help(); + +private: + GlobalData* globaldata; + ReadOTUFile* read; + InputData* input; + vector lookup; + + bool abort, allLines; + set labels; //holds labels to be used + string groups, label, outputDir, scale; + vector Groups; + + int getRelAbundance(vector, ofstream&); + +}; + +#endif + diff --git a/makefile b/makefile index 8fefa14..08dee23 100644 --- a/makefile +++ b/makefile @@ -33,7 +33,7 @@ ifeq ($(strip $(USEREADLINE)),yes) -L../readline-6.0 endif -USEMPI ?= yes +USEMPI ?= no ifeq ($(strip $(USEMPI)),yes) CC = mpic++ @@ -228,6 +228,7 @@ mothur : \ ./hclustercommand.o\ ./hcluster.o\ ./getlistcountcommand.o\ + ./getrelabundcommand.o\ ./readcluster.o\ ./ccode.o\ ./taxonomyequalizer.o\ @@ -439,6 +440,7 @@ mothur : \ ./hclustercommand.o\ ./hcluster.o\ ./getlistcountcommand.o\ + ./getrelabundcommand.o\ ./readcluster.o\ ./ccode.o\ ./taxonomyequalizer.o\ @@ -653,6 +655,7 @@ clean : ./hclustercommand.o\ ./hcluster.o\ ./getlistcountcommand.o\ + ./getrelabundcommand.o\ ./readcluster.o\ ./ccode.o\ ./taxonomyequalizer.o\ @@ -1726,4 +1729,8 @@ install : mothur ./degapseqscommand.o : degapseqscommand.cpp $(CC) $(CC_OPTIONS) degapseqscommand.cpp -c $(INCLUDE) -o ./degapseqscommand.o +# Item # 211 -- getrelabundcommand -- +./getrelabundcommand.o : getrelabundcommand.cpp + $(CC) $(CC_OPTIONS) getrelabundcommand.cpp -c $(INCLUDE) -o ./getrelabundcommand.o + ##### END RUN ####