+int ParseFastaQCommand::findGroup(fastqRead2 thisRead, int& barcode, int& primer, TrimOligos*& trimOligos, TrimOligos*& rtrimOligos, int numBarcodes, int numPrimers) {
+ try {
+ int success = 1;
+ string trashCode = "";
+ int currentSeqsDiffs = 0;
+
+ Sequence currSeq(thisRead.seq.getName(), thisRead.seq.getAligned());
+ QualityScores currQual; currQual.setScores(convertQual(thisRead.quality));
+
+ //for reorient
+ Sequence savedSeq(currSeq.getName(), currSeq.getAligned());
+ QualityScores savedQual(currQual.getName(), currQual.getScores());
+
+ if(numLinkers != 0){
+ success = trimOligos->stripLinker(currSeq, currQual);
+ if(success > ldiffs) { trashCode += 'k'; }
+ else{ currentSeqsDiffs += success; }
+
+ }
+
+ if(numBarcodes != 0){
+ success = trimOligos->stripBarcode(currSeq, currQual, barcode);
+ if(success > bdiffs) { trashCode += 'b'; }
+ else{ currentSeqsDiffs += success; }
+ }
+
+ if(numSpacers != 0){
+ success = trimOligos->stripSpacer(currSeq, currQual);
+ if(success > sdiffs) { trashCode += 's'; }
+ else{ currentSeqsDiffs += success; }
+
+ }
+
+ if(numPrimers != 0){
+ success = trimOligos->stripForward(currSeq, currQual, primer, true);
+ if(success > pdiffs) { trashCode += 'f'; }
+ else{ currentSeqsDiffs += success; }
+ }
+
+ if (currentSeqsDiffs > tdiffs) { trashCode += 't'; }
+
+ if(numRPrimers != 0){
+ success = trimOligos->stripReverse(currSeq, currQual);
+ if(!success) { trashCode += 'r'; }
+ }
+
+ if (reorient && (trashCode != "")) { //if you failed and want to check the reverse
+ int thisSuccess = 0;
+ string thisTrashCode = "";
+ int thisCurrentSeqsDiffs = 0;
+
+ int thisBarcodeIndex = 0;
+ int thisPrimerIndex = 0;
+ //cout << currSeq.getName() << '\t' << savedSeq.getUnaligned() << endl;
+ if(numBarcodes != 0){
+ thisSuccess = rtrimOligos->stripBarcode(savedSeq, savedQual, thisBarcodeIndex);
+ if(thisSuccess > bdiffs) { thisTrashCode += "b"; }
+ else{ thisCurrentSeqsDiffs += thisSuccess; }
+ }
+ //cout << currSeq.getName() << '\t' << savedSeq.getUnaligned() << endl;
+ if(numPrimers != 0){
+ thisSuccess = rtrimOligos->stripForward(savedSeq, savedQual, thisPrimerIndex, true);
+ if(thisSuccess > pdiffs) { thisTrashCode += "f"; }
+ else{ thisCurrentSeqsDiffs += thisSuccess; }
+ }
+
+ if (thisCurrentSeqsDiffs > tdiffs) { thisTrashCode += 't'; }
+
+ if (thisTrashCode == "") {
+ trashCode = thisTrashCode;
+ success = thisSuccess;
+ currentSeqsDiffs = thisCurrentSeqsDiffs;
+ barcode = thisBarcodeIndex;
+ primer = thisPrimerIndex;
+ savedSeq.reverseComplement();
+ currSeq.setAligned(savedSeq.getAligned());
+ savedQual.flipQScores();
+ currQual.setScores(savedQual.getScores());
+ }else { trashCode += "(" + thisTrashCode + ")"; }
+ }
+
+ if (trashCode.length() == 0) { //is this sequence in the ignore group
+ string thisGroup = oligos.getGroupName(barcode, primer);
+
+ int pos = thisGroup.find("ignore");
+ if (pos != string::npos) { trashCode += "i"; }
+ }
+
+
+ return trashCode.length();
+ }
+ catch(exception& e) {
+ m->errorOut(e, "ParseFastaQCommand", "findGroup");
+ exit(1);
+ }
+}
+//**********************************************************************************************************************
+int ParseFastaQCommand::findGroup(fastqRead2 thisRead, int& barcode, int& primer, string groupMode) {
+ try {
+ string trashCode = "";
+ primer = 0;
+
+ string group = groupMap->getGroup(thisRead.seq.getName());
+ if (group == "not found") { trashCode += "g"; } //scrap for group
+
+ return trashCode.length();
+ }
+ catch(exception& e) {
+ m->errorOut(e, "ParseFastaQCommand", "findGroup");
+ exit(1);
+ }
+}
+//***************************************************************************************************************
+
+bool ParseFastaQCommand::readOligos(string oligoFile){
+ try {
+ bool allBlank = false;
+ oligos.read(oligosfile);
+
+ if (m->control_pressed) { return false; } //error in reading oligos
+
+ if (oligos.hasPairedBarcodes()) {
+ pairedOligos = true;
+ numPrimers = oligos.getPairedPrimers().size();
+ numBarcodes = oligos.getPairedBarcodes().size();
+ }else {
+ pairedOligos = false;
+ numPrimers = oligos.getPrimers().size();
+ numBarcodes = oligos.getBarcodes().size();
+ }
+
+ numLinkers = oligos.getLinkers().size();
+ numSpacers = oligos.getSpacers().size();
+ numRPrimers = oligos.getReversePrimers().size();
+
+ vector<string> groupNames = oligos.getGroupNames();
+ if (groupNames.size() == 0) { allBlank = true; }
+
+
+ fastqFileNames.resize(oligos.getBarcodeNames().size());
+ for(int i=0;i<fastqFileNames.size();i++){
+ for(int j=0;j<oligos.getPrimerNames().size();j++){ fastqFileNames[i].push_back(""); }
+ }
+
+ set<string> uniqueNames; //used to cleanup outputFileNames
+ if (pairedOligos) {
+ map<int, oligosPair> barcodes = oligos.getPairedBarcodes();
+ map<int, oligosPair> primers = oligos.getPairedPrimers();
+ for(map<int, oligosPair>::iterator itBar = barcodes.begin();itBar != barcodes.end();itBar++){
+ for(map<int, oligosPair>::iterator itPrimer = primers.begin();itPrimer != primers.end(); itPrimer++){
+
+ string primerName = oligos.getPrimerName(itPrimer->first);
+ string barcodeName = oligos.getBarcodeName(itBar->first);
+
+ if ((primerName == "ignore") || (barcodeName == "ignore")) { } //do nothing
+ else if ((primerName == "") && (barcodeName == "")) { } //do nothing
+ else {
+ string comboGroupName = "";
+ string fastaFileName = "";
+ string qualFileName = "";
+ string nameFileName = "";
+ string countFileName = "";
+
+ if(primerName == ""){
+ comboGroupName = barcodeName;
+ }else{
+ if(barcodeName == ""){
+ comboGroupName = primerName;
+ }
+ else{
+ comboGroupName = barcodeName + "." + primerName;
+ }
+ }
+
+ ofstream temp;
+ map<string, string> variables;
+ variables["[filename]"] = outputDir + m->getRootName(m->getSimpleName(fastaQFile));
+ variables["[group]"] = comboGroupName;
+ string fastqFileName = getOutputFileName("fastq", variables);
+ if (uniqueNames.count(fastqFileName) == 0) {
+ outputNames.push_back(fastqFileName);
+ outputTypes["fastq"].push_back(fastqFileName);
+ uniqueNames.insert(fastqFileName);
+ }
+
+ fastqFileNames[itBar->first][itPrimer->first] = fastqFileName;
+ m->openOutputFile(fastqFileName, temp); temp.close();
+ }
+ }
+ }
+ }else {
+ map<string, int> barcodes = oligos.getBarcodes() ;
+ map<string, int> primers = oligos.getPrimers();
+ for(map<string, int>::iterator itBar = barcodes.begin();itBar != barcodes.end();itBar++){
+ for(map<string, int>::iterator itPrimer = primers.begin();itPrimer != primers.end(); itPrimer++){
+
+ string primerName = oligos.getPrimerName(itPrimer->second);
+ string barcodeName = oligos.getBarcodeName(itBar->second);
+
+ if ((primerName == "ignore") || (barcodeName == "ignore")) { } //do nothing
+ else if ((primerName == "") && (barcodeName == "")) { } //do nothing
+ else {
+ string comboGroupName = "";
+ string fastaFileName = "";
+ string qualFileName = "";
+ string nameFileName = "";
+ string countFileName = "";
+
+ if(primerName == ""){
+ comboGroupName = barcodeName;
+ }else{
+ if(barcodeName == ""){
+ comboGroupName = primerName;
+ }
+ else{
+ comboGroupName = barcodeName + "." + primerName;
+ }
+ }
+
+ ofstream temp;
+ map<string, string> variables;
+ variables["[filename]"] = outputDir + m->getRootName(m->getSimpleName(fastaQFile));
+ variables["[group]"] = comboGroupName;
+ string fastqFileName = getOutputFileName("fastq", variables);
+ if (uniqueNames.count(fastqFileName) == 0) {
+ outputNames.push_back(fastqFileName);
+ outputTypes["fastq"].push_back(fastqFileName);
+ uniqueNames.insert(fastqFileName);
+ }
+
+ fastqFileNames[itBar->second][itPrimer->second] = fastqFileName;
+ m->openOutputFile(fastqFileName, temp); temp.close();
+ }
+ }
+ }
+ }
+
+ if (allBlank) {
+ m->mothurOut("[WARNING]: your oligos file does not contain any group names. mothur will not create a groupfile."); m->mothurOutEndLine();
+ return false;
+ }
+
+ ofstream temp;
+ map<string, string> variables;
+ variables["[filename]"] = outputDir + m->getRootName(m->getSimpleName(fastaQFile));
+ variables["[group]"] = "scrap";
+ noMatchFile = getOutputFileName("fastq", variables);
+ m->openOutputFile(noMatchFile, temp); temp.close();
+
+ return true;
+
+ }
+ catch(exception& e) {
+ m->errorOut(e, "ParseFastaQCommand", "getOligos");
+ exit(1);
+ }
+}
+//***************************************************************************************************************
+bool ParseFastaQCommand::readGroup(string groupfile){
+ try {
+ fastqFileNames.clear();
+
+ groupMap = new GroupMap();
+ groupMap->readMap(groupfile);
+
+ //like barcodeNameVector - no primer names
+ vector<string> groups = groupMap->getNamesOfGroups();
+
+ fastqFileNames.resize(groups.size());
+ for (int i = 0; i < fastqFileNames.size(); i++) {
+ for (int j = 0; j < 1; j++) {
+
+ map<string, string> variables;
+ variables["[filename]"] = outputDir + m->getRootName(m->getSimpleName(fastaQFile));
+ variables["[group]"] = groups[i];
+ string thisFilename = getOutputFileName("fastq",variables);
+ outputNames.push_back(thisFilename);
+ outputTypes["fastq"].push_back(thisFilename);
+
+ ofstream temp;
+ m->openOutputFileBinary(thisFilename, temp); temp.close();
+ fastqFileNames[i].push_back(thisFilename);
+ }
+ }
+
+ map<string, string> variables;
+ variables["[filename]"] = outputDir + m->getRootName(m->getSimpleName(fastaQFile));
+ variables["[group]"] = "scrap";
+ noMatchFile = getOutputFileName("fastq",variables);
+ m->mothurRemove(noMatchFile);
+
+ return true;
+
+ }
+ catch(exception& e) {
+ m->errorOut(e, "ParseFastaQCommand", "readGroup");
+ exit(1);
+ }
+}
+//**********************************************************************************************************************