2 * summaryqualcommand.cpp
5 * Created by westcott on 11/28/11.
6 * Copyright 2011 Schloss Lab. All rights reserved.
10 #include "summaryqualcommand.h"
13 //**********************************************************************************************************************
14 vector<string> SummaryQualCommand::setParameters(){
16 CommandParameter pqual("qfile", "InputTypes", "", "", "none", "none", "none",false,true); parameters.push_back(pqual);
17 CommandParameter pname("name", "InputTypes", "", "", "none", "none", "none",false,false); parameters.push_back(pname);
18 CommandParameter pprocessors("processors", "Number", "", "1", "", "", "",false,false); parameters.push_back(pprocessors);
19 CommandParameter pinputdir("inputdir", "String", "", "", "", "", "",false,false); parameters.push_back(pinputdir);
20 CommandParameter poutputdir("outputdir", "String", "", "", "", "", "",false,false); parameters.push_back(poutputdir);
22 vector<string> myArray;
23 for (int i = 0; i < parameters.size(); i++) { myArray.push_back(parameters[i].name); }
27 m->errorOut(e, "SummaryQualCommand", "setParameters");
31 //**********************************************************************************************************************
32 string SummaryQualCommand::getHelpString(){
34 string helpString = "";
35 helpString += "The summary.qual command reads a quality file and an optional name file, and summarizes the quality information.\n";
36 helpString += "The summary.tax command parameters are qfile, name and processors. qfile is required, unless you have a valid current quality file.\n";
37 helpString += "The name parameter allows you to enter a name file associated with your quality file. \n";
38 helpString += "The summary.qual command should be in the following format: \n";
39 helpString += "summary.qual(qfile=yourQualityFile) \n";
40 helpString += "Note: No spaces between parameter labels (i.e. qfile), '=' and parameters (i.e.yourQualityFile).\n";
44 m->errorOut(e, "SummaryQualCommand", "getHelpString");
49 //**********************************************************************************************************************
50 SummaryQualCommand::SummaryQualCommand(){
52 abort = true; calledHelp = true;
54 vector<string> tempOutNames;
55 outputTypes["summary"] = tempOutNames;
58 m->errorOut(e, "SummaryQualCommand", "SummaryQualCommand");
62 //***************************************************************************************************************
64 SummaryQualCommand::SummaryQualCommand(string option) {
66 abort = false; calledHelp = false;
68 //allow user to run help
69 if(option == "help") { help(); abort = true; calledHelp = true; }
70 else if(option == "citation") { citation(); abort = true; calledHelp = true;}
73 vector<string> myArray = setParameters();
75 OptionParser parser(option);
76 map<string,string> parameters = parser.getParameters();
78 ValidParameters validParameter;
79 map<string,string>::iterator it;
81 //check to make sure all parameters are valid for command
82 for (it = parameters.begin(); it != parameters.end(); it++) {
83 if (validParameter.isValidParameter(it->first, myArray, it->second) != true) { abort = true; }
86 //if the user changes the input directory command factory will send this info to us in the output parameter
87 string inputDir = validParameter.validFile(parameters, "inputdir", false);
88 if (inputDir == "not found"){ inputDir = ""; }
91 it = parameters.find("qfile");
92 //user has given a template file
93 if(it != parameters.end()){
94 path = m->hasPath(it->second);
95 //if the user has not given a path then, add inputdir. else leave path alone.
96 if (path == "") { parameters["qfile"] = inputDir + it->second; }
99 it = parameters.find("name");
100 //user has given a template file
101 if(it != parameters.end()){
102 path = m->hasPath(it->second);
103 //if the user has not given a path then, add inputdir. else leave path alone.
104 if (path == "") { parameters["name"] = inputDir + it->second; }
108 //initialize outputTypes
109 vector<string> tempOutNames;
110 outputTypes["summary"] = tempOutNames;
112 //check for required parameters
113 qualfile = validParameter.validFile(parameters, "qfile", true);
114 if (qualfile == "not open") { qualfile = ""; abort = true; }
115 else if (qualfile == "not found") {
116 qualfile = m->getQualFile();
117 if (qualfile != "") { m->mothurOut("Using " + qualfile + " as input file for the qfile parameter."); m->mothurOutEndLine(); }
118 else { m->mothurOut("You have no current quality file and the qfile parameter is required."); m->mothurOutEndLine(); abort = true; }
119 }else { m->setQualFile(qualfile); }
121 namefile = validParameter.validFile(parameters, "name", true);
122 if (namefile == "not open") { namefile = ""; abort = true; }
123 else if (namefile == "not found") { namefile = ""; }
124 else { m->setNameFile(namefile); }
126 //if the user changes the output directory command factory will send this info to us in the output parameter
127 outputDir = validParameter.validFile(parameters, "outputdir", false); if (outputDir == "not found"){
129 outputDir += m->hasPath(qualfile); //if user entered a file with a path then preserve it
132 string temp = validParameter.validFile(parameters, "processors", false); if (temp == "not found"){ temp = m->getProcessors(); }
133 m->setProcessors(temp);
134 m->mothurConvert(temp, processors);
136 if (namefile == "") {
137 vector<string> files; files.push_back(qualfile);
138 parser.getNameFile(files);
142 catch(exception& e) {
143 m->errorOut(e, "SummaryQualCommand", "SummaryQualCommand");
147 //***************************************************************************************************************
148 int SummaryQualCommand::execute(){
151 if (abort == true) { if (calledHelp) { return 0; } return 2; }
153 int start = time(NULL);
156 vector<int> position;
157 vector<int> averageQ;
158 vector< vector<int> > scores;
160 if (m->control_pressed) { return 0; }
162 if (namefile != "") { nameMap = m->readNames(namefile); }
164 vector<unsigned long long> positions;
165 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux) || (__linux__) || (__unix__) || (__unix)
166 positions = m->divideFile(qualfile, processors);
167 for (int i = 0; i < (positions.size()-1); i++) { lines.push_back(linePair(positions[i], positions[(i+1)])); }
169 if (processors == 1) {
170 lines.push_back(linePair(0, 1000));
172 positions = m->setFilePosFasta(qualfile, numSeqs);
173 if (positions.size() < processors) { processors = positions.size(); }
175 //figure out how many sequences you have to process
176 int numSeqsPerProcessor = numSeqs / processors;
177 for (int i = 0; i < processors; i++) {
178 int startIndex = i * numSeqsPerProcessor;
179 if(i == (processors - 1)){ numSeqsPerProcessor = numSeqs - i * numSeqsPerProcessor; }
180 lines.push_back(linePair(positions[startIndex], numSeqsPerProcessor));
186 if(processors == 1){ numSeqs = driverCreateSummary(position, averageQ, scores, qualfile, lines[0]); }
187 else{ numSeqs = createProcessesCreateSummary(position, averageQ, scores, qualfile); }
189 if (m->control_pressed) { return 0; }
192 string summaryFile = outputDir + m->getRootName(m->getSimpleName(qualfile)) + "qual.summary";
193 printQual(summaryFile, position, averageQ, scores);
195 if (m->control_pressed) { m->mothurRemove(summaryFile); return 0; }
197 //output results to screen
198 cout.setf(ios::fixed, ios::floatfield); cout.setf(ios::showpoint);
199 m->mothurOutEndLine();
200 m->mothurOut("Position\tNumSeqs\tAverageQ"); m->mothurOutEndLine();
201 for (int i = 0; i < position.size(); i+=100) {
202 float average = averageQ[i] / (float) position[i];
203 cout << i << '\t' << position[i] << '\t' << average;
204 m->mothurOutJustToLog(toString(i) + "\t" + toString(position[i]) + "\t" + toString(average)); m->mothurOutEndLine();
207 m->mothurOutEndLine();
208 m->mothurOut("It took " + toString(time(NULL) - start) + " secs to create the summary file for " + toString(numSeqs) + " sequences."); m->mothurOutEndLine(); m->mothurOutEndLine();
209 m->mothurOutEndLine();
210 m->mothurOut("Output File Name: "); m->mothurOutEndLine();
211 m->mothurOut(summaryFile); m->mothurOutEndLine(); outputNames.push_back(summaryFile); outputTypes["summary"].push_back(summaryFile);
212 m->mothurOutEndLine();
216 catch(exception& e) {
217 m->errorOut(e, "SummaryQualCommand", "execute");
221 /**************************************************************************************/
222 int SummaryQualCommand::driverCreateSummary(vector<int>& position, vector<int>& averageQ, vector< vector<int> >& scores, string filename, linePair filePos) {
225 m->openInputFile(filename, in);
227 in.seekg(filePos.start);
234 if (m->control_pressed) { in.close(); return 1; }
236 QualityScores current(in); m->gobble(in);
238 if (current.getName() != "") {
241 if (namefile != "") {
242 //make sure this sequence is in the namefile, else error
243 map<string, int>::iterator it = nameMap.find(current.getName());
245 if (it == nameMap.end()) { m->mothurOut("[ERROR]: " + current.getName() + " is not in your namefile, please correct."); m->mothurOutEndLine(); m->control_pressed = true; }
246 else { num = it->second; }
249 vector<int> thisScores = current.getQualityScores();
251 //resize to num of positions setting number of seqs with that size to 1
252 if (position.size() < thisScores.size()) { position.resize(thisScores.size(), 0); }
253 if (averageQ.size() < thisScores.size()) { averageQ.resize(thisScores.size(), 0); }
254 if (scores.size() < thisScores.size()) {
255 scores.resize(thisScores.size());
256 for (int i = 0; i < scores.size(); i++) { scores[i].resize(41, 0); }
259 //increase counts of number of seqs with this position
260 //average is really the total, we will average in execute
261 for (int i = 0; i < thisScores.size(); i++) {
263 averageQ[i] += (thisScores[i] * num); //weighting for namesfile
264 if (thisScores[i] > 40) { m->mothurOut("[ERROR]: " + current.getName() + " has a quality scores of " + toString(thisScores[i]) + ", expecting values to be less than 40."); m->mothurOutEndLine(); m->control_pressed = true; }
265 else { scores[i][thisScores[i]] += num; }
271 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux) || (__linux__) || (__unix__) || (__unix)
272 unsigned long long pos = in.tellg();
273 if ((pos == -1) || (pos >= filePos.end)) { break; }
275 if (in.eof()) { break; }
283 catch(exception& e) {
284 m->errorOut(e, "SummaryQualCommand", "driverCreateSummary");
288 /**************************************************************************************************/
289 int SummaryQualCommand::createProcessesCreateSummary(vector<int>& position, vector<int>& averageQ, vector< vector<int> >& scores, string filename) {
295 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux) || (__linux__) || (__unix__) || (__unix)
297 //loop through and create all the processes you want
298 while (process != processors) {
302 processIDS.push_back(pid); //create map from line number to pid so you can append files in correct order later
305 numSeqs = driverCreateSummary(position, averageQ, scores, qualfile, lines[process]);
307 //pass numSeqs to parent
309 string tempFile = qualfile + toString(getpid()) + ".num.temp";
310 m->openOutputFile(tempFile, out);
312 out << numSeqs << endl;
313 out << position.size() << endl;
314 for (int k = 0; k < position.size(); k++) { out << position[k] << '\t'; } out << endl;
315 for (int k = 0; k < averageQ.size(); k++) { out << averageQ[k] << '\t'; } out << endl;
316 for (int k = 0; k < scores.size(); k++) {
317 for (int j = 0; j < 41; j++) {
318 out << scores[k][j] << '\t';
328 m->mothurOut("[ERROR]: unable to spawn the necessary processes."); m->mothurOutEndLine();
329 for (int i = 0; i < processIDS.size(); i++) { kill (processIDS[i], SIGINT); }
335 numSeqs = driverCreateSummary(position, averageQ, scores, qualfile, lines[0]);
337 //force parent to wait until all the processes are done
338 for (int i=0;i<processIDS.size();i++) {
339 int temp = processIDS[i];
343 //parent reads in and combine Filter info
344 for (int i = 0; i < processIDS.size(); i++) {
345 string tempFilename = qualfile + toString(processIDS[i]) + ".num.temp";
347 m->openInputFile(tempFilename, in);
350 in >> tempNum; m->gobble(in); numSeqs += tempNum;
351 in >> tempNum; m->gobble(in);
353 if (position.size() < tempNum) { position.resize(tempNum, 0); }
354 if (averageQ.size() < tempNum) { averageQ.resize(tempNum, 0); }
355 if (scores.size() < tempNum) {
356 scores.resize(tempNum);
357 for (int i = 0; i < scores.size(); i++) { scores[i].resize(41, 0); }
360 for (int k = 0; k < tempNum; k++) { in >> temp; position[k] += temp; } m->gobble(in);
361 for (int k = 0; k < tempNum; k++) { in >> temp; averageQ[k] += temp; } m->gobble(in);
362 for (int k = 0; k < tempNum; k++) {
363 for (int j = 0; j < 41; j++) {
364 in >> temp; scores[k][j] += temp;
370 m->mothurRemove(tempFilename);
374 //////////////////////////////////////////////////////////////////////////////////////////////////////
375 //Windows version shared memory, so be careful when passing variables through the seqSumQualData struct.
376 //Above fork() will clone, so memory is separate, but that's not the case with windows,
377 //Taking advantage of shared memory to pass results vectors.
378 //////////////////////////////////////////////////////////////////////////////////////////////////////
380 vector<seqSumQualData*> pDataArray;
381 DWORD dwThreadIdArray[processors];
382 HANDLE hThreadArray[processors];
384 //Create processor worker threads.
385 for( int i=0; i<processors; i++ ){
387 // Allocate memory for thread data.
388 seqSumQualData* tempSum = new seqSumQualData(filename, m, lines[i].start, lines[i].end, namefile, nameMap);
389 pDataArray.push_back(tempSum);
390 processIDS.push_back(i);
392 hThreadArray[i] = CreateThread(NULL, 0, MySeqSumQualThreadFunction, pDataArray[i], 0, &dwThreadIdArray[i]);
395 //Wait until all threads have terminated.
396 WaitForMultipleObjects(processors, hThreadArray, TRUE, INFINITE);
398 //Close all thread handles and free memory allocations.
399 for(int i=0; i < pDataArray.size(); i++){
400 numSeqs += pDataArray[i]->count;
401 int tempNum = pDataArray[i]->position.size();
402 if (position.size() < tempNum) { position.resize(tempNum, 0); }
403 if (averageQ.size() < tempNum) { averageQ.resize(tempNum, 0); }
404 if (scores.size() < tempNum) {
405 scores.resize(tempNum);
406 for (int i = 0; i < scores.size(); i++) { scores[i].resize(41, 0); }
409 for (int k = 0; k < tempNum; k++) { position[k] += pDataArray[i]->position[k]; }
410 for (int k = 0; k < tempNum; k++) { averageQ[k] += pDataArray[i]->averageQ[k]; }
411 for (int k = 0; k < tempNum; k++) { for (int j = 0; j < 41; j++) { scores[k][j] += pDataArray[i]->scores[k][j]; } }
413 CloseHandle(hThreadArray[i]);
414 delete pDataArray[i];
419 catch(exception& e) {
420 m->errorOut(e, "SummaryQualCommand", "createProcessesCreateSummary");
424 /**************************************************************************************************/
425 int SummaryQualCommand::printQual(string sumFile, vector<int>& position, vector<int>& averageQ, vector< vector<int> >& scores) {
428 m->openOutputFile(sumFile, out);
429 out.setf(ios::fixed, ios::floatfield); out.setf(ios::showpoint);
430 outputNames.push_back(sumFile); outputTypes["summary"].push_back(sumFile);
433 out << "Position\tnumSeqs\tAverageQ\t";
434 for (int i = 0; i < 41; i++) { out << "q" << i << '\t'; }
437 for (int i = 0; i < position.size(); i++) {
439 if (m->control_pressed) { out.close(); return 0; }
441 float average = averageQ[i] / (float) position[i];
442 out << i << '\t' << position[i] << '\t' << average << '\t';
444 for (int j = 0; j < 41; j++) {
445 out << scores[i][j] << '\t';
454 catch(exception& e) {
455 m->errorOut(e, "SummaryQualCommand", "printQual");
460 /**************************************************************************************/