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