X-Git-Url: https://git.donarmstrong.com/?a=blobdiff_plain;f=preclustercommand.cpp;h=951b2008b062c26c5fabb54dfd8ef2d2a858da67;hb=3cd964b03f04bc569c4a7d0b99be9908c54c1d44;hp=479a862935430545757ef48c6da660913782cfa2;hpb=b5a791c81d432082bf38755a08b33863f255341d;p=mothur.git diff --git a/preclustercommand.cpp b/preclustercommand.cpp index 479a862..951b200 100644 --- a/preclustercommand.cpp +++ b/preclustercommand.cpp @@ -8,215 +8,664 @@ */ #include "preclustercommand.h" +#include "deconvolutecommand.h" //********************************************************************************************************************** -inline bool comparePriority(seqPNode first, seqPNode second) { return (first.numIdentical > second.numIdentical); } +vector PreClusterCommand::setParameters(){ + try { + CommandParameter pfasta("fasta", "InputTypes", "", "", "none", "none", "none",false,true); parameters.push_back(pfasta); + CommandParameter pname("name", "InputTypes", "", "", "none", "none", "none",false,false); parameters.push_back(pname); + CommandParameter pgroup("group", "InputTypes", "", "", "none", "none", "none",false,false); parameters.push_back(pgroup); + CommandParameter pdiffs("diffs", "Number", "", "0", "", "", "",false,false); parameters.push_back(pdiffs); + CommandParameter pprocessors("processors", "Number", "", "1", "", "", "",false,false); parameters.push_back(pprocessors); + CommandParameter pinputdir("inputdir", "String", "", "", "", "", "",false,false); parameters.push_back(pinputdir); + CommandParameter poutputdir("outputdir", "String", "", "", "", "", "",false,false); parameters.push_back(poutputdir); + + vector myArray; + for (int i = 0; i < parameters.size(); i++) { myArray.push_back(parameters[i].name); } + return myArray; + } + catch(exception& e) { + m->errorOut(e, "PreClusterCommand", "setParameters"); + exit(1); + } +} +//********************************************************************************************************************** +string PreClusterCommand::getHelpString(){ + try { + string helpString = ""; + helpString += "The pre.cluster command groups sequences that are within a given number of base mismatches.\n"; + helpString += "The pre.cluster command outputs a new fasta and name file.\n"; + helpString += "The pre.cluster command parameters are fasta, names and diffs. The fasta parameter is required. \n"; + helpString += "The names parameter allows you to give a list of seqs that are identical. This file is 2 columns, first column is name or representative sequence, second column is a list of its identical sequences separated by commas.\n"; + helpString += "The group parameter allows you to provide a group file so you can cluster by group. \n"; + helpString += "The diffs parameter allows you to specify maximum number of mismatched bases allowed between sequences in a grouping. The default is 1.\n"; + helpString += "The pre.cluster command should be in the following format: \n"; + helpString += "pre.cluster(fasta=yourFastaFile, names=yourNamesFile, diffs=yourMaxDiffs) \n"; + helpString += "Example pre.cluster(fasta=amazon.fasta, diffs=2).\n"; + helpString += "Note: No spaces between parameter labels (i.e. fasta), '=' and parameters (i.e.yourFasta).\n"; + return helpString; + } + catch(exception& e) { + m->errorOut(e, "PreClusterCommand", "getHelpString"); + exit(1); + } +} +//********************************************************************************************************************** +string PreClusterCommand::getOutputFileNameTag(string type, string inputName=""){ + try { + string outputFileName = ""; + map >::iterator it; + + //is this a type this command creates + it = outputTypes.find(type); + if (it == outputTypes.end()) { m->mothurOut("[ERROR]: this command doesn't create a " + type + " output file.\n"); } + else { + if (type == "fasta") { outputFileName = "precluster" + m->getExtension(inputName); } + else if (type == "name") { outputFileName = "precluster.names"; } + else if (type == "map") { outputFileName = "precluster.map"; } + else { m->mothurOut("[ERROR]: No definition for type " + type + " output file tag.\n"); m->control_pressed = true; } + } + return outputFileName; + } + catch(exception& e) { + m->errorOut(e, "PreClusterCommand", "getOutputFileNameTag"); + exit(1); + } +} +//********************************************************************************************************************** +PreClusterCommand::PreClusterCommand(){ + try { + abort = true; calledHelp = true; + setParameters(); + vector tempOutNames; + outputTypes["fasta"] = tempOutNames; + outputTypes["name"] = tempOutNames; + outputTypes["map"] = tempOutNames; + } + catch(exception& e) { + m->errorOut(e, "PreClusterCommand", "PreClusterCommand"); + exit(1); + } +} //********************************************************************************************************************** -PreClusterCommand::PreClusterCommand(string option){ +PreClusterCommand::PreClusterCommand(string option) { try { - abort = false; + abort = false; calledHelp = false; //allow user to run help - if(option == "help") { help(); abort = true; } + if(option == "help") { help(); abort = true; calledHelp = true; } + else if(option == "citation") { citation(); abort = true; calledHelp = true;} else { - //valid paramters for this command - string Array[] = {"fasta", "name", "diffs"}; - vector myArray (Array, Array+(sizeof(Array)/sizeof(string))); + vector myArray = setParameters(); OptionParser parser(option); map parameters = parser.getParameters(); ValidParameters validParameter; + map::iterator it; //check to make sure all parameters are valid for command for (map::iterator it2 = parameters.begin(); it2 != parameters.end(); it2++) { if (validParameter.isValidParameter(it2->first, myArray, it2->second) != true) { abort = true; } } + //initialize outputTypes + vector tempOutNames; + outputTypes["fasta"] = tempOutNames; + outputTypes["name"] = tempOutNames; + outputTypes["map"] = tempOutNames; + + //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 = ""; } + else { + string path; + it = parameters.find("fasta"); + //user has given a template file + if(it != parameters.end()){ + path = m->hasPath(it->second); + //if the user has not given a path then, add inputdir. else leave path alone. + if (path == "") { parameters["fasta"] = inputDir + it->second; } + } + + it = parameters.find("name"); + //user has given a template file + if(it != parameters.end()){ + path = m->hasPath(it->second); + //if the user has not given a path then, add inputdir. else leave path alone. + if (path == "") { parameters["name"] = inputDir + it->second; } + } + + it = parameters.find("group"); + //user has given a template file + if(it != parameters.end()){ + path = m->hasPath(it->second); + //if the user has not given a path then, add inputdir. else leave path alone. + if (path == "") { parameters["group"] = inputDir + it->second; } + } + } + //check for required parameters fastafile = validParameter.validFile(parameters, "fasta", true); - if (fastafile == "not found") { mothurOut("fasta is a required parameter for the pre.cluster command."); mothurOutEndLine(); abort = true; } + if (fastafile == "not found") { + fastafile = m->getFastaFile(); + if (fastafile != "") { m->mothurOut("Using " + fastafile + " as input file for the fasta parameter."); m->mothurOutEndLine(); } + else { m->mothurOut("You have no current fastafile and the fasta parameter is required."); m->mothurOutEndLine(); abort = true; } + } else if (fastafile == "not open") { abort = true; } + else { m->setFastaFile(fastafile); } + //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 += m->hasPath(fastafile); //if user entered a file with a path then preserve it + } + //check for optional parameter and set defaults // ...at some point should added some additional type checking... namefile = validParameter.validFile(parameters, "name", true); if (namefile == "not found") { namefile = ""; } - else if (namefile == "not open") { abort = true; } - else { readNameFile(); } + else if (namefile == "not open") { namefile = ""; abort = true; } + else { m->setNameFile(namefile); } + + groupfile = validParameter.validFile(parameters, "group", true); + if (groupfile == "not found") { groupfile = ""; bygroup = false; } + else if (groupfile == "not open") { abort = true; groupfile = ""; } + else { m->setGroupFile(groupfile); bygroup = true; } + + string temp = validParameter.validFile(parameters, "diffs", false); if(temp == "not found"){ temp = "1"; } + m->mothurConvert(temp, diffs); - string temp = validParameter.validFile(parameters, "diffs", false); if(temp == "not found"){ temp = "1"; } - convert(temp, diffs); + temp = validParameter.validFile(parameters, "processors", false); if (temp == "not found"){ temp = m->getProcessors(); } + m->setProcessors(temp); + m->mothurConvert(temp, processors); + + if (namefile == "") { + vector files; files.push_back(fastafile); + parser.getNameFile(files); + } } } catch(exception& e) { - errorOut(e, "PreClusterCommand", "PreClusterCommand"); + m->errorOut(e, "PreClusterCommand", "PreClusterCommand"); exit(1); } } - -//********************************************************************************************************************** -PreClusterCommand::~PreClusterCommand(){} //********************************************************************************************************************** -void PreClusterCommand::help(){ +int PreClusterCommand::execute(){ try { - mothurOut("The pre.cluster command groups sequences that are within a given number of base mismatches.\n"); - mothurOut("The pre.cluster command outputs a new fasta and name file.\n"); - mothurOut("The pre.cluster command parameters are fasta, names and diffs. The fasta parameter is required. \n"); - mothurOut("The names parameter allows you to give a list of seqs that are identical. This file is 2 columns, first column is name or representative sequence, second column is a list of its identical sequences separated by commas.\n"); - mothurOut("The diffs parameter allows you to specify maximum number of mismatched bases allowed between sequences in a grouping. The default is 1.\n"); - mothurOut("The pre.cluster command should be in the following format: \n"); - mothurOut("pre.cluster(fasta=yourFastaFile, names=yourNamesFile, diffs=yourMaxDiffs) \n"); - mothurOut("Example pre.cluster(fasta=amazon.fasta, diffs=2).\n"); - mothurOut("Note: No spaces between parameter labels (i.e. fasta), '=' and parameters (i.e.yourFasta).\n\n"); + + if (abort == true) { if (calledHelp) { return 0; } return 2; } + + int start = time(NULL); + + string fileroot = outputDir + m->getRootName(m->getSimpleName(fastafile)); + string newFastaFile = fileroot + getOutputFileNameTag("fasta", fastafile); + string newNamesFile = fileroot + getOutputFileNameTag("name"); + string newMapFile = fileroot + getOutputFileNameTag("map"); //add group name if by group + outputNames.push_back(newFastaFile); outputTypes["fasta"].push_back(newFastaFile); + outputNames.push_back(newNamesFile); outputTypes["name"].push_back(newNamesFile); + + + if (bygroup) { + //clear out old files + ofstream outFasta; m->openOutputFile(newFastaFile, outFasta); outFasta.close(); + ofstream outNames; m->openOutputFile(newNamesFile, outNames); outNames.close(); + newMapFile = fileroot + "precluster."; + + //parse fasta and name file by group + SequenceParser* parser; + if (namefile != "") { parser = new SequenceParser(groupfile, fastafile, namefile); } + else { parser = new SequenceParser(groupfile, fastafile); } + + vector groups = parser->getNamesOfGroups(); + + if(processors == 1) { driverGroups(parser, newFastaFile, newNamesFile, newMapFile, 0, groups.size(), groups); } + else { createProcessesGroups(parser, newFastaFile, newNamesFile, newMapFile, groups); } + + delete parser; + + if (m->control_pressed) { for (int i = 0; i < outputNames.size(); i++) { m->mothurRemove(outputNames[i]); } return 0; } + + //run unique.seqs for deconvolute results + string inputString = "fasta=" + newFastaFile; + if (namefile != "") { inputString += ", name=" + newNamesFile; } + m->mothurOutEndLine(); + m->mothurOut("/******************************************/"); m->mothurOutEndLine(); + m->mothurOut("Running command: unique.seqs(" + inputString + ")"); m->mothurOutEndLine(); + m->mothurCalling = true; + + Command* uniqueCommand = new DeconvoluteCommand(inputString); + uniqueCommand->execute(); + + map > filenames = uniqueCommand->getOutputFiles(); + + delete uniqueCommand; + m->mothurCalling = false; + m->mothurOut("/******************************************/"); m->mothurOutEndLine(); + + m->renameFile(filenames["fasta"][0], newFastaFile); + m->renameFile(filenames["name"][0], newNamesFile); + + m->mothurOut("It took " + toString(time(NULL) - start) + " secs to run pre.cluster."); m->mothurOutEndLine(); + + }else { + if (namefile != "") { readNameFile(); } + + //reads fasta file and return number of seqs + int numSeqs = readFASTA(); //fills alignSeqs and makes all seqs active + + if (m->control_pressed) { for (int i = 0; i < outputNames.size(); i++) { m->mothurRemove(outputNames[i]); } return 0; } + + if (numSeqs == 0) { m->mothurOut("Error reading fasta file...please correct."); m->mothurOutEndLine(); return 0; } + if (diffs > length) { m->mothurOut("Error: diffs is greater than your sequence length."); m->mothurOutEndLine(); return 0; } + + int count = process(newMapFile); + outputNames.push_back(newMapFile); outputTypes["map"].push_back(newMapFile); + + if (m->control_pressed) { for (int i = 0; i < outputNames.size(); i++) { m->mothurRemove(outputNames[i]); } return 0; } + + m->mothurOut("Total number of sequences before precluster was " + toString(alignSeqs.size()) + "."); m->mothurOutEndLine(); + m->mothurOut("pre.cluster removed " + toString(count) + " sequences."); m->mothurOutEndLine(); m->mothurOutEndLine(); + printData(newFastaFile, newNamesFile); + + m->mothurOut("It took " + toString(time(NULL) - start) + " secs to cluster " + toString(numSeqs) + " sequences."); m->mothurOutEndLine(); + } + + if (m->control_pressed) { for (int i = 0; i < outputNames.size(); i++) { m->mothurRemove(outputNames[i]); } return 0; } + + m->mothurOutEndLine(); + m->mothurOut("Output File Names: "); m->mothurOutEndLine(); + for (int i = 0; i < outputNames.size(); i++) { m->mothurOut(outputNames[i]); m->mothurOutEndLine(); } + m->mothurOutEndLine(); + + //set fasta file as new current fastafile + string current = ""; + itTypes = outputTypes.find("fasta"); + if (itTypes != outputTypes.end()) { + if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setFastaFile(current); } + } + + itTypes = outputTypes.find("name"); + if (itTypes != outputTypes.end()) { + if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setNameFile(current); } + } + + return 0; + } catch(exception& e) { - errorOut(e, "PreClusterCommand", "help"); + m->errorOut(e, "PreClusterCommand", "execute"); exit(1); } } -//********************************************************************************************************************** - -int PreClusterCommand::execute(){ +/**************************************************************************************************/ +int PreClusterCommand::createProcessesGroups(SequenceParser* parser, string newFName, string newNName, string newMFile, vector groups) { try { - if (abort == true) { return 0; } + vector processIDS; + int process = 1; + int num = 0; - //reads fasta file and return number of seqs - int numSeqs = readSeqs(); //fills alignSeqs and makes all seqs active - - if (numSeqs == 0) { mothurOut("Error reading fasta file...please correct."); mothurOutEndLine(); return 0; } - if (diffs > length) { mothurOut("Error: diffs is greater than your sequence length."); mothurOutEndLine(); return 0; } + //sanity check + if (groups.size() < processors) { processors = groups.size(); } + + //divide the groups between the processors + vector lines; + int numGroupsPerProcessor = groups.size() / processors; + for (int i = 0; i < processors; i++) { + int startIndex = i * numGroupsPerProcessor; + int endIndex = (i+1) * numGroupsPerProcessor; + if(i == (processors - 1)){ endIndex = groups.size(); } + lines.push_back(linePair(startIndex, endIndex)); + } + +#if defined (__APPLE__) || (__MACH__) || (linux) || (__linux) || (__linux__) || (__unix__) || (__unix) + + //loop through and create all the processes you want + while (process != processors) { + int pid = fork(); + + if (pid > 0) { + processIDS.push_back(pid); //create map from line number to pid so you can append files in correct order later + process++; + }else if (pid == 0){ + outputNames.clear(); + num = driverGroups(parser, newFName + toString(getpid()) + ".temp", newNName + toString(getpid()) + ".temp", newMFile, lines[process].start, lines[process].end, groups); + + string tempFile = toString(getpid()) + ".outputNames.temp"; + ofstream outTemp; + m->openOutputFile(tempFile, outTemp); + + outTemp << outputNames.size(); + for (int i = 0; i < outputNames.size(); i++) { outTemp << outputNames[i] << endl; } + outTemp.close(); + + exit(0); + }else { + m->mothurOut("[ERROR]: unable to spawn the necessary processes."); m->mothurOutEndLine(); + for (int i = 0; i < processIDS.size(); i++) { kill (processIDS[i], SIGINT); } + exit(0); + } + } + + //do my part + num = driverGroups(parser, newFName, newNName, newMFile, lines[0].start, lines[0].end, groups); + + //force parent to wait until all the processes are done + for (int i=0;iopenInputFile(tempFile, intemp); + + int num; + intemp >> num; + for (int k = 0; k < num; k++) { + string name = ""; + intemp >> name; m->gobble(intemp); + + outputNames.push_back(name); outputTypes["map"].push_back(name); + } + intemp.close(); + m->mothurRemove(tempFile); + } +#else + + ////////////////////////////////////////////////////////////////////////////////////////////////////// + //Windows version shared memory, so be careful when passing variables through the preClusterData struct. + //Above fork() will clone, so memory is separate, but that's not the case with windows, + ////////////////////////////////////////////////////////////////////////////////////////////////////// + + vector pDataArray; + DWORD dwThreadIdArray[processors-1]; + HANDLE hThreadArray[processors-1]; + + //Create processor worker threads. + for( int i=1; imapFileNames.size(); j++) { + outputNames.push_back(pDataArray[i]->mapFileNames[j]); outputTypes["map"].push_back(pDataArray[i]->mapFileNames[j]); + } + CloseHandle(hThreadArray[i]); + delete pDataArray[i]; + } + +#endif + + //append output files + for(int i=0;igetFullPathName(".\\" + newFName); + //newNName = m->getFullPathName(".\\" + newNName); + + m->appendFiles((newFName + toString(processIDS[i]) + ".temp"), newFName); + m->mothurRemove((newFName + toString(processIDS[i]) + ".temp")); + + m->appendFiles((newNName + toString(processIDS[i]) + ".temp"), newNName); + m->mothurRemove((newNName + toString(processIDS[i]) + ".temp")); + } + + return num; + + } + catch(exception& e) { + m->errorOut(e, "PreClusterCommand", "createProcessesGroups"); + exit(1); + } +} +/**************************************************************************************************/ +int PreClusterCommand::driverGroups(SequenceParser* parser, string newFFile, string newNFile, string newMFile, int start, int end, vector groups){ + try { + + int numSeqs = 0; + + //precluster each group + for (int i = start; i < end; i++) { + + start = time(NULL); + + if (m->control_pressed) { return 0; } + + m->mothurOutEndLine(); m->mothurOut("Processing group " + groups[i] + ":"); m->mothurOutEndLine(); + + map thisNameMap; + if (namefile != "") { thisNameMap = parser->getNameMap(groups[i]); } + vector thisSeqs = parser->getSeqs(groups[i]); + + //fill alignSeqs with this groups info. + numSeqs = loadSeqs(thisNameMap, thisSeqs); + + if (m->control_pressed) { return 0; } + + if (diffs > length) { m->mothurOut("Error: diffs is greater than your sequence length."); m->mothurOutEndLine(); m->control_pressed = true; return 0; } + + int count = process(newMFile+groups[i]+".map"); + outputNames.push_back(newMFile+groups[i]+".map"); outputTypes["map"].push_back(newMFile+groups[i]+".map"); + + if (m->control_pressed) { return 0; } + + m->mothurOut("Total number of sequences before pre.cluster was " + toString(alignSeqs.size()) + "."); m->mothurOutEndLine(); + m->mothurOut("pre.cluster removed " + toString(count) + " sequences."); m->mothurOutEndLine(); m->mothurOutEndLine(); + printData(newFFile, newNFile); + + m->mothurOut("It took " + toString(time(NULL) - start) + " secs to cluster " + toString(numSeqs) + " sequences."); m->mothurOutEndLine(); + + } + + return numSeqs; + } + catch(exception& e) { + m->errorOut(e, "PreClusterCommand", "driverGroups"); + exit(1); + } +} +/**************************************************************************************************/ +int PreClusterCommand::process(string newMapFile){ + try { + ofstream out; + m->openOutputFile(newMapFile, out); - //clear sizes since you only needed this info to build the alignSeqs seqPNode structs - sizes.clear(); - //sort seqs by number of identical seqs sort(alignSeqs.begin(), alignSeqs.end(), comparePriority); - - //go through active list and cluster everthing you can, until all nodes are inactive - //taking advantage of the fact that maps are already sorted - map::iterator itActive; - map::iterator it2Active; - int count = 0; - for (int i = 0; i < alignSeqs.size(); i++) { + int count = 0; + int numSeqs = alignSeqs.size(); - //are you active - itActive = active.find(alignSeqs[i].seq.getName()); + //think about running through twice... + for (int i = 0; i < numSeqs; i++) { - if (itActive != active.end()) { //this sequence has not been merged yet + //are you active + // itActive = active.find(alignSeqs[i].seq.getName()); + if (alignSeqs[i].active) { //this sequence has not been merged yet + + string chunk = alignSeqs[i].seq.getName() + "\t" + toString(alignSeqs[i].numIdentical) + "\t" + toString(0) + "\t" + alignSeqs[i].seq.getAligned() + "\n"; + //try to merge it with all smaller seqs - for (int j = i; j < alignSeqs.size(); j++) { + for (int j = i+1; j < numSeqs; j++) { + + if (m->control_pressed) { out.close(); return 0; } - if (i != j) { - //are you active - it2Active = active.find(alignSeqs[j].seq.getName()); - if (it2Active != active.end()) { //this sequence has not been merged yet - //are you within "diff" bases - int mismatch = calcMisMatches(alignSeqs[i].seq.getAligned(), alignSeqs[j].seq.getAligned()); + if (alignSeqs[j].active) { //this sequence has not been merged yet + //are you within "diff" bases + int mismatch = calcMisMatches(alignSeqs[i].seq.getAligned(), alignSeqs[j].seq.getAligned()); + + if (mismatch <= diffs) { + //merge + alignSeqs[i].names += ',' + alignSeqs[j].names; + alignSeqs[i].numIdentical += alignSeqs[j].numIdentical; - if (mismatch <= diffs) { - //merge - names[alignSeqs[i].seq.getName()] += "," + names[alignSeqs[j].seq.getName()]; + chunk += alignSeqs[j].seq.getName() + "\t" + toString(alignSeqs[j].numIdentical) + "\t" + toString(mismatch) + "\t" + alignSeqs[j].seq.getAligned() + "\n"; - //remove from active list - active.erase(it2Active); - - //set numIdentical to 0, so you only print the representative seqs in the fasta file - alignSeqs[j].numIdentical = 0; - count++; - } - }//end if j active - }//end if i != j - }//end for loop + alignSeqs[j].active = 0; + alignSeqs[j].numIdentical = 0; + count++; + } + }//end if j active + }//end for loop j //remove from active list - active.erase(itActive); + alignSeqs[i].active = 0; + + out << "ideal_seq_" << (i+1) << '\t' << alignSeqs[i].numIdentical << endl << chunk << endl;; + }//end if active i + if(i % 100 == 0) { m->mothurOut(toString(i) + "\t" + toString(numSeqs - count) + "\t" + toString(count)); m->mothurOutEndLine(); } } - - string newFastaFile = getRootName(fastafile) + "precluster" + getExtension(fastafile); - string newNamesFile = getRootName(fastafile) + "precluster.names"; - - printData(newFastaFile, newNamesFile); + out.close(); - mothurOut("Total number of sequences before precluster was " + toString(alignSeqs.size()) + "."); mothurOutEndLine(); - mothurOut("pre.cluster removed " + toString(count) + " sequences."); mothurOutEndLine(); + if(numSeqs % 100 != 0) { m->mothurOut(toString(numSeqs) + "\t" + toString(numSeqs - count) + "\t" + toString(count)); m->mothurOutEndLine(); } - return 0; + return count; } catch(exception& e) { - errorOut(e, "PreClusterCommand", "execute"); + m->errorOut(e, "PreClusterCommand", "process"); exit(1); } } /**************************************************************************************************/ -int PreClusterCommand::readSeqs(){ +int PreClusterCommand::readFASTA(){ try { + //ifstream inNames; ifstream inFasta; - openInputFile(fastafile, inFasta); - length = 0; - map::iterator it; - + + //m->openInputFile(namefile, inNames); + m->openInputFile(fastafile, inFasta); + + //string firstCol, secondCol, nameString; + set lengths; + while (!inFasta.eof()) { - Sequence temp(inFasta); //read seq - if (temp.getName() != "") { + if (m->control_pressed) { inFasta.close(); return 0; } + + //inNames >> firstCol >> secondCol; + //nameString = secondCol; + + //m->gobble(inNames); + //int size = 1; + //while (secondCol.find_first_of(',') != -1) { + // size++; + // secondCol = secondCol.substr(secondCol.find_first_of(',')+1, secondCol.length()); + //} + + Sequence seq(inFasta); m->gobble(inFasta); + + if (seq.getName() != "") { //can get "" if commented line is at end of fasta file if (namefile != "") { - //make sure fasta and name files match - it = names.find(temp.getName()); - if (it == names.end()) { mothurOut(temp.getName() + " is not in your names file, please correct."); mothurOutEndLine(); exit(1); } - }else { sizes[temp.getName()] = 1; } - - seqPNode tempNode(sizes[temp.getName()], temp); - alignSeqs.push_back(tempNode); - active[temp.getName()] = true; + itSize = sizes.find(seq.getName()); + + if (itSize == sizes.end()) { m->mothurOut(seq.getName() + " is not in your names file, please correct."); m->mothurOutEndLine(); exit(1); } + else{ + seqPNode tempNode(itSize->second, seq, names[seq.getName()]); + alignSeqs.push_back(tempNode); + lengths.insert(seq.getAligned().length()); + } + }else { //no names file, you are identical to yourself + seqPNode tempNode(1, seq, seq.getName()); + alignSeqs.push_back(tempNode); + lengths.insert(seq.getAligned().length()); + } } - gobble(inFasta); } inFasta.close(); - - if (alignSeqs.size() != 0) { length = alignSeqs[0].seq.getAligned().length(); } - + //inNames.close(); + + if (lengths.size() > 1) { m->control_pressed = true; m->mothurOut("[ERROR]: your sequences are not all the same length. pre.cluster requires sequences to be aligned."); m->mothurOutEndLine(); } + else if (lengths.size() == 1) { length = *(lengths.begin()); } + return alignSeqs.size(); } + catch(exception& e) { - errorOut(e, "PreClusterCommand", "readSeqs"); + m->errorOut(e, "PreClusterCommand", "readFASTA"); exit(1); } } /**************************************************************************************************/ -void PreClusterCommand::readNameFile(){ +int PreClusterCommand::loadSeqs(map& thisName, vector& thisSeqs){ try { - ifstream in; - openInputFile(namefile, in); - string firstCol, secondCol; + set lengths; + alignSeqs.clear(); + map::iterator it; + bool error = false; + + for (int i = 0; i < thisSeqs.size(); i++) { + + if (m->control_pressed) { return 0; } + + if (namefile != "") { + it = thisName.find(thisSeqs[i].getName()); - while (!in.eof()) { - in >> firstCol >> secondCol; gobble(in); - names[firstCol] = secondCol; - int size = 1; - while (secondCol.find_first_of(',') != -1) { - size++; - secondCol = secondCol.substr(secondCol.find_first_of(',')+1, secondCol.length()); + //should never be true since parser checks for this + if (it == thisName.end()) { m->mothurOut(thisSeqs[i].getName() + " is not in your names file, please correct."); m->mothurOutEndLine(); error = true; } + else{ + //get number of reps + int numReps = 1; + for(int j=0;j<(it->second).length();j++){ + if((it->second)[j] == ','){ numReps++; } + } + + seqPNode tempNode(numReps, thisSeqs[i], it->second); + alignSeqs.push_back(tempNode); + lengths.insert(thisSeqs[i].getAligned().length()); + } + }else { //no names file, you are identical to yourself + seqPNode tempNode(1, thisSeqs[i], thisSeqs[i].getName()); + alignSeqs.push_back(tempNode); + lengths.insert(thisSeqs[i].getAligned().length()); } - sizes[firstCol] = size; } - in.close(); + + if (lengths.size() > 1) { error = true; m->mothurOut("[ERROR]: your sequences are not all the same length. pre.cluster requires sequences to be aligned."); m->mothurOutEndLine(); } + else if (lengths.size() == 1) { length = *(lengths.begin()); } + + //sanity check + if (error) { m->control_pressed = true; } + + thisSeqs.clear(); + + return alignSeqs.size(); } + catch(exception& e) { - errorOut(e, "PreClusterCommand", "readNameFile"); + m->errorOut(e, "PreClusterCommand", "loadSeqs"); exit(1); } } + /**************************************************************************************************/ + int PreClusterCommand::calcMisMatches(string seq1, string seq2){ try { int numBad = 0; @@ -230,27 +679,30 @@ int PreClusterCommand::calcMisMatches(string seq1, string seq2){ return numBad; } catch(exception& e) { - errorOut(e, "PreClusterCommand", "calcMisMatches"); + m->errorOut(e, "PreClusterCommand", "calcMisMatches"); exit(1); } } + /**************************************************************************************************/ + void PreClusterCommand::printData(string newfasta, string newname){ try { ofstream outFasta; ofstream outNames; - openOutputFile(newfasta, outFasta); - openOutputFile(newname, outNames); - - map::iterator itNames; + + if (bygroup) { + m->openOutputFileAppend(newfasta, outFasta); + m->openOutputFileAppend(newname, outNames); + }else { + m->openOutputFile(newfasta, outFasta); + m->openOutputFile(newname, outNames); + } for (int i = 0; i < alignSeqs.size(); i++) { if (alignSeqs[i].numIdentical != 0) { alignSeqs[i].seq.printSequence(outFasta); - - itNames = names.find(alignSeqs[i].seq.getName()); - - outNames << itNames->first << '\t' << itNames->second << endl; + outNames << alignSeqs[i].seq.getName() << '\t' << alignSeqs[i].names << endl; } } @@ -259,7 +711,32 @@ void PreClusterCommand::printData(string newfasta, string newname){ } catch(exception& e) { - errorOut(e, "PreClusterCommand", "printData"); + m->errorOut(e, "PreClusterCommand", "printData"); + exit(1); + } +} +/**************************************************************************************************/ + +void PreClusterCommand::readNameFile(){ + try { + ifstream in; + m->openInputFile(namefile, in); + string firstCol, secondCol; + + while (!in.eof()) { + in >> firstCol >> secondCol; m->gobble(in); + names[firstCol] = secondCol; + int size = 1; + + for(int i=0;ierrorOut(e, "PreClusterCommand", "readNameFile"); exit(1); } }