2 * pairwiseseqscommand.cpp
5 * Created by westcott on 10/20/10.
6 * Copyright 2010 Schloss Lab. All rights reserved.
10 #include "pairwiseseqscommand.h"
12 //**********************************************************************************************************************
13 vector<string> PairwiseSeqsCommand::setParameters(){
15 CommandParameter pfasta("fasta", "InputTypes", "", "", "none", "none", "none",false,true); parameters.push_back(pfasta);
16 CommandParameter palign("align", "Multiple", "needleman-gotoh-blast-noalign", "needleman", "", "", "",false,false); parameters.push_back(palign);
17 CommandParameter pmatch("match", "Number", "", "1.0", "", "", "",false,false); parameters.push_back(pmatch);
18 CommandParameter pmismatch("mismatch", "Number", "", "-1.0", "", "", "",false,false); parameters.push_back(pmismatch);
19 CommandParameter pgapopen("gapopen", "Number", "", "-2.0", "", "", "",false,false); parameters.push_back(pgapopen);
20 CommandParameter pgapextend("gapextend", "Number", "", "-1.0", "", "", "",false,false); parameters.push_back(pgapextend);
21 CommandParameter pprocessors("processors", "Number", "", "1", "", "", "",false,false); parameters.push_back(pprocessors);
22 CommandParameter poutput("output", "Multiple", "column-lt-square", "column", "", "", "",false,false); parameters.push_back(poutput);
23 CommandParameter pcalc("calc", "Multiple", "nogaps-eachgap-onegap", "onegap", "", "", "",false,false); parameters.push_back(pcalc);
24 CommandParameter pcountends("countends", "Boolean", "", "T", "", "", "",false,false); parameters.push_back(pcountends);
25 CommandParameter pcompress("compress", "Boolean", "", "F", "", "", "",false,false); parameters.push_back(pcompress);
26 CommandParameter pcutoff("cutoff", "Number", "", "1.0", "", "", "",false,false); parameters.push_back(pcutoff);
27 CommandParameter pinputdir("inputdir", "String", "", "", "", "", "",false,false); parameters.push_back(pinputdir);
28 CommandParameter poutputdir("outputdir", "String", "", "", "", "", "",false,false); parameters.push_back(poutputdir);
30 vector<string> myArray;
31 for (int i = 0; i < parameters.size(); i++) { myArray.push_back(parameters[i].name); }
35 m->errorOut(e, "PairwiseSeqsCommand", "setParameters");
39 //**********************************************************************************************************************
40 string PairwiseSeqsCommand::getHelpString(){
42 string helpString = "";
43 helpString += "The pairwise.seqs command reads a fasta file and creates distance matrix.\n";
44 helpString += "The pairwise.seqs command parameters are fasta, align, match, mismatch, gapopen, gapextend, calc, output, cutoff and processors.\n";
45 helpString += "The fasta parameter is required. You may enter multiple fasta files by separating their names with dashes. ie. fasta=abrecovery.fasta-amzon.fasta \n";
46 helpString += "The align parameter allows you to specify the alignment method to use. Your options are: gotoh, needleman, blast and noalign. The default is needleman.\n";
47 helpString += "The match parameter allows you to specify the bonus for having the same base. The default is 1.0.\n";
48 helpString += "The mistmatch parameter allows you to specify the penalty for having different bases. The default is -1.0.\n";
49 helpString += "The gapopen parameter allows you to specify the penalty for opening a gap in an alignment. The default is -2.0.\n";
50 helpString += "The gapextend parameter allows you to specify the penalty for extending a gap in an alignment. The default is -1.0.\n";
51 helpString += "The calc parameter allows you to specify the method of calculating the distances. Your options are: nogaps, onegap or eachgap. The default is onegap.\n";
52 helpString += "The countends parameter allows you to specify whether to include terminal gaps in distance. Your options are: T or F. The default is T.\n";
53 helpString += "The cutoff parameter allows you to specify maximum distance to keep. The default is 1.0.\n";
54 helpString += "The output parameter allows you to specify format of your distance matrix. Options are column, lt, and square. The default is column.\n";
55 helpString += "The compress parameter allows you to indicate that you want the resulting distance file compressed. The default is false.\n";
56 helpString += "The pairwise.seqs command should be in the following format: \n";
57 helpString += "pairwise.seqs(fasta=yourfastaFile, align=yourAlignmentMethod) \n";
58 helpString += "Example pairwise.seqs(fasta=candidate.fasta, align=blast)\n";
59 helpString += "Note: No spaces between parameter labels (i.e. fasta), '=' and parameters (i.e.yourFastaFile).\n";
63 m->errorOut(e, "PairwiseSeqsCommand", "getHelpString");
68 //**********************************************************************************************************************
69 PairwiseSeqsCommand::PairwiseSeqsCommand(){
71 abort = true; calledHelp = true;
73 vector<string> tempOutNames;
74 outputTypes["phylip"] = tempOutNames;
75 outputTypes["column"] = tempOutNames;
78 m->errorOut(e, "PairwiseSeqsCommand", "PairwiseSeqsCommand");
82 //**********************************************************************************************************************
83 PairwiseSeqsCommand::PairwiseSeqsCommand(string option) {
85 abort = false; calledHelp = false;
87 //allow user to run help
88 if(option == "help") { help(); abort = true; calledHelp = true; }
89 else if(option == "citation") { citation(); abort = true; calledHelp = true;}
92 vector<string> myArray = setParameters();
94 OptionParser parser(option);
95 map<string, string> parameters = parser.getParameters();
97 ValidParameters validParameter("pairwise.seqs");
98 map<string, string>::iterator it;
100 //check to make sure all parameters are valid for command
101 for (it = parameters.begin(); it != parameters.end(); it++) {
102 if (validParameter.isValidParameter(it->first, myArray, it->second) != true) { abort = true; }
105 //initialize outputTypes
106 vector<string> tempOutNames;
107 outputTypes["phylip"] = tempOutNames;
108 outputTypes["column"] = tempOutNames;
110 //if the user changes the output directory command factory will send this info to us in the output parameter
111 outputDir = validParameter.validFile(parameters, "outputdir", false); if (outputDir == "not found"){ outputDir = ""; }
114 //if the user changes the input directory command factory will send this info to us in the output parameter
115 string inputDir = validParameter.validFile(parameters, "inputdir", false);
117 if (inputDir == "not found"){ inputDir = ""; }
119 fastaFileName = validParameter.validFile(parameters, "fasta", false);
120 if (fastaFileName == "not found") {
121 //if there is a current fasta file, use it
122 string filename = m->getFastaFile();
123 if (filename != "") { fastaFileNames.push_back(filename); m->mothurOut("Using " + filename + " as input file for the fasta parameter."); m->mothurOutEndLine(); }
124 else { m->mothurOut("You have no current fastafile and the fasta parameter is required."); m->mothurOutEndLine(); abort = true; }
127 m->splitAtDash(fastaFileName, fastaFileNames);
129 //go through files and make sure they are good, if not, then disregard them
130 for (int i = 0; i < fastaFileNames.size(); i++) {
133 if (fastaFileNames[i] == "current") {
134 fastaFileNames[i] = m->getFastaFile();
135 if (fastaFileNames[i] != "") { m->mothurOut("Using " + fastaFileNames[i] + " as input file for the fasta parameter where you had given current."); m->mothurOutEndLine(); }
137 m->mothurOut("You have no current fastafile, ignoring current."); m->mothurOutEndLine(); ignore=true;
138 //erase from file list
139 fastaFileNames.erase(fastaFileNames.begin()+i);
146 if (inputDir != "") {
147 string path = m->hasPath(fastaFileNames[i]);
148 //if the user has not given a path then, add inputdir. else leave path alone.
149 if (path == "") { fastaFileNames[i] = inputDir + fastaFileNames[i]; }
155 ableToOpen = m->openInputFile(fastaFileNames[i], in, "noerror");
157 //if you can't open it, try default location
158 if (ableToOpen == 1) {
159 if (m->getDefaultPath() != "") { //default path is set
160 string tryPath = m->getDefaultPath() + m->getSimpleName(fastaFileNames[i]);
161 m->mothurOut("Unable to open " + fastaFileNames[i] + ". Trying default " + tryPath); m->mothurOutEndLine();
163 ableToOpen = m->openInputFile(tryPath, in2, "noerror");
165 fastaFileNames[i] = tryPath;
169 //if you can't open it, try output location
170 if (ableToOpen == 1) {
171 if (m->getOutputDir() != "") { //default path is set
172 string tryPath = m->getOutputDir() + m->getSimpleName(fastaFileNames[i]);
173 m->mothurOut("Unable to open " + fastaFileNames[i] + ". Trying output directory " + tryPath); m->mothurOutEndLine();
175 ableToOpen = m->openInputFile(tryPath, in2, "noerror");
177 fastaFileNames[i] = tryPath;
183 if (ableToOpen == 1) {
184 m->mothurOut("Unable to open " + fastaFileNames[i] + ". It will be disregarded."); m->mothurOutEndLine();
185 //erase from file list
186 fastaFileNames.erase(fastaFileNames.begin()+i);
189 m->setFastaFile(fastaFileNames[i]);
194 //make sure there is at least one valid file left
195 if (fastaFileNames.size() == 0) { m->mothurOut("no valid files."); m->mothurOutEndLine(); abort = true; }
198 //check for optional parameter and set defaults
199 // ...at some point should added some additional type checking...
201 temp = validParameter.validFile(parameters, "match", false); if (temp == "not found"){ temp = "1.0"; }
202 m->mothurConvert(temp, match);
204 temp = validParameter.validFile(parameters, "mismatch", false); if (temp == "not found"){ temp = "-1.0"; }
205 m->mothurConvert(temp, misMatch);
206 if (misMatch > 0) { m->mothurOut("[ERROR]: mismatch must be negative.\n"); abort=true; }
208 temp = validParameter.validFile(parameters, "gapopen", false); if (temp == "not found"){ temp = "-2.0"; }
209 m->mothurConvert(temp, gapOpen);
210 if (gapOpen > 0) { m->mothurOut("[ERROR]: gapopen must be negative.\n"); abort=true; }
212 temp = validParameter.validFile(parameters, "gapextend", false); if (temp == "not found"){ temp = "-1.0"; }
213 m->mothurConvert(temp, gapExtend);
214 if (gapExtend > 0) { m->mothurOut("[ERROR]: gapextend must be negative.\n"); abort=true; }
216 temp = validParameter.validFile(parameters, "processors", false); if (temp == "not found"){ temp = m->getProcessors(); }
217 m->setProcessors(temp);
218 m->mothurConvert(temp, processors);
220 temp = validParameter.validFile(parameters, "cutoff", false); if(temp == "not found"){ temp = "1.0"; }
221 m->mothurConvert(temp, cutoff);
223 temp = validParameter.validFile(parameters, "countends", false); if(temp == "not found"){ temp = "T"; }
224 countends = m->isTrue(temp);
226 temp = validParameter.validFile(parameters, "compress", false); if(temp == "not found"){ temp = "F"; }
227 compress = m->isTrue(temp);
229 align = validParameter.validFile(parameters, "align", false); if (align == "not found"){ align = "needleman"; }
231 output = validParameter.validFile(parameters, "output", false); if(output == "not found"){ output = "column"; }
232 if ((output != "column") && (output != "lt") && (output != "square")) { m->mothurOut(output + " is not a valid output form. Options are column, lt and square. I will use column."); m->mothurOutEndLine(); output = "column"; }
234 calc = validParameter.validFile(parameters, "calc", false);
235 if (calc == "not found") { calc = "onegap"; }
237 if (calc == "default") { calc = "onegap"; }
239 m->splitAtDash(calc, Estimators);
243 catch(exception& e) {
244 m->errorOut(e, "PairwiseSeqsCommand", "PairwiseSeqsCommand");
248 //**********************************************************************************************************************
250 int PairwiseSeqsCommand::execute(){
252 if (abort == true) { if (calledHelp) { return 0; } return 2; }
254 longestBase = 2000; //will need to update this in driver if we find sequences with more bases. hardcoded so we don't have the pre-read user fasta file.
258 for (int s = 0; s < fastaFileNames.size(); s++) {
259 if (m->control_pressed) { outputTypes.clear(); return 0; }
261 m->mothurOut("Processing sequences from " + fastaFileNames[s] + " ..." ); m->mothurOutEndLine();
263 if (outputDir == "") { outputDir += m->hasPath(fastaFileNames[s]); }
266 m->openInputFile(fastaFileNames[s], inFASTA);
267 alignDB = SequenceDB(inFASTA);
270 int numSeqs = alignDB.getNumSeqs();
271 int startTime = time(NULL);
272 string outputFile = "";
274 if (output == "lt") { //does the user want lower triangle phylip formatted file
275 outputFile = outputDir + m->getRootName(m->getSimpleName(fastaFileNames[s])) + "phylip.dist";
276 m->mothurRemove(outputFile); outputTypes["phylip"].push_back(outputFile);
277 }else if (output == "column") { //user wants column format
278 outputFile = outputDir + m->getRootName(m->getSimpleName(fastaFileNames[s])) + "dist";
279 outputTypes["column"].push_back(outputFile);
280 m->mothurRemove(outputFile);
281 }else { //assume square
282 outputFile = outputDir + m->getRootName(m->getSimpleName(fastaFileNames[s])) + "square.dist";
283 m->mothurRemove(outputFile);
284 outputTypes["phylip"].push_back(outputFile);
293 MPI_Comm_size(MPI_COMM_WORLD, &processors); //set processors to the number of mpi processes running
294 MPI_Comm_rank(MPI_COMM_WORLD, &pid); //find out who we are
296 //each process gets where it should start and stop in the file
297 if (output != "square") {
298 start = int (sqrt(float(pid)/float(processors)) * numSeqs);
299 end = int (sqrt(float(pid+1)/float(processors)) * numSeqs);
301 start = int ((float(pid)/float(processors)) * numSeqs);
302 end = int ((float(pid+1)/float(processors)) * numSeqs);
305 if (output == "column") {
307 int amode=MPI_MODE_CREATE|MPI_MODE_WRONLY;
310 strcpy(filename, outputFile.c_str());
312 MPI_File_open(MPI_COMM_WORLD, filename, amode, MPI_INFO_NULL, &outMPI);
314 if (pid == 0) { //you are the root process
319 driverMPI(start, end, outMPI, cutoff);
321 if (m->control_pressed) { outputTypes.clear(); MPI_File_close(&outMPI); m->mothurRemove(outputFile); return 0; }
324 for(int i = 1; i < processors; i++) {
325 if (m->control_pressed) { outputTypes.clear(); MPI_File_close(&outMPI); m->mothurRemove(outputFile); return 0; }
328 MPI_Recv(buf, 5, MPI_CHAR, i, tag, MPI_COMM_WORLD, &status);
330 }else { //you are a child process
332 driverMPI(start, end, outMPI, cutoff);
334 if (m->control_pressed) { outputTypes.clear(); MPI_File_close(&outMPI); m->mothurRemove(outputFile); return 0; }
338 //tell parent you are done.
339 MPI_Send(buf, 5, MPI_CHAR, 0, tag, MPI_COMM_WORLD);
342 MPI_File_close(&outMPI);
344 }else { //lower triangle format
345 if (pid == 0) { //you are the root process
349 unsigned long long mySize;
351 if (output != "square"){ driverMPI(start, end, outputFile, mySize); }
352 else { driverMPI(start, end, outputFile, mySize, output); }
354 if (m->control_pressed) { outputTypes.clear(); m->mothurRemove(outputFile); return 0; }
356 int amode=MPI_MODE_APPEND|MPI_MODE_WRONLY|MPI_MODE_CREATE; //
361 strcpy(filename, outputFile.c_str());
363 MPI_File_open(MPI_COMM_SELF, filename, amode, MPI_INFO_NULL, &outMPI);
366 for(int b = 1; b < processors; b++) {
367 unsigned long long fileSize;
369 if (m->control_pressed) { outputTypes.clear(); MPI_File_close(&outMPI); m->mothurRemove(outputFile); return 0; }
371 MPI_Recv(&fileSize, 1, MPI_LONG, b, tag, MPI_COMM_WORLD, &status);
373 string outTemp = outputFile + toString(b) + ".temp";
375 char* buf = new char[outTemp.length()];
376 memcpy(buf, outTemp.c_str(), outTemp.length());
378 MPI_File_open(MPI_COMM_SELF, buf, MPI_MODE_DELETE_ON_CLOSE|MPI_MODE_RDONLY, MPI_INFO_NULL, &inMPI);
382 while (count < fileSize) {
384 MPI_File_read(inMPI, buf2, 1, MPI_CHAR, &status);
385 MPI_File_write(outMPI, buf2, 1, MPI_CHAR, &status);
389 MPI_File_close(&inMPI); //deleted on close
392 MPI_File_close(&outMPI);
393 }else { //you are a child process
395 unsigned long long size;
396 if (output != "square"){ driverMPI(start, end, (outputFile + toString(pid) + ".temp"), size); }
397 else { driverMPI(start, end, (outputFile + toString(pid) + ".temp"), size, output); }
399 if (m->control_pressed) { return 0; }
401 //tell parent you are done.
402 MPI_Send(&size, 1, MPI_LONG, 0, tag, MPI_COMM_WORLD);
405 MPI_Barrier(MPI_COMM_WORLD); //make everyone wait - just in case
408 //#if defined (__APPLE__) || (__MACH__) || (linux) || (__linux) || (__linux__) || (__unix__) || (__unix)
409 //if you don't need to fork anything
411 if (output != "square") { driver(0, numSeqs, outputFile, cutoff); }
412 else { driver(0, numSeqs, outputFile, "square"); }
413 }else{ //you have multiple processors
415 for (int i = 0; i < processors; i++) {
416 distlinePair tempLine;
417 lines.push_back(tempLine);
418 if (output != "square") {
419 lines[i].start = int (sqrt(float(i)/float(processors)) * numSeqs);
420 lines[i].end = int (sqrt(float(i+1)/float(processors)) * numSeqs);
422 lines[i].start = int ((float(i)/float(processors)) * numSeqs);
423 lines[i].end = int ((float(i+1)/float(processors)) * numSeqs);
427 createProcesses(outputFile);
431 //if (output != "square") { driver(0, numSeqs, outputFile, cutoff); }
432 //else { driver(0, numSeqs, outputFile, "square"); }
436 if (m->control_pressed) { outputTypes.clear(); m->mothurRemove(outputFile); return 0; }
439 MPI_Comm_rank(MPI_COMM_WORLD, &pid);
441 if (pid == 0) { //only one process should output to screen
445 fileHandle.open(outputFile.c_str());
447 m->gobble(fileHandle);
448 if (fileHandle.eof()) { m->mothurOut(outputFile + " is blank. This can result if there are no distances below your cutoff."); m->mothurOutEndLine(); }
452 m->mothurOut("Compressing..."); m->mothurOutEndLine();
453 m->mothurOut("(Replacing " + outputFile + " with " + outputFile + ".gz)"); m->mothurOutEndLine();
454 system(("gzip -v " + outputFile).c_str());
455 outputNames.push_back(outputFile + ".gz");
456 }else { outputNames.push_back(outputFile); }
462 m->mothurOut("It took " + toString(time(NULL) - startTime) + " to calculate the distances for " + toString(numSeqs) + " sequences."); m->mothurOutEndLine();
464 if (m->control_pressed) { outputTypes.clear(); m->mothurRemove(outputFile); return 0; }
467 //set phylip file as new current phylipfile
469 itTypes = outputTypes.find("phylip");
470 if (itTypes != outputTypes.end()) {
471 if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setPhylipFile(current); }
474 //set column file as new current columnfile
475 itTypes = outputTypes.find("column");
476 if (itTypes != outputTypes.end()) {
477 if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setColumnFile(current); }
480 m->mothurOutEndLine();
481 m->mothurOut("Output File Name: "); m->mothurOutEndLine();
482 for (int i = 0; i < outputNames.size(); i++) { m->mothurOut(outputNames[i]); m->mothurOutEndLine(); }
483 m->mothurOutEndLine();
488 catch(exception& e) {
489 m->errorOut(e, "PairwiseSeqsCommand", "execute");
494 /**************************************************************************************************/
495 void PairwiseSeqsCommand::createProcesses(string filename) {
500 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux) || (__linux__) || (__unix__) || (__unix)
503 //loop through and create all the processes you want
504 while (process != processors) {
508 processIDS.push_back(pid);
511 if (output != "square") { driver(lines[process].start, lines[process].end, filename + toString(getpid()) + ".temp", cutoff); }
512 else { driver(lines[process].start, lines[process].end, filename + toString(getpid()) + ".temp", "square"); }
515 m->mothurOut("[ERROR]: unable to spawn the necessary processes."); m->mothurOutEndLine();
516 for (int i=0;i<processIDS.size();i++) { int temp = processIDS[i]; kill (temp, SIGINT); }
522 if (output != "square") { driver(lines[0].start, lines[0].end, filename, cutoff); }
523 else { driver(lines[0].start, lines[0].end, filename, "square"); }
526 //force parent to wait until all the processes are done
527 for (int i=0;i<processIDS.size();i++) {
528 int temp = processIDS[i];
532 //////////////////////////////////////////////////////////////////////////////////////////////////////
533 //Windows version shared memory, so be careful when passing variables through the distanceData struct.
534 //Above fork() will clone, so memory is separate, but that's not the case with windows,
535 //that's why the distance calculator was moved inside of the driver to make separate copies.
536 //////////////////////////////////////////////////////////////////////////////////////////////////////
538 vector<pairwiseData*> pDataArray; //[processors-1];
539 DWORD dwThreadIdArray[processors-1];
540 HANDLE hThreadArray[processors-1];
542 //Create processor-1 worker threads.
543 for( int i=0; i<processors-1; i++ ){
544 string extension = toString(i) + ".temp";
546 // Allocate memory for thread data.
547 pairwiseData* tempDist = new pairwiseData((filename+extension), align, "square", Estimators[0], countends, output, alignDB, m, lines[i+1].start, lines[i+1].end, match, misMatch, gapOpen, gapExtend, longestBase, i);
548 pDataArray.push_back(tempDist);
549 processIDS.push_back(i);
551 if (output != "square") { hThreadArray[i] = CreateThread(NULL, 0, MyPairwiseThreadFunction, pDataArray[i], 0, &dwThreadIdArray[i]); }
552 else { hThreadArray[i] = CreateThread(NULL, 0, MyPairwiseSquareThreadFunction, pDataArray[i], 0, &dwThreadIdArray[i]); }
556 if (output != "square") { driver(lines[0].start, lines[0].end, filename, cutoff); }
557 else { driver(lines[0].start, lines[0].end, filename, "square"); }
559 //Wait until all threads have terminated.
560 WaitForMultipleObjects(processors-1, hThreadArray, TRUE, INFINITE);
562 //Close all thread handles and free memory allocations.
563 for(int i=0; i < pDataArray.size(); i++){
564 CloseHandle(hThreadArray[i]);
565 delete pDataArray[i];
570 //append and remove temp files
571 for (int i=0;i<processIDS.size();i++) {
572 m->appendFiles((filename + toString(processIDS[i]) + ".temp"), filename);
573 m->mothurRemove((filename + toString(processIDS[i]) + ".temp"));
577 catch(exception& e) {
578 m->errorOut(e, "PairwiseSeqsCommand", "createProcesses");
583 /**************************************************************************************************/
584 /////// need to fix to work with calcs and sequencedb
585 int PairwiseSeqsCommand::driver(int startLine, int endLine, string dFileName, float cutoff){
588 int startTime = time(NULL);
590 Alignment* alignment;
591 if(align == "gotoh") { alignment = new GotohOverlap(gapOpen, gapExtend, match, misMatch, longestBase); }
592 else if(align == "needleman") { alignment = new NeedlemanOverlap(gapOpen, match, misMatch, longestBase); }
593 else if(align == "blast") { alignment = new BlastAlignment(gapOpen, gapExtend, match, misMatch); }
594 else if(align == "noalign") { alignment = new NoAlign(); }
596 m->mothurOut(align + " is not a valid alignment option. I will run the command using needleman.");
597 m->mothurOutEndLine();
598 alignment = new NeedlemanOverlap(gapOpen, match, misMatch, longestBase);
601 ValidCalculators validCalculator;
602 Dist* distCalculator;
604 if (validCalculator.isValidCalculator("distance", Estimators[0]) == true) {
605 if (Estimators[0] == "nogaps") { distCalculator = new ignoreGaps(); }
606 else if (Estimators[0] == "eachgap") { distCalculator = new eachGapDist(); }
607 else if (Estimators[0] == "onegap") { distCalculator = new oneGapDist(); }
610 if (validCalculator.isValidCalculator("distance", Estimators[0]) == true) {
611 if (Estimators[0] == "nogaps") { distCalculator = new ignoreGaps(); }
612 else if (Estimators[0] == "eachgap"){ distCalculator = new eachGapIgnoreTermGapDist(); }
613 else if (Estimators[0] == "onegap") { distCalculator = new oneGapIgnoreTermGapDist(); }
618 ofstream outFile(dFileName.c_str(), ios::trunc);
619 outFile.setf(ios::fixed, ios::showpoint);
620 outFile << setprecision(4);
622 if((output == "lt") && startLine == 0){ outFile << alignDB.getNumSeqs() << endl; }
624 for(int i=startLine;i<endLine;i++){
626 string name = alignDB.get(i).getName();
627 if (name.length() < 10) { //pad with spaces to make compatible
628 while (name.length() < 10) { name += " "; }
630 outFile << name << '\t';
633 for(int j=0;j<i;j++){
635 if (m->control_pressed) { outFile.close(); delete alignment; delete distCalculator; return 0; }
637 if (alignDB.get(i).getUnaligned().length() > alignment->getnRows()) {
638 alignment->resize(alignDB.get(i).getUnaligned().length()+1);
641 if (alignDB.get(j).getUnaligned().length() > alignment->getnRows()) {
642 alignment->resize(alignDB.get(j).getUnaligned().length()+1);
645 Sequence seqI(alignDB.get(i).getName(), alignDB.get(i).getAligned());
646 Sequence seqJ(alignDB.get(j).getName(), alignDB.get(j).getAligned());
648 alignment->align(seqI.getUnaligned(), seqJ.getUnaligned());
649 seqI.setAligned(alignment->getSeqAAln());
650 seqJ.setAligned(alignment->getSeqBAln());
653 distCalculator->calcDist(seqI, seqJ);
654 double dist = distCalculator->getDist();
657 if (output == "column") { outFile << alignDB.get(i).getName() << ' ' << alignDB.get(j).getName() << ' ' << dist << endl; }
659 if (output == "lt") { outFile << dist << '\t'; }
662 if (output == "lt") { outFile << endl; }
665 m->mothurOut(toString(i) + "\t" + toString(time(NULL) - startTime)); m->mothurOutEndLine();
669 m->mothurOut(toString(endLine-1) + "\t" + toString(time(NULL) - startTime)); m->mothurOutEndLine();
673 delete distCalculator;
677 catch(exception& e) {
678 m->errorOut(e, "PairwiseSeqsCommand", "driver");
682 /**************************************************************************************************/
683 /////// need to fix to work with calcs and sequencedb
684 int PairwiseSeqsCommand::driver(int startLine, int endLine, string dFileName, string square){
687 int startTime = time(NULL);
689 Alignment* alignment;
690 if(align == "gotoh") { alignment = new GotohOverlap(gapOpen, gapExtend, match, misMatch, longestBase); }
691 else if(align == "needleman") { alignment = new NeedlemanOverlap(gapOpen, match, misMatch, longestBase); }
692 else if(align == "blast") { alignment = new BlastAlignment(gapOpen, gapExtend, match, misMatch); }
693 else if(align == "noalign") { alignment = new NoAlign(); }
695 m->mothurOut(align + " is not a valid alignment option. I will run the command using needleman.");
696 m->mothurOutEndLine();
697 alignment = new NeedlemanOverlap(gapOpen, match, misMatch, longestBase);
700 ValidCalculators validCalculator;
701 Dist* distCalculator;
703 if (validCalculator.isValidCalculator("distance", Estimators[0]) == true) {
704 if (Estimators[0] == "nogaps") { distCalculator = new ignoreGaps(); }
705 else if (Estimators[0] == "eachgap") { distCalculator = new eachGapDist(); }
706 else if (Estimators[0] == "onegap") { distCalculator = new oneGapDist(); }
709 if (validCalculator.isValidCalculator("distance", Estimators[0]) == true) {
710 if (Estimators[0] == "nogaps") { distCalculator = new ignoreGaps(); }
711 else if (Estimators[0] == "eachgap"){ distCalculator = new eachGapIgnoreTermGapDist(); }
712 else if (Estimators[0] == "onegap") { distCalculator = new oneGapIgnoreTermGapDist(); }
717 ofstream outFile(dFileName.c_str(), ios::trunc);
718 outFile.setf(ios::fixed, ios::showpoint);
719 outFile << setprecision(4);
721 if(startLine == 0){ outFile << alignDB.getNumSeqs() << endl; }
723 for(int i=startLine;i<endLine;i++){
725 string name = alignDB.get(i).getName();
726 //pad with spaces to make compatible
727 if (name.length() < 10) { while (name.length() < 10) { name += " "; } }
729 outFile << name << '\t';
731 for(int j=0;j<alignDB.getNumSeqs();j++){
733 if (m->control_pressed) { outFile.close(); delete alignment; delete distCalculator; return 0; }
735 if (alignDB.get(i).getUnaligned().length() > alignment->getnRows()) {
736 alignment->resize(alignDB.get(i).getUnaligned().length()+1);
739 if (alignDB.get(j).getUnaligned().length() > alignment->getnRows()) {
740 alignment->resize(alignDB.get(j).getUnaligned().length()+1);
743 Sequence seqI(alignDB.get(i).getName(), alignDB.get(i).getAligned());
744 Sequence seqJ(alignDB.get(j).getName(), alignDB.get(j).getAligned());
746 alignment->align(seqI.getUnaligned(), seqJ.getUnaligned());
747 seqI.setAligned(alignment->getSeqAAln());
748 seqJ.setAligned(alignment->getSeqBAln());
750 distCalculator->calcDist(seqI, seqJ);
751 double dist = distCalculator->getDist();
753 outFile << dist << '\t';
759 m->mothurOut(toString(i) + "\t" + toString(time(NULL) - startTime)); m->mothurOutEndLine();
763 m->mothurOut(toString(endLine-1) + "\t" + toString(time(NULL) - startTime)); m->mothurOutEndLine();
767 delete distCalculator;
771 catch(exception& e) {
772 m->errorOut(e, "PairwiseSeqsCommand", "driver");
777 /**************************************************************************************************/
778 /////// need to fix to work with calcs and sequencedb
779 int PairwiseSeqsCommand::driverMPI(int startLine, int endLine, MPI_File& outMPI, float cutoff){
782 int startTime = time(NULL);
784 Alignment* alignment;
785 if(align == "gotoh") { alignment = new GotohOverlap(gapOpen, gapExtend, match, misMatch, longestBase); }
786 else if(align == "needleman") { alignment = new NeedlemanOverlap(gapOpen, match, misMatch, longestBase); }
787 else if(align == "blast") { alignment = new BlastAlignment(gapOpen, gapExtend, match, misMatch); }
788 else if(align == "noalign") { alignment = new NoAlign(); }
790 m->mothurOut(align + " is not a valid alignment option. I will run the command using needleman.");
791 m->mothurOutEndLine();
792 alignment = new NeedlemanOverlap(gapOpen, match, misMatch, longestBase);
795 ValidCalculators validCalculator;
796 Dist* distCalculator;
798 if (validCalculator.isValidCalculator("distance", Estimators[0]) == true) {
799 if (Estimators[0] == "nogaps") { distCalculator = new ignoreGaps(); }
800 else if (Estimators[0] == "eachgap") { distCalculator = new eachGapDist(); }
801 else if (Estimators[0] == "onegap") { distCalculator = new oneGapDist(); }
804 if (validCalculator.isValidCalculator("distance", Estimators[0]) == true) {
805 if (Estimators[0] == "nogaps") { distCalculator = new ignoreGaps(); }
806 else if (Estimators[0] == "eachgap"){ distCalculator = new eachGapIgnoreTermGapDist(); }
807 else if (Estimators[0] == "onegap") { distCalculator = new oneGapIgnoreTermGapDist(); }
811 string outputString = "";
813 for(int i=startLine;i<endLine;i++){
815 for(int j=0;j<i;j++){
817 if (m->control_pressed) { delete alignment; delete distCalculator; return 0; }
819 if (alignDB.get(i).getUnaligned().length() > alignment->getnRows()) {
820 alignment->resize(alignDB.get(i).getUnaligned().length()+1);
823 if (alignDB.get(j).getUnaligned().length() > alignment->getnRows()) {
824 alignment->resize(alignDB.get(j).getUnaligned().length()+1);
827 Sequence seqI(alignDB.get(i).getName(), alignDB.get(i).getAligned());
828 Sequence seqJ(alignDB.get(j).getName(), alignDB.get(j).getAligned());
830 alignment->align(seqI.getUnaligned(), seqJ.getUnaligned());
831 seqI.setAligned(alignment->getSeqAAln());
832 seqJ.setAligned(alignment->getSeqBAln());
834 distCalculator->calcDist(seqI, seqJ);
835 double dist = distCalculator->getDist();
838 outputString += (alignDB.get(i).getName() + ' ' + alignDB.get(j).getName() + ' ' + toString(dist) + '\n');
843 //m->mothurOut(toString(i) + "\t" + toString(time(NULL) - startTime)); m->mothurOutEndLine();
844 cout << i << '\t' << (time(NULL) - startTime) << endl;
848 //send results to parent
849 int length = outputString.length();
851 char* buf = new char[length];
852 memcpy(buf, outputString.c_str(), length);
854 MPI_File_write_shared(outMPI, buf, length, MPI_CHAR, &status);
860 delete distCalculator;
863 catch(exception& e) {
864 m->errorOut(e, "PairwiseSeqsCommand", "driverMPI");
868 /**************************************************************************************************/
869 /////// need to fix to work with calcs and sequencedb
870 int PairwiseSeqsCommand::driverMPI(int startLine, int endLine, string file, unsigned long long& size){
875 int amode=MPI_MODE_CREATE|MPI_MODE_WRONLY;
878 strcpy(filename, file.c_str());
880 MPI_File_open(MPI_COMM_SELF, filename, amode, MPI_INFO_NULL, &outMPI);
882 Alignment* alignment;
883 if(align == "gotoh") { alignment = new GotohOverlap(gapOpen, gapExtend, match, misMatch, longestBase); }
884 else if(align == "needleman") { alignment = new NeedlemanOverlap(gapOpen, match, misMatch, longestBase); }
885 else if(align == "blast") { alignment = new BlastAlignment(gapOpen, gapExtend, match, misMatch); }
886 else if(align == "noalign") { alignment = new NoAlign(); }
888 m->mothurOut(align + " is not a valid alignment option. I will run the command using needleman.");
889 m->mothurOutEndLine();
890 alignment = new NeedlemanOverlap(gapOpen, match, misMatch, longestBase);
893 ValidCalculators validCalculator;
894 Dist* distCalculator;
896 if (validCalculator.isValidCalculator("distance", Estimators[0]) == true) {
897 if (Estimators[0] == "nogaps") { distCalculator = new ignoreGaps(); }
898 else if (Estimators[0] == "eachgap") { distCalculator = new eachGapDist(); }
899 else if (Estimators[0] == "onegap") { distCalculator = new oneGapDist(); }
902 if (validCalculator.isValidCalculator("distance", Estimators[0]) == true) {
903 if (Estimators[0] == "nogaps") { distCalculator = new ignoreGaps(); }
904 else if (Estimators[0] == "eachgap"){ distCalculator = new eachGapIgnoreTermGapDist(); }
905 else if (Estimators[0] == "onegap") { distCalculator = new oneGapIgnoreTermGapDist(); }
910 string outputString = "";
913 if(startLine == 0){ outputString += toString(alignDB.getNumSeqs()) + "\n"; }
915 for(int i=startLine;i<endLine;i++){
917 string name = alignDB.get(i).getName();
918 if (name.length() < 10) { //pad with spaces to make compatible
919 while (name.length() < 10) { name += " "; }
921 outputString += name + "\t";
923 for(int j=0;j<i;j++){
925 if (m->control_pressed) { delete alignment; delete distCalculator; return 0; }
927 if (alignDB.get(i).getUnaligned().length() > alignment->getnRows()) {
928 alignment->resize(alignDB.get(i).getUnaligned().length()+1);
931 if (alignDB.get(j).getUnaligned().length() > alignment->getnRows()) {
932 alignment->resize(alignDB.get(j).getUnaligned().length()+1);
935 Sequence seqI(alignDB.get(i).getName(), alignDB.get(i).getAligned());
936 Sequence seqJ(alignDB.get(j).getName(), alignDB.get(j).getAligned());
938 alignment->align(seqI.getUnaligned(), seqJ.getUnaligned());
939 seqI.setAligned(alignment->getSeqAAln());
940 seqJ.setAligned(alignment->getSeqBAln());
942 distCalculator->calcDist(seqI, seqJ);
943 double dist = distCalculator->getDist();
945 outputString += toString(dist) + "\t";
948 outputString += "\n";
950 //send results to parent
951 int length = outputString.length();
952 char* buf = new char[length];
953 memcpy(buf, outputString.c_str(), length);
955 MPI_File_write(outMPI, buf, length, MPI_CHAR, &status);
956 size += outputString.length();
961 MPI_File_close(&outMPI);
963 delete distCalculator;
967 catch(exception& e) {
968 m->errorOut(e, "PairwiseSeqsCommand", "driverMPI");
972 /**************************************************************************************************/
973 /////// need to fix to work with calcs and sequencedb
974 int PairwiseSeqsCommand::driverMPI(int startLine, int endLine, string file, unsigned long long& size, string square){
979 int amode=MPI_MODE_CREATE|MPI_MODE_WRONLY;
982 strcpy(filename, file.c_str());
984 MPI_File_open(MPI_COMM_SELF, filename, amode, MPI_INFO_NULL, &outMPI);
986 Alignment* alignment;
987 if(align == "gotoh") { alignment = new GotohOverlap(gapOpen, gapExtend, match, misMatch, longestBase); }
988 else if(align == "needleman") { alignment = new NeedlemanOverlap(gapOpen, match, misMatch, longestBase); }
989 else if(align == "blast") { alignment = new BlastAlignment(gapOpen, gapExtend, match, misMatch); }
990 else if(align == "noalign") { alignment = new NoAlign(); }
992 m->mothurOut(align + " is not a valid alignment option. I will run the command using needleman.");
993 m->mothurOutEndLine();
994 alignment = new NeedlemanOverlap(gapOpen, match, misMatch, longestBase);
997 ValidCalculators validCalculator;
998 Dist* distCalculator;
1000 if (validCalculator.isValidCalculator("distance", Estimators[0]) == true) {
1001 if (Estimators[0] == "nogaps") { distCalculator = new ignoreGaps(); }
1002 else if (Estimators[0] == "eachgap") { distCalculator = new eachGapDist(); }
1003 else if (Estimators[0] == "onegap") { distCalculator = new oneGapDist(); }
1006 if (validCalculator.isValidCalculator("distance", Estimators[0]) == true) {
1007 if (Estimators[0] == "nogaps") { distCalculator = new ignoreGaps(); }
1008 else if (Estimators[0] == "eachgap"){ distCalculator = new eachGapIgnoreTermGapDist(); }
1009 else if (Estimators[0] == "onegap") { distCalculator = new oneGapIgnoreTermGapDist(); }
1013 string outputString = "";
1016 if(startLine == 0){ outputString += toString(alignDB.getNumSeqs()) + "\n"; }
1018 for(int i=startLine;i<endLine;i++){
1020 string name = alignDB.get(i).getName();
1021 if (name.length() < 10) { //pad with spaces to make compatible
1022 while (name.length() < 10) { name += " "; }
1024 outputString += name + "\t";
1026 for(int j=0;j<alignDB.getNumSeqs();j++){
1028 if (m->control_pressed) { delete alignment; return 0; }
1030 if (alignDB.get(i).getUnaligned().length() > alignment->getnRows()) {
1031 alignment->resize(alignDB.get(i).getUnaligned().length()+1);
1034 if (alignDB.get(j).getUnaligned().length() > alignment->getnRows()) {
1035 alignment->resize(alignDB.get(j).getUnaligned().length()+1);
1038 Sequence seqI(alignDB.get(i).getName(), alignDB.get(i).getAligned());
1039 Sequence seqJ(alignDB.get(j).getName(), alignDB.get(j).getAligned());
1041 alignment->align(seqI.getUnaligned(), seqJ.getUnaligned());
1042 seqI.setAligned(alignment->getSeqAAln());
1043 seqJ.setAligned(alignment->getSeqBAln());
1045 distCalculator->calcDist(seqI, seqJ);
1046 double dist = distCalculator->getDist();
1048 outputString += toString(dist) + "\t";
1051 outputString += "\n";
1053 //send results to parent
1054 int length = outputString.length();
1055 char* buf = new char[length];
1056 memcpy(buf, outputString.c_str(), length);
1058 MPI_File_write(outMPI, buf, length, MPI_CHAR, &status);
1059 size += outputString.length();
1064 MPI_File_close(&outMPI);
1069 catch(exception& e) {
1070 m->errorOut(e, "PairwiseSeqsCommand", "driverMPI");
1075 /**************************************************************************************************/