1 /*
2  *  fileoutput.cpp
3  *  Dotur
4  *
5  *  Created by Sarah Westcott on 11/18/08.
6  *  Copyright 2008 Schloss Lab UMASS Amherst. All rights reserved.
7  *
8  */
9 
10 #include "fileoutput.h"
11 
12 /***********************************************************************/
setLabelName(string label)13 void ThreeColumnFile::setLabelName(string label){
14 	try {
15 		if(!firstLabel)     { fileHeader += "\t" + label + "\tlci\thci";             }
16         else                { fileHeader = "numsampled\t" + label + "\tlci\thci";    }
17     }
18 	catch(exception& e) {
19 		m->errorOut(e, "ThreeColumnFile", "setLabelName");
20 		exit(1);
21 	}
22 }
23 /***********************************************************************/
updateOutput(int nSeqs,vector<double> data)24 void ThreeColumnFile::updateOutput(int nSeqs, vector<double> data){
25 	try {
26 
27         map<int, int>::iterator it = nseqsToRow.find(nSeqs);
28         if (it != nseqsToRow.end()) { //new column in row for next label
29             int resultsIndex = it->second;
30             results[resultsIndex].push_back(data[0]);
31             results[resultsIndex].push_back(data[1]);
32             results[resultsIndex].push_back(data[2]);
33         }else{
34             //new row
35             nseqsToRow[nSeqs] = results.size();
36             vector<double> theseResults;
37             theseResults.push_back(nSeqs);
38             theseResults.push_back(data[0]);
39             theseResults.push_back(data[1]);
40             theseResults.push_back(data[2]);
41             results.push_back(theseResults);
42         }
43 	}
44 	catch(exception& e) {
45 		m->errorOut(e, "ThreeColumnFile", "updateOutput");
46 		exit(1);
47 	}
48 }
49 /***********************************************************************/
setLabelName(string label,vector<string> tags)50 void ColumnFile::setLabelName(string label, vector<string> tags){
51 	try {
52 		if(firstLabel){ fileHeader = ""; }
53 
54         for(int i = 0; i < tags.size(); i++) { fileHeader += label + tags[i] + '\t'; }
55 	}
56 	catch(exception& e) {
57 		m->errorOut(e, "ColumnFile", "setLabelName");
58 		exit(1);
59 	}
60 }
61 /***********************************************************************/
updateOutput(vector<double> data)62 void ColumnFile::updateOutput(vector<double> data){
63 	try {
64         vector<double> theseResults;
65         for (size_t i = 0; i < data.size(); i++) { theseResults.push_back(data[i]);  }
66         results.push_back(theseResults);
67 	}
68 	catch(exception& e) {
69 		m->errorOut(e, "ColumnFile", "updateOutput");
70 		exit(1);
71 	}
72 }
73 /***********************************************************************/
printFile()74 void FileOutput::printFile(){
75 	try {
76         ofstream outFile;
77         util.openOutputFile(filename, outFile);
78 
79         outFile.setf(ios::fixed, ios::floatfield); outFile.setf(ios::showpoint);
80         cout.setf(ios::fixed, ios::floatfield); cout.setf(ios::showpoint);
81 
82         outFile << fileHeader << endl;
83         for (size_t i = 0; i < results.size(); i++) {
84             for (size_t j = 0; j < results[i].size(); j++) {
85                 outFile << setprecision(6) << results[i][j] << '\t';
86             }
87             outFile << endl;
88         }
89         outFile << endl;
90 
91         outFile.close();
92     }
93 	catch(exception& e) {
94 		m->errorOut(e, "FileOutput", "printFile");
95 		exit(1);
96 	}
97 }
98 /***********************************************************************/
setLabelName(string label)99 void SharedThreeColumnFile::setLabelName(string label){
100 	try {
101         if (groupLabel != "") { groupLabel = "_" + groupLabel; }
102 
103         if(!firstLabel)     { fileHeader += "\t" + label + groupLabel + "\tlci" + groupLabel + "\thci"+ groupLabel; }
104         else                { fileHeader = "numsampled\t" + label + groupLabel + "\tlci" + groupLabel + "\thci"+ groupLabel;  }
105 	}
106 	catch(exception& e) {
107 		m->errorOut(e, "SharedThreeColumnFile", "setLabelName");
108 		exit(1);
109 	}
110 }
111 /***********************************************************************/
updateOutput(int nSeqs,vector<double> data)112 void SharedThreeColumnFile::updateOutput(int nSeqs, vector<double> data){
113 	try {
114 
115         map<int, int>::iterator it = nseqsToRow.find(nSeqs);
116         if (it != nseqsToRow.end()) { //new column in row for next label
117             int resultsIndex = it->second;
118             results[resultsIndex].push_back(data[0]);
119             results[resultsIndex].push_back(data[1]);
120             results[resultsIndex].push_back(data[2]);
121         }else{ //new row
122             nseqsToRow[nSeqs] = results.size();
123             vector<double> theseResults;
124             theseResults.push_back(numGroup); numGroup++;
125             theseResults.push_back(data[0]);
126             theseResults.push_back(data[1]);
127             theseResults.push_back(data[2]);
128             results.push_back(theseResults);
129         }
130     }
131 	catch(exception& e) {
132 		m->errorOut(e, "SharedThreeColumnFile", "output");
133 		exit(1);
134 	}
135 }
136 /***********************************************************************/
setLabelName(string label)137 void OneColumnFile::setLabelName(string label){
138 	try {
139         if(!firstLabel)     { fileHeader += "\t" + label;            }
140         else                { fileHeader = "numsampled\t" + label;   }
141     }
142 	catch(exception& e) {
143 		m->errorOut(e, "OneColumnFile", "setLabelName");
144 		exit(1);
145 	}
146 }
147 /***********************************************************************/
updateOutput(int nSeqs,vector<double> data)148 void OneColumnFile::updateOutput(int nSeqs, vector<double> data){
149 	try {
150         map<int, int>::iterator it = nseqsToRow.find(nSeqs);
151         if (it != nseqsToRow.end()) { //new column in row for next label
152             int resultsIndex = it->second;
153             results[resultsIndex].push_back(data[0]);
154         }else{
155             //new row
156             nseqsToRow[nSeqs] = results.size();
157             vector<double> theseResults;
158             theseResults.push_back(nSeqs);
159             theseResults.push_back(data[0]);
160             results.push_back(theseResults);
161         }
162 	}
163 	catch(exception& e) {
164 		m->errorOut(e, "OneColumnFile", "updateOutput");
165 		exit(1);
166 	}
167 }
168 /***********************************************************************/
169 
setLabelName(string label)170 void SharedOneColumnFile::setLabelName(string label){
171 	try {
172         if(!firstLabel)     { fileHeader += "\t" + label;            }
173         else                { fileHeader = "sampled\t" + label;   }
174     }
175 	catch(exception& e) {
176 		m->errorOut(e, "SharedOneColumnFile", "setLabelName");
177 		exit(1);
178 	}
179 }
180 /***********************************************************************/
updateOutput(int nSeqs,vector<double> data)181 void SharedOneColumnFile::updateOutput(int nSeqs, vector<double> data){
182 	try {
183 
184         map<int, int>::iterator it = nseqsToRow.find(nSeqs);
185         if (it != nseqsToRow.end()) { //new column in row for next label
186             int resultsIndex = it->second;
187             for (int i = 0; i < data.size(); i++) { results[resultsIndex].push_back(data[i]); }
188         }else{
189             //new row
190             nseqsToRow[nSeqs] = results.size();
191             vector<double> theseResults;
192             theseResults.push_back(nSeqs);
193             for (int i = 0; i < data.size(); i++) { theseResults.push_back(data[i]); }
194             results.push_back(theseResults);
195         }
196     }
197 	catch(exception& e) {
198 		m->errorOut(e, "SharedOneColumnFile", "updateOutput");
199 		exit(1);
200 	}
201 }
202 /***********************************************************************/
203 
204 
205