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);
207 temp = validParameter.validFile(parameters, "gapopen", false); if (temp == "not found"){ temp = "-2.0"; }
208 m->mothurConvert(temp, gapOpen);
210 temp = validParameter.validFile(parameters, "gapextend", false); if (temp == "not found"){ temp = "-1.0"; }
211 m->mothurConvert(temp, gapExtend);
213 temp = validParameter.validFile(parameters, "processors", false); if (temp == "not found"){ temp = m->getProcessors(); }
214 m->setProcessors(temp);
215 m->mothurConvert(temp, processors);
217 temp = validParameter.validFile(parameters, "cutoff", false); if(temp == "not found"){ temp = "1.0"; }
218 m->mothurConvert(temp, cutoff);
220 temp = validParameter.validFile(parameters, "countends", false); if(temp == "not found"){ temp = "T"; }
221 countends = m->isTrue(temp);
223 temp = validParameter.validFile(parameters, "compress", false); if(temp == "not found"){ temp = "F"; }
224 compress = m->isTrue(temp);
226 align = validParameter.validFile(parameters, "align", false); if (align == "not found"){ align = "needleman"; }
228 output = validParameter.validFile(parameters, "output", false); if(output == "not found"){ output = "column"; }
229 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"; }
231 calc = validParameter.validFile(parameters, "calc", false);
232 if (calc == "not found") { calc = "onegap"; }
234 if (calc == "default") { calc = "onegap"; }
236 m->splitAtDash(calc, Estimators);
240 catch(exception& e) {
241 m->errorOut(e, "PairwiseSeqsCommand", "PairwiseSeqsCommand");
245 //**********************************************************************************************************************
247 int PairwiseSeqsCommand::execute(){
249 if (abort == true) { if (calledHelp) { return 0; } return 2; }
251 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.
255 for (int s = 0; s < fastaFileNames.size(); s++) {
256 if (m->control_pressed) { outputTypes.clear(); return 0; }
258 m->mothurOut("Processing sequences from " + fastaFileNames[s] + " ..." ); m->mothurOutEndLine();
260 if (outputDir == "") { outputDir += m->hasPath(fastaFileNames[s]); }
263 m->openInputFile(fastaFileNames[s], inFASTA);
264 alignDB = SequenceDB(inFASTA);
267 int numSeqs = alignDB.getNumSeqs();
268 int startTime = time(NULL);
269 string outputFile = "";
271 if (output == "lt") { //does the user want lower triangle phylip formatted file
272 outputFile = outputDir + m->getRootName(m->getSimpleName(fastaFileNames[s])) + "phylip.dist";
273 m->mothurRemove(outputFile); outputTypes["phylip"].push_back(outputFile);
274 }else if (output == "column") { //user wants column format
275 outputFile = outputDir + m->getRootName(m->getSimpleName(fastaFileNames[s])) + "dist";
276 outputTypes["column"].push_back(outputFile);
277 m->mothurRemove(outputFile);
278 }else { //assume square
279 outputFile = outputDir + m->getRootName(m->getSimpleName(fastaFileNames[s])) + "square.dist";
280 m->mothurRemove(outputFile);
281 outputTypes["phylip"].push_back(outputFile);
290 MPI_Comm_size(MPI_COMM_WORLD, &processors); //set processors to the number of mpi processes running
291 MPI_Comm_rank(MPI_COMM_WORLD, &pid); //find out who we are
293 //each process gets where it should start and stop in the file
294 if (output != "square") {
295 start = int (sqrt(float(pid)/float(processors)) * numSeqs);
296 end = int (sqrt(float(pid+1)/float(processors)) * numSeqs);
298 start = int ((float(pid)/float(processors)) * numSeqs);
299 end = int ((float(pid+1)/float(processors)) * numSeqs);
302 if (output == "column") {
304 int amode=MPI_MODE_CREATE|MPI_MODE_WRONLY;
307 strcpy(filename, outputFile.c_str());
309 MPI_File_open(MPI_COMM_WORLD, filename, amode, MPI_INFO_NULL, &outMPI);
311 if (pid == 0) { //you are the root process
316 driverMPI(start, end, outMPI, cutoff);
318 if (m->control_pressed) { outputTypes.clear(); MPI_File_close(&outMPI); m->mothurRemove(outputFile); return 0; }
321 for(int i = 1; i < processors; i++) {
322 if (m->control_pressed) { outputTypes.clear(); MPI_File_close(&outMPI); m->mothurRemove(outputFile); return 0; }
325 MPI_Recv(buf, 5, MPI_CHAR, i, tag, MPI_COMM_WORLD, &status);
327 }else { //you are a child process
329 driverMPI(start, end, outMPI, cutoff);
331 if (m->control_pressed) { outputTypes.clear(); MPI_File_close(&outMPI); m->mothurRemove(outputFile); return 0; }
335 //tell parent you are done.
336 MPI_Send(buf, 5, MPI_CHAR, 0, tag, MPI_COMM_WORLD);
339 MPI_File_close(&outMPI);
341 }else { //lower triangle format
342 if (pid == 0) { //you are the root process
346 unsigned long long mySize;
348 if (output != "square"){ driverMPI(start, end, outputFile, mySize); }
349 else { driverMPI(start, end, outputFile, mySize, output); }
351 if (m->control_pressed) { outputTypes.clear(); m->mothurRemove(outputFile); return 0; }
353 int amode=MPI_MODE_APPEND|MPI_MODE_WRONLY|MPI_MODE_CREATE; //
358 strcpy(filename, outputFile.c_str());
360 MPI_File_open(MPI_COMM_SELF, filename, amode, MPI_INFO_NULL, &outMPI);
363 for(int b = 1; b < processors; b++) {
364 unsigned long long fileSize;
366 if (m->control_pressed) { outputTypes.clear(); MPI_File_close(&outMPI); m->mothurRemove(outputFile); return 0; }
368 MPI_Recv(&fileSize, 1, MPI_LONG, b, tag, MPI_COMM_WORLD, &status);
370 string outTemp = outputFile + toString(b) + ".temp";
372 char* buf = new char[outTemp.length()];
373 memcpy(buf, outTemp.c_str(), outTemp.length());
375 MPI_File_open(MPI_COMM_SELF, buf, MPI_MODE_DELETE_ON_CLOSE|MPI_MODE_RDONLY, MPI_INFO_NULL, &inMPI);
379 while (count < fileSize) {
381 MPI_File_read(inMPI, buf2, 1, MPI_CHAR, &status);
382 MPI_File_write(outMPI, buf2, 1, MPI_CHAR, &status);
386 MPI_File_close(&inMPI); //deleted on close
389 MPI_File_close(&outMPI);
390 }else { //you are a child process
392 unsigned long long size;
393 if (output != "square"){ driverMPI(start, end, (outputFile + toString(pid) + ".temp"), size); }
394 else { driverMPI(start, end, (outputFile + toString(pid) + ".temp"), size, output); }
396 if (m->control_pressed) { return 0; }
398 //tell parent you are done.
399 MPI_Send(&size, 1, MPI_LONG, 0, tag, MPI_COMM_WORLD);
402 MPI_Barrier(MPI_COMM_WORLD); //make everyone wait - just in case
405 //#if defined (__APPLE__) || (__MACH__) || (linux) || (__linux) || (__linux__) || (__unix__) || (__unix)
406 //if you don't need to fork anything
408 if (output != "square") { driver(0, numSeqs, outputFile, cutoff); }
409 else { driver(0, numSeqs, outputFile, "square"); }
410 }else{ //you have multiple processors
412 for (int i = 0; i < processors; i++) {
413 distlinePair tempLine;
414 lines.push_back(tempLine);
415 if (output != "square") {
416 lines[i].start = int (sqrt(float(i)/float(processors)) * numSeqs);
417 lines[i].end = int (sqrt(float(i+1)/float(processors)) * numSeqs);
419 lines[i].start = int ((float(i)/float(processors)) * numSeqs);
420 lines[i].end = int ((float(i+1)/float(processors)) * numSeqs);
424 createProcesses(outputFile);
428 //if (output != "square") { driver(0, numSeqs, outputFile, cutoff); }
429 //else { driver(0, numSeqs, outputFile, "square"); }
433 if (m->control_pressed) { outputTypes.clear(); m->mothurRemove(outputFile); return 0; }
436 MPI_Comm_rank(MPI_COMM_WORLD, &pid);
438 if (pid == 0) { //only one process should output to screen
442 fileHandle.open(outputFile.c_str());
444 m->gobble(fileHandle);
445 if (fileHandle.eof()) { m->mothurOut(outputFile + " is blank. This can result if there are no distances below your cutoff."); m->mothurOutEndLine(); }
449 m->mothurOut("Compressing..."); m->mothurOutEndLine();
450 m->mothurOut("(Replacing " + outputFile + " with " + outputFile + ".gz)"); m->mothurOutEndLine();
451 system(("gzip -v " + outputFile).c_str());
452 outputNames.push_back(outputFile + ".gz");
453 }else { outputNames.push_back(outputFile); }
459 m->mothurOut("It took " + toString(time(NULL) - startTime) + " to calculate the distances for " + toString(numSeqs) + " sequences."); m->mothurOutEndLine();
461 if (m->control_pressed) { outputTypes.clear(); m->mothurRemove(outputFile); return 0; }
464 //set phylip file as new current phylipfile
466 itTypes = outputTypes.find("phylip");
467 if (itTypes != outputTypes.end()) {
468 if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setPhylipFile(current); }
471 //set column file as new current columnfile
472 itTypes = outputTypes.find("column");
473 if (itTypes != outputTypes.end()) {
474 if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setColumnFile(current); }
477 m->mothurOutEndLine();
478 m->mothurOut("Output File Name: "); m->mothurOutEndLine();
479 for (int i = 0; i < outputNames.size(); i++) { m->mothurOut(outputNames[i]); m->mothurOutEndLine(); }
480 m->mothurOutEndLine();
485 catch(exception& e) {
486 m->errorOut(e, "PairwiseSeqsCommand", "execute");
491 /**************************************************************************************************/
492 void PairwiseSeqsCommand::createProcesses(string filename) {
497 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux) || (__linux__) || (__unix__) || (__unix)
500 //loop through and create all the processes you want
501 while (process != processors) {
505 processIDS.push_back(pid);
508 if (output != "square") { driver(lines[process].start, lines[process].end, filename + toString(getpid()) + ".temp", cutoff); }
509 else { driver(lines[process].start, lines[process].end, filename + toString(getpid()) + ".temp", "square"); }
512 m->mothurOut("[ERROR]: unable to spawn the necessary processes."); m->mothurOutEndLine();
513 for (int i=0;i<processIDS.size();i++) { int temp = processIDS[i]; kill (temp, SIGINT); }
519 if (output != "square") { driver(lines[0].start, lines[0].end, filename, cutoff); }
520 else { driver(lines[0].start, lines[0].end, filename, "square"); }
523 //force parent to wait until all the processes are done
524 for (int i=0;i<processIDS.size();i++) {
525 int temp = processIDS[i];
529 //////////////////////////////////////////////////////////////////////////////////////////////////////
530 //Windows version shared memory, so be careful when passing variables through the distanceData struct.
531 //Above fork() will clone, so memory is separate, but that's not the case with windows,
532 //that's why the distance calculator was moved inside of the driver to make separate copies.
533 //////////////////////////////////////////////////////////////////////////////////////////////////////
535 vector<pairwiseData*> pDataArray; //[processors-1];
536 DWORD dwThreadIdArray[processors-1];
537 HANDLE hThreadArray[processors-1];
539 //Create processor-1 worker threads.
540 for( int i=0; i<processors-1; i++ ){
541 string extension = toString(i) + ".temp";
543 // Allocate memory for thread data.
544 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);
545 pDataArray.push_back(tempDist);
546 processIDS.push_back(i);
548 if (output != "square") { hThreadArray[i] = CreateThread(NULL, 0, MyPairwiseThreadFunction, pDataArray[i], 0, &dwThreadIdArray[i]); }
549 else { hThreadArray[i] = CreateThread(NULL, 0, MyPairwiseSquareThreadFunction, pDataArray[i], 0, &dwThreadIdArray[i]); }
553 if (output != "square") { driver(lines[0].start, lines[0].end, filename, cutoff); }
554 else { driver(lines[0].start, lines[0].end, filename, "square"); }
556 //Wait until all threads have terminated.
557 WaitForMultipleObjects(processors-1, hThreadArray, TRUE, INFINITE);
559 //Close all thread handles and free memory allocations.
560 for(int i=0; i < pDataArray.size(); i++){
561 CloseHandle(hThreadArray[i]);
562 delete pDataArray[i];
567 //append and remove temp files
568 for (int i=0;i<processIDS.size();i++) {
569 m->appendFiles((filename + toString(processIDS[i]) + ".temp"), filename);
570 m->mothurRemove((filename + toString(processIDS[i]) + ".temp"));
574 catch(exception& e) {
575 m->errorOut(e, "PairwiseSeqsCommand", "createProcesses");
580 /**************************************************************************************************/
581 /////// need to fix to work with calcs and sequencedb
582 int PairwiseSeqsCommand::driver(int startLine, int endLine, string dFileName, float cutoff){
585 int startTime = time(NULL);
587 Alignment* alignment;
588 if(align == "gotoh") { alignment = new GotohOverlap(gapOpen, gapExtend, match, misMatch, longestBase); }
589 else if(align == "needleman") { alignment = new NeedlemanOverlap(gapOpen, match, misMatch, longestBase); }
590 else if(align == "blast") { alignment = new BlastAlignment(gapOpen, gapExtend, match, misMatch); }
591 else if(align == "noalign") { alignment = new NoAlign(); }
593 m->mothurOut(align + " is not a valid alignment option. I will run the command using needleman.");
594 m->mothurOutEndLine();
595 alignment = new NeedlemanOverlap(gapOpen, match, misMatch, longestBase);
598 ValidCalculators validCalculator;
599 Dist* distCalculator;
601 if (validCalculator.isValidCalculator("distance", Estimators[0]) == true) {
602 if (Estimators[0] == "nogaps") { distCalculator = new ignoreGaps(); }
603 else if (Estimators[0] == "eachgap") { distCalculator = new eachGapDist(); }
604 else if (Estimators[0] == "onegap") { distCalculator = new oneGapDist(); }
607 if (validCalculator.isValidCalculator("distance", Estimators[0]) == true) {
608 if (Estimators[0] == "nogaps") { distCalculator = new ignoreGaps(); }
609 else if (Estimators[0] == "eachgap"){ distCalculator = new eachGapIgnoreTermGapDist(); }
610 else if (Estimators[0] == "onegap") { distCalculator = new oneGapIgnoreTermGapDist(); }
615 ofstream outFile(dFileName.c_str(), ios::trunc);
616 outFile.setf(ios::fixed, ios::showpoint);
617 outFile << setprecision(4);
619 if((output == "lt") && startLine == 0){ outFile << alignDB.getNumSeqs() << endl; }
621 for(int i=startLine;i<endLine;i++){
623 string name = alignDB.get(i).getName();
624 if (name.length() < 10) { //pad with spaces to make compatible
625 while (name.length() < 10) { name += " "; }
627 outFile << name << '\t';
630 for(int j=0;j<i;j++){
632 if (m->control_pressed) { outFile.close(); delete alignment; delete distCalculator; return 0; }
634 if (alignDB.get(i).getUnaligned().length() > alignment->getnRows()) {
635 alignment->resize(alignDB.get(i).getUnaligned().length()+1);
638 if (alignDB.get(j).getUnaligned().length() > alignment->getnRows()) {
639 alignment->resize(alignDB.get(j).getUnaligned().length()+1);
642 Sequence seqI(alignDB.get(i).getName(), alignDB.get(i).getAligned());
643 Sequence seqJ(alignDB.get(j).getName(), alignDB.get(j).getAligned());
645 alignment->align(seqI.getUnaligned(), seqJ.getUnaligned());
646 seqI.setAligned(alignment->getSeqAAln());
647 seqJ.setAligned(alignment->getSeqBAln());
650 distCalculator->calcDist(seqI, seqJ);
651 double dist = distCalculator->getDist();
654 if (output == "column") { outFile << alignDB.get(i).getName() << ' ' << alignDB.get(j).getName() << ' ' << dist << endl; }
656 if (output == "lt") { outFile << dist << '\t'; }
659 if (output == "lt") { outFile << endl; }
662 m->mothurOut(toString(i) + "\t" + toString(time(NULL) - startTime)); m->mothurOutEndLine();
666 m->mothurOut(toString(endLine-1) + "\t" + toString(time(NULL) - startTime)); m->mothurOutEndLine();
670 delete distCalculator;
674 catch(exception& e) {
675 m->errorOut(e, "PairwiseSeqsCommand", "driver");
679 /**************************************************************************************************/
680 /////// need to fix to work with calcs and sequencedb
681 int PairwiseSeqsCommand::driver(int startLine, int endLine, string dFileName, string square){
684 int startTime = time(NULL);
686 Alignment* alignment;
687 if(align == "gotoh") { alignment = new GotohOverlap(gapOpen, gapExtend, match, misMatch, longestBase); }
688 else if(align == "needleman") { alignment = new NeedlemanOverlap(gapOpen, match, misMatch, longestBase); }
689 else if(align == "blast") { alignment = new BlastAlignment(gapOpen, gapExtend, match, misMatch); }
690 else if(align == "noalign") { alignment = new NoAlign(); }
692 m->mothurOut(align + " is not a valid alignment option. I will run the command using needleman.");
693 m->mothurOutEndLine();
694 alignment = new NeedlemanOverlap(gapOpen, match, misMatch, longestBase);
697 ValidCalculators validCalculator;
698 Dist* distCalculator;
700 if (validCalculator.isValidCalculator("distance", Estimators[0]) == true) {
701 if (Estimators[0] == "nogaps") { distCalculator = new ignoreGaps(); }
702 else if (Estimators[0] == "eachgap") { distCalculator = new eachGapDist(); }
703 else if (Estimators[0] == "onegap") { distCalculator = new oneGapDist(); }
706 if (validCalculator.isValidCalculator("distance", Estimators[0]) == true) {
707 if (Estimators[0] == "nogaps") { distCalculator = new ignoreGaps(); }
708 else if (Estimators[0] == "eachgap"){ distCalculator = new eachGapIgnoreTermGapDist(); }
709 else if (Estimators[0] == "onegap") { distCalculator = new oneGapIgnoreTermGapDist(); }
714 ofstream outFile(dFileName.c_str(), ios::trunc);
715 outFile.setf(ios::fixed, ios::showpoint);
716 outFile << setprecision(4);
718 if(startLine == 0){ outFile << alignDB.getNumSeqs() << endl; }
720 for(int i=startLine;i<endLine;i++){
722 string name = alignDB.get(i).getName();
723 //pad with spaces to make compatible
724 if (name.length() < 10) { while (name.length() < 10) { name += " "; } }
726 outFile << name << '\t';
728 for(int j=0;j<alignDB.getNumSeqs();j++){
730 if (m->control_pressed) { outFile.close(); delete alignment; delete distCalculator; return 0; }
732 if (alignDB.get(i).getUnaligned().length() > alignment->getnRows()) {
733 alignment->resize(alignDB.get(i).getUnaligned().length()+1);
736 if (alignDB.get(j).getUnaligned().length() > alignment->getnRows()) {
737 alignment->resize(alignDB.get(j).getUnaligned().length()+1);
740 Sequence seqI(alignDB.get(i).getName(), alignDB.get(i).getAligned());
741 Sequence seqJ(alignDB.get(j).getName(), alignDB.get(j).getAligned());
743 alignment->align(seqI.getUnaligned(), seqJ.getUnaligned());
744 seqI.setAligned(alignment->getSeqAAln());
745 seqJ.setAligned(alignment->getSeqBAln());
747 distCalculator->calcDist(seqI, seqJ);
748 double dist = distCalculator->getDist();
750 outFile << dist << '\t';
756 m->mothurOut(toString(i) + "\t" + toString(time(NULL) - startTime)); m->mothurOutEndLine();
760 m->mothurOut(toString(endLine-1) + "\t" + toString(time(NULL) - startTime)); m->mothurOutEndLine();
764 delete distCalculator;
768 catch(exception& e) {
769 m->errorOut(e, "PairwiseSeqsCommand", "driver");
774 /**************************************************************************************************/
775 /////// need to fix to work with calcs and sequencedb
776 int PairwiseSeqsCommand::driverMPI(int startLine, int endLine, MPI_File& outMPI, float cutoff){
779 int startTime = time(NULL);
781 Alignment* alignment;
782 if(align == "gotoh") { alignment = new GotohOverlap(gapOpen, gapExtend, match, misMatch, longestBase); }
783 else if(align == "needleman") { alignment = new NeedlemanOverlap(gapOpen, match, misMatch, longestBase); }
784 else if(align == "blast") { alignment = new BlastAlignment(gapOpen, gapExtend, match, misMatch); }
785 else if(align == "noalign") { alignment = new NoAlign(); }
787 m->mothurOut(align + " is not a valid alignment option. I will run the command using needleman.");
788 m->mothurOutEndLine();
789 alignment = new NeedlemanOverlap(gapOpen, match, misMatch, longestBase);
792 ValidCalculators validCalculator;
793 Dist* distCalculator;
795 if (validCalculator.isValidCalculator("distance", Estimators[0]) == true) {
796 if (Estimators[0] == "nogaps") { distCalculator = new ignoreGaps(); }
797 else if (Estimators[0] == "eachgap") { distCalculator = new eachGapDist(); }
798 else if (Estimators[0] == "onegap") { distCalculator = new oneGapDist(); }
801 if (validCalculator.isValidCalculator("distance", Estimators[0]) == true) {
802 if (Estimators[0] == "nogaps") { distCalculator = new ignoreGaps(); }
803 else if (Estimators[0] == "eachgap"){ distCalculator = new eachGapIgnoreTermGapDist(); }
804 else if (Estimators[0] == "onegap") { distCalculator = new oneGapIgnoreTermGapDist(); }
808 string outputString = "";
810 for(int i=startLine;i<endLine;i++){
812 for(int j=0;j<i;j++){
814 if (m->control_pressed) { delete alignment; delete distCalculator; return 0; }
816 if (alignDB.get(i).getUnaligned().length() > alignment->getnRows()) {
817 alignment->resize(alignDB.get(i).getUnaligned().length()+1);
820 if (alignDB.get(j).getUnaligned().length() > alignment->getnRows()) {
821 alignment->resize(alignDB.get(j).getUnaligned().length()+1);
824 Sequence seqI(alignDB.get(i).getName(), alignDB.get(i).getAligned());
825 Sequence seqJ(alignDB.get(j).getName(), alignDB.get(j).getAligned());
827 alignment->align(seqI.getUnaligned(), seqJ.getUnaligned());
828 seqI.setAligned(alignment->getSeqAAln());
829 seqJ.setAligned(alignment->getSeqBAln());
831 distCalculator->calcDist(seqI, seqJ);
832 double dist = distCalculator->getDist();
835 outputString += (alignDB.get(i).getName() + ' ' + alignDB.get(j).getName() + ' ' + toString(dist) + '\n');
840 //m->mothurOut(toString(i) + "\t" + toString(time(NULL) - startTime)); m->mothurOutEndLine();
841 cout << i << '\t' << (time(NULL) - startTime) << endl;
845 //send results to parent
846 int length = outputString.length();
848 char* buf = new char[length];
849 memcpy(buf, outputString.c_str(), length);
851 MPI_File_write_shared(outMPI, buf, length, MPI_CHAR, &status);
857 delete distCalculator;
860 catch(exception& e) {
861 m->errorOut(e, "PairwiseSeqsCommand", "driverMPI");
865 /**************************************************************************************************/
866 /////// need to fix to work with calcs and sequencedb
867 int PairwiseSeqsCommand::driverMPI(int startLine, int endLine, string file, unsigned long long& size){
872 int amode=MPI_MODE_CREATE|MPI_MODE_WRONLY;
875 strcpy(filename, file.c_str());
877 MPI_File_open(MPI_COMM_SELF, filename, amode, MPI_INFO_NULL, &outMPI);
879 Alignment* alignment;
880 if(align == "gotoh") { alignment = new GotohOverlap(gapOpen, gapExtend, match, misMatch, longestBase); }
881 else if(align == "needleman") { alignment = new NeedlemanOverlap(gapOpen, match, misMatch, longestBase); }
882 else if(align == "blast") { alignment = new BlastAlignment(gapOpen, gapExtend, match, misMatch); }
883 else if(align == "noalign") { alignment = new NoAlign(); }
885 m->mothurOut(align + " is not a valid alignment option. I will run the command using needleman.");
886 m->mothurOutEndLine();
887 alignment = new NeedlemanOverlap(gapOpen, match, misMatch, longestBase);
890 ValidCalculators validCalculator;
891 Dist* distCalculator;
893 if (validCalculator.isValidCalculator("distance", Estimators[0]) == true) {
894 if (Estimators[0] == "nogaps") { distCalculator = new ignoreGaps(); }
895 else if (Estimators[0] == "eachgap") { distCalculator = new eachGapDist(); }
896 else if (Estimators[0] == "onegap") { distCalculator = new oneGapDist(); }
899 if (validCalculator.isValidCalculator("distance", Estimators[0]) == true) {
900 if (Estimators[0] == "nogaps") { distCalculator = new ignoreGaps(); }
901 else if (Estimators[0] == "eachgap"){ distCalculator = new eachGapIgnoreTermGapDist(); }
902 else if (Estimators[0] == "onegap") { distCalculator = new oneGapIgnoreTermGapDist(); }
907 string outputString = "";
910 if(startLine == 0){ outputString += toString(alignDB.getNumSeqs()) + "\n"; }
912 for(int i=startLine;i<endLine;i++){
914 string name = alignDB.get(i).getName();
915 if (name.length() < 10) { //pad with spaces to make compatible
916 while (name.length() < 10) { name += " "; }
918 outputString += name + "\t";
920 for(int j=0;j<i;j++){
922 if (m->control_pressed) { delete alignment; delete distCalculator; return 0; }
924 if (alignDB.get(i).getUnaligned().length() > alignment->getnRows()) {
925 alignment->resize(alignDB.get(i).getUnaligned().length()+1);
928 if (alignDB.get(j).getUnaligned().length() > alignment->getnRows()) {
929 alignment->resize(alignDB.get(j).getUnaligned().length()+1);
932 Sequence seqI(alignDB.get(i).getName(), alignDB.get(i).getAligned());
933 Sequence seqJ(alignDB.get(j).getName(), alignDB.get(j).getAligned());
935 alignment->align(seqI.getUnaligned(), seqJ.getUnaligned());
936 seqI.setAligned(alignment->getSeqAAln());
937 seqJ.setAligned(alignment->getSeqBAln());
939 distCalculator->calcDist(seqI, seqJ);
940 double dist = distCalculator->getDist();
942 outputString += toString(dist) + "\t";
945 outputString += "\n";
947 //send results to parent
948 int length = outputString.length();
949 char* buf = new char[length];
950 memcpy(buf, outputString.c_str(), length);
952 MPI_File_write(outMPI, buf, length, MPI_CHAR, &status);
953 size += outputString.length();
958 MPI_File_close(&outMPI);
960 delete distCalculator;
964 catch(exception& e) {
965 m->errorOut(e, "PairwiseSeqsCommand", "driverMPI");
969 /**************************************************************************************************/
970 /////// need to fix to work with calcs and sequencedb
971 int PairwiseSeqsCommand::driverMPI(int startLine, int endLine, string file, unsigned long long& size, string square){
976 int amode=MPI_MODE_CREATE|MPI_MODE_WRONLY;
979 strcpy(filename, file.c_str());
981 MPI_File_open(MPI_COMM_SELF, filename, amode, MPI_INFO_NULL, &outMPI);
983 Alignment* alignment;
984 if(align == "gotoh") { alignment = new GotohOverlap(gapOpen, gapExtend, match, misMatch, longestBase); }
985 else if(align == "needleman") { alignment = new NeedlemanOverlap(gapOpen, match, misMatch, longestBase); }
986 else if(align == "blast") { alignment = new BlastAlignment(gapOpen, gapExtend, match, misMatch); }
987 else if(align == "noalign") { alignment = new NoAlign(); }
989 m->mothurOut(align + " is not a valid alignment option. I will run the command using needleman.");
990 m->mothurOutEndLine();
991 alignment = new NeedlemanOverlap(gapOpen, match, misMatch, longestBase);
994 ValidCalculators validCalculator;
995 Dist* distCalculator;
997 if (validCalculator.isValidCalculator("distance", Estimators[0]) == true) {
998 if (Estimators[0] == "nogaps") { distCalculator = new ignoreGaps(); }
999 else if (Estimators[0] == "eachgap") { distCalculator = new eachGapDist(); }
1000 else if (Estimators[0] == "onegap") { distCalculator = new oneGapDist(); }
1003 if (validCalculator.isValidCalculator("distance", Estimators[0]) == true) {
1004 if (Estimators[0] == "nogaps") { distCalculator = new ignoreGaps(); }
1005 else if (Estimators[0] == "eachgap"){ distCalculator = new eachGapIgnoreTermGapDist(); }
1006 else if (Estimators[0] == "onegap") { distCalculator = new oneGapIgnoreTermGapDist(); }
1010 string outputString = "";
1013 if(startLine == 0){ outputString += toString(alignDB.getNumSeqs()) + "\n"; }
1015 for(int i=startLine;i<endLine;i++){
1017 string name = alignDB.get(i).getName();
1018 if (name.length() < 10) { //pad with spaces to make compatible
1019 while (name.length() < 10) { name += " "; }
1021 outputString += name + "\t";
1023 for(int j=0;j<alignDB.getNumSeqs();j++){
1025 if (m->control_pressed) { delete alignment; return 0; }
1027 if (alignDB.get(i).getUnaligned().length() > alignment->getnRows()) {
1028 alignment->resize(alignDB.get(i).getUnaligned().length()+1);
1031 if (alignDB.get(j).getUnaligned().length() > alignment->getnRows()) {
1032 alignment->resize(alignDB.get(j).getUnaligned().length()+1);
1035 Sequence seqI(alignDB.get(i).getName(), alignDB.get(i).getAligned());
1036 Sequence seqJ(alignDB.get(j).getName(), alignDB.get(j).getAligned());
1038 alignment->align(seqI.getUnaligned(), seqJ.getUnaligned());
1039 seqI.setAligned(alignment->getSeqAAln());
1040 seqJ.setAligned(alignment->getSeqBAln());
1042 distCalculator->calcDist(seqI, seqJ);
1043 double dist = distCalculator->getDist();
1045 outputString += toString(dist) + "\t";
1048 outputString += "\n";
1050 //send results to parent
1051 int length = outputString.length();
1052 char* buf = new char[length];
1053 memcpy(buf, outputString.c_str(), length);
1055 MPI_File_write(outMPI, buf, length, MPI_CHAR, &status);
1056 size += outputString.length();
1061 MPI_File_close(&outMPI);
1066 catch(exception& e) {
1067 m->errorOut(e, "PairwiseSeqsCommand", "driverMPI");
1072 /**************************************************************************************************/