]> git.donarmstrong.com Git - mothur.git/blob - flowdata.cpp
Merge remote-tracking branch 'mothur/master'
[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                 //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<4;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 += 4;
114                 }
115                 endFlow = deadSpot;
116
117         }
118         catch(exception& e) {
119                 m->errorOut(e, "FlowData", "findDeadSpot");
120                 exit(1);
121         }
122 }
123
124 //**********************************************************************************************************************
125
126 void FlowData::translateFlow(){
127         
128         try{
129                 sequence = "";
130                 for(int i=0;i<endFlow;i++){
131                         int intensity = (int)(flowData[i] + 0.5);
132                         char base = baseFlow[i % 4];
133                         
134                         for(int j=0;j<intensity;j++){
135                                 sequence += base;
136                         }
137                 }
138
139                 if(sequence.size() > 4){
140                         sequence = sequence.substr(4);
141                 }
142                 else{
143                         sequence = "NNNN";
144                 }
145         }
146         catch(exception& e) {
147                 m->errorOut(e, "FlowData", "translateFlow");
148                 exit(1);
149         }
150 }
151
152 //**********************************************************************************************************************
153
154 void FlowData::capFlows(int mF){
155         
156         try{
157                 
158                 maxFlows = mF;
159                 if(endFlow > maxFlows){ endFlow = maxFlows;     }       
160         translateFlow();
161                 
162         }
163         catch(exception& e) {
164                 m->errorOut(e, "FlowData", "capFlows");
165                 exit(1);
166         }
167 }
168
169 //**********************************************************************************************************************
170
171 bool FlowData::hasMinFlows(int minFlows){
172         
173         try{
174                 bool pastMin = 0;
175                 if(endFlow >= minFlows){        pastMin = 1;    }
176
177                 return pastMin;
178         }
179         catch(exception& e) {
180                 m->errorOut(e, "FlowData", "hasMinFlows");
181                 exit(1);
182         }
183 }
184
185 //**********************************************************************************************************************
186
187 Sequence FlowData::getSequence(){
188         
189         try{
190                 return Sequence(seqName, sequence);
191         }
192         catch(exception& e) {
193                 m->errorOut(e, "FlowData", "getSequence");
194                 exit(1);
195         }
196 }
197
198 //**********************************************************************************************************************
199
200 void FlowData::printFlows(ofstream& outFlowFile){
201         try{
202 //      outFlowFile << '>' << seqName << locationString << " length=" << seqLength << " numflows=" << maxFlows << endl;
203                 outFlowFile << seqName << ' ' << endFlow << ' ' << setprecision(2);
204
205                 for(int i=0;i<maxFlows;i++){
206                         outFlowFile << flowData[i] << ' ';
207                 }
208                 outFlowFile << endl;
209         }
210         catch(exception& e) {
211                 m->errorOut(e, "FlowData", "printFlows");
212                 exit(1);
213         }
214 }
215
216 //**********************************************************************************************************************
217
218 void FlowData::printFlows(ofstream& outFlowFile, string scrapCode){
219         try{
220                 outFlowFile << seqName << '|' << scrapCode << ' ' << endFlow << ' ' << setprecision(2);
221                 
222                 for(int i=0;i<numFlows;i++){
223                         outFlowFile << flowData[i] << ' ';
224                 }
225                 outFlowFile << endl;
226         }
227         catch(exception& e) {
228                 m->errorOut(e, "FlowData", "printFlows");
229                 exit(1);
230         }
231 }
232
233 //**********************************************************************************************************************
234
235 string FlowData::getName(){
236         
237         try{
238                 return seqName;
239         }
240         catch(exception& e) {
241                 m->errorOut(e, "FlowData", "getName");
242                 exit(1);
243         }
244 }
245
246 //**********************************************************************************************************************