2 * chimeracheckcommand.cpp
5 * Created by westcott on 3/31/10.
6 * Copyright 2010 Schloss Lab. All rights reserved.
10 #include "chimeracheckcommand.h"
11 #include "chimeracheckrdp.h"
13 //***************************************************************************************************************
15 ChimeraCheckCommand::ChimeraCheckCommand(string option) {
19 //allow user to run help
20 if(option == "help") { help(); abort = true; }
23 //valid paramters for this command
24 string Array[] = {"fasta","processors","increment","template","ksize","svg", "name","outputdir","inputdir" };
25 vector<string> myArray (Array, Array+(sizeof(Array)/sizeof(string)));
27 OptionParser parser(option);
28 map<string,string> parameters = parser.getParameters();
30 ValidParameters validParameter;
31 map<string,string>::iterator it;
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; }
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 = ""; }
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; }
51 it = parameters.find("template");
52 //user has given a template file
53 if(it != parameters.end()){
54 path = hasPath(it->second);
55 //if the user has not given a path then, add inputdir. else leave path alone.
56 if (path == "") { parameters["template"] = inputDir + it->second; }
59 it = parameters.find("name");
60 //user has given a template file
61 if(it != parameters.end()){
62 path = hasPath(it->second);
63 //if the user has not given a path then, add inputdir. else leave path alone.
64 if (path == "") { parameters["name"] = inputDir + it->second; }
69 //check for required parameters
70 fastafile = validParameter.validFile(parameters, "fasta", true);
71 if (fastafile == "not open") { abort = true; }
72 else if (fastafile == "not found") { fastafile = ""; m->mothurOut("fasta is a required parameter for the chimera.check command."); m->mothurOutEndLine(); abort = true; }
74 //if the user changes the output directory command factory will send this info to us in the output parameter
75 outputDir = validParameter.validFile(parameters, "outputdir", false); if (outputDir == "not found"){
77 outputDir += hasPath(fastafile); //if user entered a file with a path then preserve it
80 templatefile = validParameter.validFile(parameters, "template", true);
81 if (templatefile == "not open") { abort = true; }
82 else if (templatefile == "not found") { templatefile = ""; m->mothurOut("template is a required parameter for the chimera.check command."); m->mothurOutEndLine(); abort = true; }
84 namefile = validParameter.validFile(parameters, "name", true);
85 if (namefile == "not open") { abort = true; }
86 else if (namefile == "not found") { namefile = ""; }
88 string temp = validParameter.validFile(parameters, "processors", false); if (temp == "not found") { temp = "1"; }
89 convert(temp, processors);
91 temp = validParameter.validFile(parameters, "ksize", false); if (temp == "not found") { temp = "7"; }
94 temp = validParameter.validFile(parameters, "svg", false); if (temp == "not found") { temp = "F"; }
97 temp = validParameter.validFile(parameters, "increment", false); if (temp == "not found") { temp = "10"; }
98 convert(temp, increment);
101 catch(exception& e) {
102 m->errorOut(e, "ChimeraCheckCommand", "ChimeraCheckCommand");
106 //**********************************************************************************************************************
108 void ChimeraCheckCommand::help(){
111 m->mothurOut("The chimera.check command reads a fastafile and templatefile and outputs potentially chimeric sequences.\n");
112 m->mothurOut("This command was created using the algorythms described in CHIMERA_CHECK version 2.7 written by Niels Larsen. \n");
113 m->mothurOut("The chimera.check command parameters are fasta, template, processors, ksize, increment, svg and name.\n");
114 m->mothurOut("The fasta parameter allows you to enter the fasta file containing your potentially chimeric sequences, and is required. \n");
115 m->mothurOut("The template parameter allows you to enter a template file containing known non-chimeric sequences, and is required. \n");
116 m->mothurOut("The processors parameter allows you to specify how many processors you would like to use. The default is 1. \n");
118 m->mothurOut("When using MPI, the processors parameter is set to the number of MPI processes running. \n");
120 m->mothurOut("The increment parameter allows you to specify how far you move each window while finding chimeric sequences, default is 10.\n");
121 m->mothurOut("The ksize parameter allows you to input kmersize, default is 7. \n");
122 m->mothurOut("The svg parameter allows you to specify whether or not you would like a svg file outputted for each query sequence, default is False.\n");
123 m->mothurOut("The name parameter allows you to enter a file containing names of sequences you would like .svg files for.\n");
124 m->mothurOut("The chimera.check command should be in the following format: \n");
125 m->mothurOut("chimera.check(fasta=yourFastaFile, template=yourTemplateFile, processors=yourProcessors, ksize=yourKmerSize) \n");
126 m->mothurOut("Example: chimera.check(fasta=AD.fasta, template=core_set_aligned,imputed.fasta, processors=4, ksize=8) \n");
127 m->mothurOut("Note: No spaces between parameter labels (i.e. fasta), '=' and parameters (i.e.yourFastaFile).\n\n");
129 catch(exception& e) {
130 m->errorOut(e, "ChimeraCheckCommand", "help");
135 //***************************************************************************************************************
137 ChimeraCheckCommand::~ChimeraCheckCommand(){ /* do nothing */ }
139 //***************************************************************************************************************
141 int ChimeraCheckCommand::execute(){
144 if (abort == true) { return 0; }
146 int start = time(NULL);
148 chimera = new ChimeraCheckRDP(fastafile, templatefile, namefile, svg, increment, ksize, outputDir);
150 if (m->control_pressed) { delete chimera; return 0; }
152 string outputFileName = outputDir + getRootName(getSimpleName(fastafile)) + "chimeracheck.chimeras";
156 int pid, end, numSeqsPerProcessor;
161 MPI_Comm_rank(MPI_COMM_WORLD, &pid); //find out who we are
162 MPI_Comm_size(MPI_COMM_WORLD, &processors);
167 int outMode=MPI_MODE_CREATE|MPI_MODE_WRONLY;
168 int inMode=MPI_MODE_RDONLY;
170 //char* outFilename = new char[outputFileName.length()];
171 //memcpy(outFilename, outputFileName.c_str(), outputFileName.length());
173 char outFilename[1024];
174 strcpy(outFilename, outputFileName.c_str());
176 //char* inFileName = new char[fastafile.length()];
177 //memcpy(inFileName, fastafile.c_str(), fastafile.length());
179 char inFileName[1024];
180 strcpy(inFileName, fastafile.c_str());
182 MPI_File_open(MPI_COMM_WORLD, inFileName, inMode, MPI_INFO_NULL, &inMPI); //comm, filename, mode, info, filepointer
183 MPI_File_open(MPI_COMM_WORLD, outFilename, outMode, MPI_INFO_NULL, &outMPI);
185 //delete outFilename;
188 if (m->control_pressed) { MPI_File_close(&inMPI); MPI_File_close(&outMPI); delete chimera; return 0; }
190 if (pid == 0) { //you are the root process
191 MPIPos = setFilePosFasta(fastafile, numSeqs); //fills MPIPos, returns numSeqs
193 //send file positions to all processes
194 MPI_Bcast(&numSeqs, 1, MPI_INT, 0, MPI_COMM_WORLD); //send numSeqs
195 MPI_Bcast(&MPIPos[0], (numSeqs+1), MPI_LONG, 0, MPI_COMM_WORLD); //send file pos
197 //figure out how many sequences you have to align
198 numSeqsPerProcessor = numSeqs / processors;
199 int startIndex = pid * numSeqsPerProcessor;
200 if(pid == (processors - 1)){ numSeqsPerProcessor = numSeqs - pid * numSeqsPerProcessor; }
204 driverMPI(startIndex, numSeqsPerProcessor, inMPI, outMPI, MPIPos);
206 if (m->control_pressed) { MPI_File_close(&inMPI); MPI_File_close(&outMPI); remove(outputFileName.c_str()); delete chimera; return 0; }
209 for(int i = 1; i < processors; i++) {
211 MPI_Recv(buf, 4, MPI_CHAR, i, tag, MPI_COMM_WORLD, &status);
213 }else{ //you are a child process
214 MPI_Bcast(&numSeqs, 1, MPI_INT, 0, MPI_COMM_WORLD); //get numSeqs
215 MPIPos.resize(numSeqs+1);
216 MPI_Bcast(&MPIPos[0], (numSeqs+1), MPI_LONG, 0, MPI_COMM_WORLD); //get file positions
218 //figure out how many sequences you have to align
219 numSeqsPerProcessor = numSeqs / processors;
220 int startIndex = pid * numSeqsPerProcessor;
221 if(pid == (processors - 1)){ numSeqsPerProcessor = numSeqs - pid * numSeqsPerProcessor; }
225 driverMPI(startIndex, numSeqsPerProcessor, inMPI, outMPI, MPIPos);
227 if (m->control_pressed) { MPI_File_close(&inMPI); MPI_File_close(&outMPI); delete chimera; return 0; }
229 //tell parent you are done.
232 MPI_Send(buf, 4, MPI_CHAR, 0, tag, MPI_COMM_WORLD);
236 MPI_File_close(&inMPI);
237 MPI_File_close(&outMPI);
241 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux)
244 openInputFile(fastafile, inFASTA);
245 numSeqs=count(istreambuf_iterator<char>(inFASTA),istreambuf_iterator<char>(), '>');
248 lines.push_back(new linePair(0, numSeqs));
250 driver(lines[0], outputFileName, fastafile);
252 if (m->control_pressed) {
253 remove(outputFileName.c_str());
254 for (int i = 0; i < lines.size(); i++) { delete lines[i]; } lines.clear();
260 vector<int> positions;
261 processIDS.resize(0);
264 openInputFile(fastafile, inFASTA);
267 while(!inFASTA.eof()){
268 input = getline(inFASTA);
269 if (input.length() != 0) {
270 if(input[0] == '>'){ long int pos = inFASTA.tellg(); positions.push_back(pos - input.length() - 1); }
275 numSeqs = positions.size();
277 int numSeqsPerProcessor = numSeqs / processors;
279 for (int i = 0; i < processors; i++) {
280 long int startPos = positions[ i * numSeqsPerProcessor ];
281 if(i == processors - 1){
282 numSeqsPerProcessor = numSeqs - i * numSeqsPerProcessor;
284 lines.push_back(new linePair(startPos, numSeqsPerProcessor));
288 createProcesses(outputFileName, fastafile);
290 rename((outputFileName + toString(processIDS[0]) + ".temp").c_str(), outputFileName.c_str());
292 //append output files
293 for(int i=1;i<processors;i++){
294 appendFiles((outputFileName + toString(processIDS[i]) + ".temp"), outputFileName);
295 remove((outputFileName + toString(processIDS[i]) + ".temp").c_str());
298 if (m->control_pressed) {
299 remove(outputFileName.c_str());
300 for (int i = 0; i < lines.size(); i++) { delete lines[i]; } lines.clear();
308 openInputFile(fastafile, inFASTA);
309 numSeqs=count(istreambuf_iterator<char>(inFASTA),istreambuf_iterator<char>(), '>');
311 lines.push_back(new linePair(0, numSeqs));
313 driver(lines[0], outputFileName, fastafile);
315 if (m->control_pressed) {
316 remove(outputFileName.c_str());
317 for (int i = 0; i < lines.size(); i++) { delete lines[i]; } lines.clear();
324 for (int i = 0; i < lines.size(); i++) { delete lines[i]; } lines.clear();
326 m->mothurOutEndLine(); m->mothurOut("This method does not determine if a sequence is chimeric, but allows you to make that determination based on the IS values."); m->mothurOutEndLine();
328 m->mothurOutEndLine();
329 m->mothurOut("Output File Names: "); m->mothurOutEndLine();
330 m->mothurOut(outputFileName); m->mothurOutEndLine();
331 m->mothurOutEndLine();
332 m->mothurOutEndLine(); m->mothurOut("It took " + toString(time(NULL) - start) + " secs to check " + toString(numSeqs) + " sequences."); m->mothurOutEndLine();
337 catch(exception& e) {
338 m->errorOut(e, "ChimeraCheckCommand", "execute");
342 //**********************************************************************************************************************
344 int ChimeraCheckCommand::driver(linePair* line, string outputFName, string filename){
347 openOutputFile(outputFName, out);
352 openInputFile(filename, inFASTA);
354 inFASTA.seekg(line->start);
356 for(int i=0;i<line->numSeqs;i++){
358 if (m->control_pressed) { return 1; }
360 Sequence* candidateSeq = new Sequence(inFASTA); gobble(inFASTA);
362 if (candidateSeq->getName() != "") { //incase there is a commented sequence at the end of a file
364 chimera->getChimeras(candidateSeq);
366 if (m->control_pressed) { delete candidateSeq; return 1; }
369 chimera->print(out, out2);
374 if((i+1) % 100 == 0){ m->mothurOut("Processing sequence: " + toString(i+1)); m->mothurOutEndLine(); }
377 if((line->numSeqs) % 100 != 0){ m->mothurOut("Processing sequence: " + toString(line->numSeqs)); m->mothurOutEndLine(); }
384 catch(exception& e) {
385 m->errorOut(e, "ChimeraCheckCommand", "driver");
389 //**********************************************************************************************************************
391 int ChimeraCheckCommand::driverMPI(int start, int num, MPI_File& inMPI, MPI_File& outMPI, vector<long>& MPIPos){
396 MPI_Comm_rank(MPI_COMM_WORLD, &pid); //find out who we are
398 for(int i=0;i<num;i++){
400 if (m->control_pressed) { return 0; }
403 int length = MPIPos[start+i+1] - MPIPos[start+i];
405 char* buf4 = new char[length];
406 MPI_File_read_at(inMPI, MPIPos[start+i], buf4, length, MPI_CHAR, &status);
408 string tempBuf = buf4;
409 if (tempBuf.length() > length) { tempBuf = tempBuf.substr(0, length); }
410 istringstream iss (tempBuf,istringstream::in);
413 Sequence* candidateSeq = new Sequence(iss); gobble(iss);
415 if (candidateSeq->getName() != "") { //incase there is a commented sequence at the end of a file
417 chimera->getChimeras(candidateSeq);
420 chimera->print(outMPI, outAccMPI);
425 if((i+1) % 100 == 0){ cout << "Processing sequence: " << (i+1) << endl; m->mothurOutJustToLog("Processing sequence: " + toString(i+1) + "\n"); }
428 if(num % 100 != 0){ cout << "Processing sequence: " << num << endl; m->mothurOutJustToLog("Processing sequence: " + toString(num) + "\n"); }
432 catch(exception& e) {
433 m->errorOut(e, "ChimeraCheckCommand", "driverMPI");
439 /**************************************************************************************************/
441 int ChimeraCheckCommand::createProcesses(string outputFileName, string filename) {
443 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux)
445 // processIDS.resize(0);
447 //loop through and create all the processes you want
448 while (process != processors) {
452 processIDS.push_back(pid); //create map from line number to pid so you can append files in correct order later
455 driver(lines[process], outputFileName + toString(getpid()) + ".temp", filename);
457 }else { m->mothurOut("unable to spawn the necessary processes."); m->mothurOutEndLine(); exit(0); }
460 //force parent to wait until all the processes are done
461 for (int i=0;i<processors;i++) {
462 int temp = processIDS[i];
469 catch(exception& e) {
470 m->errorOut(e, "ChimeraCheckCommand", "createProcesses");
474 /**************************************************************************************************/