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
|
//
// getdistscommand.cpp
// Mothur
//
// Created by Sarah Westcott on 1/28/13.
// Copyright (c) 2013 Schloss Lab. All rights reserved.
//
#include "getdistscommand.h"
//**********************************************************************************************************************
vector<string> GetDistsCommand::setParameters(){
try {
CommandParameter pphylip("phylip", "InputTypes", "", "", "none", "PhylipColumn", "none","phylip",false,false,true); parameters.push_back(pphylip);
CommandParameter pcolumn("column", "InputTypes", "", "", "none", "PhylipColumn", "none","column",false,false,true); parameters.push_back(pcolumn);
CommandParameter paccnos("accnos", "InputTypes", "", "", "none", "none", "none","",false,true,true); parameters.push_back(paccnos);
CommandParameter pinputdir("inputdir", "String", "", "", "", "", "","",false,false); parameters.push_back(pinputdir);
CommandParameter poutputdir("outputdir", "String", "", "", "", "", "","",false,false); parameters.push_back(poutputdir);
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, "GetDistsCommand", "setParameters");
exit(1);
}
}
//**********************************************************************************************************************
string GetDistsCommand::getHelpString(){
try {
string helpString = "";
helpString += "The get.dists command selects distances from a phylip or column file related to groups or sequences listed in an accnos file.\n";
helpString += "The get.dists command parameters are accnos, phylip and column.\n";
helpString += "The get.dists command should be in the following format: get.dists(accnos=yourAccnos, phylip=yourPhylip).\n";
helpString += "Example get.dists(accnos=final.accnos, phylip=final.an.thetayc.0.03.lt.ave.dist).\n";
helpString += "Note: No spaces between parameter labels (i.e. accnos), '=' and parameters (i.e.final.accnos).\n";
return helpString;
}
catch(exception& e) {
m->errorOut(e, "GetDistsCommand", "getHelpString");
exit(1);
}
}
//**********************************************************************************************************************
string GetDistsCommand::getOutputPattern(string type) {
try {
string pattern = "";
if (type == "phylip") { pattern = "[filename],pick,[extension]"; }
else if (type == "column") { pattern = "[filename],pick,[extension]"; }
else { m->mothurOut("[ERROR]: No definition for type " + type + " output pattern.\n"); m->control_pressed = true; }
return pattern;
}
catch(exception& e) {
m->errorOut(e, "GetDistsCommand", "getOutputPattern");
exit(1);
}
}
//**********************************************************************************************************************
GetDistsCommand::GetDistsCommand(){
try {
abort = true; calledHelp = true;
setParameters();
vector<string> tempOutNames;
outputTypes["phylip"] = tempOutNames;
outputTypes["column"] = tempOutNames;
}
catch(exception& e) {
m->errorOut(e, "GetDistsCommand", "GetDistsCommand");
exit(1);
}
}
//**********************************************************************************************************************
GetDistsCommand::GetDistsCommand(string option) {
try {
abort = false; calledHelp = false;
//allow user to run help
if(option == "help") { help(); abort = true; calledHelp = true; }
else if(option == "citation") { citation(); abort = true; calledHelp = true;}
else {
vector<string> myArray = setParameters();
OptionParser parser(option);
map<string,string> parameters = parser.getParameters();
ValidParameters validParameter;
map<string,string>::iterator it;
//check to make sure all parameters are valid for command
for (it = parameters.begin(); it != parameters.end(); it++) {
if (validParameter.isValidParameter(it->first, myArray, it->second) != true) { abort = true; }
}
//initialize outputTypes
vector<string> tempOutNames;
outputTypes["column"] = tempOutNames;
outputTypes["phylip"] = tempOutNames;
//if the user changes the output directory command factory will send this info to us in the output parameter
outputDir = validParameter.validFile(parameters, "outputdir", false); if (outputDir == "not found"){ outputDir = ""; }
//if the user changes the input directory command factory will send this info to us in the output parameter
string inputDir = validParameter.validFile(parameters, "inputdir", false);
if (inputDir == "not found"){ inputDir = ""; }
else {
string path;
it = parameters.find("phylip");
//user has given a template file
if(it != parameters.end()){
path = m->hasPath(it->second);
//if the user has not given a path then, add inputdir. else leave path alone.
if (path == "") { parameters["phylip"] = inputDir + it->second; }
}
it = parameters.find("column");
//user has given a template file
if(it != parameters.end()){
path = m->hasPath(it->second);
//if the user has not given a path then, add inputdir. else leave path alone.
if (path == "") { parameters["column"] = inputDir + it->second; }
}
it = parameters.find("accnos");
//user has given a template file
if(it != parameters.end()){
path = m->hasPath(it->second);
//if the user has not given a path then, add inputdir. else leave path alone.
if (path == "") { parameters["accnos"] = inputDir + it->second; }
}
}
//check for required parameters
accnosfile = validParameter.validFile(parameters, "accnos", true);
if (accnosfile == "not open") { abort = true; }
else if (accnosfile == "not found") {
accnosfile = m->getAccnosFile();
if (accnosfile != "") { m->mothurOut("Using " + accnosfile + " as input file for the accnos parameter."); m->mothurOutEndLine(); }
else {
m->mothurOut("You have no valid accnos file and accnos is required."); m->mothurOutEndLine();
abort = true;
}
}else { m->setAccnosFile(accnosfile); }
phylipfile = validParameter.validFile(parameters, "phylip", true);
if (phylipfile == "not open") { phylipfile = ""; abort = true; }
else if (phylipfile == "not found") { phylipfile = ""; }
else { m->setPhylipFile(phylipfile); }
columnfile = validParameter.validFile(parameters, "column", true);
if (columnfile == "not open") { columnfile = ""; abort = true; }
else if (columnfile == "not found") { columnfile = ""; }
else { m->setColumnFile(columnfile); }
if ((phylipfile == "") && (columnfile == "")) {
//is there are current file available for either of these?
//give priority to column, then phylip
columnfile = m->getColumnFile();
if (columnfile != "") { m->mothurOut("Using " + columnfile + " as input file for the column parameter."); m->mothurOutEndLine(); }
else {
phylipfile = m->getPhylipFile();
if (phylipfile != "") { m->mothurOut("Using " + phylipfile + " as input file for the phylip parameter."); m->mothurOutEndLine(); }
else {
m->mothurOut("No valid current files. You must provide a phylip or column file."); m->mothurOutEndLine();
abort = true;
}
}
}
}
}
catch(exception& e) {
m->errorOut(e, "GetDistsCommand", "GetDistsCommand");
exit(1);
}
}
//**********************************************************************************************************************
int GetDistsCommand::execute(){
try {
if (abort == true) { if (calledHelp) { return 0; } return 2; }
//get names you want to keep
names = m->readAccnos(accnosfile);
if (m->control_pressed) { return 0; }
//read through the correct file and output lines you want to keep
if (phylipfile != "") { readPhylip(); }
if (columnfile != "") { readColumn(); }
if (m->control_pressed) { for (int i = 0; i < outputNames.size(); i++) { m->mothurRemove(outputNames[i]); } return 0; }
if (outputNames.size() != 0) {
m->mothurOutEndLine();
m->mothurOut("Output File names: "); m->mothurOutEndLine();
for (int i = 0; i < outputNames.size(); i++) { m->mothurOut(outputNames[i]); m->mothurOutEndLine(); }
m->mothurOutEndLine();
//set fasta file as new current fastafile
string current = "";
itTypes = outputTypes.find("phylip");
if (itTypes != outputTypes.end()) {
if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setPhylipFile(current); }
}
itTypes = outputTypes.find("column");
if (itTypes != outputTypes.end()) {
if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setColumnFile(current); }
}
}
return 0;
}
catch(exception& e) {
m->errorOut(e, "GetDistsCommand", "execute");
exit(1);
}
}
//**********************************************************************************************************************
int GetDistsCommand::readPhylip(){
try {
string thisOutputDir = outputDir;
if (outputDir == "") { thisOutputDir += m->hasPath(phylipfile); }
map<string, string> variables;
variables["[filename]"] = thisOutputDir + m->getRootName(m->getSimpleName(phylipfile));
variables["[extension]"] = m->getExtension(phylipfile);
string outputFileName = getOutputFileName("phylip", variables);
ifstream in;
m->openInputFile(phylipfile, in);
float distance;
int square, nseqs;
string name;
unsigned int row;
set<unsigned int> rows; //converts names in names to a index
row = 0;
string numTest;
in >> numTest >> name;
if (!m->isContainingOnlyDigits(numTest)) { m->mothurOut("[ERROR]: expected a number and got " + numTest + ", quitting."); m->mothurOutEndLine(); exit(1); }
else { convert(numTest, nseqs); }
if (names.count(name) != 0) { rows.insert(row); }
row++;
//is the matrix square?
char d;
while((d=in.get()) != EOF){
if(isalnum(d)){
square = 1;
in.putback(d);
for(int i=0;i<nseqs;i++){
in >> distance;
}
break;
}
if(d == '\n'){
square = 0;
break;
}
}
//map name to row/column
if(square == 0){
for(int i=1;i<nseqs;i++){
in >> name;
if (names.count(name) != 0) { rows.insert(row); }
row++;
for(int j=0;j<i;j++){
if (m->control_pressed) { in.close(); return 0; }
in >> distance;
}
}
}
else{
for(int i=1;i<nseqs;i++){
in >> name;
if (names.count(name) != 0) { rows.insert(row); }
row++;
for(int j=0;j<nseqs;j++){
if (m->control_pressed) { in.close(); return 0; }
in >> distance;
}
}
}
in.close();
if (m->control_pressed) { return 0; }
//read through file only printing rows and columns of seqs in names
ifstream inPhylip;
m->openInputFile(phylipfile, inPhylip);
inPhylip >> numTest;
ofstream out;
m->openOutputFile(outputFileName, out);
outputTypes["phylip"].push_back(outputFileName); outputNames.push_back(outputFileName);
out << names.size() << endl;
unsigned int count = 0;
if(square == 0){
for(int i=0;i<nseqs;i++){
inPhylip >> name;
bool ignoreRow = false;
if (names.count(name) == 0) { ignoreRow = true; }
else{ out << name << '\t'; count++; }
for(int j=0;j<i;j++){
if (m->control_pressed) { inPhylip.close(); out.close(); return 0; }
inPhylip >> distance;
if (!ignoreRow) {
//is this a column we want
if(rows.count(j) != 0) { out << distance << '\t'; }
}
}
if (!ignoreRow) { out << endl; }
}
}
else{
for(int i=0;i<nseqs;i++){
inPhylip >> name;
bool ignoreRow = false;
if (names.count(name) == 0) { ignoreRow = true; }
else{ out << name << '\t'; count++; }
for(int j=0;j<nseqs;j++){
if (m->control_pressed) { inPhylip.close(); out.close(); return 0; }
inPhylip >> distance;
if (!ignoreRow) {
//is this a column we want
if(rows.count(j) != 0) { out << distance << '\t'; }
}
}
if (!ignoreRow) { out << endl; }
}
}
inPhylip.close();
out.close();
if (count == 0) { m->mothurOut("Your file does NOT contain distances related to groups or sequences listed in the accnos file."); m->mothurOutEndLine(); }
else if (count != names.size()) {
m->mothurOut("[WARNING]: Your accnos file contains " + toString(names.size()) + " groups or sequences, but I only found " + toString(count) + " of them in the phylip file."); m->mothurOutEndLine();
//rewrite with new number
m->renameFile(outputFileName, outputFileName+".temp");
ofstream out2;
m->openOutputFile(outputFileName, out2);
out2 << count << endl;
ifstream in3;
m->openInputFile(outputFileName+".temp", in3);
in3 >> nseqs; m->gobble(in3);
char buffer[4096];
while (!in3.eof()) {
in3.read(buffer, 4096);
out2.write(buffer, in3.gcount());
}
in3.close();
out2.close();
m->mothurRemove(outputFileName+".temp");
}
m->mothurOut("Selected " + toString(count) + " groups or sequences from your phylip file."); m->mothurOutEndLine();
return 0;
}
catch(exception& e) {
m->errorOut(e, "GetDistsCommand", "readPhylip");
exit(1);
}
}
//**********************************************************************************************************************
int GetDistsCommand::readColumn(){
try {
string thisOutputDir = outputDir;
if (outputDir == "") { thisOutputDir += m->hasPath(columnfile); }
map<string, string> variables;
variables["[filename]"] = thisOutputDir + m->getRootName(m->getSimpleName(columnfile));
variables["[extension]"] = m->getExtension(columnfile);
string outputFileName = getOutputFileName("column", variables);
outputTypes["column"].push_back(outputFileName); outputNames.push_back(outputFileName);
ofstream out;
m->openOutputFile(outputFileName, out);
ifstream in;
m->openInputFile(columnfile, in);
set<string> foundNames;
string firstName, secondName;
float distance;
while (!in.eof()) {
if (m->control_pressed) { out.close(); in.close(); return 0; }
in >> firstName >> secondName >> distance; m->gobble(in);
//are both names in the accnos file
if ((names.count(firstName) != 0) && (names.count(secondName) != 0)) {
out << firstName << '\t' << secondName << '\t' << distance << endl;
foundNames.insert(firstName);
foundNames.insert(secondName);
}
}
in.close();
out.close();
if (foundNames.size() == 0) { m->mothurOut("Your file does NOT contain distances related to groups or sequences listed in the accnos file."); m->mothurOutEndLine(); }
else if (foundNames.size() != names.size()) {
m->mothurOut("[WARNING]: Your accnos file contains " + toString(names.size()) + " groups or sequences, but I only found " + toString(foundNames.size()) + " of them in the column file."); m->mothurOutEndLine();
}
m->mothurOut("Selected " + toString(foundNames.size()) + " groups or sequences from your column file."); m->mothurOutEndLine();
return 0;
}
catch(exception& e) {
m->errorOut(e, "GetDistsCommand", "readColumn");
exit(1);
}
}
//**********************************************************************************************************************
|