#include "trimflowscommand.h"
#include "needlemanoverlap.hpp"
-//**********************************************************************************************************************
-vector<string> TrimFlowsCommand::getValidParameters(){
+//**********************************************************************************************************************
+vector<string> TrimFlowsCommand::setParameters(){
try {
- string Array[] = {"flow", "maxflows", "minflows",
- "fasta", "minlength", "maxlength", "maxhomop", "signal", "noise"
- "oligos", "pdiffs", "bdiffs", "tdiffs", "order",
- "allfiles", "processors",
- "outputdir","inputdir"
+ CommandParameter pflow("flow", "InputTypes", "", "", "none", "none", "none",false,true); parameters.push_back(pflow);
+ CommandParameter poligos("oligos", "InputTypes", "", "", "none", "none", "none",false,false); parameters.push_back(poligos);
+ CommandParameter pmaxhomop("maxhomop", "Number", "", "9", "", "", "",false,false); parameters.push_back(pmaxhomop);
+ CommandParameter pmaxflows("maxflows", "Number", "", "450", "", "", "",false,false); parameters.push_back(pmaxflows);
+ CommandParameter pminflows("minflows", "Number", "", "450", "", "", "",false,false); parameters.push_back(pminflows);
+ CommandParameter ppdiffs("pdiffs", "Number", "", "0", "", "", "",false,false); parameters.push_back(ppdiffs);
+ CommandParameter pbdiffs("bdiffs", "Number", "", "0", "", "", "",false,false); parameters.push_back(pbdiffs);
+ CommandParameter pldiffs("ldiffs", "Number", "", "0", "", "", "",false,false); parameters.push_back(pldiffs);
+ CommandParameter psdiffs("sdiffs", "Number", "", "0", "", "", "",false,false); parameters.push_back(psdiffs);
+ CommandParameter ptdiffs("tdiffs", "Number", "", "0", "", "", "",false,false); parameters.push_back(ptdiffs);
+ CommandParameter pprocessors("processors", "Number", "", "1", "", "", "",false,false); parameters.push_back(pprocessors);
+ CommandParameter psignal("signal", "Number", "", "0.50", "", "", "",false,false); parameters.push_back(psignal);
+ CommandParameter pnoise("noise", "Number", "", "0.70", "", "", "",false,false); parameters.push_back(pnoise);
+ CommandParameter pallfiles("allfiles", "Boolean", "", "t", "", "", "",false,false); parameters.push_back(pallfiles);
+ CommandParameter porder("order", "String", "", "", "", "", "",false,false); parameters.push_back(porder);
+ CommandParameter pfasta("fasta", "Boolean", "", "F", "", "", "",false,false); parameters.push_back(pfasta);
+ CommandParameter pinputdir("inputdir", "String", "", "", "", "", "",false,false); parameters.push_back(pinputdir);
+ CommandParameter poutputdir("outputdir", "String", "", "", "", "", "",false,false); parameters.push_back(poutputdir);
- };
- vector<string> myArray (Array, Array+(sizeof(Array)/sizeof(string)));
+ vector<string> myArray;
+ for (int i = 0; i < parameters.size(); i++) { myArray.push_back(parameters[i].name); }
return myArray;
}
catch(exception& e) {
- m->errorOut(e, "TrimFlowsCommand", "getValidParameters");
+ m->errorOut(e, "TrimFlowsCommand", "setParameters");
exit(1);
}
}
-
//**********************************************************************************************************************
-
-vector<string> TrimFlowsCommand::getRequiredParameters(){
+string TrimFlowsCommand::getHelpString(){
try {
- string Array[] = {"flow"};
- vector<string> myArray (Array, Array+(sizeof(Array)/sizeof(string)));
- return myArray;
+ string helpString = "";
+ helpString += "The trim.flows command reads a flowgram file and creates .....\n";
+ helpString += "Note: No spaces between parameter labels (i.e. fasta), '=' and parameters (i.e.yourFasta).\n";
+ helpString += "For more details please check out the wiki http://www.mothur.org/wiki/Trim.flows.\n";
+ return helpString;
}
catch(exception& e) {
- m->errorOut(e, "TrimFlowsCommand", "getRequiredParameters");
+ m->errorOut(e, "TrimFlowsCommand", "getHelpString");
exit(1);
}
}
-
//**********************************************************************************************************************
-
-vector<string> TrimFlowsCommand::getRequiredFiles(){
+string TrimFlowsCommand::getOutputFileNameTag(string type, string inputName=""){
try {
- vector<string> myArray;
- return myArray;
+ string outputFileName = "";
+ map<string, vector<string> >::iterator it;
+
+ //is this a type this command creates
+ it = outputTypes.find(type);
+ if (it == outputTypes.end()) { m->mothurOut("[ERROR]: this command doesn't create a " + type + " output file.\n"); }
+ else {
+ if (type == "flow") { outputFileName = "flow"; }
+ else if (type == "fasta") { outputFileName = "flow.fasta"; }
+ else if (type == "file") { outputFileName = "flow.files"; }
+ else { m->mothurOut("[ERROR]: No definition for type " + type + " output file tag.\n"); m->control_pressed = true; }
+ }
+ return outputFileName;
}
catch(exception& e) {
- m->errorOut(e, "TrimFlowsCommand", "getRequiredFiles");
+ m->errorOut(e, "TrimFlowsCommand", "getOutputFileNameTag");
exit(1);
}
}
-
//**********************************************************************************************************************
TrimFlowsCommand::TrimFlowsCommand(){
try {
abort = true; calledHelp = true;
+ setParameters();
vector<string> tempOutNames;
outputTypes["flow"] = tempOutNames;
outputTypes["fasta"] = tempOutNames;
+ outputTypes["file"] = tempOutNames;
}
catch(exception& e) {
m->errorOut(e, "TrimFlowsCommand", "TrimFlowsCommand");
exit(1);
}
}
-
-//***************************************************************************************************************
-
-TrimFlowsCommand::~TrimFlowsCommand(){ /* do nothing */ }
-
-//***************************************************************************************************************
-
-void TrimFlowsCommand::help(){
- try {
- m->mothurOut("The trim.flows command reads a flowgram file and creates .....\n");
- m->mothurOut("Note: No spaces between parameter labels (i.e. fasta), '=' and parameters (i.e.yourFasta).\n");
- m->mothurOut("For more details please check out the wiki http://www.mothur.org/wiki/Trim.flows.\n\n");
-
- }
- catch(exception& e) {
- m->errorOut(e, "TrimFlowsCommand", "help");
- exit(1);
- }
-}
-
//**********************************************************************************************************************
TrimFlowsCommand::TrimFlowsCommand(string option) {
//allow user to run help
if(option == "help") { help(); abort = true; calledHelp = true; }
+ else if(option == "citation") { citation(); abort = true; calledHelp = true;}
else {
- //valid paramters for this command
- string AlignArray[] = {"flow", "maxflows", "minflows",
- "fasta", "minlength", "maxlength", "maxhomop", "signal", "noise",
- "oligos", "pdiffs", "bdiffs", "tdiffs", "order",
- "allfiles", "processors",
-
- // "group",
- "outputdir","inputdir"
-
- };
-
- vector<string> myArray (AlignArray, AlignArray+(sizeof(AlignArray)/sizeof(string)));
+
+ vector<string> myArray = setParameters();
OptionParser parser(option);
map<string,string> parameters = parser.getParameters();
vector<string> tempOutNames;
outputTypes["flow"] = tempOutNames;
outputTypes["fasta"] = tempOutNames;
+ outputTypes["file"] = tempOutNames;
//if the user changes the input directory command factory will send this info to us in the output parameter
string inputDir = validParameter.validFile(parameters, "inputdir", false);
if (path == "") { parameters["oligos"] = inputDir + it->second; }
}
-
-// it = parameters.find("group");
-// //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["group"] = inputDir + it->second; }
-// }
}
//check for required parameters
flowFileName = validParameter.validFile(parameters, "flow", true);
- if (flowFileName == "not found") { m->mothurOut("flow is a required parameter for the trim.flows command."); m->mothurOutEndLine(); abort = true; }
- else if (flowFileName == "not open") { abort = true; }
+ if (flowFileName == "not found") {
+ flowFileName = m->getFlowFile();
+ if (flowFileName != "") { m->mothurOut("Using " + flowFileName + " as input file for the flow parameter."); m->mothurOutEndLine(); }
+ else {
+ m->mothurOut("No valid current flow file. You must provide a flow file."); m->mothurOutEndLine();
+ abort = true;
+ }
+ }else if (flowFileName == "not open") { flowFileName = ""; abort = true; }
//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"){
// ...at some point should added some additional type checking...
string temp;
- temp = validParameter.validFile(parameters, "minflows", false); if (temp == "not found") { temp = "360"; }
- convert(temp, minFlows);
+ temp = validParameter.validFile(parameters, "minflows", false); if (temp == "not found") { temp = "450"; }
+ m->mothurConvert(temp, minFlows);
- temp = validParameter.validFile(parameters, "maxflows", false); if (temp == "not found") { temp = "720"; }
- convert(temp, maxFlows);
+ temp = validParameter.validFile(parameters, "maxflows", false); if (temp == "not found") { temp = "450"; }
+ m->mothurConvert(temp, maxFlows);
temp = validParameter.validFile(parameters, "oligos", true);
if (temp == "not found") { oligoFileName = ""; }
else if(temp == "not open") { abort = true; }
- else { oligoFileName = temp; }
+ else { oligoFileName = temp; m->setOligosFile(oligoFileName); }
temp = validParameter.validFile(parameters, "fasta", false); if (temp == "not found"){ fasta = 0; }
else if(m->isTrue(temp)) { fasta = 1; }
temp = validParameter.validFile(parameters, "maxhomop", false); if (temp == "not found"){ temp = "9"; }
- convert(temp, maxHomoP);
+ m->mothurConvert(temp, maxHomoP);
temp = validParameter.validFile(parameters, "signal", false); if (temp == "not found"){ temp = "0.50"; }
- convert(temp, signal);
+ m->mothurConvert(temp, signal);
temp = validParameter.validFile(parameters, "noise", false); if (temp == "not found"){ temp = "0.70"; }
- convert(temp, noise);
-
- temp = validParameter.validFile(parameters, "minlength", false); if (temp == "not found"){ temp = "0"; }
- convert(temp, minLength);
-
- temp = validParameter.validFile(parameters, "maxlength", false); if (temp == "not found"){ temp = "0"; }
- convert(temp, maxLength);
-
+ m->mothurConvert(temp, noise);
+
temp = validParameter.validFile(parameters, "bdiffs", false); if (temp == "not found"){ temp = "0"; }
- convert(temp, bdiffs);
+ m->mothurConvert(temp, bdiffs);
temp = validParameter.validFile(parameters, "pdiffs", false); if (temp == "not found"){ temp = "0"; }
- convert(temp, pdiffs);
+ m->mothurConvert(temp, pdiffs);
+
+ temp = validParameter.validFile(parameters, "ldiffs", false); if (temp == "not found") { temp = "0"; }
+ m->mothurConvert(temp, ldiffs);
+
+ temp = validParameter.validFile(parameters, "sdiffs", false); if (temp == "not found") { temp = "0"; }
+ m->mothurConvert(temp, sdiffs);
- temp = validParameter.validFile(parameters, "tdiffs", false);
- if (temp == "not found"){ int tempTotal = pdiffs + bdiffs; temp = toString(tempTotal); }
- convert(temp, tdiffs);
- if(tdiffs == 0){ tdiffs = bdiffs + pdiffs; }
+ temp = validParameter.validFile(parameters, "tdiffs", false); if (temp == "not found") { int tempTotal = pdiffs + bdiffs + ldiffs + sdiffs; temp = toString(tempTotal); }
+ m->mothurConvert(temp, tdiffs);
- temp = validParameter.validFile(parameters, "allfiles", false); if (temp == "not found"){ temp = "T"; }
- allFiles = m->isTrue(temp);
+ if(tdiffs == 0){ tdiffs = bdiffs + pdiffs + ldiffs + sdiffs; }
+
- temp = validParameter.validFile(parameters, "processors", false); if (temp == "not found"){ temp = "1"; }
- convert(temp, processors);
+ temp = validParameter.validFile(parameters, "processors", false); if (temp == "not found"){ temp = m->getProcessors(); }
+ m->setProcessors(temp);
+ m->mothurConvert(temp, processors);
flowOrder = validParameter.validFile(parameters, "order", false);
if (flowOrder == "not found"){ flowOrder = "TACG"; }
m->mothurOut("The value of the order option must be four bases long\n");
}
- if(oligoFileName == ""){ allFiles = 0; }
+ if(oligoFileName == "") { allFiles = 0; }
+ else { allFiles = 1; }
numFPrimers = 0;
numRPrimers = 0;
+ numLinkers = 0;
+ numSpacers = 0;
}
}
catch(exception& e) {
- m->errorOut(e, "TrimFlowsCommand", "TrimSeqsCommand");
+ m->errorOut(e, "TrimFlowsCommand", "TrimFlowsCommand");
exit(1);
}
}
if (abort == true) { if (calledHelp) { return 0; } return 2; }
- string trimFlowFileName = outputDir + m->getRootName(m->getSimpleName(flowFileName)) + "trim.flow";
+ string trimFlowFileName = outputDir + m->getRootName(m->getSimpleName(flowFileName)) + "trim." + getOutputFileNameTag("flow");
outputNames.push_back(trimFlowFileName); outputTypes["flow"].push_back(trimFlowFileName);
- string scrapFlowFileName = outputDir + m->getRootName(m->getSimpleName(flowFileName)) + "scrap.flow";
+ string scrapFlowFileName = outputDir + m->getRootName(m->getSimpleName(flowFileName)) + "scrap." + getOutputFileNameTag("flow");;
outputNames.push_back(scrapFlowFileName); outputTypes["flow"].push_back(scrapFlowFileName);
- string fastaFileName = outputDir + m->getRootName(m->getSimpleName(flowFileName)) + "flow.fasta";
+ string fastaFileName = outputDir + m->getRootName(m->getSimpleName(flowFileName)) + getOutputFileNameTag("fasta");
if(fasta){
outputNames.push_back(fastaFileName); outputTypes["fasta"].push_back(fastaFileName);
}
- vector<unsigned long int> flowFilePos = getFlowFileBreaks();
+ vector<unsigned long long> flowFilePos;
+ #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux) || (__linux__) || (__unix__) || (__unix)
+ flowFilePos = getFlowFileBreaks();
for (int i = 0; i < (flowFilePos.size()-1); i++) {
lines.push_back(new linePair(flowFilePos[i], flowFilePos[(i+1)]));
}
-
+ #else
+ ifstream in; m->openInputFile(flowFileName, in); in >> numFlows; in.close();
+ ///////////////////////////////////////// until I fix multiple processors for windows //////////////////
+ processors = 1;
+ ///////////////////////////////////////// until I fix multiple processors for windows //////////////////
+ if (processors == 1) {
+ lines.push_back(new linePair(0, 1000));
+ }else {
+ int numFlowLines;
+ flowFilePos = m->setFilePosEachLine(flowFileName, numFlowLines);
+ flowFilePos.erase(flowFilePos.begin() + 1); numFlowLines--;
+
+ //figure out how many sequences you have to process
+ int numSeqsPerProcessor = numFlowLines / processors;
+ cout << numSeqsPerProcessor << '\t' << numFlowLines << endl;
+ for (int i = 0; i < processors; i++) {
+ int startIndex = i * numSeqsPerProcessor;
+ if(i == (processors - 1)){ numSeqsPerProcessor = numFlowLines - i * numSeqsPerProcessor; }
+ lines.push_back(new linePair(flowFilePos[startIndex], numSeqsPerProcessor));
+ cout << flowFilePos[startIndex] << '\t' << numSeqsPerProcessor << endl;
+ }
+ }
+ #endif
+
vector<vector<string> > barcodePrimerComboFileNames;
if(oligoFileName != ""){
getOligos(barcodePrimerComboFileNames);
}
-#if defined (__APPLE__) || (__MACH__) || (linux) || (__linux)
if(processors == 1){
driverCreateTrim(flowFileName, trimFlowFileName, scrapFlowFileName, fastaFileName, barcodePrimerComboFileNames, lines[0]);
}else{
createProcessesCreateTrim(flowFileName, trimFlowFileName, scrapFlowFileName, fastaFileName, barcodePrimerComboFileNames);
}
-#else
- driverCreateTrim(flowFileName, trimFlowFileName, scrapFlowFileName, fastaFileName, barcodePrimerComboFileNames, lines[0]);
-#endif
if (m->control_pressed) { return 0; }
ofstream output;
if(allFiles){
-
- flowFilesFileName = outputDir + m->getRootName(m->getSimpleName(flowFileName)) + "flow.files";
+ set<string> namesAlreadyProcessed;
+ flowFilesFileName = outputDir + m->getRootName(m->getSimpleName(flowFileName)) + getOutputFileNameTag("file");
m->openOutputFile(flowFilesFileName, output);
for(int i=0;i<barcodePrimerComboFileNames.size();i++){
for(int j=0;j<barcodePrimerComboFileNames[0].size();j++){
-
- FILE * pFile;
- unsigned long int size;
-
- //get num bytes in file
- pFile = fopen (barcodePrimerComboFileNames[i][j].c_str(),"rb");
- if (pFile==NULL) perror ("Error opening file");
- else{
- fseek (pFile, 0, SEEK_END);
- size=ftell (pFile);
- fclose (pFile);
- }
-
- if(size < 10){
- remove(barcodePrimerComboFileNames[i][j].c_str());
- }
- else{
- output << barcodePrimerComboFileNames[i][j] << endl;
+ if (namesAlreadyProcessed.count(barcodePrimerComboFileNames[i][j]) == 0) {
+ FILE * pFile;
+ unsigned long long size;
+
+ //get num bytes in file
+ pFile = fopen (barcodePrimerComboFileNames[i][j].c_str(),"rb");
+ if (pFile==NULL) perror ("Error opening file");
+ else{
+ fseek (pFile, 0, SEEK_END);
+ size=ftell(pFile);
+ fclose (pFile);
+ }
+
+ if(size < 10){
+ m->mothurRemove(barcodePrimerComboFileNames[i][j]);
+ }
+ else{
+ output << m->getFullPathName(barcodePrimerComboFileNames[i][j]) << endl;
+ outputNames.push_back(barcodePrimerComboFileNames[i][j]);
+ outputTypes["flow"].push_back(barcodePrimerComboFileNames[i][j]);
+ }
+ namesAlreadyProcessed.insert(barcodePrimerComboFileNames[i][j]);
}
}
}
output.close();
}
else{
- flowFilesFileName = outputDir + m->getRootName(m->getSimpleName(flowFileName)) + "flow.files";
+ flowFilesFileName = outputDir + m->getRootName(m->getSimpleName(flowFileName)) + getOutputFileNameTag("file");
m->openOutputFile(flowFilesFileName, output);
- output << trimFlowFileName << endl;
+ output << m->getFullPathName(trimFlowFileName) << endl;
output.close();
}
outputTypes["flow.files"].push_back(flowFilesFileName);
- outputNames.push_back(flowFileName);
+ outputNames.push_back(flowFilesFileName);
- //set fasta file as new current fastafile
- string current = "";
- itTypes = outputTypes.find("fasta");
- if (itTypes != outputTypes.end()) {
- if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setFastaFile(current); }
- }
+// set fasta file as new current fastafile
+// string current = "";
+// itTypes = outputTypes.find("fasta");
+// if (itTypes != outputTypes.end()) {
+// if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setFastaFile(current); }
+// }
m->mothurOutEndLine();
m->mothurOut("Output File Names: "); m->mothurOutEndLine();
//***************************************************************************************************************
-int TrimFlowsCommand::driverCreateTrim(string flowFileName, string trimFlowFileName, string scrapFlowFileName, string fastaFileName, vector<vector<string> > barcodePrimerComboFileNames, linePair* line){
+int TrimFlowsCommand::driverCreateTrim(string flowFileName, string trimFlowFileName, string scrapFlowFileName, string fastaFileName, vector<vector<string> > thisBarcodePrimerComboFileNames, linePair* line){
try {
-
ofstream trimFlowFile;
m->openOutputFile(trimFlowFileName, trimFlowFile);
trimFlowFile.setf(ios::fixed, ios::floatfield); trimFlowFile.setf(ios::showpoint);
ofstream scrapFlowFile;
m->openOutputFile(scrapFlowFileName, scrapFlowFile);
scrapFlowFile.setf(ios::fixed, ios::floatfield); scrapFlowFile.setf(ios::showpoint);
-
- if(line->start == 4){
+
+ ofstream fastaFile;
+ if(fasta){ m->openOutputFile(fastaFileName, fastaFile); }
+
+ ifstream flowFile;
+ m->openInputFile(flowFileName, flowFile);
+
+ flowFile.seekg(line->start);
+
+ if(line->start == 0){
+ flowFile >> numFlows; m->gobble(flowFile);
scrapFlowFile << maxFlows << endl;
trimFlowFile << maxFlows << endl;
if(allFiles){
- for(int i=0;i<barcodePrimerComboFileNames.size();i++){
- for(int j=0;j<barcodePrimerComboFileNames[0].size();j++){
- // barcodePrimerComboFileNames[i][j] += toString(getpid()) + ".temp";
+ for(int i=0;i<thisBarcodePrimerComboFileNames.size();i++){
+ for(int j=0;j<thisBarcodePrimerComboFileNames[0].size();j++){
ofstream temp;
- m->openOutputFile(barcodePrimerComboFileNames[i][j], temp);
+ m->openOutputFile(thisBarcodePrimerComboFileNames[i][j], temp);
temp << maxFlows << endl;
temp.close();
}
}
FlowData flowData(numFlows, signal, noise, maxHomoP, flowOrder);
-
- ofstream fastaFile;
- if(fasta){ m->openOutputFile(fastaFileName, fastaFile); }
-
- ifstream flowFile;
- m->openInputFile(flowFileName, flowFile);
-
- flowFile.seekg(line->start);
-
+ //cout << " driver flowdata address " << &flowData << &flowFile << endl;
int count = 0;
bool moreSeqs = 1;
-
+
+ TrimOligos trimOligos(pdiffs, bdiffs, ldiffs, sdiffs, primers, barcodes, revPrimer, linker, spacer);
+
while(moreSeqs) {
+
+ if (m->control_pressed) { break; }
int success = 1;
int currentSeqDiffs = 0;
string trashCode = "";
- flowData.getNext(flowFile);
+ flowData.getNext(flowFile);
+ //cout << "driver good bit " << flowFile.good() << endl;
flowData.capFlows(maxFlows);
Sequence currSeq = flowData.getSequence();
+
if(!flowData.hasMinFlows(minFlows)){ //screen to see if sequence is of a minimum number of flows
success = 0;
trashCode += 'l';
}
- if(minLength > 0 || maxLength > 0){ //screen to see if sequence is above and below a specific number of bases
- int seqLength = currSeq.getNumBases();
- if(seqLength < minLength || seqLength > maxLength){
- success = 0;
- trashCode += 'l';
- }
- }
-
int primerIndex = 0;
int barcodeIndex = 0;
+ if(numLinkers != 0){
+ success = trimOligos.stripLinker(currSeq);
+ if(success > ldiffs) { trashCode += 'k'; }
+ else{ currentSeqDiffs += success; }
+
+ }
+
if(barcodes.size() != 0){
- success = stripBarcode(currSeq, barcodeIndex);
+ success = trimOligos.stripBarcode(currSeq, barcodeIndex);
if(success > bdiffs) { trashCode += 'b'; }
else{ currentSeqDiffs += success; }
}
+ if(numSpacers != 0){
+ success = trimOligos.stripSpacer(currSeq);
+ if(success > sdiffs) { trashCode += 's'; }
+ else{ currentSeqDiffs += success; }
+
+ }
+
if(numFPrimers != 0){
- success = stripForward(currSeq, primerIndex);
+ success = trimOligos.stripForward(currSeq, primerIndex);
if(success > pdiffs) { trashCode += 'f'; }
else{ currentSeqDiffs += success; }
}
if (currentSeqDiffs > tdiffs) { trashCode += 't'; }
if(numRPrimers != 0){
- success = stripReverse(currSeq);
+ success = trimOligos.stripReverse(currSeq);
if(!success) { trashCode += 'r'; }
}
-
+
if(trashCode.length() == 0){
flowData.printFlows(trimFlowFile);
if(allFiles){
ofstream output;
- m->openOutputFileAppend(barcodePrimerComboFileNames[barcodeIndex][primerIndex], output);
+ m->openOutputFileAppend(thisBarcodePrimerComboFileNames[barcodeIndex][primerIndex], output);
output.setf(ios::fixed, ios::floatfield); trimFlowFile.setf(ios::showpoint);
flowData.printFlows(output);
}
count++;
-
+ //cout << "driver" << '\t' << currSeq.getName() << endl;
//report progress
if((count) % 10000 == 0){ m->mothurOut(toString(count)); m->mothurOutEndLine(); }
-#if defined (__APPLE__) || (__MACH__) || (linux) || (__linux)
- unsigned long int pos = flowFile.tellg();
+#if defined (__APPLE__) || (__MACH__) || (linux) || (__linux) || (__linux__) || (__unix__) || (__unix)
+ unsigned long long pos = flowFile.tellg();
if ((pos == -1) || (pos >= line->end)) { break; }
#else
}
else if(type == "REVERSE"){
- Sequence oligoRC("reverse", oligo);
- oligoRC.reverseComplement();
- revPrimer.push_back(oligoRC.getUnaligned());
+ string oligoRC = reverseOligo(oligo);
+ revPrimer.push_back(oligoRC);
}
else if(type == "BARCODE"){
oligosFile >> group;
barcodes[oligo]=indexBarcode; indexBarcode++;
barcodeNameVector.push_back(group);
+ }else if(type == "LINKER"){
+ linker.push_back(oligo);
+ }else if(type == "SPACER"){
+ spacer.push_back(oligo);
}
else{
m->mothurOut(type + " is not recognized as a valid type. Choices are forward, reverse, and barcode. Ignoring " + oligo + "."); m->mothurOutEndLine();
fileName = outputDir + m->getRootName(m->getSimpleName(flowFileName)) + comboGroupName + ".flow";
}
- outputNames.push_back(fileName);
- outputTypes["flow"].push_back(fileName);
outFlowFileNames[itBar->second][itPrimer->second] = fileName;
ofstream temp;
numFPrimers = primers.size();
numRPrimers = revPrimer.size();
+ numLinkers = linker.size();
+ numSpacers = spacer.size();
}
catch(exception& e) {
exit(1);
}
}
-
-//***************************************************************************************************************
-
-int TrimFlowsCommand::stripBarcode(Sequence& seq, int& group){
- try {
-
- string rawSequence = seq.getUnaligned();
- int success = bdiffs + 1; //guilty until proven innocent
-
- //can you find the barcode
- for(map<string,int>::iterator it=barcodes.begin();it!=barcodes.end();it++){
- string oligo = it->first;
- if(rawSequence.length() < oligo.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(oligo, rawSequence.substr(0,oligo.length()))){
- group = it->second;
- seq.setUnaligned(rawSequence.substr(oligo.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
-
- int maxLength = 0;
-
- Alignment* alignment;
- if (barcodes.size() > 0) {
- map<string,int>::iterator it=barcodes.begin();
-
- for(it;it!=barcodes.end();it++){
- if(it->first.length() > maxLength){
- maxLength = it->first.length();
- }
- }
- alignment = new NeedlemanOverlap(-1.0, 1.0, -1.0, (maxLength+bdiffs+1));
-
- }else{ alignment = NULL; }
-
- //can you find the barcode
- int minDiff = 1e6;
- int minCount = 1;
- int minGroup = -1;
- int minPos = 0;
-
- for(map<string,int>::iterator it=barcodes.begin();it!=barcodes.end();it++){
- string oligo = it->first;
- // int length = oligo.length();
-
- if(rawSequence.length() < maxLength){ //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, rawSequence.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(numDiff < minDiff){
- minDiff = numDiff;
- minCount = 1;
- minGroup = it->second;
- minPos = 0;
- for(int i=0;i<alnLength;i++){
- if(temp[i] != '-'){
- minPos++;
- }
- }
- }
- else if(numDiff == minDiff){
- minCount++;
- }
-
- }
-
- if(minDiff > bdiffs) { success = minDiff; } //no good matches
- else if(minCount > 1) { success = bdiffs + 100; } //can't tell the difference between multiple barcodes
- else{ //use the best match
- group = minGroup;
- seq.setUnaligned(rawSequence.substr(minPos));
- success = minDiff;
- }
-
- if (alignment != NULL) { delete alignment; }
-
- }
-
- return success;
-
- }
- catch(exception& e) {
- m->errorOut(e, "TrimFlowsCommand", "stripBarcode");
- exit(1);
- }
-
-}
-
-//***************************************************************************************************************
-
-int TrimFlowsCommand::stripForward(Sequence& seq, int& group){
- try {
-
- string rawSequence = seq.getUnaligned();
- int success = pdiffs + 1; //guilty until proven innocent
-
- //can you find the primer
- for(map<string,int>::iterator it=primers.begin();it!=primers.end();it++){
- string oligo = it->first;
- if(rawSequence.length() < oligo.length()){ //let's just assume that the primers are the same length
- success = pdiffs + 10; //if the sequence is shorter than the barcode then bail out
- break;
- }
-
- if(compareDNASeq(oligo, rawSequence.substr(0,oligo.length()))){
- group = it->second;
- seq.setUnaligned(rawSequence.substr(oligo.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
-
- int maxLength = 0;
-
- Alignment* alignment;
- if (primers.size() > 0) {
- map<string,int>::iterator it=primers.begin();
-
- for(it;it!=primers.end();it++){
- if(it->first.length() > maxLength){
- maxLength = it->first.length();
- }
- }
- alignment = new NeedlemanOverlap(-1.0, 1.0, -1.0, (maxLength+pdiffs+1));
-
- }else{ alignment = NULL; }
-
- //can you find the barcode
- int minDiff = 1e6;
- int minCount = 1;
- int minGroup = -1;
- int minPos = 0;
-
- for(map<string,int>::iterator it=primers.begin();it!=primers.end();it++){
- string oligo = it->first;
- // int length = oligo.length();
-
- if(rawSequence.length() < maxLength){
- success = pdiffs + 100;
- break;
- }
-
- //use needleman to align first barcode.length()+numdiffs of sequence to each barcode
- alignment->align(oligo, rawSequence.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(numDiff < minDiff){
- minDiff = numDiff;
- minCount = 1;
- minGroup = it->second;
- minPos = 0;
- for(int i=0;i<alnLength;i++){
- if(temp[i] != '-'){
- minPos++;
- }
- }
- }
- else if(numDiff == minDiff){
- minCount++;
- }
-
- }
-
- if(minDiff > pdiffs) { success = minDiff; } //no good matches
- else if(minCount > 1) { success = pdiffs + 10; } //can't tell the difference between multiple primers
- else{ //use the best match
- group = minGroup;
- seq.setUnaligned(rawSequence.substr(minPos));
- success = minDiff;
- }
-
- if (alignment != NULL) { delete alignment; }
-
- }
-
- return success;
-
- }
- catch(exception& e) {
- m->errorOut(e, "TrimFlowsCommand", "stripForward");
- exit(1);
- }
-}
-
-//***************************************************************************************************************
-
-bool TrimFlowsCommand::stripReverse(Sequence& seq){
- try {
-
- string rawSequence = seq.getUnaligned();
- bool success = 0; //guilty until proven innocent
-
- for(int i=0;i<numRPrimers;i++){
- string oligo = revPrimer[i];
-
- if(rawSequence.length() < oligo.length()){
- success = 0;
- break;
- }
-
- if(compareDNASeq(oligo, rawSequence.substr(rawSequence.length()-oligo.length(),oligo.length()))){
- seq.setUnaligned(rawSequence.substr(0,rawSequence.length()-oligo.length()));
- success = 1;
- break;
- }
- }
-
- return success;
-
- }
- catch(exception& e) {
- m->errorOut(e, "TrimFlowsCommand", "stripReverse");
- exit(1);
- }
-}
-
-
-//***************************************************************************************************************
-
-bool TrimFlowsCommand::compareDNASeq(string oligo, string seq){
- try {
- bool success = 1;
- int length = oligo.length();
-
- for(int i=0;i<length;i++){
-
- if(oligo[i] != seq[i]){
- if(oligo[i] == 'A' || oligo[i] == 'T' || oligo[i] == 'G' || oligo[i] == 'C') { success = 0; }
- else if((oligo[i] == 'N' || oligo[i] == 'I') && (seq[i] == 'N')) { success = 0; }
- else if(oligo[i] == 'R' && (seq[i] != 'A' && seq[i] != 'G')) { success = 0; }
- else if(oligo[i] == 'Y' && (seq[i] != 'C' && seq[i] != 'T')) { success = 0; }
- else if(oligo[i] == 'M' && (seq[i] != 'C' && seq[i] != 'A')) { success = 0; }
- else if(oligo[i] == 'K' && (seq[i] != 'T' && seq[i] != 'G')) { success = 0; }
- else if(oligo[i] == 'W' && (seq[i] != 'T' && seq[i] != 'A')) { success = 0; }
- else if(oligo[i] == 'S' && (seq[i] != 'C' && seq[i] != 'G')) { success = 0; }
- else if(oligo[i] == 'B' && (seq[i] != 'C' && seq[i] != 'T' && seq[i] != 'G')) { success = 0; }
- else if(oligo[i] == 'D' && (seq[i] != 'A' && seq[i] != 'T' && seq[i] != 'G')) { success = 0; }
- else if(oligo[i] == 'H' && (seq[i] != 'A' && seq[i] != 'T' && seq[i] != 'C')) { success = 0; }
- else if(oligo[i] == 'V' && (seq[i] != 'A' && seq[i] != 'C' && seq[i] != 'G')) { success = 0; }
-
- if(success == 0) { break; }
- }
- else{
- success = 1;
- }
- }
-
- return success;
- }
- catch(exception& e) {
- m->errorOut(e, "TrimFlowsCommand", "compareDNASeq");
- exit(1);
- }
-
-}
-
-//***************************************************************************************************************
-
-int TrimFlowsCommand::countDiffs(string oligo, string seq){
+//********************************************************************/
+string TrimFlowsCommand::reverseOligo(string oligo){
try {
-
- int length = oligo.length();
- int countDiffs = 0;
-
- for(int i=0;i<length;i++){
-
- if(oligo[i] != seq[i]){
- if(oligo[i] == 'A' || oligo[i] == 'T' || oligo[i] == 'G' || oligo[i] == 'C' || oligo[i] == '-' || oligo[i] == '.') { countDiffs++; }
- else if((oligo[i] == 'N' || oligo[i] == 'I') && (seq[i] == 'N')) { countDiffs++; }
- else if(oligo[i] == 'R' && (seq[i] != 'A' && seq[i] != 'G')) { countDiffs++; }
- else if(oligo[i] == 'Y' && (seq[i] != 'C' && seq[i] != 'T')) { countDiffs++; }
- else if(oligo[i] == 'M' && (seq[i] != 'C' && seq[i] != 'A')) { countDiffs++; }
- else if(oligo[i] == 'K' && (seq[i] != 'T' && seq[i] != 'G')) { countDiffs++; }
- else if(oligo[i] == 'W' && (seq[i] != 'T' && seq[i] != 'A')) { countDiffs++; }
- else if(oligo[i] == 'S' && (seq[i] != 'C' && seq[i] != 'G')) { countDiffs++; }
- else if(oligo[i] == 'B' && (seq[i] != 'C' && seq[i] != 'T' && seq[i] != 'G')) { countDiffs++; }
- else if(oligo[i] == 'D' && (seq[i] != 'A' && seq[i] != 'T' && seq[i] != 'G')) { countDiffs++; }
- else if(oligo[i] == 'H' && (seq[i] != 'A' && seq[i] != 'T' && seq[i] != 'C')) { countDiffs++; }
- else if(oligo[i] == 'V' && (seq[i] != 'A' && seq[i] != 'C' && seq[i] != 'G')) { countDiffs++; }
- }
-
- }
-
- return countDiffs;
- }
+ string reverse = "";
+
+ for(int i=oligo.length()-1;i>=0;i--){
+
+ if(oligo[i] == 'A') { reverse += 'T'; }
+ else if(oligo[i] == 'T'){ reverse += 'A'; }
+ else if(oligo[i] == 'U'){ reverse += 'A'; }
+
+ else if(oligo[i] == 'G'){ reverse += 'C'; }
+ else if(oligo[i] == 'C'){ reverse += 'G'; }
+
+ else if(oligo[i] == 'R'){ reverse += 'Y'; }
+ else if(oligo[i] == 'Y'){ reverse += 'R'; }
+
+ else if(oligo[i] == 'M'){ reverse += 'K'; }
+ else if(oligo[i] == 'K'){ reverse += 'M'; }
+
+ else if(oligo[i] == 'W'){ reverse += 'W'; }
+ else if(oligo[i] == 'S'){ reverse += 'S'; }
+
+ else if(oligo[i] == 'B'){ reverse += 'V'; }
+ else if(oligo[i] == 'V'){ reverse += 'B'; }
+
+ else if(oligo[i] == 'D'){ reverse += 'H'; }
+ else if(oligo[i] == 'H'){ reverse += 'D'; }
+
+ else { reverse += 'N'; }
+ }
+
+
+ return reverse;
+ }
catch(exception& e) {
- m->errorOut(e, "TrimFlowsCommand", "countDiffs");
+ m->errorOut(e, "TrimFlowsCommand", "reverseOligo");
exit(1);
}
-
}
/**************************************************************************************************/
-
-vector<unsigned long int> TrimFlowsCommand::getFlowFileBreaks() {
+vector<unsigned long long> TrimFlowsCommand::getFlowFileBreaks() {
try{
- vector<unsigned long int> filePos;
+ vector<unsigned long long> filePos;
filePos.push_back(0);
FILE * pFile;
- unsigned long int size;
+ unsigned long long size;
//get num bytes in file
pFile = fopen (flowFileName.c_str(),"rb");
}
//estimate file breaks
- unsigned long int chunkSize = 0;
+ unsigned long long chunkSize = 0;
chunkSize = size / processors;
//file too small to divide by processors
//for each process seekg to closest file break and search for next '>' char. make that the filebreak
for (int i = 0; i < processors; i++) {
- unsigned long int spot = (i+1) * chunkSize;
+ unsigned long long spot = (i+1) * chunkSize;
ifstream in;
m->openInputFile(flowFileName, in);
string dummy = m->getline(in);
//there was not another sequence before the end of the file
- unsigned long int sanityPos = in.tellg();
+ unsigned long long sanityPos = in.tellg();
// if (sanityPos == -1) { break; }
// else { filePos.push_back(newSpot); }
m->openInputFile(flowFileName, in);
in >> numFlows;
m->gobble(in);
- unsigned long int spot = in.tellg();
- filePos[0] = spot;
+ //unsigned long long spot = in.tellg();
+ //filePos[0] = spot;
in.close();
processors = (filePos.size() - 1);
int TrimFlowsCommand::createProcessesCreateTrim(string flowFileName, string trimFlowFileName, string scrapFlowFileName, string fastaFileName, vector<vector<string> > barcodePrimerComboFileNames){
try {
-#if defined (__APPLE__) || (__MACH__) || (linux) || (__linux)
- int process = 1;
- int exitCommand = 1;
processIDS.clear();
+ int exitCommand = 1;
+
+#if defined (__APPLE__) || (__MACH__) || (linux) || (__linux) || (__linux__) || (__unix__) || (__unix)
+ int process = 1;
//loop through and create all the processes you want
while (process != processors) {
int temp = processIDS[i];
wait(&temp);
}
+#else
+ //////////////////////////////////////////////////////////////////////////////////////////////////////
+ //Windows version shared memory, so be careful when passing variables through the trimFlowData struct.
+ //Above fork() will clone, so memory is separate, but that's not the case with windows,
+ //////////////////////////////////////////////////////////////////////////////////////////////////////
+
+ vector<trimFlowData*> pDataArray;
+ DWORD dwThreadIdArray[processors-1];
+ HANDLE hThreadArray[processors-1];
+
+ //Create processor worker threads.
+ for( int i=0; i<processors-1; i++ ){
+ // Allocate memory for thread data.
+ string extension = "";
+ if (i != 0) { extension = toString(i) + ".temp"; processIDS.push_back(i); }
+
+ vector<vector<string> > tempBarcodePrimerComboFileNames = barcodePrimerComboFileNames;
+ if(allFiles){
+ for(int i=0;i<tempBarcodePrimerComboFileNames.size();i++){
+ for(int j=0;j<tempBarcodePrimerComboFileNames[0].size();j++){
+ tempBarcodePrimerComboFileNames[i][j] += extension;
+ ofstream temp;
+ m->openOutputFile(tempBarcodePrimerComboFileNames[i][j], temp);
+ temp.close();
+
+ }
+ }
+ }
+
+ trimFlowData* tempflow = new trimFlowData(flowFileName, (trimFlowFileName + extension), (scrapFlowFileName + extension), fastaFileName, flowOrder, tempBarcodePrimerComboFileNames, barcodes, primers, revPrimer, fasta, allFiles, lines[i]->start, lines[i]->end, m, signal, noise, numFlows, maxFlows, minFlows, maxHomoP, tdiffs, bdiffs, pdiffs, i);
+ pDataArray.push_back(tempflow);
+
+ //MyTrimFlowThreadFunction is in header. It must be global or static to work with the threads.
+ //default security attributes, thread function name, argument to thread function, use default creation flags, returns the thread identifier
+ hThreadArray[i] = CreateThread(NULL, 0, MyTrimFlowThreadFunction, pDataArray[i], 0, &dwThreadIdArray[i]);
+ }
+ //using the main process as a worker saves time and memory
+ ofstream temp;
+ m->openOutputFile(trimFlowFileName, temp);
+ temp.close();
+
+ m->openOutputFile(scrapFlowFileName, temp);
+ temp.close();
+
+ if(fasta){
+ m->openOutputFile(fastaFileName, temp);
+ temp.close();
+ }
+
+ vector<vector<string> > tempBarcodePrimerComboFileNames = barcodePrimerComboFileNames;
+ if(allFiles){
+ for(int i=0;i<tempBarcodePrimerComboFileNames.size();i++){
+ for(int j=0;j<tempBarcodePrimerComboFileNames[0].size();j++){
+ tempBarcodePrimerComboFileNames[i][j] += toString(processors-1) + ".temp";
+ ofstream temp;
+ m->openOutputFile(tempBarcodePrimerComboFileNames[i][j], temp);
+ temp.close();
+
+ }
+ }
+ }
+
+ //do my part - do last piece because windows is looking for eof
+ int num = driverCreateTrim(flowFileName, (trimFlowFileName + toString(processors-1) + ".temp"), (scrapFlowFileName + toString(processors-1) + ".temp"), (fastaFileName + toString(processors-1) + ".temp"), tempBarcodePrimerComboFileNames, lines[processors-1]);
+ processIDS.push_back((processors-1));
+
+ //Wait until all threads have terminated.
+ WaitForMultipleObjects(processors-1, hThreadArray, TRUE, INFINITE);
+
+ //Close all thread handles and free memory allocations.
+ for(int i=0; i < pDataArray.size(); i++){
+ num += pDataArray[i]->count;
+ CloseHandle(hThreadArray[i]);
+ delete pDataArray[i];
+ }
+
+
+#endif
//append files
m->mothurOutEndLine();
for(int i=0;i<processIDS.size();i++){
m->mothurOut("Appending files from process " + toString(processIDS[i])); m->mothurOutEndLine();
m->appendFiles((trimFlowFileName + toString(processIDS[i]) + ".temp"), trimFlowFileName);
- remove((trimFlowFileName + toString(processIDS[i]) + ".temp").c_str());
+ m->mothurRemove((trimFlowFileName + toString(processIDS[i]) + ".temp"));
// m->mothurOut("\tDone with trim.flow file"); m->mothurOutEndLine();
m->appendFiles((scrapFlowFileName + toString(processIDS[i]) + ".temp"), scrapFlowFileName);
- remove((scrapFlowFileName + toString(processIDS[i]) + ".temp").c_str());
+ m->mothurRemove((scrapFlowFileName + toString(processIDS[i]) + ".temp"));
// m->mothurOut("\tDone with scrap.flow file"); m->mothurOutEndLine();
if(fasta){
m->appendFiles((fastaFileName + toString(processIDS[i]) + ".temp"), fastaFileName);
- remove((fastaFileName + toString(processIDS[i]) + ".temp").c_str());
+ m->mothurRemove((fastaFileName + toString(processIDS[i]) + ".temp"));
// m->mothurOut("\tDone with flow.fasta file"); m->mothurOutEndLine();
}
if(allFiles){
for (int j = 0; j < barcodePrimerComboFileNames.size(); j++) {
for (int k = 0; k < barcodePrimerComboFileNames[0].size(); k++) {
m->appendFiles((barcodePrimerComboFileNames[j][k] + toString(processIDS[i]) + ".temp"), barcodePrimerComboFileNames[j][k]);
- remove((barcodePrimerComboFileNames[j][k] + toString(processIDS[i]) + ".temp").c_str());
+ m->mothurRemove((barcodePrimerComboFileNames[j][k] + toString(processIDS[i]) + ".temp"));
}
}
}
}
return exitCommand;
-#endif
+
}
catch(exception& e) {
m->errorOut(e, "TrimFlowsCommand", "createProcessesCreateTrim");