]> git.donarmstrong.com Git - mothur.git/blobdiff - pintail.cpp
changes while testing
[mothur.git] / pintail.cpp
index 1122c242d35e4a5891e87ac3aeaaaced048a9a56..b9f2434d19d4cc842bfac1dce4f6de5a0d1b742d 100644 (file)
@@ -74,7 +74,7 @@ int Pintail::doPrep() {
        #ifdef USE_MPI
                //do nothing
        #else
-               #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux)
+               #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux) || (__linux__) || (__unix__) || (__unix)
                        //find breakup of templatefile for quantiles
                        if (processors == 1) {   templateLines.push_back(new linePair(0, templateSeqs.size()));  }
                        else { 
@@ -92,7 +92,7 @@ int Pintail::doPrep() {
                m->mothurOut("Getting conservation... "); cout.flush();
                if (consfile == "") { 
                        m->mothurOut("Calculating probability of conservation for your template sequences.  This can take a while...  I will output the frequency of the highest base in each position to a .freq file so that you can input them using the conservation parameter next time you run this command.  Providing the .freq file will improve speed.    "); cout.flush();
-                       probabilityProfile = decalc->calcFreq(templateSeqs, outputDir + getSimpleName(templateFileName)); 
+                       probabilityProfile = decalc->calcFreq(templateSeqs, templateFileName); 
                        if (m->control_pressed) {  return 0;  }
                        m->mothurOut("Done."); m->mothurOutEndLine();
                }else                           {   probabilityProfile = readFreq();    m->mothurOut("Done.");            }
@@ -170,20 +170,20 @@ int Pintail::doPrep() {
                        string noOutliers, outliers;
                        
                        if ((!filter) && (seqMask == "")) {
-                               noOutliers = outputDir + getRootName(getSimpleName(templateFileName)) + "pintail.quan";
+                               noOutliers = m->getRootName(m->getSimpleName(templateFileName)) + "pintail.quan";
                        }else if ((!filter) && (seqMask != "")) { 
-                               noOutliers = outputDir + getRootName(getSimpleName(templateFileName)) + "pintail.masked.quan";
+                               noOutliers =m->getRootName(m->getSimpleName(templateFileName)) + "pintail.masked.quan";
                        }else if ((filter) && (seqMask != "")) { 
-                               noOutliers = outputDir + getRootName(getSimpleName(templateFileName)) + "pintail.filtered." + getSimpleName(getRootName(fastafile)) + "masked.quan";
+                               noOutliers = m->getRootName(m->getSimpleName(templateFileName)) + "pintail.filtered." + m->getSimpleName(m->getRootName(fastafile)) + "masked.quan";
                        }else if ((filter) && (seqMask == "")) { 
-                               noOutliers = outputDir + getRootName(getSimpleName(templateFileName)) + "pintail.filtered." + getSimpleName(getRootName(fastafile)) + "quan";
+                               noOutliers = m->getRootName(m->getSimpleName(templateFileName)) + "pintail.filtered." + m->getSimpleName(m->getRootName(fastafile)) + "quan";
                        }
 
                        decalc->removeObviousOutliers(quantilesMembers, templateSeqs.size());
                        
                        if (m->control_pressed) {  return 0;  }
                
-                       string outputString = "";
+                       string outputString = "#" + m->getVersion() + "\n";
                        
                        //adjust quantiles
                        for (int i = 0; i < quantilesMembers.size(); i++) {
@@ -196,20 +196,20 @@ int Pintail::doPrep() {
                                        }
                                }else{
                                        
-                                       sort(quantilesMembers[i].begin(), quantilesMembers[i].end(), compareQuanMembers);
+                                       sort(quantilesMembers[i].begin(), quantilesMembers[i].end());
                                        
                                        //save 10%
-                                       temp.push_back(quantilesMembers[i][int(quantilesMembers[i].size() * 0.10)].score);
+                                       temp.push_back(quantilesMembers[i][int(quantilesMembers[i].size() * 0.10)]);
                                        //save 25%
-                                       temp.push_back(quantilesMembers[i][int(quantilesMembers[i].size() * 0.25)].score);
+                                       temp.push_back(quantilesMembers[i][int(quantilesMembers[i].size() * 0.25)]);
                                        //save 50%
-                                       temp.push_back(quantilesMembers[i][int(quantilesMembers[i].size() * 0.5)].score);
+                                       temp.push_back(quantilesMembers[i][int(quantilesMembers[i].size() * 0.5)]);
                                        //save 75%
-                                       temp.push_back(quantilesMembers[i][int(quantilesMembers[i].size() * 0.75)].score);
+                                       temp.push_back(quantilesMembers[i][int(quantilesMembers[i].size() * 0.75)]);
                                        //save 95%
-                                       temp.push_back(quantilesMembers[i][int(quantilesMembers[i].size() * 0.95)].score);
+                                       temp.push_back(quantilesMembers[i][int(quantilesMembers[i].size() * 0.95)]);
                                        //save 99%
-                                       temp.push_back(quantilesMembers[i][int(quantilesMembers[i].size() * 0.99)].score);
+                                       temp.push_back(quantilesMembers[i][int(quantilesMembers[i].size() * 0.99)]);
                                        
                                }
                                
@@ -224,6 +224,9 @@ int Pintail::doPrep() {
                        
                        printQuanFile(noOutliers, outputString);
                        
+                       //free memory
+                       quantilesMembers.clear();
+                       
                        m->mothurOut("Done."); m->mothurOutEndLine();
                }
                
@@ -246,8 +249,9 @@ int Pintail::doPrep() {
        }
 }
 //***************************************************************************************************************
-int Pintail::print(ostream& out, ostream& outAcc) {
+Sequence Pintail::print(ostream& out, ostream& outAcc) {
        try {
+               
                int index = ceil(deviation);
                
                //is your DE value higher than the 95%
@@ -276,7 +280,7 @@ int Pintail::print(ostream& out, ostream& outAcc) {
                for (int m = 0; m < expectedDistance.size(); m++) {  out << expectedDistance[m] << '\t';  }
                out << endl;
                
-               return 0;
+               return *querySeq;
                
        }
        catch(exception& e) {
@@ -286,9 +290,9 @@ int Pintail::print(ostream& out, ostream& outAcc) {
 }
 #ifdef USE_MPI
 //***************************************************************************************************************
-int Pintail::print(MPI_File& out, MPI_File& outAcc) {
+Sequence Pintail::print(MPI_File& out, MPI_File& outAcc) {
        try {
-               bool results = false;
+               
                string outputString = "";
                int index = ceil(deviation);
                
@@ -310,13 +314,13 @@ int Pintail::print(MPI_File& out, MPI_File& outAcc) {
                        
                        MPI_Status statusAcc;
                        int length = outAccString.length();
-                       char* buf = new char[length];\r
+                       char* buf = new char[length];
                        memcpy(buf, outAccString.c_str(), length);
                                
                        MPI_File_write_shared(outAcc, buf, length, MPI_CHAR, &statusAcc);
                        delete buf;
 
-                       results = true;
+                       return *querySeq;
                }
                outputString += "Observed\t";
                
@@ -330,13 +334,13 @@ int Pintail::print(MPI_File& out, MPI_File& outAcc) {
                
                MPI_Status status;
                int length = outputString.length();
-               char* buf2 = new char[length];\r
+               char* buf2 = new char[length];
                memcpy(buf2, outputString.c_str(), length);
                                
                MPI_File_write_shared(out, buf2, length, MPI_CHAR, &status);
                delete buf2;
                
-               return results;
+               return *querySeq;
        }
        catch(exception& e) {
                m->errorOut(e, "Pintail", "print");
@@ -427,12 +431,15 @@ vector<float> Pintail::readFreq() {
                MPI_Offset size;
                MPI_Status status;
 
-               char* inFileName = new char[consfile.length()];\r
-               memcpy(inFileName, consfile.c_str(), consfile.length());
+               //char* inFileName = new char[consfile.length()];
+               //memcpy(inFileName, consfile.c_str(), consfile.length());
+               
+               char inFileName[1024];
+               strcpy(inFileName, consfile.c_str());
 
                MPI_File_open(MPI_COMM_WORLD, inFileName, MPI_MODE_RDONLY, MPI_INFO_NULL, &inMPI);  
                MPI_File_get_size(inMPI, &size);
-               delete inFileName;
+               //delete inFileName;
 
                char* buffer = new char[size];
                MPI_File_read(inMPI, buffer, size, MPI_CHAR, &status);
@@ -443,6 +450,9 @@ vector<float> Pintail::readFreq() {
                if (tempBuf.length() > size) { tempBuf = tempBuf.substr(0, size);  }
                istringstream iss (tempBuf,istringstream::in);
                
+               //read version
+               string line = m->getline(iss); m->gobble(iss);
+               
                while(!iss.eof()) {
                        iss >> pos >> num;
        
@@ -457,7 +467,7 @@ vector<float> Pintail::readFreq() {
                                prob.push_back(Pi);  
                        }
                        
-                       gobble(iss);
+                       m->gobble(iss);
                }
        
                MPI_File_close(&inMPI);
@@ -465,7 +475,10 @@ vector<float> Pintail::readFreq() {
        #else   
 
                ifstream in;
-               openInputFile(consfile, in);
+               m->openInputFile(consfile, in);
+               
+               //read version
+               string line = m->getline(in); m->gobble(in);
                                
                while(!in.eof()){
                        
@@ -482,7 +495,7 @@ vector<float> Pintail::readFreq() {
                                prob.push_back(Pi);  
                        }
                        
-                       gobble(in);
+                       m->gobble(in);
                }
                in.close();
                
@@ -516,8 +529,8 @@ Sequence* Pintail::findPairs(Sequence* q) {
 //**************************************************************************************************
 void Pintail::createProcessesQuan() {
        try {
-#if defined (__APPLE__) || (__MACH__) || (linux) || (__linux)
-               int process = 0;
+#if defined (__APPLE__) || (__MACH__) || (linux) || (__linux) || (__linux__) || (__unix__) || (__unix)
+               int process = 1;
                vector<int> processIDS;
                                
                //loop through and create all the processes you want
@@ -534,14 +547,13 @@ void Pintail::createProcessesQuan() {
                                //write out data to file so parent can read it
                                ofstream out;
                                string s = toString(getpid()) + ".temp";
-                               openOutputFile(s, out);
-                               
+                               m->openOutputFile(s, out);
                                                                
                                //output observed distances
                                for (int i = 0; i < quantilesMembers.size(); i++) {
                                        out << quantilesMembers[i].size() << '\t';
                                        for (int j = 0; j < quantilesMembers[i].size(); j++) {
-                                               out << quantilesMembers[i][j].score << '\t' << quantilesMembers[i][j].member1 << '\t' << quantilesMembers[i][j].member2 << '\t';
+                                               out << quantilesMembers[i][j] << '\t';
                                        }
                                        out << endl;
                                }
@@ -549,41 +561,46 @@ void Pintail::createProcessesQuan() {
                                out.close();
                                
                                exit(0);
-                       }else { m->mothurOut("unable to spawn the necessary processes."); m->mothurOutEndLine(); exit(0); }
+                       }else { 
+                               m->mothurOut("[ERROR]: unable to spawn the necessary processes."); m->mothurOutEndLine(); 
+                               for (int i = 0; i < processIDS.size(); i++) { kill (processIDS[i], SIGINT); }
+                               exit(0);
+                       }
                }
                
+               //parent does its part
+               quantilesMembers = decalc->getQuantiles(templateSeqs, windowSizesTemplate, window, probabilityProfile, increment, templateLines[0]->start, templateLines[0]->end);
+               
                //force parent to wait until all the processes are done
-               for (int i=0;i<processors;i++) { 
+               for (int i=0;i<(processors-1);i++) { 
                        int temp = processIDS[i];
                        wait(&temp);
                }
 
                //get data created by processes
-               for (int i=0;i<processors;i++) { 
+               for (int i=0;i<(processors-1);i++) { 
                        ifstream in;
                        string s = toString(processIDS[i]) + ".temp";
-                       openInputFile(s, in);
+                       m->openInputFile(s, in);
                        
-                       vector< vector<quanMember> > quan; 
+                       vector< vector<float> > quan; 
                        quan.resize(100);
                        
                        //get quantiles
-                       for (int m = 0; m < quan.size(); m++) {
+                       for (int h = 0; h < quan.size(); h++) {
                                int num;
                                in >> num; 
                                
-                               gobble(in);
+                               m->gobble(in);
 
-                               vector<quanMember> q;  float w; int b, n;
+                               vector<float> q;  float w; 
                                for (int j = 0; j < num; j++) {
-                                       in >> w >> b >> n;
-       
-                                       quanMember newMember(w, b, n);
-                                       q.push_back(newMember);
+                                       in >> w;
+                                       q.push_back(w);
                                }
 
-                               quan[m] = q;
-                               gobble(in);
+                               quan[h] = q;
+                               m->gobble(in);
                        }
                        
        
@@ -595,9 +612,9 @@ void Pintail::createProcessesQuan() {
                        }
                                        
                        in.close();
-                       remove(s.c_str());
+                       m->mothurRemove(s);
                }
-               
+
 #else
                quantilesMembers = decalc->getQuantiles(templateSeqs, windowSizesTemplate, window, probabilityProfile, increment, 0, templateSeqs.size());
 #endif         
@@ -625,12 +642,15 @@ vector< vector<float> > Pintail::readQuantiles() {
                MPI_Offset size;
                MPI_Status status;
                
-               char* inFileName = new char[quanfile.length()];\r
-               memcpy(inFileName, quanfile.c_str(), quanfile.length());
+               //char* inFileName = new char[quanfile.length()];
+               //memcpy(inFileName, quanfile.c_str(), quanfile.length());
+               
+               char inFileName[1024];
+               strcpy(inFileName, quanfile.c_str());
 
                MPI_File_open(MPI_COMM_WORLD, inFileName, MPI_MODE_RDONLY, MPI_INFO_NULL, &inMPI);  
                MPI_File_get_size(inMPI, &size);
-               delete inFileName;
+               //delete inFileName;
 
 
                char* buffer = new char[size];
@@ -641,6 +661,9 @@ vector< vector<float> > Pintail::readQuantiles() {
                istringstream iss (tempBuf,istringstream::in);
                delete buffer;
                
+               //read version
+               string line = m->getline(iss); m->gobble(iss);
+               
                while(!iss.eof()) {
                        iss >> num >> ten >> twentyfive >> fifty >> seventyfive >> ninetyfive >> ninetynine; 
                        
@@ -655,7 +678,7 @@ vector< vector<float> > Pintail::readQuantiles() {
                        
                        quan.push_back(temp);  
                        
-                       gobble(iss);
+                       m->gobble(iss);
                }
        
                MPI_File_close(&inMPI);
@@ -663,7 +686,10 @@ vector< vector<float> > Pintail::readQuantiles() {
        #else   
 
                ifstream in;
-               openInputFile(quanfile, in);
+               m->openInputFile(quanfile, in);
+               
+               //read version
+               string line = m->getline(in); m->gobble(in);
                        
                while(!in.eof()){
                        
@@ -680,7 +706,7 @@ vector< vector<float> > Pintail::readQuantiles() {
                        
                        quan.push_back(temp);  
        
-                       gobble(in);
+                       m->gobble(in);
                }
                in.close();
        #endif
@@ -708,14 +734,17 @@ void Pintail::printQuanFile(string file, string outputString) {
 
                        int outMode=MPI_MODE_CREATE|MPI_MODE_WRONLY;
 
-                       char* FileName = new char[file.length()];\r
-                       memcpy(FileName, file.c_str(), file.length());
+                       //char* FileName = new char[file.length()];
+                       //memcpy(FileName, file.c_str(), file.length());
+                       
+                       char FileName[1024];
+                       strcpy(FileName, file.c_str());
                        
                        if (pid == 0) {
                                MPI_File_open(MPI_COMM_SELF, FileName, outMode, MPI_INFO_NULL, &outQuan);  //comm, filename, mode, info, filepointer
                                
                                int length = outputString.length();
-                               char* buf = new char[length];\r
+                               char* buf = new char[length];
                                memcpy(buf, outputString.c_str(), length);
                                        
                                MPI_File_write(outQuan, buf, length, MPI_CHAR, &status);
@@ -724,10 +753,10 @@ void Pintail::printQuanFile(string file, string outputString) {
                                MPI_File_close(&outQuan);
                        }
 
-                       delete FileName;
+                       //delete FileName;
                #else
                        ofstream outQuan;
-                       openOutputFile(file, outQuan);
+                       m->openOutputFile(file, outQuan);
                        
                        outQuan << outputString;