]> git.donarmstrong.com Git - mothur.git/blob - chimerabellerophoncommand.cpp
made classifier faster
[mothur.git] / chimerabellerophoncommand.cpp
1 /*
2  *  chimerabellerophoncommand.cpp
3  *  Mothur
4  *
5  *  Created by westcott on 4/1/10.
6  *  Copyright 2010 Schloss Lab. All rights reserved.
7  *
8  */
9
10 #include "chimerabellerophoncommand.h"
11 #include "bellerophon.h"
12
13 //***************************************************************************************************************
14
15 ChimeraBellerophonCommand::ChimeraBellerophonCommand(string option)  {
16         try {
17                 abort = false;
18                 
19                 //allow user to run help
20                 if(option == "help") { help(); abort = true; }
21                 
22                 else {
23                         //valid paramters for this command
24                         string Array[] =  {"fasta","filter","correction","processors","window","increment","outputdir","inputdir"};
25                         vector<string> myArray (Array, Array+(sizeof(Array)/sizeof(string)));
26                         
27                         OptionParser parser(option);
28                         map<string,string> parameters = parser.getParameters();
29                         
30                         ValidParameters validParameter;
31                         map<string,string>::iterator it;
32                         
33                         //check to make sure all parameters are valid for command
34                         for (it = parameters.begin(); it != parameters.end(); it++) { 
35                                 if (validParameter.isValidParameter(it->first, myArray, it->second) != true) {  abort = true;  }
36                         }
37                         
38                         //if the user changes the input directory command factory will send this info to us in the output parameter 
39                         string inputDir = validParameter.validFile(parameters, "inputdir", false);              
40                         if (inputDir == "not found"){   inputDir = "";          }
41                         else {
42                                 string path;
43                                 it = parameters.find("fasta");
44                                 //user has given a template file
45                                 if(it != parameters.end()){ 
46                                         path = hasPath(it->second);
47                                         //if the user has not given a path then, add inputdir. else leave path alone.
48                                         if (path == "") {       parameters["fasta"] = inputDir + it->second;            }
49                                 }
50                         }
51
52                         
53                         //check for required parameters
54                         fastafile = validParameter.validFile(parameters, "fasta", true);
55                         if (fastafile == "not open") { abort = true; }
56                         else if (fastafile == "not found") { fastafile = ""; m->mothurOut("fasta is a required parameter for the chimera.bellerophon command."); m->mothurOutEndLine(); abort = true;  }        
57                         
58                         //if the user changes the output directory command factory will send this info to us in the output parameter 
59                         outputDir = validParameter.validFile(parameters, "outputdir", false);           if (outputDir == "not found"){  
60                                 outputDir = ""; 
61                                 outputDir += hasPath(fastafile); //if user entered a file with a path then preserve it  
62                         }
63
64                         string temp;
65                         temp = validParameter.validFile(parameters, "filter", false);                   if (temp == "not found") { temp = "F"; }
66                         filter = isTrue(temp);
67                         
68                         temp = validParameter.validFile(parameters, "correction", false);               if (temp == "not found") { temp = "T"; }
69                         correction = isTrue(temp);
70                         
71                         temp = validParameter.validFile(parameters, "processors", false);               if (temp == "not found") { temp = "1"; }
72                         convert(temp, processors);
73                         
74                         temp = validParameter.validFile(parameters, "window", false);                   if (temp == "not found") { temp = "0"; }
75                         convert(temp, window);
76                         
77                         temp = validParameter.validFile(parameters, "increment", false);                if (temp == "not found") { temp = "25"; }
78                         convert(temp, increment);
79                 }
80         }
81         catch(exception& e) {
82                 m->errorOut(e, "ChimeraBellerophonCommand", "ChimeraBellerophonCommand");
83                 exit(1);
84         }
85 }
86 //**********************************************************************************************************************
87
88 void ChimeraBellerophonCommand::help(){
89         try {
90                 m->mothurOut("The chimera.bellerophon command reads a fastafile and creates list of potentially chimeric sequences.\n");
91                 m->mothurOut("The chimera.bellerophon command parameters are fasta, filter, correction, processors, window, increment. The fasta parameter is required.\n");
92                 m->mothurOut("The filter parameter allows you to specify if you would like to apply a vertical and 50% soft filter, default=false. \n");
93                 m->mothurOut("The correction parameter allows you to put more emphasis on the distance between highly similar sequences and less emphasis on the differences between remote homologs, default=true.\n");
94                 m->mothurOut("The processors parameter allows you to specify how many processors you would like to use.  The default is 1. \n");
95                 #ifdef USE_MPI
96                 m->mothurOut("When using MPI, the processors parameter is set to the number of MPI processes running. \n");
97                 #endif
98                 m->mothurOut("The window parameter allows you to specify the window size for searching for chimeras, default is 1/4 sequence length. \n");
99                 m->mothurOut("The increment parameter allows you to specify how far you move each window while finding chimeric sequences, default is 25.\n");
100                 m->mothurOut("chimera.bellerophon(fasta=yourFastaFile, filter=yourFilter, correction=yourCorrection, processors=yourProcessors) \n");
101                 m->mothurOut("Example: chimera.seqs(fasta=AD.align, filter=True, correction=true, window=200) \n");
102                 m->mothurOut("Note: No spaces between parameter labels (i.e. fasta), '=' and parameters (i.e.yourFastaFile).\n\n");     
103         }
104         catch(exception& e) {
105                 m->errorOut(e, "ChimeraBellerophonCommand", "help");
106                 exit(1);
107         }
108 }
109
110 //***************************************************************************************************************
111
112 ChimeraBellerophonCommand::~ChimeraBellerophonCommand(){        /*      do nothing      */      }
113
114 //***************************************************************************************************************
115
116 int ChimeraBellerophonCommand::execute(){
117         try{
118                 
119                 if (abort == true) { return 0; }
120                 
121                 int start = time(NULL); 
122                 
123                 chimera = new Bellerophon(fastafile, filter, correction, window, increment, processors, outputDir);     
124                                 
125                 string outputFileName = outputDir + getRootName(getSimpleName(fastafile)) +  "bellerophon.chimeras";
126                 string accnosFileName = outputDir + getRootName(getSimpleName(fastafile)) + "bellerophon.accnos";
127                 bool hasAccnos = true;
128                 
129                 chimera->getChimeras();
130                 
131                 if (m->control_pressed) { delete chimera;       return 0;       }
132                 
133         #ifdef USE_MPI
134                 MPI_File outMPI;
135                 MPI_File outMPIAccnos;
136                 
137                 int outMode=MPI_MODE_CREATE|MPI_MODE_WRONLY; 
138                                                 
139                 //char* outFilename = new char[accnosFileName.length()];
140                 //memcpy(outFilename, accnosFileName.c_str(), accnosFileName.length());
141                 
142                 char outFilename[1024];
143                 strcpy(outFilename, accnosFileName.c_str());
144
145                 //char* FileName = new char[outputFileName.length()];
146                 //memcpy(FileName, outputFileName.c_str(), outputFileName.length());
147                 
148                 char FileName[1024];
149                 strcpy(FileName, outputFileName.c_str());
150
151                 MPI_File_open(MPI_COMM_WORLD, FileName, outMode, MPI_INFO_NULL, &outMPI);  //comm, filename, mode, info, filepointer
152                 MPI_File_open(MPI_COMM_WORLD, outFilename, outMode, MPI_INFO_NULL, &outMPIAccnos);
153                 
154                 //delete FileName;
155                 //delete outFilename;
156
157                 numSeqs = chimera->print(outMPI, outMPIAccnos);
158                 
159                 MPI_File_close(&outMPI);
160                 MPI_File_close(&outMPIAccnos);
161
162         #else
163         
164                 ofstream out;
165                 openOutputFile(outputFileName, out);
166                 
167                 ofstream out2;
168                 openOutputFile(accnosFileName, out2);
169                 
170                 numSeqs = chimera->print(out, out2);
171                 out.close();
172                 out2.close(); 
173                 
174         #endif
175                 
176                 if (m->control_pressed) { remove(accnosFileName.c_str()); remove(outputFileName.c_str()); delete chimera;       return 0;       }
177                 
178                 //delete accnos file if its blank 
179                 if (isBlank(accnosFileName)) {  remove(accnosFileName.c_str());  hasAccnos = false; }
180                 
181                 m->mothurOutEndLine();
182                 m->mothurOut("Output File Names: "); m->mothurOutEndLine();
183                 m->mothurOut(outputFileName); m->mothurOutEndLine();    
184                 if (hasAccnos) {  m->mothurOut(accnosFileName); m->mothurOutEndLine();  }
185                 m->mothurOutEndLine();
186                 m->mothurOutEndLine(); m->mothurOut("It took " + toString(time(NULL) - start) + " secs to check " + toString(numSeqs) + " sequences."); m->mothurOutEndLine();
187                 
188                 delete chimera;
189                 
190                 return 0;
191                                 
192         }
193         catch(exception& e) {
194                 m->errorOut(e, "ChimeraBellerophonCommand", "execute");
195                 exit(1);
196         }
197 }
198 //**********************************************************************************************************************
199