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
|
/*
* chimerapintailcommand.cpp
* Mothur
*
* Created by westcott on 4/1/10.
* Copyright 2010 Schloss Lab. All rights reserved.
*
*/
#include "chimerapintailcommand.h"
#include "pintail.h"
#include "removeseqscommand.h"
//**********************************************************************************************************************
vector<string> ChimeraPintailCommand::setParameters(){
try {
CommandParameter ptemplate("reference", "InputTypes", "", "", "none", "none", "none","",false,true,true); parameters.push_back(ptemplate);
CommandParameter pfasta("fasta", "InputTypes", "", "", "none", "none", "none","chimera-accnos",false,true,true); parameters.push_back(pfasta);
CommandParameter pconservation("conservation", "InputTypes", "", "", "none", "none", "none","",false,false); parameters.push_back(pconservation);
CommandParameter pquantile("quantile", "InputTypes", "", "", "none", "none", "none","",false,false); parameters.push_back(pquantile);
CommandParameter pfilter("filter", "Boolean", "", "F", "", "", "","",false,false); parameters.push_back(pfilter);
CommandParameter pwindow("window", "Number", "", "0", "", "", "","","",false,false); parameters.push_back(pwindow);
CommandParameter pincrement("increment", "Number", "", "25", "", "", "","",false,false); parameters.push_back(pincrement);
CommandParameter premovechimeras("removechimeras", "Boolean", "", "t", "", "", "","alns",false,false); parameters.push_back(premovechimeras);
CommandParameter pmask("mask", "String", "", "", "", "", "","",false,false); parameters.push_back(pmask);
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["chimera"] = tempOutNames;
outputTypes["fasta"] = tempOutNames;
outputTypes["accnos"] = 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, "ChimeraPintailCommand", "setParameters");
exit(1);
}
}
//**********************************************************************************************************************
string ChimeraPintailCommand::getHelpString(){
try {
string helpString = "";
helpString += "The chimera.pintail command reads a fastafile and referencefile and outputs potentially chimeric sequences.\n";
helpString += "This command was created using the algorithms described in the 'At Least 1 in 20 16S rRNA Sequence Records Currently Held in the Public Repositories is Estimated To Contain Substantial Anomalies' paper by Kevin E. Ashelford 1, Nadia A. Chuzhanova 3, John C. Fry 1, Antonia J. Jones 2 and Andrew J. Weightman 1.\n";
helpString += "The chimera.pintail command parameters are fasta, reference, filter, mask, processors, window, increment, removechimeras, conservation and quantile.\n";
helpString += "The fasta parameter allows you to enter the fasta file containing your potentially chimeric sequences, and is required unless you have a valid current fasta file. \n";
helpString += "The reference parameter allows you to enter a reference file containing known non-chimeric sequences, and is required. \n";
helpString += "The filter parameter allows you to specify if you would like to apply a vertical and 50% soft filter. \n";
helpString += "The mask parameter allows you to specify a file containing one sequence you wish to use as a mask for the your sequences, by default no mask is applied. You can apply an ecoli mask by typing, mask=default. \n";
helpString += "The window parameter allows you to specify the window size for searching for chimeras, default=300. \n";
helpString += "The increment parameter allows you to specify how far you move each window while finding chimeric sequences, default=25.\n";
helpString += "The removechimeras parameter allows you to indicate you would like to automatically remove the sequences that are flagged as chimeric. Default=t.\n";
helpString += "The conservation parameter allows you to enter a frequency file containing the highest bases frequency at each place in the alignment.\n";
helpString += "The quantile parameter allows you to enter a file containing quantiles for a template files sequences, if you use the filter the quantile file generated becomes unique to the fasta file you used.\n";
helpString += "The chimera.pintail command should be in the following format: \n";
helpString += "chimera.pintail(fasta=yourFastaFile, reference=yourTemplate) \n";
helpString += "Example: chimera.pintail(fasta=AD.align, reference=silva.bacteria.fasta) \n";
return helpString;
}
catch(exception& e) {
m->errorOut(e, "ChimeraPintailCommand", "getHelpString");
exit(1);
}
}
//**********************************************************************************************************************
string ChimeraPintailCommand::getOutputPattern(string type) {
try {
string pattern = "";
if (type == "chimera") { pattern = "[filename],[tag],pintail.chimeras-[filename],pintail.chimeras"; }
else if (type == "accnos") { pattern = "[filename],[tag],pintail.accnos-[filename],pintail.accnos"; }
else if (type == "fasta") { pattern = "[filename],[tag],pintail.fasta-[filename],pintail.fasta"; }
else { m->mothurOut("[ERROR]: No definition for type " + type + " output pattern.\n"); m->setControl_pressed(true); }
return pattern;
}
catch(exception& e) {
m->errorOut(e, "ChimeraPintailCommand", "getOutputPattern");
exit(1);
}
}
//***************************************************************************************************************
ChimeraPintailCommand::ChimeraPintailCommand(string option) : Command() {
try {
//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();
//check for required parameters
ValidParameters validParameter;
fastafile = validParameter.validFile(parameters, "fasta");
if (fastafile == "not found") {
fastafile = current->getFastaFile();
if (fastafile != "") { m->mothurOut("Using " + fastafile + " as input file for the fasta parameter.\n"); }
else { m->mothurOut("[ERROR]: You have no current fasta file and the fasta parameter is required.\n"); abort = true; }
}
else if (fastafile == "not open") { abort = true; }
else { current->setFastaFile(fastafile); }
string temp;
temp = validParameter.valid(parameters, "filter"); if (temp == "not found") { temp = "F"; }
filter = util.isTrue(temp);
temp = validParameter.valid(parameters, "window"); if (temp == "not found") { temp = "0"; }
util.mothurConvert(temp, window);
temp = validParameter.valid(parameters, "increment"); if (temp == "not found") { temp = "25"; }
util.mothurConvert(temp, increment);
//this has to go after save so that if the user sets save=t and provides no reference we abort
templatefile = validParameter.validFile(parameters, "reference");
if (templatefile == "not found") { m->mothurOut("[ERROR]: The reference parameter is a required, aborting.\n"); abort = true;
}else if (templatefile == "not open") { abort = true; }
maskfile = validParameter.validPath(parameters, "mask");
if (maskfile == "not found") { maskfile = ""; }
else if (maskfile != "default") {
if (util.checkLocations(maskfile, current->getLocations())) { }
else { m->mothurOut("Unable to open " + maskfile + ".\n"); abort = true; } //erase from file list
}else if (maskfile == "default") { m->mothurOut("Using the default 236627 EU009184.1 Shigella dysenteriae str. FBD013.\n"); }
if (outputdir == "") { outputdir = util.hasPath(fastafile); }
consfile = validParameter.validFile(parameters, "conservation");
if (consfile == "not open") { abort = true; }
else if (consfile == "not found") {
consfile = "";
//check for consfile
string tempConsFile = util.getRootName(inputDir + util.getSimpleName(templatefile)) + "freq";
ifstream FileTest(tempConsFile.c_str());
if(FileTest){
string line = util.getline(FileTest);
bool GoodFile = util.checkReleaseVersion(line, current->getVersion());
if (GoodFile) {
m->mothurOut("I found " + tempConsFile + " in your input file directory. I will use it to save time.\n"); consfile = tempConsFile; FileTest.close();
}
}else {
string tempConsFile = current->getDefaultPath()[0] + util.getRootName(util.getSimpleName(templatefile)) + "freq";
ifstream FileTest2(tempConsFile.c_str());
if(FileTest2){
string line = util.getline(FileTest2);
bool GoodFile = util.checkReleaseVersion(line, current->getVersion());
if (GoodFile) {
m->mothurOut("I found " + tempConsFile + " in your input file directory. I will use it to save time.\n"); consfile = tempConsFile; FileTest2.close();
}
}
}
}
quanfile = validParameter.validFile(parameters, "quantile");
if (quanfile == "not open") { abort = true; }
else if (quanfile == "not found") { quanfile = ""; }
temp = validParameter.valid(parameters, "removechimeras"); if (temp == "not found") { temp = "t"; }
removeChimeras = util.isTrue(temp);
}
}
catch(exception& e) {
m->errorOut(e, "ChimeraPintailCommand", "ChimeraPintailCommand");
exit(1);
}
}
//***************************************************************************************************************
int ChimeraPintailCommand::execute(){
try{
if (abort) { if (calledHelp) { return 0; } return 2; }
m->mothurOut("Checking sequences from " + fastafile + " ...\n" );
long start = time(nullptr);
//check for quantile to save the time
lookForShortcutFiles(templatefile);
numSeqs = checkChimeras();
if (m->getControl_pressed()) { outputTypes.clear(); for (int j = 0; j < outputNames.size(); j++) { util.mothurRemove(outputNames[j]); } return 0; }
m->mothurOut("\n\nIt took " + toString(time(nullptr) - start) + " secs to check " + toString(numSeqs) + " sequences.\n");
if (removeChimeras) {
string accnosFileName = "";
itTypes = outputTypes.find("accnos");
if (itTypes != outputTypes.end()) { if ((itTypes->second).size() != 0) { accnosFileName = (itTypes->second)[0]; } }
if (!util.isBlank(accnosFileName)) {
m->mothurOut("\nRemoving chimeras from your input files:\n");
string inputString = "fasta=" + fastafile + ", accnos=" + accnosFileName;
m->mothurOut("/******************************************/\n");
m->mothurOut("Running command: remove.seqs(" + inputString + ")\n");
current->setMothurCalling(true);
Command* removeCommand = new RemoveSeqsCommand(inputString);
removeCommand->execute();
map<string, vector<string> > filenames = removeCommand->getOutputFiles();
delete removeCommand;
current->setMothurCalling(false);
m->mothurOut("/******************************************/\n");
map<string, string> variables;
variables["[filename]"] = outputdir + util.getRootName(util.getSimpleName(fastafile));
string currentName = getOutputFileName("fasta", variables);
util.renameFile(filenames["fasta"][0], currentName);
util.mothurRemove(filenames["fasta"][0]);
outputNames.push_back(currentName); outputTypes["fasta"].push_back(currentName);
}else { m->mothurOut("\nNo chimeras found, skipping remove.seqs.\n"); }
}
//set accnos file as new current accnosfile
string currentName = "";
itTypes = outputTypes.find("accnos");
if (itTypes != outputTypes.end()) {
if ((itTypes->second).size() != 0) { currentName = (itTypes->second)[0]; current->setAccnosFile(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, "ChimeraPintailCommand", "execute");
exit(1);
}
}
//**********************************************************************************************************************
int ChimeraPintailCommand::checkChimeras(){
try {
MothurChimera* chimera = new Pintail(fastafile, templatefile, filter, maskfile, consfile, quanfile, window, increment, outputdir, current->getVersion());
if (m->getControl_pressed()) { delete chimera; return 0; }
if (chimera->getUnaligned()) { m->mothurOut("[ERROR]: Your reference sequences are unaligned, please correct.\n"); delete chimera; return 0; }
templateSeqsLength = chimera->getLength();
string outputFileName, accnosFileName;
map<string, string> variables;
variables["[filename]"] = outputdir + util.getRootName(util.getSimpleName(fastafile));
if (maskfile != "") { variables["[tag]"] = util.getSimpleName(util.getRootName(maskfile)); }
outputFileName = getOutputFileName("chimera", variables);
accnosFileName = getOutputFileName("accnos", variables);
outputNames.push_back(outputFileName); outputTypes["chimera"].push_back(outputFileName);
outputNames.push_back(accnosFileName); outputTypes["accnos"].push_back(accnosFileName);
ofstream out; util.openOutputFile(outputFileName, out);
ofstream out2; util.openOutputFile(accnosFileName, out2);
ifstream inFASTA; util.openInputFile(fastafile, inFASTA);
int count = 0;
while (!inFASTA.eof()) {
if (m->getControl_pressed()) { break; }
Sequence* candidateSeq = new Sequence(inFASTA); gobble(inFASTA);
if (candidateSeq->getName() != "") { //incase there is a commented sequence at the end of a file
if (candidateSeq->getAligned().length() != templateSeqsLength) { //chimeracheck does not require seqs to be aligned
m->mothurOut("[WARNING]: " + candidateSeq->getName() + " is not the same length as the template sequences. Skipping.\n");
}else{
//find chimeras
chimera->getChimeras(candidateSeq);
if (m->getControl_pressed()) { delete candidateSeq; return 1; }
//print results
chimera->print(out, out2);
}
count++;
}
delete candidateSeq;
//report progress
if((count) % 100 == 0){ m->mothurOutJustToScreen("Processing sequence: " + toString(count) + "\n"); }
}
//report progress
if((count) % 100 != 0){ m->mothurOutJustToScreen("Processing sequence: " + toString(count) + "\n"); }
out.close(); out2.close(); inFASTA.close();
delete chimera;
return count;
}
catch(exception& e) {
m->errorOut(e, "ChimeraPintailCommand", "checkChiemras");
exit(1);
}
}
//**********************************************************************************************************************
int ChimeraPintailCommand::lookForShortcutFiles(string baseName){
try {
string tempQuan = "";
if ((!filter) && (maskfile == "")) {
tempQuan = inputDir + util.getRootName(util.getSimpleName(baseName)) + "pintail.quan";
}else if ((!filter) && (maskfile != "")) {
tempQuan = inputDir + util.getRootName(util.getSimpleName(baseName)) + "pintail.masked.quan";
}else if ((filter) && (maskfile != "")) {
tempQuan = inputDir + util.getRootName(util.getSimpleName(baseName)) + "pintail.filtered." + util.getSimpleName(util.getRootName(fastafile)) + "masked.quan";
}else if ((filter) && (maskfile == "")) {
tempQuan = inputDir + util.getRootName(util.getSimpleName(baseName)) + "pintail.filtered." + util.getSimpleName(util.getRootName(fastafile)) + "quan";
}
ifstream FileTest(tempQuan.c_str());
if(FileTest){
string line = util.getline(FileTest);
bool GoodFile = util.checkReleaseVersion(line, current->getVersion());
if (GoodFile) {
m->mothurOut("I found " + tempQuan + " in your input file directory. I will use it to save time.\n"); quanfile = tempQuan; FileTest.close();
}
}else {
string tryPath = current->getDefaultPath()[0];
string tempQuan = "";
if ((!filter) && (maskfile == "")) {
tempQuan = tryPath + util.getRootName(util.getSimpleName(baseName)) + "pintail.quan";
}else if ((!filter) && (maskfile != "")) {
tempQuan = tryPath + util.getRootName(util.getSimpleName(baseName)) + "pintail.masked.quan";
}else if ((filter) && (maskfile != "")) {
tempQuan = tryPath + util.getRootName(util.getSimpleName(baseName)) + "pintail.filtered." + util.getSimpleName(util.getRootName(fastafile)) + "masked.quan";
}else if ((filter) && (maskfile == "")) {
tempQuan = tryPath + util.getRootName(util.getSimpleName(baseName)) + "pintail.filtered." + util.getSimpleName(util.getRootName(fastafile)) + "quan";
}
ifstream FileTest2(tempQuan.c_str());
if(FileTest2){
string line = util.getline(FileTest2);
bool GoodFile = util.checkReleaseVersion(line, current->getVersion());
if (GoodFile) {
m->mothurOut("I found " + tempQuan + " in your input file directory. I will use it to save time.\n"); quanfile = tempQuan; FileTest2.close();
}
}
}
return 0;
}
catch(exception& e) {
m->errorOut(e, "ChimeraPintailCommand", "lookForShortcutFiles");
exit(1);
}
}
/**************************************************************************************************/
|