if (type == "fasta") { pattern = "[filename],[tag],contigs.fasta"; }
else if (type == "qfile") { pattern = "[filename],[tag],contigs.qual"; }
- else if (type == "group") { pattern = "[filename],[tag],groups"; }
+ else if (type == "group") { pattern = "[filename],[tag],contigs.groups"; }
else if (type == "mismatch") { pattern = "[filename],[tag],contigs.mismatch"; }
else { m->mothurOut("[ERROR]: No definition for type " + type + " output pattern.\n"); m->control_pressed = true; }
if (file == "not open") { abort = true; }
else if (file == "not found") { file = ""; }
- if ((file == "") && (ffastafile == "") && (ffastqfile == "")) { abort = true; m->mothurOut("[ERROR]: ffastq and rfastq parameters are required.\n"); }
- if ((ffastqfile != "") && (rfastqfile == "")) { abort = true; }
- if ((ffastqfile == "") && (rfastqfile != "")) { abort = true; }
+ //provide at least
+ if ((file == "") && (ffastafile == "") && (ffastqfile == "")) { abort = true; m->mothurOut("[ERROR]: The file, ffastq and rfastq or ffasta and rfasta parameters are required.\n"); }
+ if ((file != "") && ((ffastafile != "") || (ffastqfile != ""))) { abort = true; m->mothurOut("[ERROR]: The file, ffastq and rfastq or ffasta and rfasta parameters are required.\n"); }
+ if ((ffastqfile != "") && (rfastqfile == "")) { abort = true; m->mothurOut("[ERROR]: If you provide use the ffastq, you must provide a rfastq file.\n"); }
+ if ((ffastqfile == "") && (rfastqfile != "")) { abort = true; m->mothurOut("[ERROR]: If you provide use the rfastq, you must provide a ffastq file.\n"); }
+ if ((ffastafile != "") && (rfastafile == "")) { abort = true; m->mothurOut("[ERROR]: If you provide use the ffasta, you must provide a rfasta file.\n"); }
+ if ((ffastafile == "") && (rfastafile != "")) { abort = true; m->mothurOut("[ERROR]: If you provide use the rfasta, you must provide a ffasta file.\n"); }
+ if ((fqualfile != "") && (rqualfile == "")) { abort = true; m->mothurOut("[ERROR]: If you provide use the fqfile, you must provide a rqfile file.\n"); }
+ if ((fqualfile == "") && (rqualfile != "")) { abort = true; m->mothurOut("[ERROR]: If you provide use the rqfile, you must provide a fqfile file.\n"); }
+ if (((fqualfile != "") || (rqualfile != "")) && ((ffastafile == "") || (rfastafile == ""))) {
+ abort = true; m->mothurOut("[ERROR]: If you provide use the rqfile or fqfile file, you must provide the ffasta and rfasta parameters.\n");
+ }
oligosfile = validParameter.validFile(parameters, "oligos", true);
if (oligosfile == "not found") { oligosfile = ""; }
else { m->setOligosFile(oligosfile); }
//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 = m->hasPath(ffastqfile); }
+ outputDir = validParameter.validFile(parameters, "outputdir", false); if (outputDir == "not found"){
+ outputDir = "";
+ }
//check for optional parameter and set defaults
//read ffastq and rfastq files creating fasta and qual files.
//this function will create a forward and reverse, fasta and qual files for each processor.
- //files has an entry for each processor. files[i][0] = forwardFasta, files[i][1] = forwardQual, files[i][2] = reverseFasta, files[i][3] = reverseQual
+ //files has an entry for each processor. files[i][0] = forwardFasta, files[i][1] = forwardQual, files[i][2] = reverseFasta, files[i][3] = reverseQual. filesToProcess is for each filepair in the file parameter file. for ffastq and rfastq this will be size 1.
unsigned long int numReads = 0;
int start = time(NULL);
longestBase = 1000;
m->mothurOut("Reading fastq data...\n");
- vector< vector<string> > files = readFastqFiles(numReads);
+ vector < vector< vector<string> > > filesToProcess = preProcessData(numReads);
m->mothurOut("Done.\n");
if (m->control_pressed) { return 0; }
- vector<vector<string> > fastaFileNames;
- vector<vector<string> > qualFileNames;
- createGroup = false;
- string outputGroupFileName;
- map<string, string> variables;
- variables["[filename]"] = outputDir + m->getRootName(m->getSimpleName(ffastqfile));
- variables["[tag]"] = "";
- if(oligosfile != ""){
- createGroup = getOligos(fastaFileNames, qualFileNames);
- if (createGroup) {
- outputGroupFileName = getOutputFileName("group",variables);
- outputNames.push_back(outputGroupFileName); outputTypes["group"].push_back(outputGroupFileName);
- }
- }
-
- variables["[tag]"] = "trim";
- string outFastaFile = getOutputFileName("fasta",variables);
- string outQualFile = getOutputFileName("qfile",variables);
- variables["[tag]"] = "scrap";
- string outScrapFastaFile = getOutputFileName("fasta",variables);
- string outScrapQualFile = getOutputFileName("qfile",variables);
-
- variables["[tag]"] = "";
- string outMisMatchFile = getOutputFileName("mismatch",variables);
- outputNames.push_back(outFastaFile); outputTypes["fasta"].push_back(outFastaFile);
- outputNames.push_back(outQualFile); outputTypes["qfile"].push_back(outQualFile);
- outputNames.push_back(outScrapFastaFile); outputTypes["fasta"].push_back(outScrapFastaFile);
- outputNames.push_back(outScrapQualFile); outputTypes["qfile"].push_back(outScrapQualFile);
- outputNames.push_back(outMisMatchFile); outputTypes["mismatch"].push_back(outMisMatchFile);
-
- m->mothurOut("Making contigs...\n");
- createProcesses(files, outFastaFile, outQualFile, outScrapFastaFile, outScrapQualFile, outMisMatchFile, fastaFileNames, qualFileNames);
- m->mothurOut("Done.\n");
-
- //remove temp fasta and qual files
- for (int i = 0; i < processors; i++) { for(int j = 0; j < files[i].size(); j++) { m->mothurRemove(files[i][j]); } }
+ map<string, string> cvars;
+ string compOutputDir = outputDir;
+ if (outputDir == "") { compOutputDir = m->hasPath(file); }
+ cvars["[filename]"] = compOutputDir + m->getRootName(m->getSimpleName(file));
+ cvars["[tag]"] = "";
+ string compositeGroupFile = getOutputFileName("group",cvars);
+ cvars["[tag]"] = "trim";
+ string compositeFastaFile = getOutputFileName("fasta",cvars);
+ string compositeQualFile = getOutputFileName("qfile",cvars);
+ cvars["[tag]"] = "scrap";
+ string compositeScrapFastaFile = getOutputFileName("fasta",cvars);
+ string compositeScrapQualFile = getOutputFileName("qfile",cvars);
+ cvars["[tag]"] = "";
+ string compositeMisMatchFile = getOutputFileName("mismatch",cvars);
- if (m->control_pressed) { for (int i = 0; i < outputNames.size(); i++) { m->mothurRemove(outputNames[i]); } return 0; }
+ if (filesToProcess.size() > 1) { //clear files for append below
+ ofstream outCTFasta, outCTQual, outCSFasta, outCSQual, outCMisMatch;
+ m->openOutputFile(compositeFastaFile, outCTFasta); outCTFasta.close();
+ m->openOutputFile(compositeScrapFastaFile, outCSFasta); outCSFasta.close();
+ m->openOutputFile(compositeMisMatchFile, outCMisMatch); outCMisMatch.close();
+ m->openOutputFile(compositeQualFile, outCTQual); outCTQual.close();
+ m->openOutputFile(compositeScrapQualFile, outCSQual); outCSQual.close();
+ outputNames.push_back(compositeFastaFile); outputTypes["fasta"].push_back(compositeFastaFile);
+ outputNames.push_back(compositeQualFile); outputTypes["qfile"].push_back(compositeQualFile);
+ outputNames.push_back(compositeMisMatchFile); outputTypes["mismatch"].push_back(compositeMisMatchFile);
+ outputNames.push_back(compositeScrapFastaFile); outputTypes["fasta"].push_back(compositeScrapFastaFile);
+ outputNames.push_back(compositeScrapQualFile); outputTypes["qfile"].push_back(compositeScrapQualFile);
+ }
- if(allFiles){
- map<string, string> uniqueFastaNames;// so we don't add the same groupfile multiple times
- map<string, string>::iterator it;
- set<string> namesToRemove;
- for(int i=0;i<fastaFileNames.size();i++){
- for(int j=0;j<fastaFileNames[0].size();j++){
- if (fastaFileNames[i][j] != "") {
- if (namesToRemove.count(fastaFileNames[i][j]) == 0) {
- if(m->isBlank(fastaFileNames[i][j])){
- m->mothurRemove(fastaFileNames[i][j]);
- namesToRemove.insert(fastaFileNames[i][j]);
-
- m->mothurRemove(qualFileNames[i][j]);
- namesToRemove.insert(qualFileNames[i][j]);
- }else{
- it = uniqueFastaNames.find(fastaFileNames[i][j]);
- if (it == uniqueFastaNames.end()) {
- uniqueFastaNames[fastaFileNames[i][j]] = barcodeNameVector[i];
- }
- }
- }
- }
- }
- }
-
- //remove names for outputFileNames, just cleans up the output
- vector<string> outputNames2;
- for(int i = 0; i < outputNames.size(); i++) { if (namesToRemove.count(outputNames[i]) == 0) { outputNames2.push_back(outputNames[i]); } }
- outputNames = outputNames2;
-
- for (it = uniqueFastaNames.begin(); it != uniqueFastaNames.end(); it++) {
- ifstream in;
- m->openInputFile(it->first, in);
+ for (int l = 0; l < filesToProcess.size(); l++) {
+
+ m->mothurOut("\n>>>>>\tProcessing " + filesToProcess[l][0][0] + " (file " + toString(l+1) + " of " + toString(filesToProcess.size()) + ")\t<<<<<\n");
+
+ vector<vector<string> > fastaFileNames;
+ vector<vector<string> > qualFileNames;
+ createGroup = false;
+ string outputGroupFileName;
+ map<string, string> variables;
+ string thisOutputDir = outputDir;
+ if (outputDir == "") { thisOutputDir = m->hasPath(filesToProcess[l][0][0]); }
+ variables["[filename]"] = thisOutputDir + m->getRootName(m->getSimpleName(filesToProcess[l][0][0]));
+ variables["[tag]"] = "";
+ if(oligosfile != ""){
+ createGroup = getOligos(fastaFileNames, qualFileNames, variables["[filename]"]);
+ if (createGroup) {
+ outputGroupFileName = getOutputFileName("group",variables);
+ outputNames.push_back(outputGroupFileName); outputTypes["group"].push_back(outputGroupFileName);
+ }
+ }
+
+ variables["[tag]"] = "trim";
+ string outFastaFile = getOutputFileName("fasta",variables);
+ string outQualFile = getOutputFileName("qfile",variables);
+ variables["[tag]"] = "scrap";
+ string outScrapFastaFile = getOutputFileName("fasta",variables);
+ string outScrapQualFile = getOutputFileName("qfile",variables);
+ variables["[tag]"] = "";
+ string outMisMatchFile = getOutputFileName("mismatch",variables);
+ outputNames.push_back(outFastaFile); outputTypes["fasta"].push_back(outFastaFile);
+ outputNames.push_back(outScrapFastaFile); outputTypes["fasta"].push_back(outScrapFastaFile);
+ if (filesToProcess[l][0][1] != "") {
+ outputNames.push_back(outQualFile); outputTypes["qfile"].push_back(outQualFile);
+ outputNames.push_back(outScrapQualFile); outputTypes["qfile"].push_back(outScrapQualFile);
+ }
+ outputNames.push_back(outMisMatchFile); outputTypes["mismatch"].push_back(outMisMatchFile);
+
+ m->mothurOut("Making contigs...\n");
+ createProcesses(filesToProcess[l], outFastaFile, outQualFile, outScrapFastaFile, outScrapQualFile, outMisMatchFile, fastaFileNames, qualFileNames);
+ m->mothurOut("Done.\n");
+
+ //remove temp fasta and qual files
+ for (int i = 0; i < processors; i++) { for(int j = 0; j < filesToProcess[l][i].size(); j++) { m->mothurRemove(filesToProcess[l][i][j]); } }
+
+ if (m->control_pressed) { for (int i = 0; i < outputNames.size(); i++) { m->mothurRemove(outputNames[i]); } return 0; }
+
+ if(allFiles){
+ map<string, string> uniqueFastaNames;// so we don't add the same groupfile multiple times
+ map<string, string>::iterator it;
+ set<string> namesToRemove;
+ for(int i=0;i<fastaFileNames.size();i++){
+ for(int j=0;j<fastaFileNames[0].size();j++){
+ if (fastaFileNames[i][j] != "") {
+ if (namesToRemove.count(fastaFileNames[i][j]) == 0) {
+ if(m->isBlank(fastaFileNames[i][j])){
+ m->mothurRemove(fastaFileNames[i][j]);
+ namesToRemove.insert(fastaFileNames[i][j]);
+
+ if (filesToProcess[l][0][1] != "") {
+ m->mothurRemove(qualFileNames[i][j]);
+ namesToRemove.insert(qualFileNames[i][j]);
+ }
+ }else{
+ it = uniqueFastaNames.find(fastaFileNames[i][j]);
+ if (it == uniqueFastaNames.end()) {
+ uniqueFastaNames[fastaFileNames[i][j]] = barcodeNameVector[i];
+ }
+ }
+ }
+ }
+ }
+ }
- ofstream out;
- string thisGroupName = outputDir + m->getRootName(m->getSimpleName(it->first));
- thisGroupName += getOutputFileName("group",variables); outputNames.push_back(thisGroupName); outputTypes["group"].push_back(thisGroupName);
- m->openOutputFile(thisGroupName, out);
-
- while (!in.eof()){
- if (m->control_pressed) { break; }
+ //remove names for outputFileNames, just cleans up the output
+ vector<string> outputNames2;
+ for(int i = 0; i < outputNames.size(); i++) { if (namesToRemove.count(outputNames[i]) == 0) { outputNames2.push_back(outputNames[i]); } }
+ outputNames = outputNames2;
+
+ for (it = uniqueFastaNames.begin(); it != uniqueFastaNames.end(); it++) {
+ ifstream in;
+ m->openInputFile(it->first, in);
+
+ ofstream out;
+ string thisGroupName = thisOutputDir + m->getRootName(m->getSimpleName(it->first));
+ thisGroupName += getOutputFileName("group",variables); outputNames.push_back(thisGroupName); outputTypes["group"].push_back(thisGroupName);
+ m->openOutputFile(thisGroupName, out);
- Sequence currSeq(in); m->gobble(in);
- out << currSeq.getName() << '\t' << it->second << endl;
+ while (!in.eof()){
+ if (m->control_pressed) { break; }
+
+ Sequence currSeq(in); m->gobble(in);
+ out << currSeq.getName() << '\t' << it->second << endl;
+ }
+ in.close();
+ out.close();
}
- in.close();
- out.close();
}
- }
-
- if (createGroup) {
- ofstream outGroup;
- m->openOutputFile(outputGroupFileName, outGroup);
- for (map<string, string>::iterator itGroup = groupMap.begin(); itGroup != groupMap.end(); itGroup++) {
- outGroup << itGroup->first << '\t' << itGroup->second << endl;
+
+ if (createGroup) {
+ ofstream outGroup;
+ m->openOutputFile(outputGroupFileName, outGroup);
+ for (map<string, string>::iterator itGroup = groupMap.begin(); itGroup != groupMap.end(); itGroup++) {
+ outGroup << itGroup->first << '\t' << itGroup->second << endl;
+ }
+ outGroup.close();
+ }
+
+ if (filesToProcess.size() > 1) { //merge into large combo files
+ if (createGroup) {
+ if (l == 0) {
+ ofstream outCGroup;
+ m->openOutputFile(compositeGroupFile, outCGroup); outCGroup.close();
+ outputNames.push_back(compositeGroupFile); outputTypes["group"].push_back(compositeGroupFile);
+ }
+ m->appendFiles(outputGroupFileName, compositeGroupFile);
+ }
+ m->appendFiles(outMisMatchFile, compositeMisMatchFile);
+ m->appendFiles(outFastaFile, compositeFastaFile);
+ m->appendFiles(outQualFile, compositeQualFile);
+ m->appendFiles(outScrapFastaFile, compositeScrapFastaFile);
+ m->appendFiles(outScrapQualFile, compositeScrapQualFile);
}
- outGroup.close();
}
m->mothurOut("It took " + toString(time(NULL) - start) + " secs to process " + toString(numReads) + " sequences.\n");
}
}
//**********************************************************************************************************************
+vector< vector< vector<string> > > MakeContigsCommand::preProcessData(unsigned long int& numReads) {
+ try {
+ vector< vector< vector<string> > > filesToProcess;
+
+ if (ffastqfile != "") { //reading one file
+ vector< vector<string> > files = readFastqFiles(numReads, ffastqfile, rfastqfile);
+ //adjust for really large processors or really small files
+ if (numReads == 0) { m->mothurOut("[ERROR]: no good reads.\n"); m->control_pressed = true; }
+ if (numReads < processors) {
+ for (int i = numReads; i < processors; i++) { for(int j = 0; j < files[i].size(); j++) { m->mothurRemove(files[i][j]); } files[i].clear(); }
+ files.resize(numReads);
+ processors = numReads;
+ }
+ filesToProcess.push_back(files);
+ }else if (file != "") { //reading multiple files
+ //return only valid pairs
+ vector< vector<string> > filePairsToProcess = readFileNames(file);
+
+ if (m->control_pressed) { return filesToProcess; }
+
+ if (filePairsToProcess.size() != 0) {
+ for (int i = 0; i < filePairsToProcess.size(); i++) {
+
+ if (m->control_pressed) { for (int l = 0; l < filesToProcess.size(); l++) { for (int k = 0; k < filesToProcess[l].size(); k++) { for(int j = 0; j < filesToProcess[l][k].size(); j++) { m->mothurRemove(filesToProcess[l][k][j]); } filesToProcess[l][k].clear(); } return filesToProcess; } }
+
+ unsigned long int thisFilesReads;
+ vector< vector<string> > files = readFastqFiles(thisFilesReads, filePairsToProcess[i][0], filePairsToProcess[i][1]);
+
+ //adjust for really large processors or really small files
+ if (thisFilesReads < processors) {
+ m->mothurOut("[ERROR]: " + filePairsToProcess[i][0] + " has less than " + toString(processors) + " good reads, skipping\n");
+ for (int k = 0; k < files.size(); k++) { for(int j = 0; j < files[k].size(); j++) { m->mothurRemove(files[k][j]); } files[k].clear(); }
+ }else {
+ filesToProcess.push_back(files);
+ numReads += thisFilesReads;
+ }
+ }
+ //all files are bad
+ if (numReads == 0) { m->control_pressed = true; }
+ }
+ }else if (ffastafile != "") {
+ vector< vector<string> > files = readFastaFiles(numReads, ffastafile, rfastafile);
+ //adjust for really large processors or really small files
+ if (numReads == 0) { m->mothurOut("[ERROR]: no good reads.\n"); m->control_pressed = true; }
+ if (numReads < processors) {
+ for (int i = numReads; i < processors; i++) { for(int j = 0; j < files[i].size(); j++) { m->mothurRemove(files[i][j]); } files[i].clear(); }
+ files.resize(numReads);
+ processors = numReads;
+ }
+ filesToProcess.push_back(files);
+ }else { m->control_pressed = true; } //should not get here
+
+ return filesToProcess;
+ }
+ catch(exception& e) {
+ m->errorOut(e, "MakeContigsCommand", "preProcessData");
+ exit(1);
+ }
+}
+//**********************************************************************************************************************
int MakeContigsCommand::createProcesses(vector< vector<string> > files, string outputFasta, string outputQual, string outputScrapFasta, string outputScrapQual, string outputMisMatches, vector<vector<string> > fastaFileNames, vector<vector<string> > qualFileNames) {
try {
int num = 0;
ofstream temp;
m->openOutputFile(outputFasta, temp); temp.close();
- m->openOutputFile(outputQual, temp); temp.close();
m->openOutputFile(outputScrapFasta, temp); temp.close();
- m->openOutputFile(outputScrapQual, temp); temp.close();
+ if (files[processors-1][1] != "") {
+ m->openOutputFile(outputScrapQual, temp); temp.close();
+ m->openOutputFile(outputQual, temp); temp.close();
+ }
//do my part
num = driver(files[processors-1], outputFasta, outputQual, outputScrapFasta, outputScrapQual, outputMisMatches, fastaFileNames, qualFileNames);
//parent do my part
ofstream temp;
m->openOutputFile(outputFasta, temp); temp.close();
- m->openOutputFile(outputQual, temp); temp.close();
m->openOutputFile(outputScrapFasta, temp); temp.close();
- m->openOutputFile(outputScrapQual, temp); temp.close();
-
+ if (files[processors-1][1] != "") {
+ m->openOutputFile(outputScrapQual, temp); temp.close();
+ m->openOutputFile(outputQual, temp); temp.close();
+ }
//do my part
num = driver(files[processors-1], (outputFasta+ toString(processors-1) + ".temp"), (outputQual+ toString(processors-1) + ".temp"), (outputScrapFasta+ toString(processors-1) + ".temp"), (outputScrapQual+ toString(processors-1) + ".temp"), (outputMisMatches+ toString(processors-1) + ".temp"), tempFASTAFileNames, tempPrimerQualFileNames);
m->appendFiles((outputFasta + toString(processIDS[i]) + ".temp"), outputFasta);
m->mothurRemove((outputFasta + toString(processIDS[i]) + ".temp"));
- m->appendFiles((outputQual + toString(processIDS[i]) + ".temp"), outputQual);
- m->mothurRemove((outputQual + toString(processIDS[i]) + ".temp"));
-
- m->appendFiles((outputScrapFasta + toString(processIDS[i]) + ".temp"), outputScrapFasta);
+ m->appendFiles((outputScrapFasta + toString(processIDS[i]) + ".temp"), outputScrapFasta);
m->mothurRemove((outputScrapFasta + toString(processIDS[i]) + ".temp"));
- m->appendFiles((outputScrapQual + toString(processIDS[i]) + ".temp"), outputScrapQual);
- m->mothurRemove((outputScrapQual + toString(processIDS[i]) + ".temp"));
+ if (files[processors-1][1] != "") {
+ m->appendFiles((outputScrapQual + toString(processIDS[i]) + ".temp"), outputScrapQual);
+ m->mothurRemove((outputScrapQual + toString(processIDS[i]) + ".temp"));
+
+ m->appendFiles((outputQual + toString(processIDS[i]) + ".temp"), outputQual);
+ m->mothurRemove((outputQual + toString(processIDS[i]) + ".temp"));
+ }
m->appendFiles((outputMisMatches + toString(processIDS[i]) + ".temp"), outputMisMatches);
m->mothurRemove((outputMisMatches + toString(processIDS[i]) + ".temp"));
m->appendFiles((fastaFileNames[j][k] + toString(processIDS[i]) + ".temp"), fastaFileNames[j][k]);
m->mothurRemove((fastaFileNames[j][k] + toString(processIDS[i]) + ".temp"));
- m->appendFiles((qualFileNames[j][k] + toString(processIDS[i]) + ".temp"), qualFileNames[j][k]);
- m->mothurRemove((qualFileNames[j][k] + toString(processIDS[i]) + ".temp"));
+ if (files[processors-1][1] != "") {
+ m->appendFiles((qualFileNames[j][k] + toString(processIDS[i]) + ".temp"), qualFileNames[j][k]);
+ m->mothurRemove((qualFileNames[j][k] + toString(processIDS[i]) + ".temp"));
+ }
}
}
}
if (m->debug) { m->mothurOut("[DEBUG]: ffasta = " + thisffastafile + ".\n[DEBUG]: fqual = " + thisfqualfile + ".\n[DEBUG]: rfasta = " + thisrfastafile + ".\n[DEBUG]: rqual = " + thisrqualfile + ".\n"); }
ifstream inFFasta, inRFasta, inFQual, inRQual;
+ ofstream outFasta, outQual, outMisMatch, outScrapFasta, outScrapQual;
m->openInputFile(thisffastafile, inFFasta);
- m->openInputFile(thisfqualfile, inFQual);
m->openInputFile(thisrfastafile, inRFasta);
- m->openInputFile(thisrqualfile, inRQual);
-
- ofstream outFasta, outQual, outMisMatch, outScrapFasta, outScrapQual;
+ if (thisfqualfile != "") {
+ m->openInputFile(thisfqualfile, inFQual);
+ m->openInputFile(thisrqualfile, inRQual);
+ m->openOutputFile(outputScrapQual, outScrapQual);
+ m->openOutputFile(outputQual, outQual);
+ }
m->openOutputFile(outputFasta, outFasta);
- m->openOutputFile(outputQual, outQual);
m->openOutputFile(outputScrapFasta, outScrapFasta);
- m->openOutputFile(outputScrapQual, outScrapQual);
m->openOutputFile(outputMisMatches, outMisMatch);
outMisMatch << "Name\tLength\tMisMatches\n";
TrimOligos trimOligos(pdiffs, bdiffs, 0, 0, primers, barcodes);
- while ((!inFQual.eof()) && (!inFFasta.eof()) && (!inRFasta.eof()) && (!inRQual.eof())) {
+ while ((!inFFasta.eof()) && (!inRFasta.eof())) {
if (m->control_pressed) { break; }
//read seqs and quality info
Sequence fSeq(inFFasta); m->gobble(inFFasta);
Sequence rSeq(inRFasta); m->gobble(inRFasta);
- QualityScores fQual(inFQual); m->gobble(inFQual);
- QualityScores rQual(inRQual); m->gobble(inRQual);
+ QualityScores* fQual = NULL; QualityScores* rQual = NULL;
+ if (thisfqualfile != "") {
+ fQual = new QualityScores(inFQual); m->gobble(inFQual);
+ rQual = new QualityScores(inRQual); m->gobble(inRQual);
+ }
int barcodeIndex = 0;
int primerIndex = 0;
if(barcodes.size() != 0){
- success = trimOligos.stripBarcode(fSeq, rSeq, fQual, rQual, barcodeIndex);
+ if (thisfqualfile != "") {
+ success = trimOligos.stripBarcode(fSeq, rSeq, *fQual, *rQual, barcodeIndex);
+ }else {
+ success = trimOligos.stripBarcode(fSeq, rSeq, barcodeIndex);
+ }
if(success > bdiffs) { trashCode += 'b'; }
else{ currentSeqsDiffs += success; }
}
if(primers.size() != 0){
- success = trimOligos.stripForward(fSeq, rSeq, fQual, rQual, primerIndex);
+ if (thisfqualfile != "") {
+ success = trimOligos.stripForward(fSeq, rSeq, *fQual, *rQual, primerIndex);
+ }else {
+ success = trimOligos.stripForward(fSeq, rSeq, primerIndex);
+ }
if(success > pdiffs) { trashCode += 'f'; }
else{ currentSeqsDiffs += success; }
}
//flip the reverse reads
rSeq.reverseComplement();
- rQual.flipQScores();
+ if (thisfqualfile != "") { rQual->flipQScores(); }
//pairwise align
alignment->align(fSeq.getUnaligned(), rSeq.getUnaligned());
int numMismatches = 0;
string seq1 = fSeq.getAligned();
string seq2 = rSeq.getAligned();
- vector<int> scores1 = fQual.getQualityScores();
- vector<int> scores2 = rQual.getQualityScores();
+ vector<int> scores1, scores2;
+ if (thisfqualfile != "") {
+ scores1 = fQual->getQualityScores();
+ scores2 = rQual->getQualityScores();
+ delete fQual; delete rQual;
+ }
// if (num < 5) { cout << fSeq.getStartPos() << '\t' << fSeq.getEndPos() << '\t' << rSeq.getStartPos() << '\t' << rSeq.getEndPos() << endl; }
int overlapStart = fSeq.getStartPos();
overlapStart = seq2Start;
for (int i = 0; i < overlapStart; i++) {
contig += seq1[i];
- contigScores.push_back(scores1[ABaseMap[i]]);
+ if (thisfqualfile != "") { contigScores.push_back(scores1[ABaseMap[i]]); }
}
}else { //seq1 starts later so take from 0 to overlapStart from seq2
for (int i = 0; i < overlapStart; i++) {
contig += seq2[i];
- contigScores.push_back(scores2[BBaseMap[i]]);
+ if (thisfqualfile != "") { contigScores.push_back(scores2[BBaseMap[i]]); }
}
}
for (int i = overlapStart; i < overlapEnd; i++) {
if (seq1[i] == seq2[i]) { //match, add base and choose highest score
contig += seq1[i];
- contigScores.push_back(scores1[ABaseMap[i]]);
- if (scores1[ABaseMap[i]] < scores2[BBaseMap[i]]) { contigScores[contigScores.size()-1] = scores2[BBaseMap[i]]; }
- }else if (((seq1[i] == '.') || (seq1[i] == '-')) && ((seq2[i] != '-') && (seq2[i] != '.'))) { //seq1 is a gap and seq2 is a base, choose seq2, unless quality score for base is below threshold. In that case eliminate base
- if (scores2[BBaseMap[i]] < threshold) { } //
- else {
- contig += seq2[i];
- contigScores.push_back(scores2[BBaseMap[i]]);
+ if (thisfqualfile != "") {
+ contigScores.push_back(scores1[ABaseMap[i]]);
+ if (scores1[ABaseMap[i]] < scores2[BBaseMap[i]]) { contigScores[contigScores.size()-1] = scores2[BBaseMap[i]]; }
}
+ }else if (((seq1[i] == '.') || (seq1[i] == '-')) && ((seq2[i] != '-') && (seq2[i] != '.'))) { //seq1 is a gap and seq2 is a base, choose seq2, unless quality score for base is below threshold. In that case eliminate base
+ if (thisfqualfile != "") {
+ if (scores2[BBaseMap[i]] < threshold) { } //
+ else {
+ contig += seq2[i];
+ contigScores.push_back(scores2[BBaseMap[i]]);
+ }
+ }else { contig += seq2[i]; } //with no quality info, then we keep it?
}else if (((seq2[i] == '.') || (seq2[i] == '-')) && ((seq1[i] != '-') && (seq1[i] != '.'))) { //seq2 is a gap and seq1 is a base, choose seq1, unless quality score for base is below threshold. In that case eliminate base
- if (scores1[ABaseMap[i]] < threshold) { } //
- else {
- contig += seq1[i];
- contigScores.push_back(scores1[ABaseMap[i]]);
- }
+ if (thisfqualfile != "") {
+ if (scores1[ABaseMap[i]] < threshold) { } //
+ else {
+ contig += seq1[i];
+ contigScores.push_back(scores1[ABaseMap[i]]);
+ }
+ }else { contig += seq1[i]; } //with no quality info, then we keep it?
}else if (((seq1[i] != '-') && (seq1[i] != '.')) && ((seq2[i] != '-') && (seq2[i] != '.'))) { //both bases choose one with better quality
- char c = seq1[i];
- contigScores.push_back(scores1[ABaseMap[i]]);
- if (scores1[ABaseMap[i]] < scores2[BBaseMap[i]]) { contigScores[contigScores.size()-1] = scores2[BBaseMap[i]]; c = seq2[i]; }
- contig += c;
- numMismatches++;
+ if (thisfqualfile != "") {
+ char c = seq1[i];
+ contigScores.push_back(scores1[ABaseMap[i]]);
+ if (scores1[ABaseMap[i]] < scores2[BBaseMap[i]]) { contigScores[contigScores.size()-1] = scores2[BBaseMap[i]]; c = seq2[i]; }
+ contig += c;
+ numMismatches++;
+ }else { numMismatches++; } //cant decide, so eliminate and mark as mismatch
}else { //should never get here
m->mothurOut("[ERROR]: case I didn't think of seq1 = " + toString(seq1[i]) + " and seq2 = " + toString(seq2[i]) + "\n");
}
if (seq1End < seq2End) { //seq1 ends before seq2 so take from overlap to length from seq2
for (int i = overlapEnd; i < length; i++) {
contig += seq2[i];
- contigScores.push_back(scores2[BBaseMap[i]]);
+ if (thisfqualfile != "") { contigScores.push_back(scores2[BBaseMap[i]]); }
}
}else { //seq2 ends before seq1 so take from overlap to length from seq1
for (int i = overlapEnd; i < length; i++) {
contig += seq1[i];
- contigScores.push_back(scores1[ABaseMap[i]]);
+ if (thisfqualfile != "") { contigScores.push_back(scores1[ABaseMap[i]]); }
}
}
output << ">" << fSeq.getName() << endl << contig << endl;
output.close();
- m->openOutputFileAppend(qualFileNames[barcodeIndex][primerIndex], output);
- output << ">" << fSeq.getName() << endl;
- for (int i = 0; i < contigScores.size(); i++) { output << contigScores[i] << ' '; }
- output << endl;
- output.close();
+ if (thisfqualfile != "") {
+ m->openOutputFileAppend(qualFileNames[barcodeIndex][primerIndex], output);
+ output << ">" << fSeq.getName() << endl;
+ for (int i = 0; i < contigScores.size(); i++) { output << contigScores[i] << ' '; }
+ output << endl;
+ output.close();
+ }
}
//output
outFasta << ">" << fSeq.getName() << endl << contig << endl;
- outQual << ">" << fSeq.getName() << endl;
- for (int i = 0; i < contigScores.size(); i++) { outQual << contigScores[i] << ' '; }
- outQual << endl;
+ if (thisfqualfile != "") {
+ outQual << ">" << fSeq.getName() << endl;
+ for (int i = 0; i < contigScores.size(); i++) { outQual << contigScores[i] << ' '; }
+ outQual << endl;
+ }
outMisMatch << fSeq.getName() << '\t' << contig.length() << '\t' << numMismatches << endl;
}else {
//output
outScrapFasta << ">" << fSeq.getName() << " | " << trashCode << endl << contig << endl;
- outScrapQual << ">" << fSeq.getName() << " | " << trashCode << endl;
- for (int i = 0; i < contigScores.size(); i++) { outScrapQual << contigScores[i] << ' '; }
- outScrapQual << endl;
+ if (thisfqualfile != "") {
+ outScrapQual << ">" << fSeq.getName() << " | " << trashCode << endl;
+ for (int i = 0; i < contigScores.size(); i++) { outScrapQual << contigScores[i] << ' '; }
+ outScrapQual << endl;
+ }
}
num++;
if((num) % 1000 != 0){ m->mothurOut(toString(num)); m->mothurOutEndLine(); }
inFFasta.close();
- inFQual.close();
inRFasta.close();
- inRQual.close();
outFasta.close();
- outQual.close();
outScrapFasta.close();
- outScrapQual.close();
outMisMatch.close();
+ if (thisfqualfile != "") {
+ inFQual.close();
+ inRQual.close();
+ outQual.close();
+ outScrapQual.close();
+ }
delete alignment;
- if (m->control_pressed) { m->mothurRemove(outputQual); m->mothurRemove(outputFasta); m->mothurRemove(outputScrapQual); m->mothurRemove(outputScrapFasta);m->mothurRemove(outputMisMatches);}
-
+ if (m->control_pressed) { m->mothurRemove(outputFasta); m->mothurRemove(outputScrapFasta);m->mothurRemove(outputMisMatches); if (thisfqualfile != "") { m->mothurRemove(outputQual); m->mothurRemove(outputScrapQual); } }
+
return num;
}
catch(exception& e) {
}
}
//**********************************************************************************************************************
-vector< vector<string> > MakeContigsCommand::readFastqFiles(unsigned long int& count){
+vector< vector<string> > MakeContigsCommand::readFastqFiles(unsigned long int& count, string ffastq, string rfastq){
try {
vector< vector<string> > files;
-
//maps processors number to file pointer
map<int, vector<ofstream*> > tempfiles; //tempfiles[0] = forwardFasta, [1] = forwardQual, [2] = reverseFasta, [3] = reverseQual
map<int, vector<ofstream*> >::iterator it;
tempfiles[i] = temp;
vector<string> names;
- string ffastafilename = outputDir + m->getRootName(m->getSimpleName(ffastqfile)) + toString(i) + "ffasta.temp";
- string rfastafilename = outputDir + m->getRootName(m->getSimpleName(rfastqfile)) + toString(i) + "rfasta.temp";
- string fqualfilename = outputDir + m->getRootName(m->getSimpleName(ffastqfile)) + toString(i) + "fqual.temp";
- string rqualfilename = outputDir + m->getRootName(m->getSimpleName(rfastqfile)) + toString(i) + "rqual.temp";
+ string thisOutputDir = outputDir;
+ if (outputDir == "") { thisOutputDir = m->hasPath(ffastq); }
+ string ffastafilename = thisOutputDir + m->getRootName(m->getSimpleName(ffastq)) + toString(i) + "ffastatemp";
+ string rfastafilename = thisOutputDir + m->getRootName(m->getSimpleName(rfastq)) + toString(i) + "rfastatemp";
+ string fqualfilename = thisOutputDir + m->getRootName(m->getSimpleName(ffastq)) + toString(i) + "fqualtemp";
+ string rqualfilename = thisOutputDir + m->getRootName(m->getSimpleName(rfastq)) + toString(i) + "rqualtemp";
names.push_back(ffastafilename); names.push_back(fqualfilename);
names.push_back(rfastafilename); names.push_back(rqualfilename);
files.push_back(names);
}
ifstream inForward;
- m->openInputFile(ffastqfile, inForward);
+ m->openInputFile(ffastq, inForward);
ifstream inReverse;
- m->openInputFile(rfastqfile, inReverse);
+ m->openInputFile(rfastq, inReverse);
count = 0;
map<string, fastqRead> uniques;
if (m->debug) { m->mothurOut(toString(count) + '\t' + fread.name + '\t' + rread.name + '\n'); }
- if (checkReads(fread, rread)) {
+ if (checkReads(fread, rread, ffastq, rfastq)) {
if (m->control_pressed) { for (it = tempfiles.begin(); it!=tempfiles.end(); it++) { for (int i = 0; i < (it->second).size(); i++) { (*(it->second)[i]).close(); delete (it->second)[i]; } } for (int i = 0; i < files.size(); i++) { for(int j = 0; j < files[i].size(); j++) { m->mothurRemove(files[i][j]); } } inForward.close(); inReverse.close(); return files; }
//if the reads are okay write to output files
inForward.close();
inReverse.close();
- //adjust for really large processors or really small files
- if (count == 0) { m->mothurOut("[ERROR]: no good reads.\n"); m->control_pressed = true; }
- if (count < processors) {
- for (int i = count; i < processors; i++) { for(int j = 0; j < files[i].size(); j++) { m->mothurRemove(files[i][j]); } files[i].clear(); }
- files.resize(count);
- processors = count;
+ return files;
+ }
+ catch(exception& e) {
+ m->errorOut(e, "MakeContigsCommand", "readFastqFiles");
+ exit(1);
+ }
+}
+//**********************************************************************************************************************
+vector< vector<string> > MakeContigsCommand::readFastaFiles(unsigned long int& count, string ffasta, string rfasta){
+ try {
+ vector< vector<string> > files;
+ //maps processors number to file pointer
+ map<int, vector<ofstream*> > tempfiles; //tempfiles[0] = forwardFasta, [1] = forwardQual, [2] = reverseFasta, [3] = reverseQual
+ map<int, vector<ofstream*> >::iterator it;
+
+ //create files to write to
+ for (int i = 0; i < processors; i++) {
+ vector<ofstream*> temp;
+ ofstream* outFF = new ofstream; temp.push_back(outFF);
+ ofstream* outFQ = new ofstream; temp.push_back(outFQ);
+ ofstream* outRF = new ofstream; temp.push_back(outRF);
+ ofstream* outRQ = new ofstream; temp.push_back(outRQ);
+ tempfiles[i] = temp;
+
+ vector<string> names;
+ string thisOutputDir = outputDir;
+ if (outputDir == "") { thisOutputDir = m->hasPath(ffasta); }
+ string ffastafilename = thisOutputDir + m->getRootName(m->getSimpleName(ffasta)) + toString(i) + "ffastatemp";
+ string rfastafilename = thisOutputDir + m->getRootName(m->getSimpleName(rfasta)) + toString(i) + "rfastatemp";
+ string fqualfilename = "";
+ if (fqualfile != "") { fqualfilename = thisOutputDir + m->getRootName(m->getSimpleName(fqualfile)) + toString(i) + "fqual.temp"; m->openOutputFile(fqualfilename, *outFQ); }
+ string rqualfilename = "";
+ if (rqualfile != "") { rqualfilename = thisOutputDir + m->getRootName(m->getSimpleName(rqualfile)) + toString(i) + "rqual.temp"; m->openOutputFile(rqualfilename, *outRQ); }
+ names.push_back(ffastafilename); names.push_back(fqualfilename);
+ names.push_back(rfastafilename); names.push_back(rqualfilename);
+ files.push_back(names);
+
+ m->openOutputFile(ffastafilename, *outFF);
+ m->openOutputFile(rfastafilename, *outRF);
+ }
+
+ if (m->control_pressed) {
+ //close files, delete ofstreams
+ for (it = tempfiles.begin(); it!=tempfiles.end(); it++) { for (int i = 0; i < (it->second).size(); i++) { (*(it->second)[i]).close(); delete (it->second)[i]; } }
+ //remove files
+ for (int i = 0; i < files.size(); i++) {
+ for(int j = 0; j < files[i].size(); j++) { m->mothurRemove(files[i][j]); }
+ }
}
+ ifstream inForwardFasta;
+ m->openInputFile(ffasta, inForwardFasta);
+
+ ifstream inReverseFasta;
+ m->openInputFile(rfasta, inReverseFasta);
+
+ ifstream inForwardQual, inReverseQual;
+ if (fqualfile != "") { m->openInputFile(fqualfile, inForwardQual); m->openInputFile(rqualfile, inReverseQual); }
+
+ count = 0;
+ map<string, fastqRead> uniques;
+ map<string, fastqRead>::iterator itUniques;
+ while ((!inForwardFasta.eof()) || (!inReverseFasta.eof())) {
+
+ if (m->control_pressed) { for (it = tempfiles.begin(); it!=tempfiles.end(); it++) { for (int i = 0; i < (it->second).size(); i++) { (*(it->second)[i]).close(); delete (it->second)[i]; } } for (int i = 0; i < files.size(); i++) { for(int j = 0; j < files[i].size(); j++) { m->mothurRemove(files[i][j]); } } inReverseFasta.close(); inForwardFasta.close(); if (fqualfile != "") { inReverseQual.close(); inReverseQual.close(); } return files; }
+
+ //get a reads from forward and reverse fasta files
+ bool ignoref, ignorer;
+ fastqRead thisFread, thisRread;
+ if (!inForwardFasta.eof()) {
+ ignoref = false;
+ Sequence temp(inForwardFasta); m->gobble(inForwardFasta);
+ thisFread.name = temp.getName();
+ thisFread.sequence = temp.getUnaligned();
+ }else { ignoref = true; }
+ if (!inReverseFasta.eof()) {
+ ignorer = false;
+ Sequence temp(inReverseFasta); m->gobble(inReverseFasta);
+ thisRread.name = temp.getName();
+ thisRread.sequence = temp.getUnaligned();
+ }else { ignorer = true; }
+
+ //get qual reads if given
+ if (fqualfile != "") {
+ if (!inForwardQual.eof() && !ignoref) {
+ QualityScores temp(inForwardQual); m->gobble(inForwardQual);
+ //if forward files dont match ignore read
+ if (thisFread.name != temp.getName()) { ignoref = true; }
+ else { thisFread.scores = temp.getQualityScores(); }
+ }else { ignoref = true; }
+ if (!inReverseQual.eof() && !ignorer) {
+ QualityScores temp(inReverseQual); m->gobble(inReverseQual);
+ //if reverse files dont match ignore read
+ if (thisRread.name != temp.getName()) { ignorer = true; }
+ else { thisRread.scores = temp.getQualityScores(); }
+ }else { ignorer = true; }
+ }
+
+ vector<pairFastqRead> reads = getReads(ignoref, ignorer, thisFread, thisRread, uniques);
+
+ for (int i = 0; i < reads.size(); i++) {
+ fastqRead fread = reads[i].forward;
+ fastqRead rread = reads[i].reverse;
+
+ if (m->debug) { m->mothurOut(toString(count) + '\t' + fread.name + '\t' + rread.name + '\n'); }
+
+ if (checkReads(fread, rread, ffasta, rfasta)) {
+ if (m->control_pressed) { for (it = tempfiles.begin(); it!=tempfiles.end(); it++) { for (int i = 0; i < (it->second).size(); i++) { (*(it->second)[i]).close(); delete (it->second)[i]; } } for (int i = 0; i < files.size(); i++) { for(int j = 0; j < files[i].size(); j++) { m->mothurRemove(files[i][j]); } } inReverseFasta.close(); inForwardFasta.close(); if (fqualfile != "") { inReverseQual.close(); inReverseQual.close(); } return files; }
+
+ //if the reads are okay write to output files
+ int process = count % processors;
+
+ *(tempfiles[process][0]) << ">" << fread.name << endl << fread.sequence << endl;
+ *(tempfiles[process][2]) << ">" << rread.name << endl << rread.sequence << endl;
+ if (fqualfile != "") { //if you have quality info, print it
+ *(tempfiles[process][1]) << ">" << fread.name << endl;
+ for (int i = 0; i < fread.scores.size(); i++) { *(tempfiles[process][1]) << fread.scores[i] << " "; }
+ *(tempfiles[process][1]) << endl;
+ *(tempfiles[process][3]) << ">" << rread.name << endl;
+ for (int i = 0; i < rread.scores.size(); i++) { *(tempfiles[process][3]) << rread.scores[i] << " "; }
+ *(tempfiles[process][3]) << endl;
+ }
+ count++;
+
+ //report progress
+ if((count) % 10000 == 0){ m->mothurOut(toString(count)); m->mothurOutEndLine(); }
+ }
+ }
+ }
+ //report progress
+ if((count) % 10000 != 0){ m->mothurOut(toString(count)); m->mothurOutEndLine(); }
+
+ if (uniques.size() != 0) {
+ for (itUniques = uniques.begin(); itUniques != uniques.end(); itUniques++) {
+ m->mothurOut("[WARNING]: did not find paired read for " + itUniques->first + ", ignoring.\n");
+ }
+ m->mothurOutEndLine();
+ }
+
+ //close files, delete ofstreams
+ for (it = tempfiles.begin(); it!=tempfiles.end(); it++) { for (int i = 0; i < (it->second).size(); i++) { (*(it->second)[i]).close(); delete (it->second)[i]; } }
+ inReverseFasta.close();
+ inForwardFasta.close();
+ if (fqualfile != "") { inReverseQual.close(); inReverseQual.close(); }
+
return files;
}
catch(exception& e) {
- m->errorOut(e, "MakeContigsCommand", "readFastqFiles");
+ m->errorOut(e, "MakeContigsCommand", "readFastaFiles");
exit(1);
}
}
}
}
//**********************************************************************************************************************
-bool MakeContigsCommand::checkReads(fastqRead& forward, fastqRead& reverse){
+bool MakeContigsCommand::checkReads(fastqRead& forward, fastqRead& reverse, string ffile, string rfile){
try {
bool good = true;
//do sequence lengths match
if (forward.sequence.length() != reverse.sequence.length()) {
- m->mothurOut("[WARNING]: For sequence " + forward.name + " I read a sequence of length " + toString(forward.sequence.length()) + " from " + ffastqfile + ", but read a sequence of length " + toString(reverse.sequence.length()) + " from " + rfastqfile + ", ignoring.\n");
+ m->mothurOut("[WARNING]: For sequence " + forward.name + " I read a sequence of length " + toString(forward.sequence.length()) + " from " + ffile + ", but read a sequence of length " + toString(reverse.sequence.length()) + " from " + rfile + ", ignoring.\n");
good = false;
}
//do number of qual scores match
if (forward.scores.size() != reverse.scores.size()) {
- m->mothurOut("[WARNING]: For sequence " + forward.name + " I read " + toString(forward.scores.size()) + " quality scores from " + ffastqfile + ", but read " + toString(reverse.scores.size()) + " quality scores from " + rfastqfile + ", ignoring.\n");
+ m->mothurOut("[WARNING]: For sequence " + forward.name + " I read " + toString(forward.scores.size()) + " quality scores from " + ffile + ", but read " + toString(reverse.scores.size()) + " quality scores from " + rfile + ", ignoring.\n");
good = false;
}
}
}
//***************************************************************************************************************
+vector< vector<string> > MakeContigsCommand::readFileNames(string filename){
+ try {
+ vector< vector<string> > files;
+ string forward, reverse;
+
+ ifstream in;
+ m->openInputFile(filename, in);
+
+ while(!in.eof()) {
+
+ if (m->control_pressed) { return files; }
+
+ in >> forward; m->gobble(in);
+ in >> reverse; m->gobble(in);
+
+ //check to make sure both are able to be opened
+ ifstream in2;
+ int openForward = m->openInputFile(forward, in2, "noerror");
+
+ //if you can't open it, try default location
+ if (openForward == 1) {
+ if (m->getDefaultPath() != "") { //default path is set
+ string tryPath = m->getDefaultPath() + m->getSimpleName(forward);
+ m->mothurOut("Unable to open " + forward + ". Trying default " + tryPath); m->mothurOutEndLine();
+ ifstream in3;
+ openForward = m->openInputFile(tryPath, in3, "noerror");
+ in3.close();
+ forward = tryPath;
+ }
+ }
+
+ //if you can't open it, try output location
+ if (openForward == 1) {
+ if (m->getOutputDir() != "") { //default path is set
+ string tryPath = m->getOutputDir() + m->getSimpleName(forward);
+ m->mothurOut("Unable to open " + forward + ". Trying output directory " + tryPath); m->mothurOutEndLine();
+ ifstream in4;
+ openForward = m->openInputFile(tryPath, in4, "noerror");
+ forward = tryPath;
+ in4.close();
+ }
+ }
+
+ if (openForward == 1) { //can't find it
+ m->mothurOut("[WARNING]: can't find " + forward + ", ignoring pair.\n");
+ }else{ in2.close(); }
+
+ ifstream in3;
+ int openReverse = m->openInputFile(reverse, in3, "noerror");
+
+ //if you can't open it, try default location
+ if (openReverse == 1) {
+ if (m->getDefaultPath() != "") { //default path is set
+ string tryPath = m->getDefaultPath() + m->getSimpleName(reverse);
+ m->mothurOut("Unable to open " + reverse + ". Trying default " + tryPath); m->mothurOutEndLine();
+ ifstream in3;
+ openReverse = m->openInputFile(tryPath, in3, "noerror");
+ in3.close();
+ reverse = tryPath;
+ }
+ }
+
+ //if you can't open it, try output location
+ if (openReverse == 1) {
+ if (m->getOutputDir() != "") { //default path is set
+ string tryPath = m->getOutputDir() + m->getSimpleName(reverse);
+ m->mothurOut("Unable to open " + reverse + ". Trying output directory " + tryPath); m->mothurOutEndLine();
+ ifstream in4;
+ openReverse = m->openInputFile(tryPath, in4, "noerror");
+ reverse = tryPath;
+ in4.close();
+ }
+ }
+
+ if (openReverse == 1) { //can't find it
+ m->mothurOut("[WARNING]: can't find " + reverse + ", ignoring pair.\n");
+ }else{ in3.close(); }
+
+ if ((openForward != 1) && (openReverse != 1)) { //good pair
+ vector<string> pair;
+ pair.push_back(forward);
+ pair.push_back(reverse);
+ files.push_back(pair);
+ }
+
+ }
+ in.close();
+
+ return files;
+ }
+ catch(exception& e) {
+ m->errorOut(e, "MakeContigsCommand", "checkReads");
+ exit(1);
+ }
+}
+//***************************************************************************************************************
//illumina data requires paired forward and reverse data
//BARCODE atgcatgc atgcatgc groupName
//PRIMER atgcatgc atgcatgc groupName
//PRIMER atgcatgc atgcatgc
-bool MakeContigsCommand::getOligos(vector<vector<string> >& fastaFileNames, vector<vector<string> >& qualFileNames){
+bool MakeContigsCommand::getOligos(vector<vector<string> >& fastaFileNames, vector<vector<string> >& qualFileNames, string rootname){
try {
ifstream in;
m->openInputFile(oligosfile, in);
ofstream temp;
- fastaFileName = outputDir + m->getRootName(m->getSimpleName(ffastqfile)) + comboGroupName + ".fasta";
+ fastaFileName = rootname + comboGroupName + ".fasta";
if (uniqueNames.count(fastaFileName) == 0) {
outputNames.push_back(fastaFileName);
outputTypes["fasta"].push_back(fastaFileName);
fastaFileNames[itBar->first][itPrimer->first] = fastaFileName;
m->openOutputFile(fastaFileName, temp); temp.close();
-
- qualFileName = outputDir + m->getRootName(m->getSimpleName(ffastqfile)) + comboGroupName + ".qual";
- if (uniqueNames.count(qualFileName) == 0) {
- outputNames.push_back(qualFileName);
- outputTypes["qfile"].push_back(qualFileName);
- }
+ if ((fqualfile != "") || (ffastqfile != "") || (file != "")) {
+ qualFileName = rootname + ".qual";
+ if (uniqueNames.count(qualFileName) == 0) {
+ outputNames.push_back(qualFileName);
+ outputTypes["qfile"].push_back(qualFileName);
+ }
- qualFileNames[itBar->first][itPrimer->first] = qualFileName;
- m->openOutputFile(qualFileName, temp); temp.close();
+ qualFileNames[itBar->first][itPrimer->first] = qualFileName;
+ m->openOutputFile(qualFileName, temp); temp.close();
+ }
}
}
}
map<string, int> groupCounts;
map<string, string> groupMap;
- //map<string, int> combos;
- //map<string, int> groupToIndex;
- //vector<string> groupVector;
fastqRead readFastq(ifstream&, bool&);
- vector< vector<string> > readFastqFiles(unsigned long int&);
- bool checkReads(fastqRead&, fastqRead&);
+ vector< vector< vector<string> > > preProcessData(unsigned long int&);
+ vector< vector<string> > readFileNames(string);
+ vector< vector<string> > readFastqFiles(unsigned long int&, string, string);
+ vector< vector<string> > readFastaFiles(unsigned long int&, string, string);
+ bool checkReads(fastqRead&, fastqRead&, string, string);
int createProcesses(vector< vector<string> >, string, string, string, string, string, vector<vector<string> >, vector<vector<string> >);
int driver(vector<string>, string, string, string, string, string, vector<vector<string> >, vector<vector<string> >);
- bool getOligos(vector<vector<string> >&, vector<vector<string> >&);
+ bool getOligos(vector<vector<string> >&, vector< vector<string> >&, string);
string reverseOligo(string);
vector<pairFastqRead> getReads(bool ignoref, bool ignorer, fastqRead forward, fastqRead reverse, map<string, fastqRead>& uniques);
};
if (pDataArray->fastaFileNames[i][j] != "") {
ofstream temp;
pDataArray->m->openOutputFile(pDataArray->fastaFileNames[i][j], temp); temp.close();
- pDataArray->m->openOutputFile(pDataArray->qualFileNames[i][j], temp); temp.close();
+ if (thisfqualfile != "") { pDataArray->m->openOutputFile(pDataArray->qualFileNames[i][j], temp); temp.close(); }
}
}
}
}
ifstream inFFasta, inRFasta, inFQual, inRQual;
+ ofstream outFasta, outQual, outMisMatch, outScrapFasta, outScrapQual;
pDataArray->m->openInputFile(thisffastafile, inFFasta);
- pDataArray->m->openInputFile(thisfqualfile, inFQual);
pDataArray->m->openInputFile(thisrfastafile, inRFasta);
- pDataArray->m->openInputFile(thisrqualfile, inRQual);
-
- ofstream outFasta, outQual, outMisMatch, outScrapFasta, outScrapQual;
+ if (thisfqualfile != "") {
+ pDataArray->m->openInputFile(thisfqualfile, inFQual);
+ pDataArray->m->openInputFile(thisrqualfile, inRQual);
+ pDataArray->m->openOutputFile(pDataArray->outputQual, outQual);
+ pDataArray->m->openOutputFile(pDataArray->outputScrapQual, outScrapQual);
+ }
pDataArray->m->openOutputFile(pDataArray->outputFasta, outFasta);
- pDataArray->m->openOutputFile(pDataArray->outputQual, outQual);
pDataArray->m->openOutputFile(pDataArray->outputMisMatches, outMisMatch);
pDataArray->m->openOutputFile(pDataArray->outputScrapFasta, outScrapFasta);
- pDataArray->m->openOutputFile(pDataArray->outputScrapQual, outScrapQual);
+
outMisMatch << "Name\tLength\tMisMatches\n";
TrimOligos trimOligos(pDataArray->pdiffs, pDataArray->bdiffs, 0, 0, pDataArray->primers, pDataArray->barcodes);
- while ((!inFQual.eof()) && (!inFFasta.eof()) && (!inRFasta.eof()) && (!inRQual.eof())) {
+ while ((!inFFasta.eof()) && (!inRFasta.eof())) {
if (pDataArray->m->control_pressed) { break; }
//read seqs and quality info
Sequence fSeq(inFFasta); pDataArray->m->gobble(inFFasta);
Sequence rSeq(inRFasta); pDataArray->m->gobble(inRFasta);
- QualityScores fQual(inFQual); pDataArray->m->gobble(inFQual);
- QualityScores rQual(inRQual); pDataArray->m->gobble(inRQual);
+ QualityScores* fQual = NULL; QualityScores* rQual = NULL;
+ if (thisfqualfile != "") {
+ fQual = new QualityScores(inFQual); pDataArray->m->gobble(inFQual);
+ rQual = new QualityScores(inRQual); pDataArray->m->gobble(inRQual);
+ }
int barcodeIndex = 0;
int primerIndex = 0;
if(pDataArray->barcodes.size() != 0){
- success = trimOligos.stripBarcode(fSeq, rSeq, fQual, rQual, barcodeIndex);
+ if (thisfqualfile != "") {
+ success = trimOligos.stripBarcode(fSeq, rSeq, *fQual, *rQual, barcodeIndex);
+ }else {
+ success = trimOligos.stripBarcode(fSeq, rSeq, barcodeIndex);
+ }
if(success > pDataArray->bdiffs) { trashCode += 'b'; }
else{ currentSeqsDiffs += success; }
}
if(pDataArray->primers.size() != 0){
- success = trimOligos.stripForward(fSeq, rSeq, fQual, rQual, primerIndex);
+ if (thisfqualfile != "") {
+ success = trimOligos.stripForward(fSeq, rSeq, *fQual, *rQual, primerIndex);
+ }else {
+ success = trimOligos.stripForward(fSeq, rSeq, primerIndex);
+ }
if(success > pDataArray->pdiffs) { trashCode += 'f'; }
else{ currentSeqsDiffs += success; }
}
//flip the reverse reads
rSeq.reverseComplement();
- rQual.flipQScores();
+ if (thisfqualfile != "") { rQual->flipQScores(); }
//pairwise align
alignment->align(fSeq.getUnaligned(), rSeq.getUnaligned());
int numMismatches = 0;
string seq1 = fSeq.getAligned();
string seq2 = rSeq.getAligned();
- vector<int> scores1 = fQual.getQualityScores();
- vector<int> scores2 = rQual.getQualityScores();
+ vector<int> scores1, scores2;
+ if (thisfqualfile != "") {
+ scores1 = fQual->getQualityScores();
+ scores2 = rQual->getQualityScores();
+ delete fQual; delete rQual;
+ }
int overlapStart = fSeq.getStartPos();
int seq2Start = rSeq.getStartPos();
overlapStart = seq2Start;
for (int i = 0; i < overlapStart; i++) {
contig += seq1[i];
- contigScores.push_back(scores1[ABaseMap[i]]);
+ if (thisfqualfile != "") { contigScores.push_back(scores1[ABaseMap[i]]); }
}
}else { //seq1 starts later so take from 0 to overlapStart from seq2
for (int i = 0; i < overlapStart; i++) {
contig += seq2[i];
- contigScores.push_back(scores2[BBaseMap[i]]);
+ if (thisfqualfile != "") { contigScores.push_back(scores2[BBaseMap[i]]); }
}
}
for (int i = overlapStart; i < overlapEnd; i++) {
if (seq1[i] == seq2[i]) { //match, add base and choose highest score
contig += seq1[i];
- contigScores.push_back(scores1[ABaseMap[i]]);
- if (scores1[ABaseMap[i]] < scores2[BBaseMap[i]]) { contigScores[contigScores.size()-1] = scores2[BBaseMap[i]]; }
- }else if (((seq1[i] == '.') || (seq1[i] == '-')) && ((seq2[i] != '-') && (seq2[i] != '.'))) { //seq1 is a gap and seq2 is a base, choose seq2, unless quality score for base is below threshold. In that case eliminate base
- if (scores2[BBaseMap[i]] < pDataArray->threshold) { } //
- else {
- contig += seq2[i];
- contigScores.push_back(scores2[BBaseMap[i]]);
- }
- }else if (((seq2[i] == '.') || (seq2[i] == '-')) && ((seq1[i] != '-') && (seq1[i] != '.'))) { //seq2 is a gap and seq1 is a base, choose seq1, unless quality score for base is below threshold. In that case eliminate base
- if (scores1[ABaseMap[i]] < pDataArray->threshold) { } //
- else {
- contig += seq1[i];
+ if (thisfqualfile != "") {
contigScores.push_back(scores1[ABaseMap[i]]);
+ if (scores1[ABaseMap[i]] < scores2[BBaseMap[i]]) { contigScores[contigScores.size()-1] = scores2[BBaseMap[i]]; }
}
+ }else if (((seq1[i] == '.') || (seq1[i] == '-')) && ((seq2[i] != '-') && (seq2[i] != '.'))) { //seq1 is a gap and seq2 is a base, choose seq2, unless quality score for base is below threshold. In that case eliminate base
+ if (thisfqualfile != "") {
+ if (scores2[BBaseMap[i]] < pDataArray->threshold) { } //
+ else {
+ contig += seq2[i];
+ contigScores.push_back(scores2[BBaseMap[i]]);
+ }
+ }else { contig += seq2[i]; }
+ }else if (((seq2[i] == '.') || (seq2[i] == '-')) && ((seq1[i] != '-') && (seq1[i] != '.'))) { //seq2 is a gap and seq1 is a base, choose seq1, unless quality score for base is below threshold. In that case eliminate base
+ if (thisfqualfile != "") {
+ if (scores1[ABaseMap[i]] < pDataArray->threshold) { } //
+ else {
+ contig += seq1[i];
+ contigScores.push_back(scores1[ABaseMap[i]]);
+ }
+ }else { contig += seq1[i]; }
}else if (((seq1[i] != '-') && (seq1[i] != '.')) && ((seq2[i] != '-') && (seq2[i] != '.'))) { //both bases choose one with better quality
- char c = seq1[i];
- contigScores.push_back(scores1[ABaseMap[i]]);
- if (scores1[ABaseMap[i]] < scores2[BBaseMap[i]]) { contigScores[contigScores.size()-1] = scores2[BBaseMap[i]]; c = seq2[i]; }
- contig += c;
- numMismatches++;
+ if (thisfqualfile != "") {
+ char c = seq1[i];
+ contigScores.push_back(scores1[ABaseMap[i]]);
+ if (scores1[ABaseMap[i]] < scores2[BBaseMap[i]]) { contigScores[contigScores.size()-1] = scores2[BBaseMap[i]]; c = seq2[i]; }
+ contig += c;
+ numMismatches++;
+ }else { numMismatches++; }
}else { //should never get here
pDataArray->m->mothurOut("[ERROR]: case I didn't think of seq1 = " + toString(seq1[i]) + " and seq2 = " + toString(seq2[i]) + "\n");
}
if (seq1End < seq2End) { //seq1 ends before seq2 so take from overlap to length from seq2
for (int i = overlapEnd; i < length; i++) {
contig += seq2[i];
- contigScores.push_back(scores2[BBaseMap[i]]);
+ if (thisfqualfile != "") { contigScores.push_back(scores2[BBaseMap[i]]); }
}
}else { //seq2 ends before seq1 so take from overlap to length from seq1
for (int i = overlapEnd; i < length; i++) {
contig += seq1[i];
- contigScores.push_back(scores1[ABaseMap[i]]);
+ if (thisfqualfile != "") { contigScores.push_back(scores1[ABaseMap[i]]); }
}
}
output << ">" << fSeq.getName() << endl << contig << endl;
output.close();
- pDataArray->m->openOutputFileAppend(pDataArray->qualFileNames[barcodeIndex][primerIndex], output);
- output << ">" << fSeq.getName() << endl;
- for (int i = 0; i < contigScores.size(); i++) { output << contigScores[i] << ' '; }
- output << endl;
- output.close();
+ if (thisfqualfile != "") {
+ pDataArray->m->openOutputFileAppend(pDataArray->qualFileNames[barcodeIndex][primerIndex], output);
+ output << ">" << fSeq.getName() << endl;
+ for (int i = 0; i < contigScores.size(); i++) { output << contigScores[i] << ' '; }
+ output << endl;
+ output.close();
+ }
}
//output
outFasta << ">" << fSeq.getName() << endl << contig << endl;
- outQual << ">" << fSeq.getName() << endl;
- for (int i = 0; i < contigScores.size(); i++) { outQual << contigScores[i] << ' '; }
- outQual << endl;
+ if (thisfqualfile != "") {
+ outQual << ">" << fSeq.getName() << endl;
+ for (int i = 0; i < contigScores.size(); i++) { outQual << contigScores[i] << ' '; }
+ outQual << endl;
+ }
outMisMatch << fSeq.getName() << '\t' << contig.length() << '\t' << numMismatches << endl;
}else {
//output
outScrapFasta << ">" << fSeq.getName() << " | " << trashCode << endl << contig << endl;
- outScrapQual << ">" << fSeq.getName() << " | " << trashCode << endl;
- for (int i = 0; i < contigScores.size(); i++) { outScrapQual << contigScores[i] << ' '; }
- outScrapQual << endl;
+ if (thisfqualfile != "") {
+ outScrapQual << ">" << fSeq.getName() << " | " << trashCode << endl;
+ for (int i = 0; i < contigScores.size(); i++) { outScrapQual << contigScores[i] << ' '; }
+ outScrapQual << endl;
+ }
}
num++;
if((num) % 1000 != 0){ pDataArray->m->mothurOut(toString(num)); pDataArray->m->mothurOutEndLine(); }
inFFasta.close();
- inFQual.close();
inRFasta.close();
- inRQual.close();
outFasta.close();
- outQual.close();
outMisMatch.close();
outScrapFasta.close();
- outScrapQual.close();
+ if (thisfqualfile != "") {
+ inFQual.close();
+ inRQual.close();
+ outQual.close();
+ outScrapQual.close();
+ }
delete alignment;
- if (pDataArray->m->control_pressed) { pDataArray->m->mothurRemove(pDataArray->outputQual); pDataArray->m->mothurRemove(pDataArray->outputFasta); pDataArray->m->mothurRemove(pDataArray->outputMisMatches); pDataArray->m->mothurRemove(pDataArray->outputScrapQual); pDataArray->m->mothurRemove(pDataArray->outputScrapFasta);}
+ if (pDataArray->m->control_pressed) { pDataArray->m->mothurRemove(pDataArray->outputFasta); pDataArray->m->mothurRemove(pDataArray->outputMisMatches); pDataArray->m->mothurRemove(pDataArray->outputScrapFasta); if (thisfqualfile != "") { pDataArray->m->mothurRemove(pDataArray->outputQual); pDataArray->m->mothurRemove(pDataArray->outputScrapQual); } }
return 0;
}
}
//*******************************************************************/
-int TrimOligos::stripBarcode(Sequence& forwardSeq, Sequence& reverseSeq, QualityScores& forwardQual, QualityScores& reverseQual, int& group){
+int TrimOligos::stripBarcode(Sequence& forwardSeq, Sequence& reverseSeq, int& group){
try {
//look for forward barcode
string rawFSequence = forwardSeq.getUnaligned();
group = it->first;
forwardSeq.setUnaligned(rawFSequence.substr(foligo.length()));
reverseSeq.setUnaligned(rawRSequence.substr(0,(rawRSequence.length()-roligo.length())));
- forwardQual.trimQScores(foligo.length(), -1);
- reverseQual.trimQScores(-1, rawRSequence.length()-roligo.length());
success = 0;
break;
}
rStart = minRPos[0];
}
+ //we have an acceptable match for the forward and reverse, but do they match?
+ if (foundMatch) {
+ forwardSeq.setUnaligned(rawFSequence.substr(fStart));
+ reverseSeq.setUnaligned(rawRSequence.substr(rStart));
+ success = minDiff;
+ }else { success = bdiffs + 100; }
+ }
+ }
+
+ if (alignment != NULL) { delete alignment; }
+ }
+
+ return success;
+
+ }
+ catch(exception& e) {
+ m->errorOut(e, "TrimOligos", "stripIBarcode");
+ exit(1);
+ }
+
+}
+//*******************************************************************/
+int TrimOligos::stripBarcode(Sequence& forwardSeq, Sequence& reverseSeq, QualityScores& forwardQual, QualityScores& reverseQual, int& group){
+ try {
+ //look for forward barcode
+ string rawFSequence = forwardSeq.getUnaligned();
+ string rawRSequence = reverseSeq.getUnaligned();
+ int success = bdiffs + 1; //guilty until proven innocent
+
+ //can you find the forward barcode
+ for(map<int,oligosPair>::iterator it=ipbarcodes.begin();it!=ipbarcodes.end();it++){
+ string foligo = it->second.forward;
+ string roligo = it->second.reverse;
+
+ if(rawFSequence.length() < foligo.length()){ //let's just assume that the barcodes are the same length
+ success = bdiffs + 10; //if the sequence is shorter than the barcode then bail out
+ break;
+ }
+ if(rawRSequence.length() < roligo.length()){ //let's just assume that the barcodes are the same length
+ success = bdiffs + 10; //if the sequence is shorter than the barcode then bail out
+ break;
+ }
+
+ if((compareDNASeq(foligo, rawFSequence.substr(0,foligo.length()))) && (compareDNASeq(roligo, rawRSequence.substr((rawRSequence.length()-roligo.length()),roligo.length())))) {
+ group = it->first;
+ forwardSeq.setUnaligned(rawFSequence.substr(foligo.length()));
+ reverseSeq.setUnaligned(rawRSequence.substr(0,(rawRSequence.length()-roligo.length())));
+ forwardQual.trimQScores(foligo.length(), -1);
+ reverseQual.trimQScores(-1, rawRSequence.length()-roligo.length());
+ success = 0;
+ break;
+ }
+ }
+
+ //if you found the barcode or if you don't want to allow for diffs
+ if ((bdiffs == 0) || (success == 0)) { return success; }
+ else { //try aligning and see if you can find it
+ Alignment* alignment;
+ if (ifbarcodes.size() > 0) { alignment = new NeedlemanOverlap(-1.0, 1.0, -1.0, (maxFBarcodeLength+bdiffs+1)); }
+ else{ alignment = NULL; }
+
+ //can you find the barcode
+ int minDiff = 1e6;
+ int minCount = 1;
+ vector< vector<int> > minFGroup;
+ vector<int> minFPos;
+
+ //the pair can have duplicates, but we only want to search for the unique forward and reverses, example
+ /*
+ 1 Sarah Westcott
+ 2 John Westcott
+ 3 Anna Westcott
+ 4 Sarah Schloss
+ 5 Pat Schloss
+ 6 Gail Brown
+ 7 Pat Moore
+
+ only want to look for forward = Sarah, John, Anna, Pat, Gail
+ reverse = Westcott, Schloss, Brown, Moore
+
+ but if best match forward = 4, and reverse = 1, we want to count as a valid match because forward 1 and forward 4 are the same. so both barcodes map to same group.
+ */
+ //cout << endl << forwardSeq.getName() << endl;
+ for(map<string, vector<int> >::iterator it=ifbarcodes.begin();it!=ifbarcodes.end();it++){
+ string oligo = it->first;
+
+ if(rawFSequence.length() < maxFBarcodeLength){ //let's just assume that the barcodes are the same length
+ success = bdiffs + 10;
+ break;
+ }
+ //cout << "before = " << oligo << '\t' << rawFSequence.substr(0,oligo.length()+bdiffs) << endl;
+ //use needleman to align first barcode.length()+numdiffs of sequence to each barcode
+ alignment->align(oligo, rawFSequence.substr(0,oligo.length()+bdiffs));
+ oligo = alignment->getSeqAAln();
+ string temp = alignment->getSeqBAln();
+
+ int alnLength = oligo.length();
+
+ for(int i=oligo.length()-1;i>=0;i--){ if(oligo[i] != '-'){ alnLength = i+1; break; } }
+ oligo = oligo.substr(0,alnLength);
+ temp = temp.substr(0,alnLength);
+ int numDiff = countDiffs(oligo, temp);
+
+ if (alnLength == 0) { numDiff = bdiffs + 100; }
+ //cout << "after = " << oligo << '\t' << temp << '\t' << numDiff << endl;
+
+ if(numDiff < minDiff){
+ minDiff = numDiff;
+ minCount = 1;
+ minFGroup.clear();
+ minFGroup.push_back(it->second);
+ int tempminFPos = 0;
+ minFPos.clear();
+ for(int i=0;i<alnLength;i++){
+ if(temp[i] != '-'){
+ tempminFPos++;
+ }
+ }
+ minFPos.push_back(tempminFPos);
+ }else if(numDiff == minDiff){
+ minFGroup.push_back(it->second);
+ int tempminFPos = 0;
+ for(int i=0;i<alnLength;i++){
+ if(temp[i] != '-'){
+ tempminFPos++;
+ }
+ }
+ minFPos.push_back(tempminFPos);
+ }
+ }
+
+ //cout << minDiff << '\t' << minCount << '\t' << endl;
+ if(minDiff > bdiffs) { success = minDiff; } //no good matches
+ else{
+ //check for reverse match
+ if (alignment != NULL) { delete alignment; }
+
+ if (irbarcodes.size() > 0) { alignment = new NeedlemanOverlap(-1.0, 1.0, -1.0, (maxRBarcodeLength+bdiffs+1)); }
+ else{ alignment = NULL; }
+
+ //can you find the barcode
+ minDiff = 1e6;
+ minCount = 1;
+ vector< vector<int> > minRGroup;
+ vector<int> minRPos;
+
+ for(map<string, vector<int> >::iterator it=irbarcodes.begin();it!=irbarcodes.end();it++){
+ string oligo = it->first;
+ //cout << "before = " << oligo << '\t' << rawRSequence.substr(0,oligo.length()+bdiffs) << endl;
+ if(rawRSequence.length() < maxRBarcodeLength){ //let's just assume that the barcodes are the same length
+ success = bdiffs + 10;
+ break;
+ }
+
+ //use needleman to align first barcode.length()+numdiffs of sequence to each barcode
+ alignment->align(oligo, rawRSequence.substr(0,oligo.length()+bdiffs));
+ oligo = alignment->getSeqAAln();
+ string temp = alignment->getSeqBAln();
+
+ int alnLength = oligo.length();
+ for(int i=oligo.length()-1;i>=0;i--){ if(oligo[i] != '-'){ alnLength = i+1; break; } }
+ oligo = oligo.substr(0,alnLength);
+ temp = temp.substr(0,alnLength);
+ int numDiff = countDiffs(oligo, temp);
+ if (alnLength == 0) { numDiff = bdiffs + 100; }
+
+ //cout << "after = " << oligo << '\t' << temp << '\t' << numDiff << endl;
+ if(numDiff < minDiff){
+ minDiff = numDiff;
+ minCount = 1;
+ minRGroup.clear();
+ minRGroup.push_back(it->second);
+ int tempminRPos = 0;
+ minRPos.clear();
+ for(int i=0;i<alnLength;i++){
+ if(temp[i] != '-'){
+ tempminRPos++;
+ }
+ }
+ minRPos.push_back(tempminRPos);
+ }else if(numDiff == minDiff){
+ int tempminRPos = 0;
+ for(int i=0;i<alnLength;i++){
+ if(temp[i] != '-'){
+ tempminRPos++;
+ }
+ }
+ minRPos.push_back(tempminRPos);
+ minRGroup.push_back(it->second);
+ }
+
+ }
+
+ /*cout << minDiff << '\t' << minCount << '\t' << endl;
+ for (int i = 0; i < minFGroup.size(); i++) {
+ cout << i << '\t';
+ for (int j = 0; j < minFGroup[i].size(); j++) { cout << minFGroup[i][j] << " "; }
+ cout << endl;
+ }
+ cout << endl;
+ for (int i = 0; i < minRGroup.size(); i++) {
+ cout << i << '\t';
+ for (int j = 0; j < minRGroup[i].size(); j++) { cout << minRGroup[i][j] << " "; }
+ cout << endl;
+ }
+ cout << endl;*/
+ if(minDiff > bdiffs) { success = minDiff; } //no good matches
+ else {
+ bool foundMatch = false;
+ vector<int> matches;
+ for (int i = 0; i < minFGroup.size(); i++) {
+ for (int j = 0; j < minFGroup[i].size(); j++) {
+ for (int k = 0; k < minRGroup.size(); k++) {
+ if (m->inUsersGroups(minFGroup[i][j], minRGroup[k])) { matches.push_back(minFGroup[i][j]); k+= minRGroup.size(); }
+ }
+ }
+ }
+
+ int fStart = 0;
+ int rStart = 0;
+ if (matches.size() == 1) {
+ foundMatch = true;
+ group = matches[0];
+ fStart = minFPos[0];
+ rStart = minRPos[0];
+ }
+
//we have an acceptable match for the forward and reverse, but do they match?
if (foundMatch) {
forwardSeq.setUnaligned(rawFSequence.substr(fStart));
exit(1);
}
+}
+//*******************************************************************/
+int TrimOligos::stripForward(Sequence& forwardSeq, Sequence& reverseSeq, int& group){
+ try {
+ //look for forward barcode
+ string rawFSequence = forwardSeq.getUnaligned();
+ string rawRSequence = reverseSeq.getUnaligned();
+ int success = pdiffs + 1; //guilty until proven innocent
+
+ //can you find the forward barcode
+ for(map<int,oligosPair>::iterator it=ipprimers.begin();it!=ipprimers.end();it++){
+ string foligo = it->second.forward;
+ string roligo = it->second.reverse;
+
+ if(rawFSequence.length() < foligo.length()){ //let's just assume that the barcodes are the same length
+ success = pdiffs + 10; //if the sequence is shorter than the barcode then bail out
+ break;
+ }
+ if(rawRSequence.length() < roligo.length()){ //let's just assume that the barcodes are the same length
+ success = pdiffs + 10; //if the sequence is shorter than the barcode then bail out
+ break;
+ }
+
+ if((compareDNASeq(foligo, rawFSequence.substr(0,foligo.length()))) && (compareDNASeq(roligo, rawRSequence.substr((rawRSequence.length()-roligo.length()),roligo.length())))) {
+ group = it->first;
+ forwardSeq.setUnaligned(rawFSequence.substr(foligo.length()));
+ reverseSeq.setUnaligned(rawRSequence.substr(0,(rawRSequence.length()-roligo.length())));
+ success = 0;
+ break;
+ }
+ }
+
+ //if you found the barcode or if you don't want to allow for diffs
+ if ((pdiffs == 0) || (success == 0)) { return success; }
+ else { //try aligning and see if you can find it
+ Alignment* alignment;
+ if (ifprimers.size() > 0) { alignment = new NeedlemanOverlap(-1.0, 1.0, -1.0, (maxFPrimerLength+pdiffs+1)); }
+ else{ alignment = NULL; }
+
+ //can you find the barcode
+ int minDiff = 1e6;
+ int minCount = 1;
+ vector< vector<int> > minFGroup;
+ vector<int> minFPos;
+
+ //the pair can have duplicates, but we only want to search for the unique forward and reverses, example
+ /*
+ 1 Sarah Westcott
+ 2 John Westcott
+ 3 Anna Westcott
+ 4 Sarah Schloss
+ 5 Pat Schloss
+ 6 Gail Brown
+ 7 Pat Moore
+
+ only want to look for forward = Sarah, John, Anna, Pat, Gail
+ reverse = Westcott, Schloss, Brown, Moore
+
+ but if best match forward = 4, and reverse = 1, we want to count as a valid match because forward 1 and forward 4 are the same. so both barcodes map to same group.
+ */
+ //cout << endl << forwardSeq.getName() << endl;
+ for(map<string, vector<int> >::iterator it=ifprimers.begin();it!=ifprimers.end();it++){
+ string oligo = it->first;
+
+ if(rawFSequence.length() < maxFPrimerLength){ //let's just assume that the barcodes are the same length
+ success = pdiffs + 10;
+ break;
+ }
+ //cout << "before = " << oligo << '\t' << rawFSequence.substr(0,oligo.length()+pdiffs) << endl;
+ //use needleman to align first barcode.length()+numdiffs of sequence to each barcode
+ alignment->align(oligo, rawFSequence.substr(0,oligo.length()+pdiffs));
+ oligo = alignment->getSeqAAln();
+ string temp = alignment->getSeqBAln();
+
+ int alnLength = oligo.length();
+
+ for(int i=oligo.length()-1;i>=0;i--){ if(oligo[i] != '-'){ alnLength = i+1; break; } }
+ oligo = oligo.substr(0,alnLength);
+ temp = temp.substr(0,alnLength);
+ int numDiff = countDiffs(oligo, temp);
+
+ if (alnLength == 0) { numDiff = pdiffs + 100; }
+ //cout << "after = " << oligo << '\t' << temp << '\t' << numDiff << endl;
+
+ if(numDiff < minDiff){
+ minDiff = numDiff;
+ minCount = 1;
+ minFGroup.clear();
+ minFGroup.push_back(it->second);
+ int tempminFPos = 0;
+ minFPos.clear();
+ for(int i=0;i<alnLength;i++){
+ if(temp[i] != '-'){
+ tempminFPos++;
+ }
+ }
+ minFPos.push_back(tempminFPos);
+ }else if(numDiff == minDiff){
+ minFGroup.push_back(it->second);
+ int tempminFPos = 0;
+ for(int i=0;i<alnLength;i++){
+ if(temp[i] != '-'){
+ tempminFPos++;
+ }
+ }
+ minFPos.push_back(tempminFPos);
+ }
+ }
+
+ //cout << minDiff << '\t' << minCount << '\t' << endl;
+ if(minDiff > pdiffs) { success = minDiff; } //no good matches
+ else{
+ //check for reverse match
+ if (alignment != NULL) { delete alignment; }
+
+ if (irbarcodes.size() > 0) { alignment = new NeedlemanOverlap(-1.0, 1.0, -1.0, (maxRPrimerLength+pdiffs+1)); }
+ else{ alignment = NULL; }
+
+ //can you find the barcode
+ minDiff = 1e6;
+ minCount = 1;
+ vector< vector<int> > minRGroup;
+ vector<int> minRPos;
+
+ for(map<string, vector<int> >::iterator it=irprimers.begin();it!=irprimers.end();it++){
+ string oligo = it->first;
+ //cout << "before = " << oligo << '\t' << rawRSequence.substr(0,oligo.length()+pdiffs) << endl;
+ if(rawRSequence.length() < maxRPrimerLength){ //let's just assume that the barcodes are the same length
+ success = pdiffs + 10;
+ break;
+ }
+
+ //use needleman to align first barcode.length()+numdiffs of sequence to each barcode
+ alignment->align(oligo, rawRSequence.substr(0,oligo.length()+pdiffs));
+ oligo = alignment->getSeqAAln();
+ string temp = alignment->getSeqBAln();
+
+ int alnLength = oligo.length();
+ for(int i=oligo.length()-1;i>=0;i--){ if(oligo[i] != '-'){ alnLength = i+1; break; } }
+ oligo = oligo.substr(0,alnLength);
+ temp = temp.substr(0,alnLength);
+ int numDiff = countDiffs(oligo, temp);
+ if (alnLength == 0) { numDiff = pdiffs + 100; }
+
+ //cout << "after = " << oligo << '\t' << temp << '\t' << numDiff << endl;
+ if(numDiff < minDiff){
+ minDiff = numDiff;
+ minCount = 1;
+ minRGroup.clear();
+ minRGroup.push_back(it->second);
+ int tempminRPos = 0;
+ minRPos.clear();
+ for(int i=0;i<alnLength;i++){
+ if(temp[i] != '-'){
+ tempminRPos++;
+ }
+ }
+ minRPos.push_back(tempminRPos);
+ }else if(numDiff == minDiff){
+ int tempminRPos = 0;
+ for(int i=0;i<alnLength;i++){
+ if(temp[i] != '-'){
+ tempminRPos++;
+ }
+ }
+ minRPos.push_back(tempminRPos);
+ minRGroup.push_back(it->second);
+ }
+
+ }
+
+ /*cout << minDiff << '\t' << minCount << '\t' << endl;
+ for (int i = 0; i < minFGroup.size(); i++) {
+ cout << i << '\t';
+ for (int j = 0; j < minFGroup[i].size(); j++) { cout << minFGroup[i][j] << " "; }
+ cout << endl;
+ }
+ cout << endl;
+ for (int i = 0; i < minRGroup.size(); i++) {
+ cout << i << '\t';
+ for (int j = 0; j < minRGroup[i].size(); j++) { cout << minRGroup[i][j] << " "; }
+ cout << endl;
+ }
+ cout << endl;*/
+ if(minDiff > pdiffs) { success = minDiff; } //no good matches
+ else {
+ bool foundMatch = false;
+ vector<int> matches;
+ for (int i = 0; i < minFGroup.size(); i++) {
+ for (int j = 0; j < minFGroup[i].size(); j++) {
+ for (int k = 0; k < minRGroup.size(); k++) {
+ if (m->inUsersGroups(minFGroup[i][j], minRGroup[k])) { matches.push_back(minFGroup[i][j]); k+= minRGroup.size(); }
+ }
+ }
+ }
+
+ int fStart = 0;
+ int rStart = 0;
+ if (matches.size() == 1) {
+ foundMatch = true;
+ group = matches[0];
+ fStart = minFPos[0];
+ rStart = minRPos[0];
+ }
+
+ //we have an acceptable match for the forward and reverse, but do they match?
+ if (foundMatch) {
+ forwardSeq.setUnaligned(rawFSequence.substr(fStart));
+ reverseSeq.setUnaligned(rawRSequence.substr(rStart));
+ success = minDiff;
+ }else { success = pdiffs + 100; }
+ }
+ }
+
+ if (alignment != NULL) { delete alignment; }
+ }
+
+ return success;
+
+ }
+ catch(exception& e) {
+ m->errorOut(e, "TrimOligos", "stripIForward");
+ exit(1);
+ }
+
}
//*******************************************************************/
int TrimOligos::stripBarcode(Sequence& seq, int& group){