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