1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596
|
/*
* filterseqscommand.cpp
* Mothur
*
* Created by Thomas Ryabin on 5/4/09.
* Copyright 2009 Schloss Lab UMASS Amherst. All rights reserved.
*
*/
#include "filterseqscommand.h"
#include "sequence.hpp"
//**********************************************************************************************************************
vector<string> FilterSeqsCommand::setParameters(){
try {
CommandParameter pfasta("fasta", "InputTypes", "", "", "none", "none", "none","fasta-filter",false,true, true); parameters.push_back(pfasta);
CommandParameter phard("hard", "InputTypes", "", "", "none", "none", "none","",false,false); parameters.push_back(phard);
CommandParameter ptrump("trump", "String", "", "*", "", "", "","",false,false, true); parameters.push_back(ptrump);
CommandParameter psoft("soft", "Number", "", "0", "", "", "","",false,false); parameters.push_back(psoft);
CommandParameter pvertical("vertical", "Boolean", "", "T", "", "", "","",false,false, true); parameters.push_back(pvertical);
CommandParameter pprocessors("processors", "Number", "", "1", "", "", "","",false,false, true); parameters.push_back(pprocessors);
CommandParameter pseed("seed", "Number", "", "0", "", "", "","",false,false); parameters.push_back(pseed);
CommandParameter pinputdir("inputdir", "String", "", "", "", "", "","",false,false); parameters.push_back(pinputdir);
CommandParameter poutputdir("outputdir", "String", "", "", "", "", "","",false,false); parameters.push_back(poutputdir);
abort = false; calledHelp = false;
vector<string> tempOutNames;
outputTypes["fasta"] = tempOutNames;
outputTypes["filter"] = tempOutNames;
vector<string> myArray;
for (int i = 0; i < parameters.size(); i++) { myArray.push_back(parameters[i].name); }
return myArray;
}
catch(exception& e) {
m->errorOut(e, "FilterSeqsCommand", "setParameters");
exit(1);
}
}
//**********************************************************************************************************************
string FilterSeqsCommand::getHelpString(){
try {
string helpString = "";
helpString += "The filter.seqs command reads a file containing sequences and creates a .filter and .filter.fasta file.\n";
helpString += "The filter.seqs command parameters are fasta, trump, soft, hard, processors and vertical. \n";
helpString += "The fasta parameter is required, unless you have a valid current fasta file. You may enter several fasta files to build the filter from and filter, by separating their names with |'s.\n";
helpString += "For example: fasta=abrecovery.fasta|amazon.fasta \n";
helpString += "The trump option will remove a column if the trump character is found at that position in any sequence of the alignment. Default=*, meaning no trump. \n";
helpString += "A soft mask removes any column where the dominant base (i.e. A, T, G, C, or U) does not occur in at least a designated percentage of sequences. Default=0.\n";
helpString += "The hard parameter allows you to enter a file containing the filter you want to use.\n";
helpString += "The vertical parameter removes columns where all sequences contain a gap character. The default is T.\n";
helpString += "The processors parameter allows you to specify the number of processors to use. The default is 1.\n";
helpString += "The filter.seqs command should be in the following format: \n";
helpString += "filter.seqs(fasta=yourFastaFile, trump=yourTrump) \n";
helpString += "Example filter.seqs(fasta=abrecovery.fasta, trump=.).\n";
;
return helpString;
}
catch(exception& e) {
m->errorOut(e, "FilterSeqsCommand", "getHelpString");
exit(1);
}
}
//**********************************************************************************************************************
string FilterSeqsCommand::getOutputPattern(string type) {
try {
string pattern = "";
if (type == "fasta") { pattern = "[filename],filter.fasta"; }
else if (type == "filter") { pattern = "[filename],filter"; }
else { m->mothurOut("[ERROR]: No definition for type " + type + " output pattern.\n"); m->setControl_pressed(true); }
return pattern;
}
catch(exception& e) {
m->errorOut(e, "FilterSeqsCommand", "getOutputPattern");
exit(1);
}
}
/**************************************************************************************/
FilterSeqsCommand::FilterSeqsCommand(string option) : Command() {
try {
recalced = false; filterFileName = "";
//allow user to run help
if(option == "help") { help(); abort = true; calledHelp = true; }
else if(option == "citation") { citation(); abort = true; calledHelp = true;}
else if(option == "category") { abort = true; calledHelp = true; }
else {
OptionParser parser(option, setParameters());
map<string,string> parameters = parser.getParameters();
ValidParameters validParameter;
fasta = validParameter.validPath(parameters, "fasta");
if (fasta == "not found") {
fasta = current->getFastaFile();
if (fasta != "") {
fastafileNames.push_back(fasta);
m->mothurOut("Using " + fasta + " as input file for the fasta parameter.\n");
string simpleName = util.getSimpleName(fasta);
filterFileName += simpleName.substr(0, simpleName.find_first_of('.'));
}
else { m->mothurOut("You have no current fastafile and the fasta parameter is required.\n"); abort = true; }
}
else {
util.splitAtChar(fasta, fastafileNames, '|');
//go through files and make sure they are good, if not, then disregard them
for (int i = 0; i < fastafileNames.size(); i++) {
bool ignore = false;
if (fastafileNames[i] == "current") {
fastafileNames[i] = current->getFastaFile();
if (fastafileNames[i] != "") { m->mothurOut("Using " + fastafileNames[i] + " as input file for the fasta parameter where you had given current.\n"); }
else {
m->mothurOut("You have no current fastafile, ignoring current.\n"); ignore=true;
//erase from file list
fastafileNames.erase(fastafileNames.begin()+i);
i--;
}
}
if (!ignore) {
if (util.checkLocations(fastafileNames[i], current->getLocations())) {
string simpleName = util.getSimpleName(fastafileNames[i]);
filterFileName += simpleName.substr(0, simpleName.find_first_of('.'));
current->setFastaFile(fastafileNames[i]);
}
else { fastafileNames.erase(fastafileNames.begin()+i); i--; } //erase from file list
}
}
//make sure there is at least one valid file left
if (fastafileNames.size() == 0) { m->mothurOut("no valid files.\n"); abort = true; }
}
if (!abort) {
if (outputdir == ""){
outputdir += util.hasPath(fastafileNames[0]);
}
}
//check for optional parameter and set defaults
// ...at some point should added some additional type checking...
string temp;
hard = validParameter.validFile(parameters, "hard"); if (hard == "not found") { hard = ""; }
else if (hard == "not open") { hard = ""; abort = true; }
temp = validParameter.valid(parameters, "trump"); if (temp == "not found") { temp = "*"; }
trump = temp[0];
temp = validParameter.valid(parameters, "soft"); if (temp == "not found") { soft = 0; }
else { soft = (float)atoi(temp.c_str()) / 100.0; }
temp = validParameter.valid(parameters, "processors"); if (temp == "not found"){ temp = current->getProcessors(); }
processors = current->setProcessors(temp);
vertical = validParameter.valid(parameters, "vertical");
if (vertical == "not found") {
if ((hard == "") && (trump == '*') && (soft == 0)) { vertical = "T"; } //you have not given a hard file or set the trump char.
else { vertical = "F"; }
}
numSeqs = 0;
}
}
catch(exception& e) {
m->errorOut(e, "FilterSeqsCommand", "FilterSeqsCommand");
exit(1);
}
}
/**************************************************************************************/
int FilterSeqsCommand::execute() {
try {
if (abort) { if (calledHelp) { return 0; } return 2; }
ifstream inFASTA;
util.openInputFile(fastafileNames[0], inFASTA);
Sequence testSeq(inFASTA);
alignmentLength = testSeq.getAlignLength();
inFASTA.close();
////////////create filter/////////////////
m->mothurOut("Creating Filter...\n");
filter = createFilter();
m->mothurOutEndLine(); m->mothurOutEndLine();
if (m->getControl_pressed()) { outputTypes.clear(); return 0; }
ofstream outFilter;
//prevent giantic file name
map<string, string> variables;
variables["[filename]"] = outputdir + filterFileName + ".";
if (fastafileNames.size() > 3) { variables["[filename]"] = outputdir + "merge."; }
string filterFile = getOutputFileName("filter", variables);
util.openOutputFile(filterFile, outFilter);
outFilter << filter << endl;
outFilter.close();
outputNames.push_back(filterFile); outputTypes["filter"].push_back(filterFile);
////////////run filter/////////////////
m->mothurOut("Running Filter...\n");
filterSequences();
m->mothurOutEndLine(); m->mothurOutEndLine();
int filteredLength = 0;
for(int i=0;i<alignmentLength;i++){
if(filter[i] == '1'){ filteredLength++; }
}
if (m->getControl_pressed()) { outputTypes.clear(); for(int i = 0; i < outputNames.size(); i++) { util.mothurRemove(outputNames[i]); } return 0; }
m->mothurOutEndLine();
m->mothurOut("Length of filtered alignment: " + toString(filteredLength)); m->mothurOutEndLine();
m->mothurOut("Number of columns removed: " + toString((alignmentLength-filteredLength))); m->mothurOutEndLine();
m->mothurOut("Length of the original alignment: " + toString(alignmentLength)); m->mothurOutEndLine();
m->mothurOut("Number of sequences used to construct filter: " + toString(numSeqs)); m->mothurOutEndLine();
//set fasta file as new current fastafile
string currentName = "";
itTypes = outputTypes.find("fasta");
if (itTypes != outputTypes.end()) {
if ((itTypes->second).size() != 0) { currentName = (itTypes->second)[0]; current->setFastaFile(currentName); }
}
m->mothurOut("\nOutput File Names: \n");
for(int i = 0; i < outputNames.size(); i++) { m->mothurOut(outputNames[i]); m->mothurOutEndLine(); }
m->mothurOutEndLine();
return 0;
}
catch(exception& e) {
m->errorOut(e, "FilterSeqsCommand", "execute");
exit(1);
}
}
/**************************************************************************************/
int FilterSeqsCommand::filterSequences() {
try {
numSeqs = 0;
for (int s = 0; s < fastafileNames.size(); s++) {
map<string, string> variables;
variables["[filename]"] = outputdir + util.getRootName(util.getSimpleName(fastafileNames[s]));
string filteredFasta = getOutputFileName("fasta", variables);
vector<double> positions;
if (savedPositions.size() != 0) { positions = savedPositions[s]; }
else {
#if defined NON_WINDOWS
positions = util.divideFile(fastafileNames[s], processors);
#else
positions = util.setFilePosFasta(fastafileNames[s], numSeqs);
if (numSeqs < processors) { processors = numSeqs; }
#endif
}
vector<linePair> lines;
#if defined NON_WINDOWS
for (int i = 0; i < (positions.size()-1); i++) { lines.push_back(linePair(positions[i], positions[(i+1)])); }
#else
long long numFSeqs = positions.size()-1;
if (numFSeqs < processors) { processors = numFSeqs; }
//figure out how many sequences you have to process
int numSeqsPerProcessor = numFSeqs / processors;
for (int i = 0; i < processors; i++) {
long long startIndex = i * numSeqsPerProcessor;
if(i == (processors - 1)){ numSeqsPerProcessor = numFSeqs - i * numSeqsPerProcessor; }
lines.push_back(linePair(positions[startIndex], numSeqsPerProcessor));
}
#endif
long long numFastaSeqs = createProcessesRunFilter(filter, fastafileNames[s], filteredFasta, lines);
numSeqs += numFastaSeqs;
outputNames.push_back(filteredFasta); outputTypes["fasta"].push_back(filteredFasta);
}
return 0;
}
catch(exception& e) {
m->errorOut(e, "FilterSeqsCommand", "filterSequences");
exit(1);
}
}
/**************************************************************************************/
void driverRunFilter(filterRunData* params) {
try {
ifstream in; params->util.openInputFile(params->filename, in);
in.seekg(params->start);
//adjust start if null strings
if (params->start == 0) { params->util.zapGremlins(in); gobble(in); }
bool done = false;
params->count = 0;
string outBuffer = "";
while (!done) {
if (params->m->getControl_pressed()) { break; }
Sequence seq(in); gobble(in);
if (seq.getName() != "") {
string align = seq.getAligned();
string filterSeq = "";
for(int j=0;j<params->alignmentLength;j++){ if(params->filter[j] == '1'){ filterSeq += align[j]; } }
outBuffer += '>' + seq.getName() + '\n' + filterSeq + '\n';
}
params->count++;
//report progress
if((params->count) % 1000 == 0){ params->outputWriter->write(outBuffer); outBuffer = ""; params->m->mothurOutJustToScreen(toString(params->count)+"\n"); }
#if defined NON_WINDOWS
unsigned long long pos = in.tellg();
if ((pos == -1) || (pos >= params->end)) { break; }
#else
if (params->count == params->end) { break; }
#endif
}
//report progress
if((params->count) % 1000 != 0){ params->outputWriter->write(outBuffer); params->m->mothurOutJustToScreen(toString(params->count)+"\n"); }
in.close();
}
catch(exception& e) {
params->m->errorOut(e, "FilterSeqsCommand", "driverRunFilter");
exit(1);
}
}
/**************************************************************************************************/
long long FilterSeqsCommand::createProcessesRunFilter(string F, string filename, string filteredFastaName, vector<linePair> lines) {
try {
util.mothurRemove(filteredFastaName);
long long num = 0;
//create array of worker threads
vector<std::thread*> workerThreads;
vector<filterRunData*> data;
time_t start, end;
time(&start);
auto synchronizedOutputFile = std::make_shared<SynchronizedOutputFile>(filteredFastaName);
//Lauch worker threads
for (int i = 0; i < processors-1; i++) {
OutputWriter* threadWriter = new OutputWriter(synchronizedOutputFile);
filterRunData* dataBundle = new filterRunData(filter, filename, threadWriter, lines[i+1].start, lines[i+1].end, alignmentLength);
data.push_back(dataBundle);
workerThreads.push_back(new std::thread(driverRunFilter, dataBundle));
}
OutputWriter* threadWriter = new OutputWriter(synchronizedOutputFile);
filterRunData* dataBundle = new filterRunData(filter, filename, threadWriter, lines[0].start, lines[0].end, alignmentLength);
data.push_back(dataBundle);
driverRunFilter(dataBundle);
num = dataBundle->count;
for (int i = 0; i < processors-1; i++) {
workerThreads[i]->join();
num += data[i]->count;
delete data[i]->outputWriter;
delete data[i];
delete workerThreads[i];
}
synchronizedOutputFile->close();
delete threadWriter;
delete dataBundle;
time(&end);
m->mothurOut("It took " + toString(difftime(end, start)) + " secs to filter " + toString(num) + " sequences.\n");
return num;
}
catch(exception& e) {
m->errorOut(e, "FilterSeqsCommand", "createProcessesRunFilter");
exit(1);
}
}
/**************************************************************************************/
string FilterSeqsCommand::createFilter() {
try {
string filterString = "";
Filters F;
if (!util.isEqual(soft, 0)) { F.setSoft(soft); }
if (trump != '*') { F.setTrump(trump); }
F.setLength(alignmentLength);
if(trump != '*' || util.isTrue(vertical) || !util.isEqual(soft, 0)){ F.initialize(); }
if(hard.compare("") != 0) { F.doHard(hard); }
else { F.setFilter(string(alignmentLength, '1')); }
numSeqs = 0;
if(trump != '*' || util.isTrue(vertical) || !util.isEqual(soft, 0)){
for (int s = 0; s < fastafileNames.size(); s++) {
numSeqs += createProcessesCreateFilter(F, fastafileNames[s]);
if (m->getControl_pressed()) { return filterString; }
}
}
F.setNumSeqs(numSeqs);
if(util.isTrue(vertical) == 1) { F.doVertical(); }
if(!util.isEqual(soft, 0)) { F.doSoft(); }
filterString = F.getFilter();
return filterString;
}
catch(exception& e) {
m->errorOut(e, "FilterSeqsCommand", "createFilter");
exit(1);
}
}
/**************************************************************************************/
void driverCreateFilter(filterData* params) {
try {
if (!params->util.isEqual(params->soft, 0)) { params->F.setSoft(params->soft); }
if (params->trump != '*') { params->F.setTrump(params->trump); }
params->F.setLength(params->alignmentLength);
if(params->trump != '*' || params->vertical || !params->util.isEqual(params->soft, 0)){ params->F.initialize(); }
if(params->hard.compare("") != 0) { params->F.doHard(params->hard); }
else { params->F.setFilter(string(params->alignmentLength, '1')); }
ifstream in; params->util.openInputFile(params->filename, in);
in.seekg(params->start);
//adjust start if null strings
if (params->start == 0) { params->util.zapGremlins(in); gobble(in); }
bool done = false;
params->count = 0;
bool error = false;
while (!done) {
if (params->m->getControl_pressed()) { break; }
Sequence seq(in); gobble(in);
if (seq.getName() != "") {
if (params->m->getDebug()) { params->m->mothurOutJustToScreen("[DEBUG]: " + seq.getName() + " length = " + toString(seq.getAligned().length()) + '\n'); }
if (seq.getAligned().length() != params->alignmentLength) { params->m->mothurOut("[ERROR]: Sequences are not all the same length, please correct.\n"); error = true; if (!params->m->getDebug()) { params->m->setControl_pressed(true); }else{ params->m->mothurOutJustToLog("[DEBUG]: " + seq.getName() + " length = " + toString(seq.getAligned().length()) + '\n'); } }
if(params->trump != '*') { params->F.doTrump(seq); }
if(params->vertical || !params->util.isEqual(params->soft, 0)) { params->F.getFreqs(seq); }
cout.flush();
params->count++;
}
#if defined NON_WINDOWS
unsigned long long pos = in.tellg();
if ((pos == -1) || (pos >= params->end)) { break; }
#else
if (params->count == params->end) { break; }
#endif
//report progress
if((params->count) % 1000 == 0){ params->m->mothurOutJustToScreen(toString(params->count)+"\n"); }
}
//report progress
if((params->count) % 1000 != 0){ params->m->mothurOutJustToScreen(toString(params->count)+"\n"); }
in.close();
if (error) { params->m->setControl_pressed(true); }
}
catch(exception& e) {
params->m->errorOut(e, "FilterSeqsCommand", "driverCreateFilter");
exit(1);
}
}
/**************************************************************************************************/
long long FilterSeqsCommand::createProcessesCreateFilter(Filters& F, string filename) {
try {
vector<linePair> lines;
vector<double> positions;
#if defined NON_WINDOWS
positions = util.divideFile(filename, processors);
for (int i = 0; i < (positions.size()-1); i++) { lines.push_back(linePair(positions[i], positions[(i+1)])); }
#else
long long numFastaSeqs = 0;
positions = util.setFilePosFasta(filename, numFastaSeqs);
if (numFastaSeqs < processors) { processors = numFastaSeqs; }
//figure out how many sequences you have to process
int numSeqsPerProcessor = numFastaSeqs / processors;
for (int i = 0; i < processors; i++) {
long long startIndex = i * numSeqsPerProcessor;
if(i == (processors - 1)){ numSeqsPerProcessor = numFastaSeqs - i * numSeqsPerProcessor; }
lines.push_back(linePair(positions[startIndex], numSeqsPerProcessor));
}
#endif
//save the file positions so we can reuse them in the runFilter function
if (!recalced) { savedPositions.push_back(positions); }
long long num = 0;
bool doVertical = util.isTrue(vertical);
//create array of worker threads
vector<std::thread*> workerThreads;
vector<filterData*> data;
time_t start, end;
time(&start);
//Lauch worker threads
for (int i = 0; i < processors-1; i++) {
filterData* dataBundle = new filterData(filename, lines[i+1].start, lines[i+1].end, alignmentLength, trump, doVertical, soft, hard, i+1);
data.push_back(dataBundle);
workerThreads.push_back(new std::thread(driverCreateFilter, dataBundle));
}
filterData* dataBundle = new filterData(filename, lines[0].start, lines[0].end, alignmentLength, trump, doVertical, soft, hard, 0);
driverCreateFilter(dataBundle);
num = dataBundle->count;
F.mergeFilter(dataBundle->F.getFilter());
for (int k = 0; k < alignmentLength; k++) { F.a[k] += dataBundle->F.a[k]; }
for (int k = 0; k < alignmentLength; k++) { F.t[k] += dataBundle->F.t[k]; }
for (int k = 0; k < alignmentLength; k++) { F.g[k] += dataBundle->F.g[k]; }
for (int k = 0; k < alignmentLength; k++) { F.c[k] += dataBundle->F.c[k]; }
for (int k = 0; k < alignmentLength; k++) { F.gap[k] += dataBundle->F.gap[k]; }
delete dataBundle;
for (int i = 0; i < processors-1; i++) {
workerThreads[i]->join();
num += data[i]->count;
F.mergeFilter(data[i]->F.getFilter());
for (int k = 0; k < alignmentLength; k++) { F.a[k] += data[i]->F.a[k]; }
for (int k = 0; k < alignmentLength; k++) { F.t[k] += data[i]->F.t[k]; }
for (int k = 0; k < alignmentLength; k++) { F.g[k] += data[i]->F.g[k]; }
for (int k = 0; k < alignmentLength; k++) { F.c[k] += data[i]->F.c[k]; }
for (int k = 0; k < alignmentLength; k++) { F.gap[k] += data[i]->F.gap[k]; }
delete data[i];
delete workerThreads[i];
}
time(&end);
m->mothurOut("It took " + toString(difftime(end, start)) + " secs to create filter for " + toString(num) + " sequences.\n");
if (m->getDebug()) { m->mothurOut("[DEBUG]: filter = " + F.getFilter() + "\n\n"); }
return num;
}
catch(exception& e) {
m->errorOut(e, "FilterSeqsCommand", "createProcessesCreateFilter");
exit(1);
}
}
/**************************************************************************************/
|