2 * pairwiseseqscommand.cpp
5 * Created by westcott on 10/20/10.
6 * Copyright 2010 Schloss Lab. All rights reserved.
10 #include "pairwiseseqscommand.h"
11 #include "sequence.hpp"
13 #include "gotohoverlap.hpp"
14 #include "needlemanoverlap.hpp"
15 #include "blastalign.hpp"
16 #include "noalign.hpp"
19 #include "ignoregaps.h"
20 #include "eachgapdist.h"
21 #include "eachgapignore.h"
22 #include "onegapdist.h"
23 #include "onegapignore.h"
26 //**********************************************************************************************************************
27 vector<string> PairwiseSeqsCommand::getValidParameters(){
29 string AlignArray[] = {"fasta","align","match","mismatch","gapopen","gapextend", "processors","calc","compress","cutoff","countends","output","outputdir","inputdir"};
30 vector<string> myArray (AlignArray, AlignArray+(sizeof(AlignArray)/sizeof(string)));
34 m->errorOut(e, "PairwiseSeqsCommand", "getValidParameters");
38 //**********************************************************************************************************************
39 vector<string> PairwiseSeqsCommand::getRequiredParameters(){
41 string AlignArray[] = {"fasta"};
42 vector<string> myArray (AlignArray, AlignArray+(sizeof(AlignArray)/sizeof(string)));
46 m->errorOut(e, "PairwiseSeqsCommand", "getRequiredParameters");
50 //**********************************************************************************************************************
51 vector<string> PairwiseSeqsCommand::getRequiredFiles(){
53 vector<string> myArray;
57 m->errorOut(e, "PairwiseSeqsCommand", "getRequiredFiles");
61 //**********************************************************************************************************************
62 PairwiseSeqsCommand::PairwiseSeqsCommand(){
65 //initialize outputTypes
66 vector<string> tempOutNames;
67 outputTypes["phylip"] = tempOutNames;
68 outputTypes["column"] = tempOutNames;
71 m->errorOut(e, "PairwiseSeqsCommand", "PairwiseSeqsCommand");
75 //**********************************************************************************************************************
76 PairwiseSeqsCommand::PairwiseSeqsCommand(string option) {
80 //allow user to run help
81 if(option == "help") { help(); abort = true; }
85 //valid paramters for this command
86 string AlignArray[] = {"fasta","align","match","mismatch","gapopen","gapextend", "processors","cutoff","compress","calc","countends","output","outputdir","inputdir"};
87 vector<string> myArray (AlignArray, AlignArray+(sizeof(AlignArray)/sizeof(string)));
89 OptionParser parser(option);
90 map<string, string> parameters = parser.getParameters();
92 ValidParameters validParameter("pairwise.seqs");
93 map<string, string>::iterator it;
95 //check to make sure all parameters are valid for command
96 for (it = parameters.begin(); it != parameters.end(); it++) {
97 if (validParameter.isValidParameter(it->first, myArray, it->second) != true) { abort = true; }
100 //initialize outputTypes
101 vector<string> tempOutNames;
102 outputTypes["phylip"] = tempOutNames;
103 outputTypes["column"] = tempOutNames;
105 //if the user changes the output directory command factory will send this info to us in the output parameter
106 outputDir = validParameter.validFile(parameters, "outputdir", false); if (outputDir == "not found"){ outputDir = ""; }
109 //if the user changes the input directory command factory will send this info to us in the output parameter
110 string inputDir = validParameter.validFile(parameters, "inputdir", false);
112 if (inputDir == "not found"){ inputDir = ""; }
114 fastaFileName = validParameter.validFile(parameters, "fasta", false);
115 if (fastaFileName == "not found") { m->mothurOut("fasta is a required parameter for the pairwise.seqs command."); m->mothurOutEndLine(); abort = true; }
117 m->splitAtDash(fastaFileName, fastaFileNames);
119 //go through files and make sure they are good, if not, then disregard them
120 for (int i = 0; i < fastaFileNames.size(); i++) {
122 if (inputDir != "") {
123 string path = m->hasPath(fastaFileNames[i]);
124 //if the user has not given a path then, add inputdir. else leave path alone.
125 if (path == "") { fastaFileNames[i] = inputDir + fastaFileNames[i]; }
131 ableToOpen = m->openInputFile(fastaFileNames[i], in, "noerror");
133 //if you can't open it, try default location
134 if (ableToOpen == 1) {
135 if (m->getDefaultPath() != "") { //default path is set
136 string tryPath = m->getDefaultPath() + m->getSimpleName(fastaFileNames[i]);
137 m->mothurOut("Unable to open " + fastaFileNames[i] + ". Trying default " + tryPath); m->mothurOutEndLine();
139 ableToOpen = m->openInputFile(tryPath, in2, "noerror");
141 fastaFileNames[i] = tryPath;
145 //if you can't open it, try output location
146 if (ableToOpen == 1) {
147 if (m->getOutputDir() != "") { //default path is set
148 string tryPath = m->getOutputDir() + m->getSimpleName(fastaFileNames[i]);
149 m->mothurOut("Unable to open " + fastaFileNames[i] + ". Trying output directory " + tryPath); m->mothurOutEndLine();
151 ableToOpen = m->openInputFile(tryPath, in2, "noerror");
153 fastaFileNames[i] = tryPath;
159 if (ableToOpen == 1) {
160 m->mothurOut("Unable to open " + fastaFileNames[i] + ". It will be disregarded."); m->mothurOutEndLine();
161 //erase from file list
162 fastaFileNames.erase(fastaFileNames.begin()+i);
167 //make sure there is at least one valid file left
168 if (fastaFileNames.size() == 0) { m->mothurOut("no valid files."); m->mothurOutEndLine(); abort = true; }
171 //check for optional parameter and set defaults
172 // ...at some point should added some additional type checking...
174 temp = validParameter.validFile(parameters, "match", false); if (temp == "not found"){ temp = "1.0"; }
175 convert(temp, match);
177 temp = validParameter.validFile(parameters, "mismatch", false); if (temp == "not found"){ temp = "-1.0"; }
178 convert(temp, misMatch);
180 temp = validParameter.validFile(parameters, "gapopen", false); if (temp == "not found"){ temp = "-2.0"; }
181 convert(temp, gapOpen);
183 temp = validParameter.validFile(parameters, "gapextend", false); if (temp == "not found"){ temp = "-1.0"; }
184 convert(temp, gapExtend);
186 temp = validParameter.validFile(parameters, "processors", false); if (temp == "not found"){ temp = "1"; }
187 convert(temp, processors);
189 temp = validParameter.validFile(parameters, "cutoff", false); if(temp == "not found"){ temp = "1.0"; }
190 convert(temp, cutoff);
192 temp = validParameter.validFile(parameters, "countends", false); if(temp == "not found"){ temp = "T"; }
193 countends = m->isTrue(temp);
195 temp = validParameter.validFile(parameters, "compress", false); if(temp == "not found"){ temp = "F"; }
196 compress = m->isTrue(temp);
198 align = validParameter.validFile(parameters, "align", false); if (align == "not found"){ align = "needleman"; }
200 output = validParameter.validFile(parameters, "output", false); if(output == "not found"){ output = "column"; }
201 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"; }
203 calc = validParameter.validFile(parameters, "calc", false);
204 if (calc == "not found") { calc = "onegap"; }
206 if (calc == "default") { calc = "onegap"; }
208 m->splitAtDash(calc, Estimators);
210 ValidCalculators validCalculator;
212 for (int i=0; i<Estimators.size(); i++) {
213 if (validCalculator.isValidCalculator("distance", Estimators[i]) == true) {
214 if (Estimators[i] == "nogaps") { distCalculator = new ignoreGaps(); }
215 else if (Estimators[i] == "eachgap") { distCalculator = new eachGapDist(); }
216 else if (Estimators[i] == "onegap") { distCalculator = new oneGapDist(); }
220 for (int i=0; i<Estimators.size(); i++) {
221 if (validCalculator.isValidCalculator("distance", Estimators[i]) == true) {
222 if (Estimators[i] == "nogaps") { distCalculator = new ignoreGaps(); }
223 else if (Estimators[i] == "eachgap"){ distCalculator = new eachGapIgnoreTermGapDist(); }
224 else if (Estimators[i] == "onegap") { distCalculator = new oneGapIgnoreTermGapDist(); }
231 catch(exception& e) {
232 m->errorOut(e, "PairwiseSeqsCommand", "PairwiseSeqsCommand");
236 //**********************************************************************************************************************
237 PairwiseSeqsCommand::~PairwiseSeqsCommand(){}
238 //**********************************************************************************************************************
240 void PairwiseSeqsCommand::help(){
242 m->mothurOut("The pairwise.seqs command reads a fasta file and creates distance matrix.\n");
243 m->mothurOut("The pairwise.seqs command parameters are fasta, align, match, mismatch, gapopen, gapextend, calc, output, cutoff and processors.\n");
244 m->mothurOut("The fasta parameter is required. You may enter multiple fasta files by separating their names with dashes. ie. fasta=abrecovery.fasta-amzon.fasta \n");
245 m->mothurOut("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");
246 m->mothurOut("The match parameter allows you to specify the bonus for having the same base. The default is 1.0.\n");
247 m->mothurOut("The mistmatch parameter allows you to specify the penalty for having different bases. The default is -1.0.\n");
248 m->mothurOut("The gapopen parameter allows you to specify the penalty for opening a gap in an alignment. The default is -2.0.\n");
249 m->mothurOut("The gapextend parameter allows you to specify the penalty for extending a gap in an alignment. The default is -1.0.\n");
250 m->mothurOut("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");
251 m->mothurOut("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");
252 m->mothurOut("The cutoff parameter allows you to specify maximum distance to keep. The default is 1.0.\n");
253 m->mothurOut("The output parameter allows you to specify format of your distance matrix. Options are column, lt, and square. The default is column.\n");
254 m->mothurOut("The compress parameter allows you to indicate that you want the resulting distance file compressed. The default is false.\n");
255 m->mothurOut("The pairwise.seqs command should be in the following format: \n");
256 m->mothurOut("pairwise.seqs(fasta=yourfastaFile, align=yourAlignmentMethod) \n");
257 m->mothurOut("Example pairwise.seqs(fasta=candidate.fasta, align=blast)\n");
258 m->mothurOut("Note: No spaces between parameter labels (i.e. fasta), '=' and parameters (i.e.yourFastaFile).\n\n");
260 catch(exception& e) {
261 m->errorOut(e, "PairwiseSeqsCommand", "help");
267 //**********************************************************************************************************************
269 int PairwiseSeqsCommand::execute(){
271 if (abort == true) { return 0; }
273 int 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.
275 if(align == "gotoh") { alignment = new GotohOverlap(gapOpen, gapExtend, match, misMatch, longestBase); }
276 else if(align == "needleman") { alignment = new NeedlemanOverlap(gapOpen, match, misMatch, longestBase); }
277 else if(align == "blast") { alignment = new BlastAlignment(gapOpen, gapExtend, match, misMatch); }
278 else if(align == "noalign") { alignment = new NoAlign(); }
280 m->mothurOut(align + " is not a valid alignment option. I will run the command using needleman.");
281 m->mothurOutEndLine();
282 alignment = new NeedlemanOverlap(gapOpen, match, misMatch, longestBase);
287 for (int s = 0; s < fastaFileNames.size(); s++) {
288 if (m->control_pressed) { outputTypes.clear(); return 0; }
290 m->mothurOut("Processing sequences from " + fastaFileNames[s] + " ..." ); m->mothurOutEndLine();
292 if (outputDir == "") { outputDir += m->hasPath(fastaFileNames[s]); }
295 m->openInputFile(fastaFileNames[s], inFASTA);
296 alignDB = SequenceDB(inFASTA);
299 int numSeqs = alignDB.getNumSeqs();
300 int startTime = time(NULL);
301 string outputFile = "";
303 if (output == "lt") { //does the user want lower triangle phylip formatted file
304 outputFile = outputDir + m->getRootName(m->getSimpleName(fastaFileNames[s])) + "phylip.dist";
305 remove(outputFile.c_str()); outputTypes["phylip"].push_back(outputFile);
306 }else if (output == "column") { //user wants column format
307 outputFile = outputDir + m->getRootName(m->getSimpleName(fastaFileNames[s])) + "dist";
308 outputTypes["column"].push_back(outputFile);
309 remove(outputFile.c_str());
310 }else { //assume square
311 outputFile = outputDir + m->getRootName(m->getSimpleName(fastaFileNames[s])) + "square.dist";
312 remove(outputFile.c_str());
313 outputTypes["phylip"].push_back(outputFile);
322 MPI_Comm_size(MPI_COMM_WORLD, &processors); //set processors to the number of mpi processes running
323 MPI_Comm_rank(MPI_COMM_WORLD, &pid); //find out who we are
325 //each process gets where it should start and stop in the file
326 if (output != "square") {
327 start = int (sqrt(float(pid)/float(processors)) * numSeqs);
328 end = int (sqrt(float(pid+1)/float(processors)) * numSeqs);
330 start = int ((float(pid)/float(processors)) * numSeqs);
331 end = int ((float(pid+1)/float(processors)) * numSeqs);
334 if (output == "column") {
336 int amode=MPI_MODE_CREATE|MPI_MODE_WRONLY;
339 strcpy(filename, outputFile.c_str());
341 MPI_File_open(MPI_COMM_WORLD, filename, amode, MPI_INFO_NULL, &outMPI);
343 if (pid == 0) { //you are the root process
348 driverMPI(start, end, outMPI, cutoff);
350 if (m->control_pressed) { outputTypes.clear(); MPI_File_close(&outMPI); remove(outputFile.c_str()); delete distCalculator; return 0; }
353 for(int i = 1; i < processors; i++) {
354 if (m->control_pressed) { outputTypes.clear(); MPI_File_close(&outMPI); remove(outputFile.c_str()); delete distCalculator; return 0; }
357 MPI_Recv(buf, 5, MPI_CHAR, i, tag, MPI_COMM_WORLD, &status);
359 }else { //you are a child process
361 driverMPI(start, end, outMPI, cutoff);
363 if (m->control_pressed) { outputTypes.clear(); MPI_File_close(&outMPI); remove(outputFile.c_str()); delete distCalculator; return 0; }
367 //tell parent you are done.
368 MPI_Send(buf, 5, MPI_CHAR, 0, tag, MPI_COMM_WORLD);
371 MPI_File_close(&outMPI);
373 }else { //lower triangle format
374 if (pid == 0) { //you are the root process
378 unsigned long int mySize;
380 if (output != "square"){ driverMPI(start, end, outputFile, mySize); }
381 else { driverMPI(start, end, outputFile, mySize, output); }
383 if (m->control_pressed) { outputTypes.clear(); remove(outputFile.c_str()); delete distCalculator; return 0; }
385 int amode=MPI_MODE_APPEND|MPI_MODE_WRONLY|MPI_MODE_CREATE; //
390 strcpy(filename, outputFile.c_str());
392 MPI_File_open(MPI_COMM_SELF, filename, amode, MPI_INFO_NULL, &outMPI);
395 for(int b = 1; b < processors; b++) {
396 unsigned long int fileSize;
398 if (m->control_pressed) { outputTypes.clear(); MPI_File_close(&outMPI); remove(outputFile.c_str()); delete distCalculator; return 0; }
400 MPI_Recv(&fileSize, 1, MPI_LONG, b, tag, MPI_COMM_WORLD, &status);
402 string outTemp = outputFile + toString(b) + ".temp";
404 char* buf = new char[outTemp.length()];
405 memcpy(buf, outTemp.c_str(), outTemp.length());
407 MPI_File_open(MPI_COMM_SELF, buf, MPI_MODE_DELETE_ON_CLOSE|MPI_MODE_RDONLY, MPI_INFO_NULL, &inMPI);
411 while (count < fileSize) {
413 MPI_File_read(inMPI, buf2, 1, MPI_CHAR, &status);
414 MPI_File_write(outMPI, buf2, 1, MPI_CHAR, &status);
418 MPI_File_close(&inMPI); //deleted on close
421 MPI_File_close(&outMPI);
422 }else { //you are a child process
424 unsigned long int size;
425 if (output != "square"){ driverMPI(start, end, (outputFile + toString(pid) + ".temp"), size); }
426 else { driverMPI(start, end, (outputFile + toString(pid) + ".temp"), size, output); }
428 if (m->control_pressed) { delete distCalculator; return 0; }
430 //tell parent you are done.
431 MPI_Send(&size, 1, MPI_LONG, 0, tag, MPI_COMM_WORLD);
434 MPI_Barrier(MPI_COMM_WORLD); //make everyone wait - just in case
437 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux)
438 //if you don't need to fork anything
440 if (output != "square") { driver(0, numSeqs, outputFile, cutoff); }
441 else { driver(0, numSeqs, outputFile, "square"); }
442 }else{ //you have multiple processors
444 for (int i = 0; i < processors; i++) {
445 distlinePair tempLine;
446 lines.push_back(tempLine);
447 if (output != "square") {
448 lines[i].start = int (sqrt(float(i)/float(processors)) * numSeqs);
449 lines[i].end = int (sqrt(float(i+1)/float(processors)) * numSeqs);
451 lines[i].start = int ((float(i)/float(processors)) * numSeqs);
452 lines[i].end = int ((float(i+1)/float(processors)) * numSeqs);
456 createProcesses(outputFile);
460 if (output != "square") { driver(0, numSeqs, outputFile, cutoff); }
461 else { driver(0, numSeqs, outputFile, "square"); }
465 if (m->control_pressed) { outputTypes.clear(); delete distCalculator; remove(outputFile.c_str()); return 0; }
468 MPI_Comm_rank(MPI_COMM_WORLD, &pid);
470 if (pid == 0) { //only one process should output to screen
474 fileHandle.open(outputFile.c_str());
476 m->gobble(fileHandle);
477 if (fileHandle.eof()) { m->mothurOut(outputFile + " is blank. This can result if there are no distances below your cutoff."); m->mothurOutEndLine(); }
481 m->mothurOut("Compressing..."); m->mothurOutEndLine();
482 m->mothurOut("(Replacing " + outputFile + " with " + outputFile + ".gz)"); m->mothurOutEndLine();
483 system(("gzip -v " + outputFile).c_str());
484 outputNames.push_back(outputFile + ".gz");
485 }else { outputNames.push_back(outputFile); }
491 m->mothurOut("It took " + toString(time(NULL) - startTime) + " to calculate the distances for " + toString(numSeqs) + " sequences."); m->mothurOutEndLine();
493 if (m->control_pressed) { outputTypes.clear(); delete distCalculator; remove(outputFile.c_str()); return 0; }
496 delete distCalculator;
498 m->mothurOutEndLine();
499 m->mothurOut("Output File Name: "); m->mothurOutEndLine();
500 for (int i = 0; i < outputNames.size(); i++) { m->mothurOut(outputNames[i]); m->mothurOutEndLine(); }
501 m->mothurOutEndLine();
506 catch(exception& e) {
507 m->errorOut(e, "PairwiseSeqsCommand", "execute");
512 /**************************************************************************************************/
513 void PairwiseSeqsCommand::createProcesses(string filename) {
515 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux)
519 //loop through and create all the processes you want
520 while (process != processors) {
524 processIDS.push_back(pid);
527 if (output != "square") { driver(lines[process].start, lines[process].end, filename + toString(getpid()) + ".temp", cutoff); }
528 else { driver(lines[process].start, lines[process].end, filename + toString(getpid()) + ".temp", "square"); }
531 m->mothurOut("[ERROR]: unable to spawn the necessary processes."); m->mothurOutEndLine();
532 for (int i=0;i<processIDS.size();i++) { int temp = processIDS[i]; kill (temp, SIGINT); }
538 if (output != "square") { driver(lines[0].start, lines[0].end, filename, cutoff); }
539 else { driver(lines[0].start, lines[0].end, filename, "square"); }
542 //force parent to wait until all the processes are done
543 for (int i=0;i<processIDS.size();i++) {
544 int temp = processIDS[i];
548 //append and remove temp files
549 for (int i=0;i<processIDS.size();i++) {
550 m->appendFiles((filename + toString(processIDS[i]) + ".temp"), filename);
551 remove((filename + toString(processIDS[i]) + ".temp").c_str());
555 catch(exception& e) {
556 m->errorOut(e, "PairwiseSeqsCommand", "createProcesses");
561 /**************************************************************************************************/
562 /////// need to fix to work with calcs and sequencedb
563 int PairwiseSeqsCommand::driver(int startLine, int endLine, string dFileName, float cutoff){
566 int startTime = time(NULL);
569 ofstream outFile(dFileName.c_str(), ios::trunc);
570 outFile.setf(ios::fixed, ios::showpoint);
571 outFile << setprecision(4);
573 if((output == "lt") && startLine == 0){ outFile << alignDB.getNumSeqs() << endl; }
575 for(int i=startLine;i<endLine;i++){
577 string name = alignDB.get(i).getName();
578 if (name.length() < 10) { //pad with spaces to make compatible
579 while (name.length() < 10) { name += " "; }
581 outFile << name << '\t';
584 for(int j=0;j<i;j++){
586 if (m->control_pressed) { outFile.close(); return 0; }
588 if (alignDB.get(i).getUnaligned().length() > alignment->getnRows()) {
589 alignment->resize(alignDB.get(i).getUnaligned().length()+1);
592 if (alignDB.get(j).getUnaligned().length() > alignment->getnRows()) {
593 alignment->resize(alignDB.get(j).getUnaligned().length()+1);
596 Sequence* seqI = new Sequence(alignDB.get(i).getName(), alignDB.get(i).getAligned());
597 Sequence* seqJ = new Sequence(alignDB.get(j).getName(), alignDB.get(j).getAligned());
599 Nast(alignment, seqI, seqJ);
601 distCalculator->calcDist(*seqI, *seqJ);
602 double dist = distCalculator->getDist();
604 delete seqI; delete seqJ;
607 if (output == "column") { outFile << alignDB.get(i).getName() << ' ' << alignDB.get(j).getName() << ' ' << dist << endl; }
609 if (output == "lt") { outFile << dist << '\t'; }
612 if (output == "lt") { outFile << endl; }
615 m->mothurOut(toString(i) + "\t" + toString(time(NULL) - startTime)); m->mothurOutEndLine();
619 m->mothurOut(toString(endLine-1) + "\t" + toString(time(NULL) - startTime)); m->mothurOutEndLine();
625 catch(exception& e) {
626 m->errorOut(e, "PairwiseSeqsCommand", "driver");
630 /**************************************************************************************************/
631 /////// need to fix to work with calcs and sequencedb
632 int PairwiseSeqsCommand::driver(int startLine, int endLine, string dFileName, string square){
635 int startTime = time(NULL);
638 ofstream outFile(dFileName.c_str(), ios::trunc);
639 outFile.setf(ios::fixed, ios::showpoint);
640 outFile << setprecision(4);
642 if(startLine == 0){ outFile << alignDB.getNumSeqs() << endl; }
644 for(int i=startLine;i<endLine;i++){
646 string name = alignDB.get(i).getName();
647 //pad with spaces to make compatible
648 if (name.length() < 10) { while (name.length() < 10) { name += " "; } }
650 outFile << name << '\t';
652 for(int j=0;j<alignDB.getNumSeqs();j++){
654 if (m->control_pressed) { outFile.close(); return 0; }
656 if (alignDB.get(i).getUnaligned().length() > alignment->getnRows()) {
657 alignment->resize(alignDB.get(i).getUnaligned().length()+1);
660 if (alignDB.get(j).getUnaligned().length() > alignment->getnRows()) {
661 alignment->resize(alignDB.get(j).getUnaligned().length()+1);
664 Sequence* seqI = new Sequence(alignDB.get(i).getName(), alignDB.get(i).getAligned());
665 Sequence* seqJ = new Sequence(alignDB.get(j).getName(), alignDB.get(j).getAligned());
667 Nast(alignment, seqI, seqJ);
669 distCalculator->calcDist(*seqI, *seqJ);
670 double dist = distCalculator->getDist();
672 delete seqI; delete seqJ;
674 outFile << dist << '\t';
680 m->mothurOut(toString(i) + "\t" + toString(time(NULL) - startTime)); m->mothurOutEndLine();
684 m->mothurOut(toString(endLine-1) + "\t" + toString(time(NULL) - startTime)); m->mothurOutEndLine();
690 catch(exception& e) {
691 m->errorOut(e, "PairwiseSeqsCommand", "driver");
696 /**************************************************************************************************/
697 /////// need to fix to work with calcs and sequencedb
698 int PairwiseSeqsCommand::driverMPI(int startLine, int endLine, MPI_File& outMPI, float cutoff){
701 int startTime = time(NULL);
703 string outputString = "";
705 for(int i=startLine;i<endLine;i++){
707 for(int j=0;j<i;j++){
709 if (m->control_pressed) { return 0; }
711 if (alignDB.get(i).getUnaligned().length() > alignment->getnRows()) {
712 alignment->resize(alignDB.get(i).getUnaligned().length()+1);
715 if (alignDB.get(j).getUnaligned().length() > alignment->getnRows()) {
716 alignment->resize(alignDB.get(j).getUnaligned().length()+1);
719 Sequence* seqI = new Sequence(alignDB.get(i).getName(), alignDB.get(i).getAligned());
720 Sequence* seqJ = new Sequence(alignDB.get(j).getName(), alignDB.get(j).getAligned());
722 Nast(alignment, seqI, seqJ);
724 distCalculator->calcDist(*seqI, *seqJ);
725 double dist = distCalculator->getDist();
727 delete seqI; delete seqJ;
730 outputString += (alignDB.get(i).getName() + ' ' + alignDB.get(j).getName() + ' ' + toString(dist) + '\n');
735 //m->mothurOut(toString(i) + "\t" + toString(time(NULL) - startTime)); m->mothurOutEndLine();
736 cout << i << '\t' << (time(NULL) - startTime) << endl;
740 //send results to parent
741 int length = outputString.length();
743 char* buf = new char[length];
744 memcpy(buf, outputString.c_str(), length);
746 MPI_File_write_shared(outMPI, buf, length, MPI_CHAR, &status);
754 catch(exception& e) {
755 m->errorOut(e, "PairwiseSeqsCommand", "driverMPI");
759 /**************************************************************************************************/
760 /////// need to fix to work with calcs and sequencedb
761 int PairwiseSeqsCommand::driverMPI(int startLine, int endLine, string file, unsigned long int& size){
766 int amode=MPI_MODE_CREATE|MPI_MODE_WRONLY;
769 strcpy(filename, file.c_str());
771 MPI_File_open(MPI_COMM_SELF, filename, amode, MPI_INFO_NULL, &outMPI);
775 string outputString = "";
778 if(startLine == 0){ outputString += toString(alignDB.getNumSeqs()) + "\n"; }
780 for(int i=startLine;i<endLine;i++){
782 string name = alignDB.get(i).getName();
783 if (name.length() < 10) { //pad with spaces to make compatible
784 while (name.length() < 10) { name += " "; }
786 outputString += name + "\t";
788 for(int j=0;j<i;j++){
790 if (m->control_pressed) { return 0; }
792 if (alignDB.get(i).getUnaligned().length() > alignment->getnRows()) {
793 alignment->resize(alignDB.get(i).getUnaligned().length()+1);
796 if (alignDB.get(j).getUnaligned().length() > alignment->getnRows()) {
797 alignment->resize(alignDB.get(j).getUnaligned().length()+1);
800 Sequence* seqI = new Sequence(alignDB.get(i).getName(), alignDB.get(i).getAligned());
801 Sequence* seqJ = new Sequence(alignDB.get(j).getName(), alignDB.get(j).getAligned());
803 Nast(alignment, seqI, seqJ);
805 distCalculator->calcDist(*seqI, *seqJ);
806 double dist = distCalculator->getDist();
808 delete seqI; delete seqJ;
810 outputString += toString(dist) + "\t";
813 outputString += "\n";
815 //send results to parent
816 int length = outputString.length();
817 char* buf = new char[length];
818 memcpy(buf, outputString.c_str(), length);
820 MPI_File_write(outMPI, buf, length, MPI_CHAR, &status);
821 size += outputString.length();
826 MPI_File_close(&outMPI);
830 catch(exception& e) {
831 m->errorOut(e, "PairwiseSeqsCommand", "driverMPI");
835 /**************************************************************************************************/
836 /////// need to fix to work with calcs and sequencedb
837 int PairwiseSeqsCommand::driverMPI(int startLine, int endLine, string file, unsigned long int& size, string square){
842 int amode=MPI_MODE_CREATE|MPI_MODE_WRONLY;
845 strcpy(filename, file.c_str());
847 MPI_File_open(MPI_COMM_SELF, filename, amode, MPI_INFO_NULL, &outMPI);
851 string outputString = "";
854 if(startLine == 0){ outputString += toString(alignDB.getNumSeqs()) + "\n"; }
856 for(int i=startLine;i<endLine;i++){
858 string name = alignDB.get(i).getName();
859 if (name.length() < 10) { //pad with spaces to make compatible
860 while (name.length() < 10) { name += " "; }
862 outputString += name + "\t";
864 for(int j=0;j<alignDB.getNumSeqs();j++){
866 if (m->control_pressed) { return 0; }
868 if (alignDB.get(i).getUnaligned().length() > alignment->getnRows()) {
869 alignment->resize(alignDB.get(i).getUnaligned().length()+1);
872 if (alignDB.get(j).getUnaligned().length() > alignment->getnRows()) {
873 alignment->resize(alignDB.get(j).getUnaligned().length()+1);
876 Sequence* seqI = new Sequence(alignDB.get(i).getName(), alignDB.get(i).getAligned());
877 Sequence* seqJ = new Sequence(alignDB.get(j).getName(), alignDB.get(j).getAligned());
879 Nast(alignment, seqI, seqJ);
881 distCalculator->calcDist(*seqI, *seqJ);
882 double dist = distCalculator->getDist();
884 delete seqI; delete seqJ;
886 outputString += toString(dist) + "\t";
889 outputString += "\n";
891 //send results to parent
892 int length = outputString.length();
893 char* buf = new char[length];
894 memcpy(buf, outputString.c_str(), length);
896 MPI_File_write(outMPI, buf, length, MPI_CHAR, &status);
897 size += outputString.length();
902 MPI_File_close(&outMPI);
906 catch(exception& e) {
907 m->errorOut(e, "PairwiseSeqsCommand", "driverMPI");
912 /**************************************************************************************************/