]> git.donarmstrong.com Git - mothur.git/commitdiff
changed how we name the make.group groups file, and filter file in filter.seqs, finis...
authorwestcott <westcott>
Mon, 20 Sep 2010 16:10:59 +0000 (16:10 +0000)
committerwestcott <westcott>
Mon, 20 Sep 2010 16:10:59 +0000 (16:10 +0000)
aligncommand.cpp
filterseqscommand.cpp
makegroupcommand.cpp
metastats2.c
metastatscommand.cpp
metastatscommand.h

index 877e7c76773ece0f459142e674382fb2747bb8fb..ccf5eebe68f42426f16f2465b68832beb413d2eb 100644 (file)
@@ -182,7 +182,7 @@ AlignCommand::~AlignCommand(){
 void AlignCommand::help(){
        try {
                m->mothurOut("The align.seqs command reads a file containing sequences and creates an alignment file and a report file.\n");
-               m->mothurOut("The align.seqs command parameters are template, candidate, search, ksize, align, match, mismatch, gapopen and gapextend.\n");
+               m->mothurOut("The align.seqs command parameters are template, candidate, search, ksize, align, match, mismatch, gapopen, gapextend and processors.\n");
                m->mothurOut("The template and candidate parameters are required. You may enter multiple fasta files by separating their names with dashes. ie. fasta=abrecovery.fasta-amzon.fasta \n");
                m->mothurOut("The search parameter allows you to specify the method to find most similar template.  Your options are: suffix, kmer and blast. The default is kmer.\n");
                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");
index addc5f338c9202811a7d6e341167c8a097e03ea0..e676f1fbd7af7b581b7552bd25c844255ba4e8ce 100644 (file)
@@ -198,7 +198,11 @@ int FilterSeqsCommand::execute() {
                
                ofstream outFilter;
                
-               string filterFile = outputDir + filterFileName + ".filter";
+               //prevent giantic file name
+               string filterFile;
+               if (fastafileNames.size() > 3) { filterFile = outputDir + "merge.filter"; }
+               else {  filterFile = outputDir + filterFileName + ".filter";  }
+               
                m->openOutputFile(filterFile, outFilter);
                outFilter << filter << endl;
                outFilter.close();
index 41762ab3619979fb98c33a3974cddf40d5d2deaf..8dd1d67b294517437648f314321324ca8f1baf11 100644 (file)
@@ -78,7 +78,9 @@ MakeGroupCommand::MakeGroupCommand(string option)  {
                                        }else{  filename += m->getRootName(m->getSimpleName(fastaFileNames[i]));  }
                                }
                                
-                               filename += "groups";
+                               //prevent giantic file name
+                               if (fastaFileNames.size() > 3) { filename = outputDir + "merge.groups"; }
+                               else {  filename += "groups";  }
                                
                                //make sure there is at least one valid file left
                                if (fastaFileNames.size() == 0) { m->mothurOut("no valid files."); m->mothurOutEndLine(); abort = true; }
index 3b801a0b110c5685fcc3a62f66ab3ad516a862d2..b22938af69282857aa354c4e55ec2c30a4921d8f 100644 (file)
@@ -1,6 +1,8 @@
 
 #include "metastats.h"
 
+//The following code has been modified using the original Metastats program from White, J.R., Nagarajan, N. & Pop, M. Statistical methods for detecting differentially abundant features in clinical metagenomic samples. PLoS Comput Biol 5, e1000352 (2009).
+
 int metastat_main (char* outputFileName, int numRows, int numCols, double threshold, int numPermutations, double** data, int secondGroupingStart){
   
   int size,c=0,i=0,j=0,k,counter=0, bflag=0; 
index 01e260637beeda61043202811450814cd1fc5610..a94c72cb4649c9136175bb75cb3c49846dbcdb1e 100644 (file)
@@ -9,6 +9,7 @@
 
 #include "metastatscommand.h"
 #include "metastats.h"
+#include "sharedutilities.h"
 
 //**********************************************************************************************************************
 
@@ -24,7 +25,7 @@ MetaStatsCommand::MetaStatsCommand(string option) {
                
                else {
                        //valid paramters for this command
-                       string AlignArray[] =  {"groups","label","outputdir","iters","threshold","g","design","inputdir"};
+                       string AlignArray[] =  {"groups","label","outputdir","iters","threshold","g","design","sets","processors","inputdir"};
                        vector<string> myArray (AlignArray, AlignArray+(sizeof(AlignArray)/sizeof(string)));
                        
                        OptionParser parser(option);
@@ -91,14 +92,21 @@ MetaStatsCommand::MetaStatsCommand(string option) {
                                globaldata->Groups = Groups;
                        }
                        
+                       sets = validParameter.validFile(parameters, "sets", false);                     
+                       if (sets == "not found") { sets = ""; }
+                       else { 
+                               m->splitAtDash(sets, Sets);
+                       }
+
+                       
                        string temp = validParameter.validFile(parameters, "iters", false);                     if (temp == "not found") { temp = "1000"; }
                        convert(temp, iters); 
                        
                        temp = validParameter.validFile(parameters, "threshold", false);                        if (temp == "not found") { temp = "0.05"; }
                        convert(temp, threshold); 
                        
-                       //temp = validParameter.validFile(parameters, "g", false);                                      if (temp == "not found") { temp = "0"; }
-                       //convert(temp, g); 
+                       temp = validParameter.validFile(parameters, "processors", false);                       if (temp == "not found"){       temp = "1";     }
+                       convert(temp, processors); 
                }
 
        }
@@ -112,18 +120,20 @@ MetaStatsCommand::MetaStatsCommand(string option) {
 
 void MetaStatsCommand::help(){
        try {
-               m->mothurOut("Paper reference goes here....\n");
+               m->mothurOut("This command is based on the Metastats program, White, J.R., Nagarajan, N. & Pop, M. Statistical methods for detecting differentially abundant features in clinical metagenomic samples. PLoS Comput Biol 5, e1000352 (2009).\n");
                m->mothurOut("The metastats command can only be executed after a successful read.otu command of a list and group or shared file.\n");
-               m->mothurOut("The metastats command outputs a .... file. \n");
-               m->mothurOut("The metastats command parameters are iters, threshold, groups, design and label.  No parameters are required.\n");
+               m->mothurOut("The metastats command outputs a .metastats file. \n");
+               m->mothurOut("The metastats command parameters are iters, threshold, groups, label, design, sets and processors.  The design parameter is required.\n");
                m->mothurOut("The design parameter allows you to assign your groups to sets when you are running metastat. mothur will run all pairwise comparisons of the sets. It is required. \n");
                m->mothurOut("The design file looks like the group file.  It is a 2 column tab delimited file, where the first column is the group name and the second column is the set the group belongs to.\n");
-               m->mothurOut("The iters parameter ...\n");
-               m->mothurOut("The threshold parameter ...\n");
+               m->mothurOut("The sets parameter allows you to specify which of the sets in your designfile you would like to analyze. The set names are separated by dashes. THe default is all sets in the designfile.\n");
+               m->mothurOut("The iters parameter allows you to set number of bootstrap permutations for estimating null distribution of t statistic.  The default is 1000. \n");
+               m->mothurOut("The threshold parameter allows you to set the significance level to reject null hypotheses (default 0.05).\n");
                m->mothurOut("The groups parameter allows you to specify which of the groups in your groupfile you would like included. The group names are separated by dashes.\n");
                m->mothurOut("The label parameter allows you to select what distance levels you would like, and are also separated by dashes.\n");
-               m->mothurOut("The metastats command should be in the following format: metastats(groups=yourGroups, label=yourLabels).\n");
-               m->mothurOut("Example metastats(groups=A-B-C).\n");
+               m->mothurOut("The processors parameter allows you to specify how many processors you would like to use.  The default is 1. \n");
+               m->mothurOut("The metastats command should be in the following format: metastats(design=yourDesignFile).\n");
+               m->mothurOut("Example metastats(design=temp.design, groups=A-B-C).\n");
                m->mothurOut("The default value for groups is all the groups in your groupfile, and all labels in your inputfile will be used.\n");
                m->mothurOut("Note: No spaces between parameter labels (i.e. groups), '=' and parameters (i.e.yourGroups).\n\n");
 
@@ -157,11 +167,46 @@ int MetaStatsCommand::execute(){
                //if the users enters label "0.06" and there is no "0.06" in their file use the next lowest label.
                set<string> processedLabels;
                set<string> userLabels = labels;
-
+               
+               //setup the pairwise comparions of sets for metastats
+               //calculate number of comparisons i.e. with groups A,B,C = AB, AC, BC = 3;
+               //make sure sets are all in designMap
+               SharedUtil* util = new SharedUtil();  
+               util->setGroups(Sets, designMap->namesOfGroups);  
+               delete util;
+               
+               int numGroups = Sets.size();
+               for (int a=0; a<numGroups; a++) { 
+                       for (int l = a+1; l < numGroups; l++) { 
+                               vector<string> groups; groups.push_back(Sets[a]); groups.push_back(Sets[l]);
+                               namesOfGroupCombos.push_back(groups);
+                       }
+               }
+       
+               
+               //only 1 combo
+               if (numGroups == 2) { processors = 1; }
+               else if (numGroups < 2) { m->mothurOut("Not enough sets, I need at least 2 valid sets. Unable to complete command."); m->mothurOutEndLine(); m->control_pressed = true; }
+               
+               #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux)
+                       if(processors != 1){
+                               int numPairs = namesOfGroupCombos.size();
+                               int numPairsPerProcessor = numPairs / processors;
+                       
+                               for (int i = 0; i < processors; i++) {
+                                       int startPos = i * numPairsPerProcessor;
+                                       if(i == processors - 1){
+                                               numPairsPerProcessor = numPairs - i * numPairsPerProcessor;
+                                       }
+                                       lines.push_back(linePair(startPos, numPairsPerProcessor));
+                               }
+                       }
+               #endif
+               
                //as long as you are not at the end of the file or done wih the lines you want
                while((lookup[0] != NULL) && ((allLines == 1) || (userLabels.size() != 0))) {
                        
-                       if (m->control_pressed) {  for (int i = 0; i < lookup.size(); i++) {  delete lookup[i];  } globaldata->Groups.clear(); delete read;  for (int i = 0; i < outputNames.size(); i++) {     remove(outputNames[i].c_str()); } return 0; }
+                       if (m->control_pressed) {  for (int i = 0; i < lookup.size(); i++) {  delete lookup[i];  } globaldata->Groups.clear(); delete read;  delete designMap;  for (int i = 0; i < outputNames.size(); i++) {  remove(outputNames[i].c_str()); } return 0; }
        
                        if(allLines == 1 || labels.count(lookup[0]->getLabel()) == 1){                  
 
@@ -192,13 +237,13 @@ int MetaStatsCommand::execute(){
                        //prevent memory leak
                        for (int i = 0; i < lookup.size(); i++) {  delete lookup[i]; lookup[i] = NULL; }
                        
-                       if (m->control_pressed) {  globaldata->Groups.clear(); delete read;   for (int i = 0; i < outputNames.size(); i++) {    remove(outputNames[i].c_str()); } return 0; }
+                       if (m->control_pressed) {  globaldata->Groups.clear(); delete read;  delete designMap;  for (int i = 0; i < outputNames.size(); i++) {  remove(outputNames[i].c_str()); } return 0; }
 
                        //get next line to process
                        lookup = input->getSharedRAbundVectors();                               
                }
                
-               if (m->control_pressed) { globaldata->Groups.clear(); delete read;  for (int i = 0; i < outputNames.size(); i++) {      remove(outputNames[i].c_str()); }  return 0; }
+               if (m->control_pressed) { globaldata->Groups.clear(); delete read; delete designMap;  for (int i = 0; i < outputNames.size(); i++) {    remove(outputNames[i].c_str()); }  return 0; }
 
                //output error messages about any remaining user labels
                set<string>::iterator it;
@@ -229,6 +274,7 @@ int MetaStatsCommand::execute(){
                globaldata->Groups.clear();  
                delete input; globaldata->ginput = NULL;
                delete read;
+               delete designMap;
                
                if (m->control_pressed) { for (int i = 0; i < outputNames.size(); i++) {        remove(outputNames[i].c_str()); } return 0;}
                
@@ -250,31 +296,116 @@ int MetaStatsCommand::process(vector<SharedRAbundVector*>& thisLookUp){
        try {
                if (pickedGroups) { eliminateZeroOTUS(thisLookUp); }
                
-               string outputFileName = outputDir +  m->getRootName(m->getSimpleName(globaldata->inputFileName)) + thisLookUp[0]->getLabel() + ".metastats";
-               outputNames.push_back(outputFileName);
-               int nameLength = outputFileName.length();
-               char * output = new char[nameLength];
-               strcpy(output, outputFileName.c_str());
-       
-               //build matrix from shared rabunds
-               double** data;
-               data = new double*[thisLookUp[0]->getNumBins()];
+               #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux)
+                               if(processors == 1){
+                                       driver(0, namesOfGroupCombos.size(), thisLookUp);
+                               }else{
+                                       int process = 1;
+                                       vector<int> processIDS;
+               
+                                       //loop through and create all the processes you want
+                                       while (process != processors) {
+                                               int pid = fork();
+                       
+                                               if (pid > 0) {
+                                                       processIDS.push_back(pid);  //create map from line number to pid so you can append files in correct order later
+                                                       process++;
+                                               }else if (pid == 0){
+                                                       driver(lines[process].start, lines[process].num, thisLookUp);
+                                                       exit(0);
+                                               }else { m->mothurOut("unable to spawn the necessary processes."); m->mothurOutEndLine(); exit(0); }
+                                       }
+                                       
+                                       //do my part
+                                       driver(lines[0].start, lines[0].num, thisLookUp);
+               
+                                       //force parent to wait until all the processes are done
+                                       for (int i=0;i<(processors-1);i++) { 
+                                               int temp = processIDS[i];
+                                               wait(&temp);
+                                       }
+                               }
+               #else
+                               driver(0, namesOfGroupCombos.size(), thisLookUp);
+               #endif
+
+               return 0;
                
-               for (int j = 0; j < thisLookUp[0]->getNumBins(); j++) {
-                       data[j] = new double[thisLookUp.size()];
+       }
+       catch(exception& e) {
+               m->errorOut(e, "MetaStatsCommand", "process");
+               exit(1);
+       }
+}
+//**********************************************************************************************************************
+int MetaStatsCommand::driver(int start, int num, vector<SharedRAbundVector*>& thisLookUp) { 
+       try {
+       
+               //for each combo
+               for (int c = start; c < (start+num); c++) {
+                       
+                       //get set names
+                       string setA = namesOfGroupCombos[c][0]; 
+                       string setB = namesOfGroupCombos[c][1];
+                       
+                       //get filename
+                       string outputFileName = outputDir +  m->getRootName(m->getSimpleName(globaldata->inputFileName)) + thisLookUp[0]->getLabel() + "." + setA + "-" + setB + ".metastats";
+                       outputNames.push_back(outputFileName);
+                       int nameLength = outputFileName.length();
+                       char * output = new char[nameLength];
+                       strcpy(output, outputFileName.c_str());
+       
+                       //build matrix from shared rabunds
+                       double** data;
+                       data = new double*[thisLookUp[0]->getNumBins()];
+                       
+                       vector<SharedRAbundVector*> subset;
+                       int setACount = 0;
+                       int setBCount = 0;
                        for (int i = 0; i < thisLookUp.size(); i++) {
-                               data[j][i] = (thisLookUp[i]->getAbundance(j));
+                               string thisGroup = thisLookUp[i]->getGroup();
+                               
+                               //is this group for a set we want to compare??
+                               //sorting the sets by putting setB at the back and setA in the front
+                               if ((designMap->getGroup(thisGroup) == setB)) {  
+                                       subset.push_back(thisLookUp[i]);
+                                       setBCount++;
+                               }else if ((designMap->getGroup(thisGroup) == setA)) {
+                                       subset.insert(subset.begin()+setACount, thisLookUp[i]);
+                                       setACount++;
+                               }
                        }
+                                               
+                       if ((setACount == 0) || (setBCount == 0))  { 
+                               m->mothurOut("Missing shared info for " + setA + " or " + setB + ". Skipping comparison."); m->mothurOutEndLine(); 
+                               outputNames.pop_back();
+                       }else {
+                               //fill data
+                               for (int j = 0; j < thisLookUp[0]->getNumBins(); j++) {
+                                       data[j] = new double[subset.size()];
+                                       for (int i = 0; i < subset.size(); i++) {
+                                               data[j][i] = (subset[i]->getAbundance(j));
+                                       }
+                               }
+                               
+                               m->mothurOut("Comparing " + setA + " and " + setB + "..."); m->mothurOutEndLine(); 
+                               
+                               metastat_main(output, thisLookUp[0]->getNumBins(), subset.size(), threshold, iters, data, setACount);
+                               
+                               m->mothurOutEndLine(); 
+                       }
+                       
+                       //free memory
+                       delete output;
+                       for(int i = 0; i < thisLookUp[0]->getNumBins(); i++)  {  delete[] data[i];  }
+                       delete[] data; 
                }
-       
-               if (g == 0) { g = thisLookUp.size() / 2; }
                
-               metastat_main(output, thisLookUp[0]->getNumBins(), thisLookUp.size(), threshold, iters, data, g);
-                               
                return 0;
+
        }
        catch(exception& e) {
-               m->errorOut(e, "MetaStatsCommand", "normalize");
+               m->errorOut(e, "MetaStatsCommand", "driver");
                exit(1);
        }
 }
index 9c9f455b4e8b4f45d309e5453da5c41fbba813d7..1c3f10cf49d915cb243e102ec0a7170f3ba90985 100644 (file)
@@ -26,6 +26,13 @@ public:
        void help();
        
 private:
+       struct linePair {
+               int start;
+               int num;
+               linePair(int i, int j) : start(i), num(j) {}
+       };
+       vector<linePair> lines;
+       
        GlobalData* globaldata;
        GroupMap* designMap;
        ReadOTUFile* read;
@@ -34,13 +41,15 @@ private:
        
        bool abort, allLines, pickedGroups;
        set<string> labels; //holds labels to be used
-       string groups, label, outputDir, inputDir, designfile;
-       vector<string> Groups, outputNames;
-       int iters, g;
+       string groups, label, outputDir, inputDir, designfile, sets;
+       vector<string> Groups, outputNames, Sets;
+       vector< vector<string> > namesOfGroupCombos;
+       int iters, processors;
        float threshold;
        
        int process(vector<SharedRAbundVector*>&);
        int eliminateZeroOTUS(vector<SharedRAbundVector*>&);
+       int driver(int, int, vector<SharedRAbundVector*>&);
 };
 
 #endif