]> git.donarmstrong.com Git - mothur.git/blob - flowdata.cpp
sffinfo bug with flow grams right index when clipQualRight=0
[mothur.git] / flowdata.cpp
1 /*
2  *  flowdata.cpp
3  *  Mothur
4  *
5  *  Created by Pat Schloss on 12/22/10.
6  *  Copyright 2010 Schloss Lab. All rights reserved.
7  *
8  */
9
10 #include "flowdata.h"
11
12 //**********************************************************************************************************************
13
14 FlowData::FlowData(){}
15
16 //**********************************************************************************************************************
17
18 FlowData::~FlowData(){  /*      do nothing      */      }
19
20 //**********************************************************************************************************************
21
22 FlowData::FlowData(int numFlows, float signal, float noise, int maxHomoP, string baseFlow) : 
23                         numFlows(numFlows), signalIntensity(signal), noiseIntensity(noise), maxHomoP(maxHomoP), baseFlow(baseFlow){
24
25         try {
26                 m = MothurOut::getInstance();
27
28                 flowData.assign(numFlows, 0);
29 //              baseFlow = "TACG";
30                 seqName = "";
31                 locationString = "";
32         }
33         catch(exception& e) {
34                 m->errorOut(e, "FlowData", "FlowData");
35                 exit(1);
36         }
37         
38 }
39
40 //**********************************************************************************************************************
41
42 bool FlowData::getNext(ifstream& flowFile){
43         
44         try {
45         seqName = getSequenceName(flowFile);
46                 flowFile >> endFlow;    
47         if (!m->control_pressed) {
48             for(int i=0;i<numFlows;i++) {       flowFile >> flowData[i];        }
49             updateEndFlow(); 
50             translateFlow();
51             m->gobble(flowFile);
52                 }
53            
54                 if(flowFile){   return 1;       }
55                 else            {       return 0;       }
56         }
57         catch(exception& e) {
58                 m->errorOut(e, "FlowData", "getNext");
59                 exit(1);
60         }
61         
62 }
63 //********************************************************************************************************************
64 string FlowData::getSequenceName(ifstream& flowFile) {
65         try {
66                 string name = "";
67                 
68         flowFile >> name;
69                 
70                 if (name.length() != 0) { 
71             for (int i = 0; i < name.length(); i++) {
72                 if (name[i] == ':') { name[i] = '_'; m->changedSeqNames = true; }
73             }
74         }else{ m->mothurOut("Error in reading your flowfile, at position " + toString(flowFile.tellg()) + ". Blank name."); m->mothurOutEndLine(); m->control_pressed = true;  }
75         
76                 return name;
77         }
78         catch(exception& e) {
79                 m->errorOut(e, "FlowData", "getSequenceName");
80                 exit(1);
81         }
82 }
83
84 //**********************************************************************************************************************
85
86 void FlowData::updateEndFlow(){
87         try{
88                 
89         if (baseFlow.length() > 4) { return; }
90         
91                 //int currLength = 0;
92                 float maxIntensity = (float) maxHomoP + 0.49;
93                 
94                 int deadSpot = 0;
95                         
96                 while(deadSpot < endFlow){
97                         int signal = 0;
98                         int noise = 0;
99                         
100                         for(int i=0;i<baseFlow.length();i++){
101                                 float intensity = flowData[i + deadSpot];
102                                 if(intensity > signalIntensity){
103                                         signal++;
104
105                                         if(intensity  < noiseIntensity || intensity > maxIntensity){
106                                                 noise++;
107                                         }
108                                 }
109                         }
110
111                         if(noise > 0 || signal == 0){
112                                 break;
113                         }
114                 
115                         deadSpot += baseFlow.length();
116                 }
117                 endFlow = deadSpot;
118
119         }
120         catch(exception& e) {
121                 m->errorOut(e, "FlowData", "findDeadSpot");
122                 exit(1);
123         }
124 }
125
126 //**********************************************************************************************************************
127 //TATGCT
128 //1 0 0 0 0 1
129 //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
130 //Becomes TNT
131 void FlowData::translateFlow(){
132         try{
133         sequence = "";
134         set<char> charInMiddle;
135         int oldspot = -1;
136         bool updateOld = false;
137         
138         for(int i=0;i<endFlow;i++){
139                         int intensity = (int)(flowData[i] + 0.5);
140                         char base = baseFlow[i % baseFlow.length()];
141             
142             if (intensity == 0) { //are we in the middle
143                 if (oldspot != -1) { charInMiddle.insert(base); }
144             }else if (intensity >= 1) {
145                 if (oldspot == -1) { updateOld = true;  }
146                 else {  //check for bases inbetween two 1's
147                     if (charInMiddle.count(base) != 0) { //we want to covert to an N
148                         sequence = sequence.substr(0, oldspot+1);
149                         sequence += 'N';
150                     }
151                     updateOld = true;
152                     charInMiddle.clear();
153                 }
154             }
155                         
156                         for(int j=0;j<intensity;j++){
157                                 sequence += base;
158                         }
159             
160             if (updateOld) { oldspot = sequence.length()-1;  updateOld = false; }
161                 }
162         
163                 if(sequence.size() > 4){
164                         sequence = sequence.substr(4);
165                 }
166                 else{
167                         sequence = "NNNN";
168                 }
169     }
170         catch(exception& e) {
171                 m->errorOut(e, "FlowData", "translateFlow");
172                 exit(1);
173         }
174 }
175
176 //**********************************************************************************************************************
177
178 void FlowData::capFlows(int mF){
179         
180         try{
181                 
182                 maxFlows = mF;
183                 if(endFlow > maxFlows){ endFlow = maxFlows;     }       
184         translateFlow();
185                 
186         }
187         catch(exception& e) {
188                 m->errorOut(e, "FlowData", "capFlows");
189                 exit(1);
190         }
191 }
192
193 //**********************************************************************************************************************
194
195 bool FlowData::hasMinFlows(int minFlows){
196         
197         try{
198                 bool pastMin = 0;
199                 if(endFlow >= minFlows){        pastMin = 1;    }
200
201                 return pastMin;
202         }
203         catch(exception& e) {
204                 m->errorOut(e, "FlowData", "hasMinFlows");
205                 exit(1);
206         }
207 }
208
209 //**********************************************************************************************************************
210
211 Sequence FlowData::getSequence(){
212         
213         try{
214                 return Sequence(seqName, sequence);
215         }
216         catch(exception& e) {
217                 m->errorOut(e, "FlowData", "getSequence");
218                 exit(1);
219         }
220 }
221
222 //**********************************************************************************************************************
223
224 void FlowData::printFlows(ofstream& outFlowFile){
225         try{
226 //      outFlowFile << '>' << seqName << locationString << " length=" << seqLength << " numflows=" << maxFlows << endl;
227                 outFlowFile << seqName << ' ' << endFlow << ' ' << setprecision(2);
228
229                 for(int i=0;i<maxFlows;i++){
230                         outFlowFile << flowData[i] << ' ';
231                 }
232                 outFlowFile << endl;
233         }
234         catch(exception& e) {
235                 m->errorOut(e, "FlowData", "printFlows");
236                 exit(1);
237         }
238 }
239
240 //**********************************************************************************************************************
241
242 void FlowData::printFlows(ofstream& outFlowFile, string scrapCode){
243         try{
244                 outFlowFile << seqName << '|' << scrapCode << ' ' << endFlow << ' ' << setprecision(2);
245                 
246                 for(int i=0;i<numFlows;i++){
247                         outFlowFile << flowData[i] << ' ';
248                 }
249                 outFlowFile << endl;
250         }
251         catch(exception& e) {
252                 m->errorOut(e, "FlowData", "printFlows");
253                 exit(1);
254         }
255 }
256
257 //**********************************************************************************************************************
258
259 string FlowData::getName(){
260         
261         try{
262                 return seqName;
263         }
264         catch(exception& e) {
265                 m->errorOut(e, "FlowData", "getName");
266                 exit(1);
267         }
268 }
269
270 //**********************************************************************************************************************