]> git.donarmstrong.com Git - mothur.git/blob - flowdata.cpp
some changes to trim.flows
[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                 
46                 string lengthString;
47                 string flowString;
48                 
49                 flowFile >> seqName >> endFlow;         
50                 for(int i=0;i<numFlows;i++)     {       flowFile >> flowData[i];        }
51                 
52                 updateEndFlow();
53                 translateFlow();
54                 
55                 m->gobble(flowFile);
56                 if(flowFile){   return 1;       }
57                 else            {       return 0;       }
58         }
59         catch(exception& e) {
60                 m->errorOut(e, "FlowData", "getNext");
61                 exit(1);
62         }
63         
64 }
65
66 //**********************************************************************************************************************
67
68 void FlowData::updateEndFlow(){
69         try{
70                 
71                 //int currLength = 0;
72                 float maxIntensity = (float) maxHomoP + 0.49;
73                 
74                 int deadSpot = 0;
75                                 
76                 while(deadSpot < endFlow){
77                         int signal = 0;
78                         int noise = 0;
79                         
80                         for(int i=0;i<4;i++){
81                                 float intensity = flowData[i + deadSpot];
82                                 if(intensity > signalIntensity){
83                                         signal++;
84
85                                         if(intensity  < noiseIntensity || intensity > maxIntensity){
86                                                 noise++;
87                                         }
88                                 }
89                         }
90
91                         if(noise > 0 || signal == 0){
92                                 break;
93                         }
94                 
95                         deadSpot += 4;
96                 }
97                 endFlow = deadSpot;
98
99         }
100         catch(exception& e) {
101                 m->errorOut(e, "FlowData", "findDeadSpot");
102                 exit(1);
103         }
104 }
105
106 //**********************************************************************************************************************
107
108 void FlowData::translateFlow(){
109         
110         try{
111                 sequence = "";
112                 for(int i=0;i<endFlow;i++){
113                         int intensity = (int)(flowData[i] + 0.5);
114                         char base = baseFlow[i % 4];
115                         
116                         for(int j=0;j<intensity;j++){
117                                 sequence += base;
118                         }
119                 }
120
121                 if(sequence.size() > 4){
122                         sequence = sequence.substr(4);
123                 }
124                 else{
125                         sequence = "NNNN";
126                 }
127         }
128         catch(exception& e) {
129                 m->errorOut(e, "FlowData", "translateFlow");
130                 exit(1);
131         }
132 }
133
134 //**********************************************************************************************************************
135
136 void FlowData::capFlows(int mF){
137         
138         try{
139                 
140                 maxFlows = mF;
141                 if(endFlow > maxFlows){ endFlow = maxFlows;     }               
142                 
143         }
144         catch(exception& e) {
145                 m->errorOut(e, "FlowData", "capFlows");
146                 exit(1);
147         }
148 }
149
150 //**********************************************************************************************************************
151
152 bool FlowData::hasMinFlows(int minFlows){
153         
154         try{
155                 bool pastMin = 0;
156                 if(endFlow >= minFlows){        pastMin = 1;    }
157
158                 return pastMin;
159         }
160         catch(exception& e) {
161                 m->errorOut(e, "FlowData", "hasMinFlows");
162                 exit(1);
163         }
164 }
165
166 //**********************************************************************************************************************
167
168 Sequence FlowData::getSequence(){
169         
170         try{
171                 return Sequence(seqName, sequence);
172         }
173         catch(exception& e) {
174                 m->errorOut(e, "FlowData", "getSequence");
175                 exit(1);
176         }
177 }
178
179 //**********************************************************************************************************************
180
181 void FlowData::printFlows(ofstream& outFlowFile){
182         try{
183 //      outFlowFile << '>' << seqName << locationString << " length=" << seqLength << " numflows=" << maxFlows << endl;
184                 outFlowFile << seqName << ' ' << endFlow << ' ' << setprecision(2);
185
186                 for(int i=0;i<maxFlows;i++){
187                         outFlowFile << flowData[i] << ' ';
188                 }
189                 outFlowFile << endl;
190         }
191         catch(exception& e) {
192                 m->errorOut(e, "FlowData", "printFlows");
193                 exit(1);
194         }
195 }
196
197 //**********************************************************************************************************************
198
199 void FlowData::printFlows(ofstream& outFlowFile, string scrapCode){
200         try{
201                 outFlowFile << seqName << '|' << scrapCode << ' ' << endFlow << ' ' << setprecision(2);
202                 
203                 for(int i=0;i<numFlows;i++){
204                         outFlowFile << flowData[i] << ' ';
205                 }
206                 outFlowFile << endl;
207         }
208         catch(exception& e) {
209                 m->errorOut(e, "FlowData", "printFlows");
210                 exit(1);
211         }
212 }
213
214 //**********************************************************************************************************************
215
216 string FlowData::getName(){
217         
218         try{
219                 return seqName;
220         }
221         catch(exception& e) {
222                 m->errorOut(e, "FlowData", "getName");
223                 exit(1);
224         }
225 }
226
227 //**********************************************************************************************************************