]> git.donarmstrong.com Git - mothur.git/blob - flowdata.cpp
changes while testing
[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             m->checkName(name);
72         }else{ m->mothurOut("Error in reading your flowfile, at position " + toString(flowFile.tellg()) + ". Blank name."); m->mothurOutEndLine(); m->control_pressed = true;  }
73         
74                 return name;
75         }
76         catch(exception& e) {
77                 m->errorOut(e, "FlowData", "getSequenceName");
78                 exit(1);
79         }
80 }
81
82 //**********************************************************************************************************************
83
84 void FlowData::updateEndFlow(){
85         try{
86                 
87         if (baseFlow.length() > 4) { return; }
88         
89                 //int currLength = 0;
90                 float maxIntensity = (float) maxHomoP + 0.49;
91                 
92                 int deadSpot = 0;
93                         
94                 while(deadSpot < endFlow){
95                         int signal = 0;
96                         int noise = 0;
97                         
98                         for(int i=0;i<baseFlow.length();i++){
99                                 float intensity = flowData[i + deadSpot];
100                                 if(intensity > signalIntensity){
101                                         signal++;
102
103                                         if(intensity  < noiseIntensity || intensity > maxIntensity){
104                                                 noise++;
105                                         }
106                                 }
107                         }
108
109                         if(noise > 0 || signal == 0){
110                                 break;
111                         }
112                 
113                         deadSpot += baseFlow.length();
114                 }
115                 endFlow = deadSpot;
116
117         }
118         catch(exception& e) {
119                 m->errorOut(e, "FlowData", "findDeadSpot");
120                 exit(1);
121         }
122 }
123
124 //**********************************************************************************************************************
125 //TATGCT
126 //1 0 0 0 0 1
127 //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
128 //Becomes TNT
129 void FlowData::translateFlow(){
130         try{
131         sequence = "";
132         set<char> charInMiddle;
133         int oldspot = -1;
134         bool updateOld = false;
135         
136         for(int i=0;i<endFlow;i++){
137                         int intensity = (int)(flowData[i] + 0.5);
138                         char base = baseFlow[i % baseFlow.length()];
139             
140             if (intensity == 0) { //are we in the middle
141                 if (oldspot != -1) { charInMiddle.insert(base); }
142             }else if (intensity >= 1) {
143                 if (oldspot == -1) { updateOld = true;  }
144                 else {  //check for bases inbetween two 1's
145                     if (charInMiddle.count(base) != 0) { //we want to covert to an N
146                         sequence = sequence.substr(0, oldspot+1);
147                         sequence += 'N';
148                     }
149                     updateOld = true;
150                     charInMiddle.clear();
151                 }
152             }
153                         
154                         for(int j=0;j<intensity;j++){
155                                 sequence += base;
156                         }
157             
158             if (updateOld) { oldspot = sequence.length()-1;  updateOld = false; }
159                 }
160         
161                 if(sequence.size() > 4){
162                         sequence = sequence.substr(4);
163                 }
164                 else{
165                         sequence = "NNNN";
166                 }
167     }
168         catch(exception& e) {
169                 m->errorOut(e, "FlowData", "translateFlow");
170                 exit(1);
171         }
172 }
173
174 //**********************************************************************************************************************
175
176 void FlowData::capFlows(int mF){
177         
178         try{
179                 
180                 maxFlows = mF;
181                 if(endFlow > maxFlows){ endFlow = maxFlows;     }       
182         translateFlow();
183                 
184         }
185         catch(exception& e) {
186                 m->errorOut(e, "FlowData", "capFlows");
187                 exit(1);
188         }
189 }
190
191 //**********************************************************************************************************************
192
193 bool FlowData::hasMinFlows(int minFlows){
194         
195         try{
196                 bool pastMin = 0;
197                 if(endFlow >= minFlows){        pastMin = 1;    }
198
199                 return pastMin;
200         }
201         catch(exception& e) {
202                 m->errorOut(e, "FlowData", "hasMinFlows");
203                 exit(1);
204         }
205 }
206
207 //**********************************************************************************************************************
208
209 Sequence FlowData::getSequence(){
210         
211         try{
212                 return Sequence(seqName, sequence);
213         }
214         catch(exception& e) {
215                 m->errorOut(e, "FlowData", "getSequence");
216                 exit(1);
217         }
218 }
219
220 //**********************************************************************************************************************
221
222 void FlowData::printFlows(ofstream& outFlowFile){
223         try{
224 //      outFlowFile << '>' << seqName << locationString << " length=" << seqLength << " numflows=" << maxFlows << endl;
225                 outFlowFile << seqName << ' ' << endFlow << ' ' << setprecision(2);
226
227                 for(int i=0;i<maxFlows;i++){
228                         outFlowFile << flowData[i] << ' ';
229                 }
230                 outFlowFile << endl;
231         }
232         catch(exception& e) {
233                 m->errorOut(e, "FlowData", "printFlows");
234                 exit(1);
235         }
236 }
237
238 //**********************************************************************************************************************
239
240 void FlowData::printFlows(ofstream& outFlowFile, string scrapCode){
241         try{
242                 outFlowFile << seqName << '|' << scrapCode << ' ' << endFlow << ' ' << setprecision(2);
243                 
244                 for(int i=0;i<numFlows;i++){
245                         outFlowFile << flowData[i] << ' ';
246                 }
247                 outFlowFile << endl;
248         }
249         catch(exception& e) {
250                 m->errorOut(e, "FlowData", "printFlows");
251                 exit(1);
252         }
253 }
254
255 //**********************************************************************************************************************
256
257 string FlowData::getName(){
258         
259         try{
260                 return seqName;
261         }
262         catch(exception& e) {
263                 m->errorOut(e, "FlowData", "getName");
264                 exit(1);
265         }
266 }
267
268 //**********************************************************************************************************************