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(){
64 abort = true; calledHelp = true;
65 vector<string> tempOutNames;
66 outputTypes["phylip"] = tempOutNames;
67 outputTypes["column"] = tempOutNames;
70 m->errorOut(e, "PairwiseSeqsCommand", "PairwiseSeqsCommand");
74 //**********************************************************************************************************************
75 PairwiseSeqsCommand::PairwiseSeqsCommand(string option) {
77 abort = false; calledHelp = false;
79 //allow user to run help
80 if(option == "help") { help(); abort = true; calledHelp = true; }
84 //valid paramters for this command
85 string AlignArray[] = {"fasta","align","match","mismatch","gapopen","gapextend", "processors","cutoff","compress","calc","countends","output","outputdir","inputdir"};
86 vector<string> myArray (AlignArray, AlignArray+(sizeof(AlignArray)/sizeof(string)));
88 OptionParser parser(option);
89 map<string, string> parameters = parser.getParameters();
91 ValidParameters validParameter("pairwise.seqs");
92 map<string, string>::iterator it;
94 //check to make sure all parameters are valid for command
95 for (it = parameters.begin(); it != parameters.end(); it++) {
96 if (validParameter.isValidParameter(it->first, myArray, it->second) != true) { abort = true; }
99 //initialize outputTypes
100 vector<string> tempOutNames;
101 outputTypes["phylip"] = tempOutNames;
102 outputTypes["column"] = tempOutNames;
104 //if the user changes the output directory command factory will send this info to us in the output parameter
105 outputDir = validParameter.validFile(parameters, "outputdir", false); if (outputDir == "not found"){ outputDir = ""; }
108 //if the user changes the input directory command factory will send this info to us in the output parameter
109 string inputDir = validParameter.validFile(parameters, "inputdir", false);
111 if (inputDir == "not found"){ inputDir = ""; }
113 fastaFileName = validParameter.validFile(parameters, "fasta", false);
114 if (fastaFileName == "not found") { m->mothurOut("fasta is a required parameter for the pairwise.seqs command."); m->mothurOutEndLine(); abort = true; }
116 m->splitAtDash(fastaFileName, fastaFileNames);
118 //go through files and make sure they are good, if not, then disregard them
119 for (int i = 0; i < fastaFileNames.size(); i++) {
121 if (inputDir != "") {
122 string path = m->hasPath(fastaFileNames[i]);
123 //if the user has not given a path then, add inputdir. else leave path alone.
124 if (path == "") { fastaFileNames[i] = inputDir + fastaFileNames[i]; }
130 ableToOpen = m->openInputFile(fastaFileNames[i], in, "noerror");
132 //if you can't open it, try default location
133 if (ableToOpen == 1) {
134 if (m->getDefaultPath() != "") { //default path is set
135 string tryPath = m->getDefaultPath() + m->getSimpleName(fastaFileNames[i]);
136 m->mothurOut("Unable to open " + fastaFileNames[i] + ". Trying default " + tryPath); m->mothurOutEndLine();
138 ableToOpen = m->openInputFile(tryPath, in2, "noerror");
140 fastaFileNames[i] = tryPath;
144 //if you can't open it, try output location
145 if (ableToOpen == 1) {
146 if (m->getOutputDir() != "") { //default path is set
147 string tryPath = m->getOutputDir() + m->getSimpleName(fastaFileNames[i]);
148 m->mothurOut("Unable to open " + fastaFileNames[i] + ". Trying output directory " + tryPath); m->mothurOutEndLine();
150 ableToOpen = m->openInputFile(tryPath, in2, "noerror");
152 fastaFileNames[i] = tryPath;
158 if (ableToOpen == 1) {
159 m->mothurOut("Unable to open " + fastaFileNames[i] + ". It will be disregarded."); m->mothurOutEndLine();
160 //erase from file list
161 fastaFileNames.erase(fastaFileNames.begin()+i);
166 //make sure there is at least one valid file left
167 if (fastaFileNames.size() == 0) { m->mothurOut("no valid files."); m->mothurOutEndLine(); abort = true; }
170 //check for optional parameter and set defaults
171 // ...at some point should added some additional type checking...
173 temp = validParameter.validFile(parameters, "match", false); if (temp == "not found"){ temp = "1.0"; }
174 convert(temp, match);
176 temp = validParameter.validFile(parameters, "mismatch", false); if (temp == "not found"){ temp = "-1.0"; }
177 convert(temp, misMatch);
179 temp = validParameter.validFile(parameters, "gapopen", false); if (temp == "not found"){ temp = "-2.0"; }
180 convert(temp, gapOpen);
182 temp = validParameter.validFile(parameters, "gapextend", false); if (temp == "not found"){ temp = "-1.0"; }
183 convert(temp, gapExtend);
185 temp = validParameter.validFile(parameters, "processors", false); if (temp == "not found"){ temp = "1"; }
186 convert(temp, processors);
188 temp = validParameter.validFile(parameters, "cutoff", false); if(temp == "not found"){ temp = "1.0"; }
189 convert(temp, cutoff);
191 temp = validParameter.validFile(parameters, "countends", false); if(temp == "not found"){ temp = "T"; }
192 countends = m->isTrue(temp);
194 temp = validParameter.validFile(parameters, "compress", false); if(temp == "not found"){ temp = "F"; }
195 compress = m->isTrue(temp);
197 align = validParameter.validFile(parameters, "align", false); if (align == "not found"){ align = "needleman"; }
199 output = validParameter.validFile(parameters, "output", false); if(output == "not found"){ output = "column"; }
200 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"; }
202 calc = validParameter.validFile(parameters, "calc", false);
203 if (calc == "not found") { calc = "onegap"; }
205 if (calc == "default") { calc = "onegap"; }
207 m->splitAtDash(calc, Estimators);
209 ValidCalculators validCalculator;
211 for (int i=0; i<Estimators.size(); i++) {
212 if (validCalculator.isValidCalculator("distance", Estimators[i]) == true) {
213 if (Estimators[i] == "nogaps") { distCalculator = new ignoreGaps(); }
214 else if (Estimators[i] == "eachgap") { distCalculator = new eachGapDist(); }
215 else if (Estimators[i] == "onegap") { distCalculator = new oneGapDist(); }
219 for (int i=0; i<Estimators.size(); i++) {
220 if (validCalculator.isValidCalculator("distance", Estimators[i]) == true) {
221 if (Estimators[i] == "nogaps") { distCalculator = new ignoreGaps(); }
222 else if (Estimators[i] == "eachgap"){ distCalculator = new eachGapIgnoreTermGapDist(); }
223 else if (Estimators[i] == "onegap") { distCalculator = new oneGapIgnoreTermGapDist(); }
230 catch(exception& e) {
231 m->errorOut(e, "PairwiseSeqsCommand", "PairwiseSeqsCommand");
235 //**********************************************************************************************************************
236 PairwiseSeqsCommand::~PairwiseSeqsCommand(){}
237 //**********************************************************************************************************************
239 void PairwiseSeqsCommand::help(){
241 m->mothurOut("The pairwise.seqs command reads a fasta file and creates distance matrix.\n");
242 m->mothurOut("The pairwise.seqs command parameters are fasta, align, match, mismatch, gapopen, gapextend, calc, output, cutoff and processors.\n");
243 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");
244 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");
245 m->mothurOut("The match parameter allows you to specify the bonus for having the same base. The default is 1.0.\n");
246 m->mothurOut("The mistmatch parameter allows you to specify the penalty for having different bases. The default is -1.0.\n");
247 m->mothurOut("The gapopen parameter allows you to specify the penalty for opening a gap in an alignment. The default is -2.0.\n");
248 m->mothurOut("The gapextend parameter allows you to specify the penalty for extending a gap in an alignment. The default is -1.0.\n");
249 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");
250 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");
251 m->mothurOut("The cutoff parameter allows you to specify maximum distance to keep. The default is 1.0.\n");
252 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");
253 m->mothurOut("The compress parameter allows you to indicate that you want the resulting distance file compressed. The default is false.\n");
254 m->mothurOut("The pairwise.seqs command should be in the following format: \n");
255 m->mothurOut("pairwise.seqs(fasta=yourfastaFile, align=yourAlignmentMethod) \n");
256 m->mothurOut("Example pairwise.seqs(fasta=candidate.fasta, align=blast)\n");
257 m->mothurOut("Note: No spaces between parameter labels (i.e. fasta), '=' and parameters (i.e.yourFastaFile).\n\n");
259 catch(exception& e) {
260 m->errorOut(e, "PairwiseSeqsCommand", "help");
266 //**********************************************************************************************************************
268 int PairwiseSeqsCommand::execute(){
270 if (abort == true) { if (calledHelp) { return 0; } return 2; }
272 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.
274 if(align == "gotoh") { alignment = new GotohOverlap(gapOpen, gapExtend, match, misMatch, longestBase); }
275 else if(align == "needleman") { alignment = new NeedlemanOverlap(gapOpen, match, misMatch, longestBase); }
276 else if(align == "blast") { alignment = new BlastAlignment(gapOpen, gapExtend, match, misMatch); }
277 else if(align == "noalign") { alignment = new NoAlign(); }
279 m->mothurOut(align + " is not a valid alignment option. I will run the command using needleman.");
280 m->mothurOutEndLine();
281 alignment = new NeedlemanOverlap(gapOpen, match, misMatch, longestBase);
286 for (int s = 0; s < fastaFileNames.size(); s++) {
287 if (m->control_pressed) { outputTypes.clear(); return 0; }
289 m->mothurOut("Processing sequences from " + fastaFileNames[s] + " ..." ); m->mothurOutEndLine();
291 if (outputDir == "") { outputDir += m->hasPath(fastaFileNames[s]); }
294 m->openInputFile(fastaFileNames[s], inFASTA);
295 alignDB = SequenceDB(inFASTA);
298 int numSeqs = alignDB.getNumSeqs();
299 int startTime = time(NULL);
300 string outputFile = "";
302 if (output == "lt") { //does the user want lower triangle phylip formatted file
303 outputFile = outputDir + m->getRootName(m->getSimpleName(fastaFileNames[s])) + "phylip.dist";
304 remove(outputFile.c_str()); outputTypes["phylip"].push_back(outputFile);
305 }else if (output == "column") { //user wants column format
306 outputFile = outputDir + m->getRootName(m->getSimpleName(fastaFileNames[s])) + "dist";
307 outputTypes["column"].push_back(outputFile);
308 remove(outputFile.c_str());
309 }else { //assume square
310 outputFile = outputDir + m->getRootName(m->getSimpleName(fastaFileNames[s])) + "square.dist";
311 remove(outputFile.c_str());
312 outputTypes["phylip"].push_back(outputFile);
321 MPI_Comm_size(MPI_COMM_WORLD, &processors); //set processors to the number of mpi processes running
322 MPI_Comm_rank(MPI_COMM_WORLD, &pid); //find out who we are
324 //each process gets where it should start and stop in the file
325 if (output != "square") {
326 start = int (sqrt(float(pid)/float(processors)) * numSeqs);
327 end = int (sqrt(float(pid+1)/float(processors)) * numSeqs);
329 start = int ((float(pid)/float(processors)) * numSeqs);
330 end = int ((float(pid+1)/float(processors)) * numSeqs);
333 if (output == "column") {
335 int amode=MPI_MODE_CREATE|MPI_MODE_WRONLY;
338 strcpy(filename, outputFile.c_str());
340 MPI_File_open(MPI_COMM_WORLD, filename, amode, MPI_INFO_NULL, &outMPI);
342 if (pid == 0) { //you are the root process
347 driverMPI(start, end, outMPI, cutoff);
349 if (m->control_pressed) { outputTypes.clear(); MPI_File_close(&outMPI); remove(outputFile.c_str()); delete distCalculator; return 0; }
352 for(int i = 1; i < processors; i++) {
353 if (m->control_pressed) { outputTypes.clear(); MPI_File_close(&outMPI); remove(outputFile.c_str()); delete distCalculator; return 0; }
356 MPI_Recv(buf, 5, MPI_CHAR, i, tag, MPI_COMM_WORLD, &status);
358 }else { //you are a child process
360 driverMPI(start, end, outMPI, cutoff);
362 if (m->control_pressed) { outputTypes.clear(); MPI_File_close(&outMPI); remove(outputFile.c_str()); delete distCalculator; return 0; }
366 //tell parent you are done.
367 MPI_Send(buf, 5, MPI_CHAR, 0, tag, MPI_COMM_WORLD);
370 MPI_File_close(&outMPI);
372 }else { //lower triangle format
373 if (pid == 0) { //you are the root process
377 unsigned long int mySize;
379 if (output != "square"){ driverMPI(start, end, outputFile, mySize); }
380 else { driverMPI(start, end, outputFile, mySize, output); }
382 if (m->control_pressed) { outputTypes.clear(); remove(outputFile.c_str()); delete distCalculator; return 0; }
384 int amode=MPI_MODE_APPEND|MPI_MODE_WRONLY|MPI_MODE_CREATE; //
389 strcpy(filename, outputFile.c_str());
391 MPI_File_open(MPI_COMM_SELF, filename, amode, MPI_INFO_NULL, &outMPI);
394 for(int b = 1; b < processors; b++) {
395 unsigned long int fileSize;
397 if (m->control_pressed) { outputTypes.clear(); MPI_File_close(&outMPI); remove(outputFile.c_str()); delete distCalculator; return 0; }
399 MPI_Recv(&fileSize, 1, MPI_LONG, b, tag, MPI_COMM_WORLD, &status);
401 string outTemp = outputFile + toString(b) + ".temp";
403 char* buf = new char[outTemp.length()];
404 memcpy(buf, outTemp.c_str(), outTemp.length());
406 MPI_File_open(MPI_COMM_SELF, buf, MPI_MODE_DELETE_ON_CLOSE|MPI_MODE_RDONLY, MPI_INFO_NULL, &inMPI);
410 while (count < fileSize) {
412 MPI_File_read(inMPI, buf2, 1, MPI_CHAR, &status);
413 MPI_File_write(outMPI, buf2, 1, MPI_CHAR, &status);
417 MPI_File_close(&inMPI); //deleted on close
420 MPI_File_close(&outMPI);
421 }else { //you are a child process
423 unsigned long int size;
424 if (output != "square"){ driverMPI(start, end, (outputFile + toString(pid) + ".temp"), size); }
425 else { driverMPI(start, end, (outputFile + toString(pid) + ".temp"), size, output); }
427 if (m->control_pressed) { delete distCalculator; return 0; }
429 //tell parent you are done.
430 MPI_Send(&size, 1, MPI_LONG, 0, tag, MPI_COMM_WORLD);
433 MPI_Barrier(MPI_COMM_WORLD); //make everyone wait - just in case
436 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux)
437 //if you don't need to fork anything
439 if (output != "square") { driver(0, numSeqs, outputFile, cutoff); }
440 else { driver(0, numSeqs, outputFile, "square"); }
441 }else{ //you have multiple processors
443 for (int i = 0; i < processors; i++) {
444 distlinePair tempLine;
445 lines.push_back(tempLine);
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);
459 if (output != "square") { driver(0, numSeqs, outputFile, cutoff); }
460 else { driver(0, numSeqs, outputFile, "square"); }
464 if (m->control_pressed) { outputTypes.clear(); delete distCalculator; remove(outputFile.c_str()); return 0; }
467 MPI_Comm_rank(MPI_COMM_WORLD, &pid);
469 if (pid == 0) { //only one process should output to screen
473 fileHandle.open(outputFile.c_str());
475 m->gobble(fileHandle);
476 if (fileHandle.eof()) { m->mothurOut(outputFile + " is blank. This can result if there are no distances below your cutoff."); m->mothurOutEndLine(); }
480 m->mothurOut("Compressing..."); m->mothurOutEndLine();
481 m->mothurOut("(Replacing " + outputFile + " with " + outputFile + ".gz)"); m->mothurOutEndLine();
482 system(("gzip -v " + outputFile).c_str());
483 outputNames.push_back(outputFile + ".gz");
484 }else { outputNames.push_back(outputFile); }
490 m->mothurOut("It took " + toString(time(NULL) - startTime) + " to calculate the distances for " + toString(numSeqs) + " sequences."); m->mothurOutEndLine();
492 if (m->control_pressed) { outputTypes.clear(); delete distCalculator; remove(outputFile.c_str()); return 0; }
495 delete distCalculator;
497 //set phylip file as new current phylipfile
499 itTypes = outputTypes.find("phylip");
500 if (itTypes != outputTypes.end()) {
501 if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setPhylipFile(current); }
504 //set column file as new current columnfile
505 itTypes = outputTypes.find("column");
506 if (itTypes != outputTypes.end()) {
507 if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setColumnFile(current); }
510 m->mothurOutEndLine();
511 m->mothurOut("Output File Name: "); m->mothurOutEndLine();
512 for (int i = 0; i < outputNames.size(); i++) { m->mothurOut(outputNames[i]); m->mothurOutEndLine(); }
513 m->mothurOutEndLine();
518 catch(exception& e) {
519 m->errorOut(e, "PairwiseSeqsCommand", "execute");
524 /**************************************************************************************************/
525 void PairwiseSeqsCommand::createProcesses(string filename) {
527 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux)
531 //loop through and create all the processes you want
532 while (process != processors) {
536 processIDS.push_back(pid);
539 if (output != "square") { driver(lines[process].start, lines[process].end, filename + toString(getpid()) + ".temp", cutoff); }
540 else { driver(lines[process].start, lines[process].end, filename + toString(getpid()) + ".temp", "square"); }
543 m->mothurOut("[ERROR]: unable to spawn the necessary processes."); m->mothurOutEndLine();
544 for (int i=0;i<processIDS.size();i++) { int temp = processIDS[i]; kill (temp, SIGINT); }
550 if (output != "square") { driver(lines[0].start, lines[0].end, filename, cutoff); }
551 else { driver(lines[0].start, lines[0].end, filename, "square"); }
554 //force parent to wait until all the processes are done
555 for (int i=0;i<processIDS.size();i++) {
556 int temp = processIDS[i];
560 //append and remove temp files
561 for (int i=0;i<processIDS.size();i++) {
562 m->appendFiles((filename + toString(processIDS[i]) + ".temp"), filename);
563 remove((filename + toString(processIDS[i]) + ".temp").c_str());
567 catch(exception& e) {
568 m->errorOut(e, "PairwiseSeqsCommand", "createProcesses");
573 /**************************************************************************************************/
574 /////// need to fix to work with calcs and sequencedb
575 int PairwiseSeqsCommand::driver(int startLine, int endLine, string dFileName, float cutoff){
578 int startTime = time(NULL);
581 ofstream outFile(dFileName.c_str(), ios::trunc);
582 outFile.setf(ios::fixed, ios::showpoint);
583 outFile << setprecision(4);
585 if((output == "lt") && startLine == 0){ outFile << alignDB.getNumSeqs() << endl; }
587 for(int i=startLine;i<endLine;i++){
589 string name = alignDB.get(i).getName();
590 if (name.length() < 10) { //pad with spaces to make compatible
591 while (name.length() < 10) { name += " "; }
593 outFile << name << '\t';
596 for(int j=0;j<i;j++){
598 if (m->control_pressed) { outFile.close(); return 0; }
600 if (alignDB.get(i).getUnaligned().length() > alignment->getnRows()) {
601 alignment->resize(alignDB.get(i).getUnaligned().length()+1);
604 if (alignDB.get(j).getUnaligned().length() > alignment->getnRows()) {
605 alignment->resize(alignDB.get(j).getUnaligned().length()+1);
608 Sequence* seqI = new Sequence(alignDB.get(i).getName(), alignDB.get(i).getAligned());
609 Sequence* seqJ = new Sequence(alignDB.get(j).getName(), alignDB.get(j).getAligned());
611 Nast(alignment, seqI, seqJ);
613 distCalculator->calcDist(*seqI, *seqJ);
614 double dist = distCalculator->getDist();
616 delete seqI; delete seqJ;
619 if (output == "column") { outFile << alignDB.get(i).getName() << ' ' << alignDB.get(j).getName() << ' ' << dist << endl; }
621 if (output == "lt") { outFile << dist << '\t'; }
624 if (output == "lt") { outFile << endl; }
627 m->mothurOut(toString(i) + "\t" + toString(time(NULL) - startTime)); m->mothurOutEndLine();
631 m->mothurOut(toString(endLine-1) + "\t" + toString(time(NULL) - startTime)); m->mothurOutEndLine();
637 catch(exception& e) {
638 m->errorOut(e, "PairwiseSeqsCommand", "driver");
642 /**************************************************************************************************/
643 /////// need to fix to work with calcs and sequencedb
644 int PairwiseSeqsCommand::driver(int startLine, int endLine, string dFileName, string square){
647 int startTime = time(NULL);
650 ofstream outFile(dFileName.c_str(), ios::trunc);
651 outFile.setf(ios::fixed, ios::showpoint);
652 outFile << setprecision(4);
654 if(startLine == 0){ outFile << alignDB.getNumSeqs() << endl; }
656 for(int i=startLine;i<endLine;i++){
658 string name = alignDB.get(i).getName();
659 //pad with spaces to make compatible
660 if (name.length() < 10) { while (name.length() < 10) { name += " "; } }
662 outFile << name << '\t';
664 for(int j=0;j<alignDB.getNumSeqs();j++){
666 if (m->control_pressed) { outFile.close(); return 0; }
668 if (alignDB.get(i).getUnaligned().length() > alignment->getnRows()) {
669 alignment->resize(alignDB.get(i).getUnaligned().length()+1);
672 if (alignDB.get(j).getUnaligned().length() > alignment->getnRows()) {
673 alignment->resize(alignDB.get(j).getUnaligned().length()+1);
676 Sequence* seqI = new Sequence(alignDB.get(i).getName(), alignDB.get(i).getAligned());
677 Sequence* seqJ = new Sequence(alignDB.get(j).getName(), alignDB.get(j).getAligned());
679 Nast(alignment, seqI, seqJ);
681 distCalculator->calcDist(*seqI, *seqJ);
682 double dist = distCalculator->getDist();
684 delete seqI; delete seqJ;
686 outFile << dist << '\t';
692 m->mothurOut(toString(i) + "\t" + toString(time(NULL) - startTime)); m->mothurOutEndLine();
696 m->mothurOut(toString(endLine-1) + "\t" + toString(time(NULL) - startTime)); m->mothurOutEndLine();
702 catch(exception& e) {
703 m->errorOut(e, "PairwiseSeqsCommand", "driver");
708 /**************************************************************************************************/
709 /////// need to fix to work with calcs and sequencedb
710 int PairwiseSeqsCommand::driverMPI(int startLine, int endLine, MPI_File& outMPI, float cutoff){
713 int startTime = time(NULL);
715 string outputString = "";
717 for(int i=startLine;i<endLine;i++){
719 for(int j=0;j<i;j++){
721 if (m->control_pressed) { return 0; }
723 if (alignDB.get(i).getUnaligned().length() > alignment->getnRows()) {
724 alignment->resize(alignDB.get(i).getUnaligned().length()+1);
727 if (alignDB.get(j).getUnaligned().length() > alignment->getnRows()) {
728 alignment->resize(alignDB.get(j).getUnaligned().length()+1);
731 Sequence* seqI = new Sequence(alignDB.get(i).getName(), alignDB.get(i).getAligned());
732 Sequence* seqJ = new Sequence(alignDB.get(j).getName(), alignDB.get(j).getAligned());
734 Nast(alignment, seqI, seqJ);
736 distCalculator->calcDist(*seqI, *seqJ);
737 double dist = distCalculator->getDist();
739 delete seqI; delete seqJ;
742 outputString += (alignDB.get(i).getName() + ' ' + alignDB.get(j).getName() + ' ' + toString(dist) + '\n');
747 //m->mothurOut(toString(i) + "\t" + toString(time(NULL) - startTime)); m->mothurOutEndLine();
748 cout << i << '\t' << (time(NULL) - startTime) << endl;
752 //send results to parent
753 int length = outputString.length();
755 char* buf = new char[length];
756 memcpy(buf, outputString.c_str(), length);
758 MPI_File_write_shared(outMPI, buf, length, MPI_CHAR, &status);
766 catch(exception& e) {
767 m->errorOut(e, "PairwiseSeqsCommand", "driverMPI");
771 /**************************************************************************************************/
772 /////// need to fix to work with calcs and sequencedb
773 int PairwiseSeqsCommand::driverMPI(int startLine, int endLine, string file, unsigned long int& size){
778 int amode=MPI_MODE_CREATE|MPI_MODE_WRONLY;
781 strcpy(filename, file.c_str());
783 MPI_File_open(MPI_COMM_SELF, filename, amode, MPI_INFO_NULL, &outMPI);
787 string outputString = "";
790 if(startLine == 0){ outputString += toString(alignDB.getNumSeqs()) + "\n"; }
792 for(int i=startLine;i<endLine;i++){
794 string name = alignDB.get(i).getName();
795 if (name.length() < 10) { //pad with spaces to make compatible
796 while (name.length() < 10) { name += " "; }
798 outputString += name + "\t";
800 for(int j=0;j<i;j++){
802 if (m->control_pressed) { return 0; }
804 if (alignDB.get(i).getUnaligned().length() > alignment->getnRows()) {
805 alignment->resize(alignDB.get(i).getUnaligned().length()+1);
808 if (alignDB.get(j).getUnaligned().length() > alignment->getnRows()) {
809 alignment->resize(alignDB.get(j).getUnaligned().length()+1);
812 Sequence* seqI = new Sequence(alignDB.get(i).getName(), alignDB.get(i).getAligned());
813 Sequence* seqJ = new Sequence(alignDB.get(j).getName(), alignDB.get(j).getAligned());
815 Nast(alignment, seqI, seqJ);
817 distCalculator->calcDist(*seqI, *seqJ);
818 double dist = distCalculator->getDist();
820 delete seqI; delete seqJ;
822 outputString += toString(dist) + "\t";
825 outputString += "\n";
827 //send results to parent
828 int length = outputString.length();
829 char* buf = new char[length];
830 memcpy(buf, outputString.c_str(), length);
832 MPI_File_write(outMPI, buf, length, MPI_CHAR, &status);
833 size += outputString.length();
838 MPI_File_close(&outMPI);
842 catch(exception& e) {
843 m->errorOut(e, "PairwiseSeqsCommand", "driverMPI");
847 /**************************************************************************************************/
848 /////// need to fix to work with calcs and sequencedb
849 int PairwiseSeqsCommand::driverMPI(int startLine, int endLine, string file, unsigned long int& size, string square){
854 int amode=MPI_MODE_CREATE|MPI_MODE_WRONLY;
857 strcpy(filename, file.c_str());
859 MPI_File_open(MPI_COMM_SELF, filename, amode, MPI_INFO_NULL, &outMPI);
863 string outputString = "";
866 if(startLine == 0){ outputString += toString(alignDB.getNumSeqs()) + "\n"; }
868 for(int i=startLine;i<endLine;i++){
870 string name = alignDB.get(i).getName();
871 if (name.length() < 10) { //pad with spaces to make compatible
872 while (name.length() < 10) { name += " "; }
874 outputString += name + "\t";
876 for(int j=0;j<alignDB.getNumSeqs();j++){
878 if (m->control_pressed) { return 0; }
880 if (alignDB.get(i).getUnaligned().length() > alignment->getnRows()) {
881 alignment->resize(alignDB.get(i).getUnaligned().length()+1);
884 if (alignDB.get(j).getUnaligned().length() > alignment->getnRows()) {
885 alignment->resize(alignDB.get(j).getUnaligned().length()+1);
888 Sequence* seqI = new Sequence(alignDB.get(i).getName(), alignDB.get(i).getAligned());
889 Sequence* seqJ = new Sequence(alignDB.get(j).getName(), alignDB.get(j).getAligned());
891 Nast(alignment, seqI, seqJ);
893 distCalculator->calcDist(*seqI, *seqJ);
894 double dist = distCalculator->getDist();
896 delete seqI; delete seqJ;
898 outputString += toString(dist) + "\t";
901 outputString += "\n";
903 //send results to parent
904 int length = outputString.length();
905 char* buf = new char[length];
906 memcpy(buf, outputString.c_str(), length);
908 MPI_File_write(outMPI, buf, length, MPI_CHAR, &status);
909 size += outputString.length();
914 MPI_File_close(&outMPI);
918 catch(exception& e) {
919 m->errorOut(e, "PairwiseSeqsCommand", "driverMPI");
924 /**************************************************************************************************/