]> git.donarmstrong.com Git - mothur.git/blob - chimerabellerophoncommand.cpp
69eb89533b4988e598c000b8bf5b8a111e07e5e1
[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.\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()];\r
140                 memcpy(outFilename, accnosFileName.c_str(), accnosFileName.length());
141
142                 char* FileName = new char[outputFileName.length()];\r
143                 memcpy(FileName, outputFileName.c_str(), outputFileName.length());
144
145
146                 MPI_File_open(MPI_COMM_WORLD, FileName, outMode, MPI_INFO_NULL, &outMPI);  //comm, filename, mode, info, filepointer
147                 MPI_File_open(MPI_COMM_WORLD, outFilename, outMode, MPI_INFO_NULL, &outMPIAccnos);
148                 
149                 delete FileName;
150                 delete outFilename;
151
152                 numSeqs = chimera->print(outMPI, outMPIAccnos);
153                 
154                 MPI_File_close(&outMPI);
155                 MPI_File_close(&outMPIAccnos);
156
157         #else
158         
159                 ofstream out;
160                 openOutputFile(outputFileName, out);
161                 
162                 ofstream out2;
163                 openOutputFile(accnosFileName, out2);
164                 
165                 numSeqs = chimera->print(out, out2);
166                 out.close();
167                 out2.close(); 
168                 
169         #endif
170                 
171                 if (m->control_pressed) { remove(accnosFileName.c_str()); remove(outputFileName.c_str()); delete chimera;       return 0;       }
172                 
173                 //delete accnos file if its blank 
174                 if (isBlank(accnosFileName)) {  remove(accnosFileName.c_str());  hasAccnos = false; }
175                 
176                 m->mothurOutEndLine();
177                 m->mothurOut("Output File Names: "); m->mothurOutEndLine();
178                 m->mothurOut(outputFileName); m->mothurOutEndLine();    
179                 if (hasAccnos) {  m->mothurOut(accnosFileName); m->mothurOutEndLine();  }
180                 m->mothurOutEndLine();
181                 m->mothurOutEndLine(); m->mothurOut("It took " + toString(time(NULL) - start) + " secs to check " + toString(numSeqs) + " sequences."); m->mothurOutEndLine();
182                 
183                 delete chimera;
184                 
185                 return 0;
186                                 
187         }
188         catch(exception& e) {
189                 m->errorOut(e, "ChimeraBellerophonCommand", "execute");
190                 exit(1);
191         }
192 }
193 //**********************************************************************************************************************
194