]> git.donarmstrong.com Git - mothur.git/blobdiff - flowdata.cpp
changing command name classify.shared to classifyrf.shared
[mothur.git] / flowdata.cpp
index f40fdd64b21aa0ad2dc2beae7e01e942ba46dc23..7d61f8c265e060f2dc0002c94f69edfd054f89ae 100644 (file)
@@ -15,63 +15,88 @@ FlowData::FlowData(){}
 
 //**********************************************************************************************************************
 
-FlowData::FlowData(ifstream& flowFile, float signal, float noise, int maxHomoP){
+FlowData::~FlowData(){ /*      do nothing      */      }
+
+//**********************************************************************************************************************
+
+FlowData::FlowData(int numFlows, float signal, float noise, int maxHomoP, string baseFlow) : 
+                       numFlows(numFlows), signalIntensity(signal), noiseIntensity(noise), maxHomoP(maxHomoP), baseFlow(baseFlow){
 
        try {
                m = MothurOut::getInstance();
 
-               baseFlow = "TACG";
+               flowData.assign(numFlows, 0);
+//             baseFlow = "TACG";
                seqName = "";
-               numFlows = 0;
                locationString = "";
-               seqLength = 0;
-               
-               string lengthString;
-               string flowString;
-               
-               flowFile >> seqName >> locationString >> lengthString >> flowString;
+       }
+       catch(exception& e) {
+               m->errorOut(e, "FlowData", "FlowData");
+               exit(1);
+       }
+       
+}
 
-               convert(lengthString.substr(7), seqLength);
-               convert(flowString.substr(9), numFlows);
+//**********************************************************************************************************************
 
-               flowData.resize(numFlows);
-               
-               if (seqName == "") {
-                       m->mothurOut("Error reading quality file, name blank at position, " + toString(flowFile.tellg()));
-                       m->mothurOutEndLine(); 
-               }
-               else{
-                       seqName = seqName.substr(1);
-                       for(int i=0;i<numFlows;i++)     {       flowFile >> flowData[i];        }
+bool FlowData::getNext(ifstream& flowFile){
+       
+       try {
+        seqName = getSequenceName(flowFile);
+               flowFile >> endFlow;    
+        if (!m->control_pressed) {
+            for(int i=0;i<numFlows;i++)        {       flowFile >> flowData[i];        }
+            updateEndFlow(); 
+            translateFlow();
+            m->gobble(flowFile);
                }
+           
+               if(flowFile){   return 1;       }
+               else            {       return 0;       }
+       }
+       catch(exception& e) {
+               m->errorOut(e, "FlowData", "getNext");
+               exit(1);
+       }
+       
+}
+//********************************************************************************************************************
+string FlowData::getSequenceName(ifstream& flowFile) {
+       try {
+               string name = "";
                
-               findDeadSpot(signal, noise, maxHomoP);
-               translateFlow();
+        flowFile >> name;
                
-               m->gobble(flowFile);
+               if (name.length() != 0) { 
+            m->checkName(name);
+        }else{ m->mothurOut("Error in reading your flowfile, at position " + toString(flowFile.tellg()) + ". Blank name."); m->mothurOutEndLine(); m->control_pressed = true;  }
+        
+               return name;
        }
        catch(exception& e) {
-               m->errorOut(e, "FlowData", "FlowData");
+               m->errorOut(e, "FlowData", "getSequenceName");
                exit(1);
        }
-       
 }
 
 //**********************************************************************************************************************
 
-void FlowData::findDeadSpot(float signalIntensity, float noiseIntensity, int maxHomoP){
+void FlowData::updateEndFlow(){
        try{
                
-               int currLength = 0;
+        if (baseFlow.length() > 4) { return; }
+        
+               //int currLength = 0;
                float maxIntensity = (float) maxHomoP + 0.49;
                
-               deadSpot = 0;
-               while(currLength < seqLength + 4){
+               int deadSpot = 0;
+                       
+               while(deadSpot < endFlow){
                        int signal = 0;
                        int noise = 0;
                        
-                       for(int i=0;i<4;i++){
-                               float intensity = flowData[i + 4 * deadSpot];
+                       for(int i=0;i<baseFlow.length();i++){
+                               float intensity = flowData[i + deadSpot];
                                if(intensity > signalIntensity){
                                        signal++;
 
@@ -79,18 +104,16 @@ void FlowData::findDeadSpot(float signalIntensity, float noiseIntensity, int max
                                                noise++;
                                        }
                                }
-                               currLength += (int)(intensity+0.5);
                        }
 
                        if(noise > 0 || signal == 0){
                                break;
                        }
                
-                       deadSpot++;
+                       deadSpot += baseFlow.length();
                }
-               deadSpot *= 4;
-               seqLength = currLength;
-               
+               endFlow = deadSpot;
+
        }
        catch(exception& e) {
                m->errorOut(e, "FlowData", "findDeadSpot");
@@ -99,27 +122,49 @@ void FlowData::findDeadSpot(float signalIntensity, float noiseIntensity, int max
 }
 
 //**********************************************************************************************************************
-
+//TATGCT
+//1 0 0 0 0 1
+//then the second positive flow is for a T, but you saw a T between the last and previous flow adn it wasn't positive, so something is missing
+//Becomes TNT
 void FlowData::translateFlow(){
-       
        try{
-               sequence = "";
-               for(int i=0;i<deadSpot;i++){
+        sequence = "";
+        set<char> charInMiddle;
+        int oldspot = -1;
+        bool updateOld = false;
+        
+        for(int i=0;i<endFlow;i++){
                        int intensity = (int)(flowData[i] + 0.5);
-                       char base = baseFlow[i % 4];
+                       char base = baseFlow[i % baseFlow.length()];
+            
+            if (intensity == 0) { //are we in the middle
+                if (oldspot != -1) { charInMiddle.insert(base); }
+            }else if (intensity >= 1) {
+                if (oldspot == -1) { updateOld = true;  }
+                else {  //check for bases inbetween two 1's
+                    if (charInMiddle.count(base) != 0) { //we want to covert to an N
+                        sequence = sequence.substr(0, oldspot+1);
+                        sequence += 'N';
+                    }
+                    updateOld = true;
+                    charInMiddle.clear();
+                }
+            }
                        
                        for(int j=0;j<intensity;j++){
                                sequence += base;
                        }
+            
+            if (updateOld) { oldspot = sequence.length()-1;  updateOld = false; }
                }
-
+        
                if(sequence.size() > 4){
                        sequence = sequence.substr(4);
                }
                else{
                        sequence = "NNNN";
                }
-       }
+    }
        catch(exception& e) {
                m->errorOut(e, "FlowData", "translateFlow");
                exit(1);
@@ -128,12 +173,13 @@ void FlowData::translateFlow(){
 
 //**********************************************************************************************************************
 
-void FlowData::capFlows(int maxFlows){
+void FlowData::capFlows(int mF){
        
        try{
                
-               numFlows = maxFlows;
-               if(deadSpot > maxFlows){        deadSpot = maxFlows;    }
+               maxFlows = mF;
+               if(endFlow > maxFlows){ endFlow = maxFlows;     }       
+        translateFlow();
                
        }
        catch(exception& e) {
@@ -144,13 +190,18 @@ void FlowData::capFlows(int maxFlows){
 
 //**********************************************************************************************************************
 
-bool FlowData::hasMinFlows(int minFlows){
+bool FlowData::hasGoodHomoP(){
        
        try{
-               bool pastMin = 0;
-               
-               if(deadSpot >= minFlows){       pastMin = 1;    }
-               return pastMin;
+        
+        float maxIntensity = (float) maxHomoP + 0.49;
+
+        for(int i=0;i<endFlow;i++){
+            if(flowData[i] > maxIntensity){
+                return 0;
+            }
+        }
+               return 1;
        }
        catch(exception& e) {
                m->errorOut(e, "FlowData", "hasMinFlows");
@@ -160,26 +211,29 @@ bool FlowData::hasMinFlows(int minFlows){
 
 //**********************************************************************************************************************
 
-Sequence FlowData::getSequence(){
+bool FlowData::hasMinFlows(int minFlows){
        
        try{
-               return Sequence(seqName, sequence);
+               bool pastMin = 0;
+               if(endFlow >= minFlows){        pastMin = 1;    }
+
+               return pastMin;
        }
        catch(exception& e) {
-               m->errorOut(e, "FlowData", "getSequence");
+               m->errorOut(e, "FlowData", "hasMinFlows");
                exit(1);
        }
 }
 
 //**********************************************************************************************************************
 
-int FlowData::getSeqLength(){
+Sequence FlowData::getSequence(){
        
        try{
-               return seqLength;               
+               return Sequence(seqName, sequence);
        }
        catch(exception& e) {
-               m->errorOut(e, "FlowData", "getSeqLength");
+               m->errorOut(e, "FlowData", "getSequence");
                exit(1);
        }
 }
@@ -188,10 +242,10 @@ int FlowData::getSeqLength(){
 
 void FlowData::printFlows(ofstream& outFlowFile){
        try{
-       //      outFlowFile << '>' << seqName << locationString << " length=" << seqLength << " numflows=" << maxFlows << endl;
-               outFlowFile << seqName << ' ' << deadSpot << ' ' << setprecision(2);
+//     outFlowFile << '>' << seqName << locationString << " length=" << seqLength << " numflows=" << maxFlows << endl;
+               outFlowFile << seqName << ' ' << endFlow << ' ' << setprecision(2);
 
-               for(int i=0;i<numFlows;i++){
+               for(int i=0;i<maxFlows;i++){
                        outFlowFile << flowData[i] << ' ';
                }
                outFlowFile << endl;
@@ -206,10 +260,7 @@ void FlowData::printFlows(ofstream& outFlowFile){
 
 void FlowData::printFlows(ofstream& outFlowFile, string scrapCode){
        try{
-       
-       //      outFlowFile << '>' << seqName << locationString << " length=" << seqLength << " numflows=" << maxFlows << endl;
-
-               outFlowFile << seqName << '|' << scrapCode << ' ' << deadSpot << ' ' << setprecision(2);
+               outFlowFile << seqName << '|' << scrapCode << ' ' << endFlow << ' ' << setprecision(2);
                
                for(int i=0;i<numFlows;i++){
                        outFlowFile << flowData[i] << ' ';
@@ -224,18 +275,15 @@ void FlowData::printFlows(ofstream& outFlowFile, string scrapCode){
 
 //**********************************************************************************************************************
 
-void FlowData::printFASTA(ofstream& outFASTA){
+string FlowData::getName(){
+       
        try{
-               
-               outFASTA << '>' << seqName << endl;
-               outFASTA << sequence << endl;
-
+               return seqName;
        }
        catch(exception& e) {
-               m->errorOut(e, "FlowData", "printFlows");
+               m->errorOut(e, "FlowData", "getName");
                exit(1);
        }
 }
 
-
 //**********************************************************************************************************************