2 * chimeracheckcommand.cpp
5 * Created by westcott on 3/31/10.
6 * Copyright 2010 Schloss Lab. All rights reserved.
10 #include "chimeracheckcommand.h"
12 //***************************************************************************************************************
14 ChimeraCheckCommand::ChimeraCheckCommand(string option) {
18 //allow user to run help
19 if(option == "help") { help(); abort = true; }
22 //valid paramters for this command
23 string Array[] = {"fasta","processors","increment","template","ksize","svg", "name","outputdir","inputdir" };
24 vector<string> myArray (Array, Array+(sizeof(Array)/sizeof(string)));
26 OptionParser parser(option);
27 map<string,string> parameters = parser.getParameters();
29 ValidParameters validParameter("chimera.check");
30 map<string,string>::iterator it;
32 //check to make sure all parameters are valid for command
33 for (it = parameters.begin(); it != parameters.end(); it++) {
34 if (validParameter.isValidParameter(it->first, myArray, it->second) != true) { abort = true; }
37 //if the user changes the input directory command factory will send this info to us in the output parameter
38 string inputDir = validParameter.validFile(parameters, "inputdir", false);
39 if (inputDir == "not found"){ inputDir = ""; }
41 it = parameters.find("template");
42 //user has given a template file
43 if(it != parameters.end()){
44 string path = hasPath(it->second);
45 //if the user has not given a path then, add inputdir. else leave path alone.
46 if (path == "") { parameters["template"] = inputDir + it->second; }
50 //check for required parameters
51 fastafile = validParameter.validFile(parameters, "fasta", false);
52 if (fastafile == "not found") { fastafile = ""; m->mothurOut("fasta is a required parameter for the chimera.check command."); m->mothurOutEndLine(); abort = true; }
54 splitAtDash(fastafile, fastaFileNames);
56 //go through files and make sure they are good, if not, then disregard them
57 for (int i = 0; i < fastaFileNames.size(); i++) {
59 string path = hasPath(fastaFileNames[i]);
60 //if the user has not given a path then, add inputdir. else leave path alone.
61 if (path == "") { fastaFileNames[i] = inputDir + fastaFileNames[i]; }
67 ableToOpen = openInputFile(fastaFileNames[i], in, "noerror");
69 //if you can't open it, try default location
70 if (ableToOpen == 1) {
71 if (m->getDefaultPath() != "") { //default path is set
72 string tryPath = m->getDefaultPath() + getSimpleName(fastaFileNames[i]);
73 m->mothurOut("Unable to open " + fastaFileNames[i] + ". Trying default " + tryPath); m->mothurOutEndLine();
74 ableToOpen = openInputFile(tryPath, in, "noerror");
75 fastaFileNames[i] = tryPath;
80 if (ableToOpen == 1) {
81 m->mothurOut("Unable to open " + fastaFileNames[i] +". It will be disregarded."); m->mothurOutEndLine();
82 //erase from file list
83 fastaFileNames.erase(fastaFileNames.begin()+i);
88 //make sure there is at least one valid file left
89 if (fastaFileNames.size() == 0) { m->mothurOut("no valid files."); m->mothurOutEndLine(); abort = true; }
92 //if the user changes the output directory command factory will send this info to us in the output parameter
93 outputDir = validParameter.validFile(parameters, "outputdir", false); if (outputDir == "not found"){ outputDir = ""; }
95 templatefile = validParameter.validFile(parameters, "template", true);
96 if (templatefile == "not open") { abort = true; }
97 else if (templatefile == "not found") { templatefile = ""; m->mothurOut("template is a required parameter for the chimera.check command."); m->mothurOutEndLine(); abort = true; }
99 namefile = validParameter.validFile(parameters, "name", false);
100 if (namefile == "not found") { namefile = ""; }
102 splitAtDash(namefile, nameFileNames);
104 //go through files and make sure they are good, if not, then disregard them
105 for (int i = 0; i < nameFileNames.size(); i++) {
106 if (inputDir != "") {
107 string path = hasPath(nameFileNames[i]);
108 //if the user has not given a path then, add inputdir. else leave path alone.
109 if (path == "") { nameFileNames[i] = inputDir + nameFileNames[i]; }
115 ableToOpen = openInputFile(nameFileNames[i], in, "noerror");
117 //if you can't open it, try default location
118 if (ableToOpen == 1) {
119 if (m->getDefaultPath() != "") { //default path is set
120 string tryPath = m->getDefaultPath() + getSimpleName(nameFileNames[i]);
121 m->mothurOut("Unable to open " + nameFileNames[i] + ". Trying default " + tryPath); m->mothurOutEndLine();
122 ableToOpen = openInputFile(tryPath, in, "noerror");
123 nameFileNames[i] = tryPath;
128 if (ableToOpen == 1) {
129 m->mothurOut("Unable to open " + nameFileNames[i] + ". It will be disregarded."); m->mothurOutEndLine();
130 //erase from file list
131 nameFileNames.erase(nameFileNames.begin()+i);
137 //make sure there is at least one valid file left
138 if (nameFileNames.size() != 0) {
139 if (nameFileNames.size() != fastaFileNames.size()) {
140 m->mothurOut("Different number of valid name files and fasta files, aborting command."); m->mothurOutEndLine();
146 string temp = validParameter.validFile(parameters, "processors", false); if (temp == "not found") { temp = "1"; }
147 convert(temp, processors);
149 temp = validParameter.validFile(parameters, "ksize", false); if (temp == "not found") { temp = "7"; }
150 convert(temp, ksize);
152 temp = validParameter.validFile(parameters, "svg", false); if (temp == "not found") { temp = "F"; }
154 if (nameFileNames.size() != 0) { svg = true; }
156 temp = validParameter.validFile(parameters, "increment", false); if (temp == "not found") { temp = "10"; }
157 convert(temp, increment);
160 catch(exception& e) {
161 m->errorOut(e, "ChimeraCheckCommand", "ChimeraCheckCommand");
165 //**********************************************************************************************************************
167 void ChimeraCheckCommand::help(){
170 m->mothurOut("The chimera.check command reads a fastafile and templatefile and outputs potentially chimeric sequences.\n");
171 m->mothurOut("This command was created using the algorythms described in CHIMERA_CHECK version 2.7 written by Niels Larsen. \n");
172 m->mothurOut("The chimera.check command parameters are fasta, template, processors, ksize, increment, svg and name.\n");
173 m->mothurOut("The fasta parameter allows you to enter the fasta file containing your potentially chimeric sequences, and is required. \n");
174 m->mothurOut("You may enter multiple fasta files by separating their names with dashes. ie. fasta=abrecovery.fasta-amzon.fasta \n");
175 m->mothurOut("The template parameter allows you to enter a template file containing known non-chimeric sequences, and is required. \n");
176 m->mothurOut("The processors parameter allows you to specify how many processors you would like to use. The default is 1. \n");
178 m->mothurOut("When using MPI, the processors parameter is set to the number of MPI processes running. \n");
180 m->mothurOut("The increment parameter allows you to specify how far you move each window while finding chimeric sequences, default is 10.\n");
181 m->mothurOut("The ksize parameter allows you to input kmersize, default is 7. \n");
182 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");
183 m->mothurOut("The name parameter allows you to enter a file containing names of sequences you would like .svg files for.\n");
184 m->mothurOut("You may enter multiple name files by separating their names with dashes. ie. fasta=abrecovery.svg.names-amzon.svg.names \n");
185 m->mothurOut("The chimera.check command should be in the following format: \n");
186 m->mothurOut("chimera.check(fasta=yourFastaFile, template=yourTemplateFile, processors=yourProcessors, ksize=yourKmerSize) \n");
187 m->mothurOut("Example: chimera.check(fasta=AD.fasta, template=core_set_aligned,imputed.fasta, processors=4, ksize=8) \n");
188 m->mothurOut("Note: No spaces between parameter labels (i.e. fasta), '=' and parameters (i.e.yourFastaFile).\n\n");
190 catch(exception& e) {
191 m->errorOut(e, "ChimeraCheckCommand", "help");
196 //***************************************************************************************************************
198 ChimeraCheckCommand::~ChimeraCheckCommand(){ /* do nothing */ }
200 //***************************************************************************************************************
202 int ChimeraCheckCommand::execute(){
205 if (abort == true) { return 0; }
207 for (int i = 0; i < fastaFileNames.size(); i++) {
209 m->mothurOut("Checking sequences from " + fastaFileNames[i] + " ..." ); m->mothurOutEndLine();
211 int start = time(NULL);
213 string thisNameFile = "";
214 if (nameFileNames.size() != 0) { thisNameFile = nameFileNames[i]; }
216 chimera = new ChimeraCheckRDP(fastaFileNames[i], templatefile, thisNameFile, svg, increment, ksize, outputDir);
218 if (m->control_pressed) { delete chimera; return 0; }
220 string outputFileName = outputDir + getRootName(getSimpleName(fastaFileNames[i])) + "chimeracheck.chimeras";
221 outputNames.push_back(outputFileName);
225 int pid, end, numSeqsPerProcessor;
227 vector<unsigned long int> MPIPos;
230 MPI_Comm_rank(MPI_COMM_WORLD, &pid); //find out who we are
231 MPI_Comm_size(MPI_COMM_WORLD, &processors);
236 int outMode=MPI_MODE_CREATE|MPI_MODE_WRONLY;
237 int inMode=MPI_MODE_RDONLY;
239 char outFilename[1024];
240 strcpy(outFilename, outputFileName.c_str());
242 char inFileName[1024];
243 strcpy(inFileName, fastaFileNames[i].c_str());
245 MPI_File_open(MPI_COMM_WORLD, inFileName, inMode, MPI_INFO_NULL, &inMPI); //comm, filename, mode, info, filepointer
246 MPI_File_open(MPI_COMM_WORLD, outFilename, outMode, MPI_INFO_NULL, &outMPI);
248 if (m->control_pressed) { MPI_File_close(&inMPI); MPI_File_close(&outMPI); for (int j = 0; j < outputNames.size(); j++) { remove(outputNames[j].c_str()); } delete chimera; return 0; }
250 if (pid == 0) { //you are the root process
251 MPIPos = setFilePosFasta(fastaFileNames[i], numSeqs); //fills MPIPos, returns numSeqs
253 //send file positions to all processes
254 for(int j = 1; j < processors; j++) {
255 MPI_Send(&numSeqs, 1, MPI_INT, j, tag, MPI_COMM_WORLD);
256 MPI_Send(&MPIPos[0], (numSeqs+1), MPI_LONG, j, tag, MPI_COMM_WORLD);
259 //figure out how many sequences you have to align
260 numSeqsPerProcessor = numSeqs / processors;
261 int startIndex = pid * numSeqsPerProcessor;
262 if(pid == (processors - 1)){ numSeqsPerProcessor = numSeqs - pid * numSeqsPerProcessor; }
266 driverMPI(startIndex, numSeqsPerProcessor, inMPI, outMPI, MPIPos);
268 if (m->control_pressed) { MPI_File_close(&inMPI); MPI_File_close(&outMPI); for (int j = 0; j < outputNames.size(); j++) { remove(outputNames[j].c_str()); } delete chimera; return 0; }
271 for(int j = 1; j < processors; j++) {
273 MPI_Recv(buf, 4, MPI_CHAR, j, tag, MPI_COMM_WORLD, &status);
275 }else{ //you are a child process
276 MPI_Recv(&numSeqs, 1, MPI_INT, 0, tag, MPI_COMM_WORLD, &status);
277 MPIPos.resize(numSeqs+1);
278 MPI_Recv(&MPIPos[0], (numSeqs+1), MPI_LONG, 0, tag, MPI_COMM_WORLD, &status);
280 //figure out how many sequences you have to align
281 numSeqsPerProcessor = numSeqs / processors;
282 int startIndex = pid * numSeqsPerProcessor;
283 if(pid == (processors - 1)){ numSeqsPerProcessor = numSeqs - pid * numSeqsPerProcessor; }
286 driverMPI(startIndex, numSeqsPerProcessor, inMPI, outMPI, MPIPos);
288 if (m->control_pressed) { MPI_File_close(&inMPI); MPI_File_close(&outMPI); for (int j = 0; j < outputNames.size(); j++) { remove(outputNames[j].c_str()); } delete chimera; return 0; }
290 //tell parent you are done.
293 MPI_Send(buf, 4, MPI_CHAR, 0, tag, MPI_COMM_WORLD);
297 MPI_File_close(&inMPI);
298 MPI_File_close(&outMPI);
299 MPI_Barrier(MPI_COMM_WORLD); //make everyone wait - just in case
303 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux)
306 openInputFile(fastaFileNames[i], inFASTA);
307 getNumSeqs(inFASTA, numSeqs);
310 lines.push_back(new linePair(0, numSeqs));
312 driver(lines[0], outputFileName, fastaFileNames[i]);
314 if (m->control_pressed) {
315 for (int j = 0; j < outputNames.size(); j++) { remove(outputNames[j].c_str()); }
316 for (int j = 0; j < lines.size(); j++) { delete lines[j]; } lines.clear();
322 vector<unsigned long int> positions;
323 processIDS.resize(0);
326 openInputFile(fastaFileNames[i], inFASTA);
329 while(!inFASTA.eof()){
330 input = getline(inFASTA);
331 if (input.length() != 0) {
332 if(input[0] == '>'){ unsigned long int pos = inFASTA.tellg(); positions.push_back(pos - input.length() - 1); }
337 numSeqs = positions.size();
339 int numSeqsPerProcessor = numSeqs / processors;
341 for (int j = 0; j < processors; j++) {
342 unsigned long int startPos = positions[ j * numSeqsPerProcessor ];
343 if(j == processors - 1){
344 numSeqsPerProcessor = numSeqs - j * numSeqsPerProcessor;
346 lines.push_back(new linePair(startPos, numSeqsPerProcessor));
350 createProcesses(outputFileName, fastaFileNames[i]);
352 rename((outputFileName + toString(processIDS[0]) + ".temp").c_str(), outputFileName.c_str());
354 //append output files
355 for(int j=1;j<processors;j++){
356 appendFiles((outputFileName + toString(processIDS[j]) + ".temp"), outputFileName);
357 remove((outputFileName + toString(processIDS[j]) + ".temp").c_str());
360 if (m->control_pressed) {
361 for (int j = 0; j < outputNames.size(); j++) { remove(outputNames[j].c_str()); }
362 for (int j = 0; j < lines.size(); j++) { delete lines[j]; } lines.clear();
370 openInputFile(fastaFileNames[i], inFASTA);
371 getNumSeqs(inFASTA, numSeqs);
373 lines.push_back(new linePair(0, numSeqs));
375 driver(lines[0], outputFileName, fastaFileNames[i]);
377 if (m->control_pressed) {
378 for (int j = 0; j < lines.size(); j++) { delete lines[j]; } lines.clear();
379 for (int j = 0; j < outputNames.size(); j++) { remove(outputNames[j].c_str()); }
386 for (int j = 0; j < lines.size(); j++) { delete lines[j]; } lines.clear();
388 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();
389 m->mothurOutEndLine(); m->mothurOut("It took " + toString(time(NULL) - start) + " secs to check " + toString(numSeqs) + " sequences."); m->mothurOutEndLine(); m->mothurOutEndLine();
393 m->mothurOutEndLine();
394 m->mothurOut("Output File Names: "); m->mothurOutEndLine();
395 for (int i = 0; i < outputNames.size(); i++) { m->mothurOut(outputNames[i]); m->mothurOutEndLine(); }
396 m->mothurOutEndLine();
401 catch(exception& e) {
402 m->errorOut(e, "ChimeraCheckCommand", "execute");
406 //**********************************************************************************************************************
408 int ChimeraCheckCommand::driver(linePair* line, string outputFName, string filename){
411 openOutputFile(outputFName, out);
416 openInputFile(filename, inFASTA);
418 inFASTA.seekg(line->start);
420 for(int i=0;i<line->numSeqs;i++){
422 if (m->control_pressed) { return 1; }
424 Sequence* candidateSeq = new Sequence(inFASTA); gobble(inFASTA);
426 if (candidateSeq->getName() != "") { //incase there is a commented sequence at the end of a file
428 chimera->getChimeras(candidateSeq);
430 if (m->control_pressed) { delete candidateSeq; return 1; }
433 chimera->print(out, out2);
438 if((i+1) % 100 == 0){ m->mothurOut("Processing sequence: " + toString(i+1)); m->mothurOutEndLine(); }
441 if((line->numSeqs) % 100 != 0){ m->mothurOut("Processing sequence: " + toString(line->numSeqs)); m->mothurOutEndLine(); }
448 catch(exception& e) {
449 m->errorOut(e, "ChimeraCheckCommand", "driver");
453 //**********************************************************************************************************************
455 int ChimeraCheckCommand::driverMPI(int start, int num, MPI_File& inMPI, MPI_File& outMPI, vector<unsigned long int>& MPIPos){
460 MPI_Comm_rank(MPI_COMM_WORLD, &pid); //find out who we are
462 for(int i=0;i<num;i++){
464 if (m->control_pressed) { return 0; }
467 int length = MPIPos[start+i+1] - MPIPos[start+i];
469 char* buf4 = new char[length];
470 MPI_File_read_at(inMPI, MPIPos[start+i], buf4, length, MPI_CHAR, &status);
472 string tempBuf = buf4;
473 if (tempBuf.length() > length) { tempBuf = tempBuf.substr(0, length); }
474 istringstream iss (tempBuf,istringstream::in);
477 Sequence* candidateSeq = new Sequence(iss); gobble(iss);
479 if (candidateSeq->getName() != "") { //incase there is a commented sequence at the end of a file
481 chimera->getChimeras(candidateSeq);
484 chimera->print(outMPI, outAccMPI);
489 if((i+1) % 100 == 0){ cout << "Processing sequence: " << (i+1) << endl; m->mothurOutJustToLog("Processing sequence: " + toString(i+1) + "\n"); }
492 if(num % 100 != 0){ cout << "Processing sequence: " << num << endl; m->mothurOutJustToLog("Processing sequence: " + toString(num) + "\n"); }
496 catch(exception& e) {
497 m->errorOut(e, "ChimeraCheckCommand", "driverMPI");
503 /**************************************************************************************************/
505 int ChimeraCheckCommand::createProcesses(string outputFileName, string filename) {
507 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux)
509 // processIDS.resize(0);
511 //loop through and create all the processes you want
512 while (process != processors) {
516 processIDS.push_back(pid); //create map from line number to pid so you can append files in correct order later
519 driver(lines[process], outputFileName + toString(getpid()) + ".temp", filename);
521 }else { m->mothurOut("unable to spawn the necessary processes."); m->mothurOutEndLine(); exit(0); }
524 //force parent to wait until all the processes are done
525 for (int i=0;i<processors;i++) {
526 int temp = processIDS[i];
533 catch(exception& e) {
534 m->errorOut(e, "ChimeraCheckCommand", "createProcesses");
538 /**************************************************************************************************/