X-Git-Url: https://git.donarmstrong.com/?a=blobdiff_plain;f=getseqscommand.cpp;h=a191515cb601408b2c7d884b895f7e14d668d798;hb=d04f948b1a2a1a2984fc4a45d04403b8c121c5bc;hp=39c4fe001c8b9b19ef31c5a9a7f3ad98213301d6;hpb=e3e0858c684c4608d679840024e5095ddc8e2719;p=mothur.git diff --git a/getseqscommand.cpp b/getseqscommand.cpp index 39c4fe0..a191515 100644 --- a/getseqscommand.cpp +++ b/getseqscommand.cpp @@ -14,7 +14,7 @@ //********************************************************************************************************************** vector GetSeqsCommand::getValidParameters(){ try { - string Array[] = {"fasta","name", "group", "qfile","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; } @@ -36,6 +36,7 @@ GetSeqsCommand::GetSeqsCommand(){ outputTypes["alignreport"] = tempOutNames; outputTypes["list"] = tempOutNames; outputTypes["qfile"] = tempOutNames; + outputTypes["accnosreport"] = tempOutNames; } catch(exception& e) { m->errorOut(e, "GetSeqsCommand", "GetSeqsCommand"); @@ -75,7 +76,7 @@ GetSeqsCommand::GetSeqsCommand(string option) { else { //valid paramters for this command - string Array[] = {"fasta","name", "group", "alignreport", "qfile", "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); @@ -98,6 +99,7 @@ GetSeqsCommand::GetSeqsCommand(string option) { 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 = ""; } @@ -131,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()){ @@ -178,6 +188,10 @@ 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; } + accnosfile2 = validParameter.validFile(parameters, "accnos2", true); + if (accnosfile2 == "not open") { 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 = ""; } @@ -210,7 +224,7 @@ GetSeqsCommand::GetSeqsCommand(string option) { 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 == "") && (qualfile == "")) { m->mothurOut("You must provide one of the following: fasta, name, group, alignreport, taxonomy, quality 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; } @@ -253,13 +267,14 @@ 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 (qualfile != "") { readQual(); } + 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; } @@ -746,6 +761,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); + } +} + //**********************************************************************************************************************