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 lines.push_back(new linePair());
446 if (output != "square") {
447 lines[i]->start = int (sqrt(float(i)/float(processors)) * numSeqs);
448 lines[i]->end = int (sqrt(float(i+1)/float(processors)) * numSeqs);
450 lines[i]->start = int ((float(i)/float(processors)) * numSeqs);
451 lines[i]->end = int ((float(i+1)/float(processors)) * numSeqs);
455 createProcesses(outputFile);
457 map<int, int>::iterator it = processIDS.begin();
458 rename((outputFile + toString(it->second) + ".temp").c_str(), outputFile.c_str());
461 //append and remove temp files
462 for (; it != processIDS.end(); it++) {
463 m->appendFiles((outputFile + toString(it->second) + ".temp"), outputFile);
464 remove((outputFile + toString(it->second) + ".temp").c_str());
469 if (output != "square") { driver(0, numSeqs, outputFile, cutoff); }
470 else { driver(0, numSeqs, outputFile, "square"); }
474 if (m->control_pressed) { outputTypes.clear(); delete distCalculator; remove(outputFile.c_str()); return 0; }
477 MPI_Comm_rank(MPI_COMM_WORLD, &pid);
479 if (pid == 0) { //only one process should output to screen
483 fileHandle.open(outputFile.c_str());
485 m->gobble(fileHandle);
486 if (fileHandle.eof()) { m->mothurOut(outputFile + " is blank. This can result if there are no distances below your cutoff."); m->mothurOutEndLine(); }
490 m->mothurOut("Compressing..."); m->mothurOutEndLine();
491 m->mothurOut("(Replacing " + outputFile + " with " + outputFile + ".gz)"); m->mothurOutEndLine();
492 system(("gzip -v " + outputFile).c_str());
493 outputNames.push_back(outputFile + ".gz");
494 }else { outputNames.push_back(outputFile); }
500 m->mothurOut("It took " + toString(time(NULL) - startTime) + " to calculate the distances for " + toString(numSeqs) + " sequences."); m->mothurOutEndLine();
502 if (m->control_pressed) { outputTypes.clear(); delete distCalculator; remove(outputFile.c_str()); return 0; }
505 delete distCalculator;
507 m->mothurOutEndLine();
508 m->mothurOut("Output File Name: "); m->mothurOutEndLine();
509 for (int i = 0; i < outputNames.size(); i++) { m->mothurOut(outputNames[i]); m->mothurOutEndLine(); }
510 m->mothurOutEndLine();
515 catch(exception& e) {
516 m->errorOut(e, "PairwiseSeqsCommand", "execute");
521 /**************************************************************************************************/
522 void PairwiseSeqsCommand::createProcesses(string filename) {
524 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux)
528 //loop through and create all the processes you want
529 while (process != processors) {
533 processIDS[lines[process]->end] = pid; //create map from line number to pid so you can append files in correct order later
536 if (output != "square") { driver(lines[process]->start, lines[process]->end, filename + toString(getpid()) + ".temp", cutoff); }
537 else { driver(lines[process]->start, lines[process]->end, filename + toString(getpid()) + ".temp", "square"); }
539 }else { m->mothurOut("unable to spawn the necessary processes."); m->mothurOutEndLine(); exit(0); }
542 //force parent to wait until all the processes are done
543 for (map<int, int>::iterator it = processIDS.begin(); it != processIDS.end(); it++) {
544 int temp = it->second;
549 catch(exception& e) {
550 m->errorOut(e, "PairwiseSeqsCommand", "createProcesses");
555 /**************************************************************************************************/
556 /////// need to fix to work with calcs and sequencedb
557 int PairwiseSeqsCommand::driver(int startLine, int endLine, string dFileName, float cutoff){
560 int startTime = time(NULL);
563 ofstream outFile(dFileName.c_str(), ios::trunc);
564 outFile.setf(ios::fixed, ios::showpoint);
565 outFile << setprecision(4);
567 if((output == "lt") && startLine == 0){ outFile << alignDB.getNumSeqs() << endl; }
569 for(int i=startLine;i<endLine;i++){
571 string name = alignDB.get(i).getName();
572 if (name.length() < 10) { //pad with spaces to make compatible
573 while (name.length() < 10) { name += " "; }
575 outFile << name << '\t';
578 for(int j=0;j<i;j++){
580 if (m->control_pressed) { outFile.close(); return 0; }
582 if (alignDB.get(i).getUnaligned().length() > alignment->getnRows()) {
583 alignment->resize(alignDB.get(i).getUnaligned().length()+1);
586 if (alignDB.get(j).getUnaligned().length() > alignment->getnRows()) {
587 alignment->resize(alignDB.get(j).getUnaligned().length()+1);
590 Sequence* seqI = new Sequence(alignDB.get(i).getName(), alignDB.get(i).getAligned());
591 Sequence* seqJ = new Sequence(alignDB.get(j).getName(), alignDB.get(j).getAligned());
593 Nast(alignment, seqI, seqJ);
595 distCalculator->calcDist(*seqI, *seqJ);
596 double dist = distCalculator->getDist();
598 delete seqI; delete seqJ;
601 if (output == "column") { outFile << alignDB.get(i).getName() << ' ' << alignDB.get(j).getName() << ' ' << dist << endl; }
603 if (output == "lt") { outFile << dist << '\t'; }
606 if (output == "lt") { outFile << endl; }
609 m->mothurOut(toString(i) + "\t" + toString(time(NULL) - startTime)); m->mothurOutEndLine();
613 m->mothurOut(toString(endLine-1) + "\t" + toString(time(NULL) - startTime)); m->mothurOutEndLine();
619 catch(exception& e) {
620 m->errorOut(e, "PairwiseSeqsCommand", "driver");
624 /**************************************************************************************************/
625 /////// need to fix to work with calcs and sequencedb
626 int PairwiseSeqsCommand::driver(int startLine, int endLine, string dFileName, string square){
629 int startTime = time(NULL);
632 ofstream outFile(dFileName.c_str(), ios::trunc);
633 outFile.setf(ios::fixed, ios::showpoint);
634 outFile << setprecision(4);
636 if(startLine == 0){ outFile << alignDB.getNumSeqs() << endl; }
638 for(int i=startLine;i<endLine;i++){
640 string name = alignDB.get(i).getName();
641 //pad with spaces to make compatible
642 if (name.length() < 10) { while (name.length() < 10) { name += " "; } }
644 outFile << name << '\t';
646 for(int j=0;j<alignDB.getNumSeqs();j++){
648 if (m->control_pressed) { outFile.close(); return 0; }
650 if (alignDB.get(i).getUnaligned().length() > alignment->getnRows()) {
651 alignment->resize(alignDB.get(i).getUnaligned().length()+1);
654 if (alignDB.get(j).getUnaligned().length() > alignment->getnRows()) {
655 alignment->resize(alignDB.get(j).getUnaligned().length()+1);
658 Sequence* seqI = new Sequence(alignDB.get(i).getName(), alignDB.get(i).getAligned());
659 Sequence* seqJ = new Sequence(alignDB.get(j).getName(), alignDB.get(j).getAligned());
661 Nast(alignment, seqI, seqJ);
663 distCalculator->calcDist(*seqI, *seqJ);
664 double dist = distCalculator->getDist();
666 delete seqI; delete seqJ;
668 outFile << dist << '\t';
674 m->mothurOut(toString(i) + "\t" + toString(time(NULL) - startTime)); m->mothurOutEndLine();
678 m->mothurOut(toString(endLine-1) + "\t" + toString(time(NULL) - startTime)); m->mothurOutEndLine();
684 catch(exception& e) {
685 m->errorOut(e, "PairwiseSeqsCommand", "driver");
690 /**************************************************************************************************/
691 /////// need to fix to work with calcs and sequencedb
692 int PairwiseSeqsCommand::driverMPI(int startLine, int endLine, MPI_File& outMPI, float cutoff){
695 int startTime = time(NULL);
697 string outputString = "";
699 for(int i=startLine;i<endLine;i++){
701 for(int j=0;j<i;j++){
703 if (m->control_pressed) { return 0; }
705 if (alignDB.get(i).getUnaligned().length() > alignment->getnRows()) {
706 alignment->resize(alignDB.get(i).getUnaligned().length()+1);
709 if (alignDB.get(j).getUnaligned().length() > alignment->getnRows()) {
710 alignment->resize(alignDB.get(j).getUnaligned().length()+1);
713 Sequence* seqI = new Sequence(alignDB.get(i).getName(), alignDB.get(i).getAligned());
714 Sequence* seqJ = new Sequence(alignDB.get(j).getName(), alignDB.get(j).getAligned());
716 Nast(alignment, seqI, seqJ);
718 distCalculator->calcDist(*seqI, *seqJ);
719 double dist = distCalculator->getDist();
721 delete seqI; delete seqJ;
724 outputString += (alignDB.get(i).getName() + ' ' + alignDB.get(j).getName() + ' ' + toString(dist) + '\n');
729 //m->mothurOut(toString(i) + "\t" + toString(time(NULL) - startTime)); m->mothurOutEndLine();
730 cout << i << '\t' << (time(NULL) - startTime) << endl;
734 //send results to parent
735 int length = outputString.length();
737 char* buf = new char[length];
738 memcpy(buf, outputString.c_str(), length);
740 MPI_File_write_shared(outMPI, buf, length, MPI_CHAR, &status);
748 catch(exception& e) {
749 m->errorOut(e, "PairwiseSeqsCommand", "driverMPI");
753 /**************************************************************************************************/
754 /////// need to fix to work with calcs and sequencedb
755 int PairwiseSeqsCommand::driverMPI(int startLine, int endLine, string file, unsigned long int& size){
760 int amode=MPI_MODE_CREATE|MPI_MODE_WRONLY;
763 strcpy(filename, file.c_str());
765 MPI_File_open(MPI_COMM_SELF, filename, amode, MPI_INFO_NULL, &outMPI);
767 int startTime = time(NULL);
769 string outputString = "";
772 if(startLine == 0){ outputString += toString(alignDB.getNumSeqs()) + "\n"; }
774 for(int i=startLine;i<endLine;i++){
776 string name = alignDB.get(i).getName();
777 if (name.length() < 10) { //pad with spaces to make compatible
778 while (name.length() < 10) { name += " "; }
780 outputString += name + "\t";
782 for(int j=0;j<i;j++){
784 if (m->control_pressed) { return 0; }
786 if (alignDB.get(i).getUnaligned().length() > alignment->getnRows()) {
787 alignment->resize(alignDB.get(i).getUnaligned().length()+1);
790 if (alignDB.get(j).getUnaligned().length() > alignment->getnRows()) {
791 alignment->resize(alignDB.get(j).getUnaligned().length()+1);
794 Sequence* seqI = new Sequence(alignDB.get(i).getName(), alignDB.get(i).getAligned());
795 Sequence* seqJ = new Sequence(alignDB.get(j).getName(), alignDB.get(j).getAligned());
797 Nast(alignment, seqI, seqJ);
799 distCalculator->calcDist(*seqI, *seqJ);
800 double dist = distCalculator->getDist();
802 delete seqI; delete seqJ;
804 outputString += toString(dist) + "\t";
807 outputString += "\n";
809 //send results to parent
810 int length = outputString.length();
811 char* buf = new char[length];
812 memcpy(buf, outputString.c_str(), length);
814 MPI_File_write(outMPI, buf, length, MPI_CHAR, &status);
815 size += outputString.length();
820 MPI_File_close(&outMPI);
824 catch(exception& e) {
825 m->errorOut(e, "PairwiseSeqsCommand", "driverMPI");
829 /**************************************************************************************************/
830 /////// need to fix to work with calcs and sequencedb
831 int PairwiseSeqsCommand::driverMPI(int startLine, int endLine, string file, unsigned long int& size, string square){
836 int amode=MPI_MODE_CREATE|MPI_MODE_WRONLY;
839 strcpy(filename, file.c_str());
841 MPI_File_open(MPI_COMM_SELF, filename, amode, MPI_INFO_NULL, &outMPI);
843 int startTime = time(NULL);
845 string outputString = "";
848 if(startLine == 0){ outputString += toString(alignDB.getNumSeqs()) + "\n"; }
850 for(int i=startLine;i<endLine;i++){
852 string name = alignDB.get(i).getName();
853 if (name.length() < 10) { //pad with spaces to make compatible
854 while (name.length() < 10) { name += " "; }
856 outputString += name + "\t";
858 for(int j=0;j<alignDB.getNumSeqs();j++){
860 if (m->control_pressed) { return 0; }
862 if (alignDB.get(i).getUnaligned().length() > alignment->getnRows()) {
863 alignment->resize(alignDB.get(i).getUnaligned().length()+1);
866 if (alignDB.get(j).getUnaligned().length() > alignment->getnRows()) {
867 alignment->resize(alignDB.get(j).getUnaligned().length()+1);
870 Sequence* seqI = new Sequence(alignDB.get(i).getName(), alignDB.get(i).getAligned());
871 Sequence* seqJ = new Sequence(alignDB.get(j).getName(), alignDB.get(j).getAligned());
873 Nast(alignment, seqI, seqJ);
875 distCalculator->calcDist(*seqI, *seqJ);
876 double dist = distCalculator->getDist();
878 delete seqI; delete seqJ;
880 outputString += toString(dist) + "\t";
883 outputString += "\n";
885 //send results to parent
886 int length = outputString.length();
887 char* buf = new char[length];
888 memcpy(buf, outputString.c_str(), length);
890 MPI_File_write(outMPI, buf, length, MPI_CHAR, &status);
891 size += outputString.length();
896 MPI_File_close(&outMPI);
900 catch(exception& e) {
901 m->errorOut(e, "PairwiseSeqsCommand", "driverMPI");
906 /**************************************************************************************************/