X-Git-Url: https://git.donarmstrong.com/?a=blobdiff_plain;f=getseqscommand.cpp;h=9f2b5060fcaa715f9a2fd3fc484ba585d6322da9;hb=fa08e82ec2dd48f73d051c210dad54a403308949;hp=4b93d1fdeea1aac9048013d755ceda466e5f37a1;hpb=3aa0b70bf057656ccc2bae1de9816583f2a91779;p=mothur.git diff --git a/getseqscommand.cpp b/getseqscommand.cpp index 4b93d1f..9f2b506 100644 --- a/getseqscommand.cpp +++ b/getseqscommand.cpp @@ -14,7 +14,7 @@ //********************************************************************************************************************** vector GetSeqsCommand::getValidParameters(){ try { - string Array[] = {"fasta","name", "group", "alignreport", "accnos", "dups", "list","taxonomy","outputdir","inputdir"}; + string Array[] = {"fasta","name", "group", "qfile","alignreport", "accnos", "accnos2","dups", "list","taxonomy","outputdir","inputdir"}; vector myArray (Array, Array+(sizeof(Array)/sizeof(string))); return myArray; } @@ -35,6 +35,8 @@ GetSeqsCommand::GetSeqsCommand(){ outputTypes["group"] = tempOutNames; outputTypes["alignreport"] = tempOutNames; outputTypes["list"] = tempOutNames; + outputTypes["qfile"] = tempOutNames; + outputTypes["accnosreport"] = tempOutNames; } catch(exception& e) { m->errorOut(e, "GetSeqsCommand", "GetSeqsCommand"); @@ -74,7 +76,7 @@ GetSeqsCommand::GetSeqsCommand(string option) { else { //valid paramters for this command - string Array[] = {"fasta","name", "group", "alignreport", "accnos", "dups", "list","taxonomy","outputdir","inputdir"}; + string Array[] = {"fasta","name", "group", "alignreport", "qfile", "accnos", "accnos2","dups", "list","taxonomy","outputdir","inputdir"}; vector myArray (Array, Array+(sizeof(Array)/sizeof(string))); OptionParser parser(option); @@ -96,6 +98,8 @@ GetSeqsCommand::GetSeqsCommand(string option) { outputTypes["group"] = tempOutNames; outputTypes["alignreport"] = tempOutNames; outputTypes["list"] = tempOutNames; + outputTypes["qfile"] = tempOutNames; + outputTypes["accnosreport"] = 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 = ""; } @@ -129,6 +133,14 @@ GetSeqsCommand::GetSeqsCommand(string option) { if (path == "") { parameters["accnos"] = inputDir + it->second; } } + it = parameters.find("accnos2"); + //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["accnos2"] = inputDir + it->second; } + } + it = parameters.find("list"); //user has given a template file if(it != parameters.end()){ @@ -160,6 +172,14 @@ GetSeqsCommand::GetSeqsCommand(string option) { //if the user has not given a path then, add inputdir. else leave path alone. if (path == "") { parameters["taxonomy"] = inputDir + it->second; } } + + it = parameters.find("qfile"); + //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["qfile"] = inputDir + it->second; } + } } @@ -168,6 +188,8 @@ GetSeqsCommand::GetSeqsCommand(string option) { if (accnosfile == "not open") { abort = true; } else if (accnosfile == "not found") { accnosfile = ""; m->mothurOut("You must provide an accnos file."); m->mothurOutEndLine(); abort = true; } + if (accnosfile2 == "not found") { accnosfile2 = ""; } + fastafile = validParameter.validFile(parameters, "fasta", true); if (fastafile == "not open") { abort = true; } else if (fastafile == "not found") { fastafile = ""; } @@ -192,11 +214,15 @@ GetSeqsCommand::GetSeqsCommand(string option) { if (taxfile == "not open") { abort = true; } else if (taxfile == "not found") { taxfile = ""; } + qualfile = validParameter.validFile(parameters, "qfile", true); + if (qualfile == "not open") { abort = true; } + else if (qualfile == "not found") { qualfile = ""; } + string usedDups = "true"; string temp = validParameter.validFile(parameters, "dups", false); if (temp == "not found") { temp = "false"; usedDups = ""; } dups = m->isTrue(temp); - if ((fastafile == "") && (namefile == "") && (groupfile == "") && (alignfile == "") && (listfile == "") && (taxfile == "")) { m->mothurOut("You must provide one of the following: fasta, name, group, alignreport, taxonomy or listfile."); m->mothurOutEndLine(); abort = true; } + if ((fastafile == "") && (namefile == "") && (groupfile == "") && (alignfile == "") && (listfile == "") && (taxfile == "") && (qualfile == "") && (accnosfile2 == "")) { m->mothurOut("You must provide one of the following: fasta, name, group, alignreport, taxonomy, quality or listfile."); m->mothurOutEndLine(); abort = true; } if ((usedDups != "") && (namefile == "")) { m->mothurOut("You may only use dups with the name option."); m->mothurOutEndLine(); abort = true; } @@ -212,9 +238,9 @@ GetSeqsCommand::GetSeqsCommand(string option) { void GetSeqsCommand::help(){ try { - m->mothurOut("The get.seqs command reads an .accnos file and any of the following file types: fasta, name, group, list, taxonomy or alignreport file.\n"); + m->mothurOut("The get.seqs command reads an .accnos file and any of the following file types: fasta, name, group, list, taxonomy, quality or alignreport file.\n"); m->mothurOut("It outputs a file containing only the sequences in the .accnos file.\n"); - m->mothurOut("The get.seqs command parameters are accnos, fasta, name, group, list, taxonomy, alignreport and dups. You must provide accnos and at least one of the other parameters.\n"); + m->mothurOut("The get.seqs command parameters are accnos, fasta, name, group, list, taxonomy, qfile, alignreport and dups. You must provide accnos and at least one of the other parameters.\n"); m->mothurOut("The dups parameter allows you to add the entire line from a name file if you add any name from the line. default=false. \n"); m->mothurOut("The get.seqs command should be in the following format: get.seqs(accnos=yourAccnos, fasta=yourFasta).\n"); m->mothurOut("Example get.seqs(accnos=amazon.accnos, fasta=amazon.fasta).\n"); @@ -239,15 +265,19 @@ int GetSeqsCommand::execute(){ if (m->control_pressed) { return 0; } //read through the correct file and output lines you want to keep - if (namefile != "") { readName(); } - if (fastafile != "") { readFasta(); } - if (groupfile != "") { readGroup(); } - if (alignfile != "") { readAlign(); } - if (listfile != "") { readList(); } - if (taxfile != "") { readTax(); } + if (namefile != "") { readName(); } + if (fastafile != "") { readFasta(); } + if (groupfile != "") { readGroup(); } + if (alignfile != "") { readAlign(); } + if (listfile != "") { readList(); } + if (taxfile != "") { readTax(); } + if (qualfile != "") { readQual(); } + if (accnosfile2 != "") { compareAccnos(); } if (m->control_pressed) { outputTypes.clear(); for (int i = 0; i < outputNames.size(); i++) { remove(outputNames[i].c_str()); } return 0; } + m->mothurOut("Selected " + toString(names.size()) + " sequences."); m->mothurOutEndLine(); + if (outputNames.size() != 0) { m->mothurOutEndLine(); m->mothurOut("Output File Names: "); m->mothurOutEndLine(); @@ -313,6 +343,71 @@ int GetSeqsCommand::readFasta(){ } } //********************************************************************************************************************** +int GetSeqsCommand::readQual(){ + try { + string thisOutputDir = outputDir; + if (outputDir == "") { thisOutputDir += m->hasPath(qualfile); } + string outputFileName = thisOutputDir + m->getRootName(m->getSimpleName(qualfile)) + "pick" + m->getExtension(qualfile); + ofstream out; + m->openOutputFile(outputFileName, out); + + + ifstream in; + m->openInputFile(qualfile, in); + string name; + + bool wroteSomething = false; + + + while(!in.eof()){ + string saveName = ""; + string name = ""; + string scores = ""; + + in >> name; + + if (name.length() != 0) { + saveName = name.substr(1); + while (!in.eof()) { + char c = in.get(); + if (c == 10 || c == 13){ break; } + else { name += c; } + } + m->gobble(in); + } + + while(in){ + char letter= in.get(); + if(letter == '>'){ in.putback(letter); break; } + else{ scores += letter; } + } + + m->gobble(in); + + if (names.count(saveName) != 0) { + wroteSomething = true; + + out << name << endl << scores; + } + + m->gobble(in); + } + in.close(); + out.close(); + + + if (wroteSomething == false) { m->mothurOut("Your file does not contain any sequence from the .accnos file."); m->mothurOutEndLine(); } + outputNames.push_back(outputFileName); outputTypes["qfile"].push_back(outputFileName); + + return 0; + + } + catch(exception& e) { + m->errorOut(e, "GetSeqsCommand", "readQual"); + exit(1); + } +} +//********************************************************************************************************************** int GetSeqsCommand::readList(){ try { string thisOutputDir = outputDir; @@ -664,6 +759,103 @@ int GetSeqsCommand::readAccnos(){ exit(1); } } +//********************************************************************************************************************** + +int GetSeqsCommand::compareAccnos(){ + try { + + string thisOutputDir = outputDir; + if (outputDir == "") { thisOutputDir += m->hasPath(accnosfile); } + string outputFileName = thisOutputDir + m->getRootName(m->getSimpleName(accnosfile)) + "accnos.report"; + ofstream out; + m->openOutputFile(outputFileName, out); + + ifstream in; + m->openInputFile(accnosfile2, in); + string name; + + set namesAccnos2; + set namesDups; + set namesAccnos = names; + + map nameCount; + + if (namefile != "") { + ifstream inName; + m->openInputFile(namefile, inName); + + + while(!inName.eof()){ + + if (m->control_pressed) { inName.close(); return 0; } + + string thisname, repnames; + + inName >> thisname; m->gobble(inName); //read from first column + inName >> repnames; //read from second column + + int num = m->getNumNames(repnames); + nameCount[thisname] = num; + + m->gobble(inName); + } + inName.close(); + } + + while(!in.eof()){ + in >> name; + + if (namesAccnos.count(name) == 0){ //name unique to accnos2 + namesAccnos2.insert(name); + }else { //you are in both so erase + namesAccnos.erase(name); + namesDups.insert(name); + } + + m->gobble(in); + } + in.close(); + + out << "Names in both files : " + toString(namesDups.size()) << endl; + m->mothurOut("Names in both files : " + toString(namesDups.size())); m->mothurOutEndLine(); + + for (set::iterator it = namesDups.begin(); it != namesDups.end(); it++) { + out << (*it); + if (namefile != "") { out << '\t' << nameCount[(*it)]; } + out << endl; + } + + out << "Names unique to " + accnosfile + " : " + toString(namesAccnos.size()) << endl; + m->mothurOut("Names unique to " + accnosfile + " : " + toString(namesAccnos.size())); m->mothurOutEndLine(); + + for (set::iterator it = namesAccnos.begin(); it != namesAccnos.end(); it++) { + out << (*it); + if (namefile != "") { out << '\t' << nameCount[(*it)]; } + out << endl; + } + + out << "Names unique to " + accnosfile2 + " : " + toString(namesAccnos2.size()) << endl; + m->mothurOut("Names unique to " + accnosfile2 + " : " + toString(namesAccnos2.size())); m->mothurOutEndLine(); + + for (set::iterator it = namesAccnos2.begin(); it != namesAccnos2.end(); it++) { + out << (*it); + if (namefile != "") { out << '\t' << nameCount[(*it)]; } + out << endl; + } + + out.close(); + + outputNames.push_back(outputFileName); outputTypes["accnosreport"].push_back(outputFileName); + + return 0; + + } + catch(exception& e) { + m->errorOut(e, "GetSeqsCommand", "readAccnos"); + exit(1); + } +} + //**********************************************************************************************************************