Debian Codegraph @

tools/create_coverage_h5_file/src/create_coverage_h5_file.cpp

raw
1 #include <gatb/gatb_core.hpp>
2 #include <iostream>
3 #define STR_OUT_COVERAGE_FILE_NAME "-coverage_file"
4
5
6
7 /*** Takes a set of h5 input files, coma separated, and finishing with a comma ***/
8 /*** those input files contain only a cutoff group ***/
9 /*** Return 0 except in case of failure ***/
10 int glue_simple_h5_files(std::string input_h5_file_names, std::string output_h5_file_name){
11 try{
12 std::string delimiter = ",";
13
14 // PREPARE FOR WRITING
15 Storage* storage_out = StorageFactory(STORAGE_HDF5).create (output_h5_file_name, true, false);
16 LOCAL (storage_out);
17 Group& root_out = storage_out->root();
18 Collection<NativeInt64>& myIntegers_out = root_out.getCollection<NativeInt64> ("cutoffs");
19
20
21
22
23 // PARSE ALL INPUT H5 files, Dump their content in the output h5.
24 size_t pos = 0;
25 std::string token;
26 while ((pos = input_h5_file_names.find(delimiter)) != std::string::npos) {
27 token = input_h5_file_names.substr(0, pos);
28 cout<<"reading "<<token<<endl;
29 // READING
30 Storage* storage = StorageFactory(STORAGE_HDF5).load (token);
31 LOCAL (storage);
32 Group& root = storage->root();
33 Collection<NativeInt64>& myIntegers = root.getCollection<NativeInt64> ("cutoff");
34
35
36 // We create an iterator for our collection.
37 Iterator<NativeInt64>* iterInt = myIntegers.iterator();
38 LOCAL (iterInt);
39 // WRITING the content of the input h5. should have only on value
40 for (iterInt->first(); !iterInt->isDone(); iterInt->next()) {
41
42 int n=iterInt->item().toInt();
43 myIntegers_out.insert (n);
44 break; // only one value
45 }
46 myIntegers.flush();
47
48 // Get the next asked coverage valoue
49 input_h5_file_names.erase(0, pos + delimiter.length());
50 }
51 // no need to get the last value as entry finishes with a ',' from run_discoSnp++_ML.sh
52 myIntegers_out.flush();
53 }
54 catch (OptionFailure& e)
55 {
56 return e.displayErrors (std::cerr);
57 }
58 catch (Exception& e)
59 {
60 std::cerr << "EXCEPTION: " << e.getMessage() << std::endl;
61 }
62 return 0;
63 }
64
65 /*** Takes one h5 input file, all generated by dks ***/
66 /*** Dump its cuttoff value (either the one asked by the user or the one computed by dsk if the user asked for automatic value) in an output h5 file. ***/
67 /*** Return 0 except in case of failure ***/
68 int from_large_h5_file_to_only_cutoff_h5_file(std::string input_h5_file_name, std::string output_h5_file_name){
69
70 try
71 {
72
73 // PREPARE FOR WRITING
74 Storage* storage_out = StorageFactory(STORAGE_HDF5).create (output_h5_file_name, true, false);
75 LOCAL (storage_out);
76 Group& root_out = storage_out->root();
77 Collection<NativeInt64>& myIntegers_out = root_out.getCollection<NativeInt64> ("cutoff");
78
79
80
81
82 // PARSE THE INPUT H5 file. Dump its content in the output h5.
83
84
85 // READING
86 Storage* storage = StorageFactory(STORAGE_HDF5).load (input_h5_file_name);
87 LOCAL (storage);
88 Group& root = storage->root();
89
90 Group& configGroup = storage->getGroup("configuration");
91 stringstream ss; ss << configGroup.getProperty ("xml");
92 Properties props; props.readXML (ss);
93 int asked_ab_min = props.getInt("abundance_min");
94
95
96 if (asked_ab_min != -1) // if asked_ab_min != auto
97 {
98 myIntegers_out.insert (asked_ab_min);
99 }
100 else // recover the automatic value computed by dsk in the /histogram/cutoff field.
101 {
102 Group& histo = root.getGroup("histogram");
103 Collection<NativeInt64>& myIntegers = histo.getCollection<NativeInt64> ("cutoff");
104 // We create an iterator for our collection.
105 Iterator<NativeInt64>* iterInt = myIntegers.iterator();
106 LOCAL (iterInt);
107 // WRITING the content of the input h5.
108 for (iterInt->first(); !iterInt->isDone(); iterInt->next()) {
109 int n=iterInt->item().toInt();
110 myIntegers_out.insert (n);
111 break; // only one value
112 }
113 myIntegers.flush();
114 }
115 myIntegers_out.flush();
116
117
118 }
119 catch (OptionFailure& e)
120 {
121 return e.displayErrors (std::cerr);
122 }
123 catch (Exception& e)
124 {
125 std::cerr << "EXCEPTION: " << e.getMessage() << std::endl;
126 }
127 return 0;
128 }
129
130
131 /********************************************************************************/
132 /*** Deal with h5 files for multiple large version of discoSnp ***/
133 /*** Two main features:
134 If the input file(s) contain comas, then merge cutoff value of all input h5 files in an output h5 file
135 If the input does not contain coma, this is the raw h5 output from dsk. We need only to dump the cutoff value, either asked by user or computed by dsk in case of automatic computation of the cutoff
136 ***/
137 int main (int argc, char* argv[])
138 {
139 /** We create a command line parser. */
140 OptionsParser parser ("CoverageDump");
141 parser.push_back (new OptionOneParam (STR_URI_INPUT, " set of .h5 files from which one need to grab the coverage cutoff ", true));
142 parser.push_back (new OptionOneParam (STR_OUT_COVERAGE_FILE_NAME, " name of the created .h5 coverage file ", true));
143 IProperties* options = parser.parse (argc, argv);
144 std::string input_h5_file_names = options->getStr(STR_URI_INPUT);
145 std::string output_h5_file_name = options->getStr(STR_OUT_COVERAGE_FILE_NAME);
146
147 if (input_h5_file_names.find(",") != std::string::npos) {
148 // first case, need to merge h5 files:
149 return glue_simple_h5_files(input_h5_file_names, output_h5_file_name);
150 }
151 else{
152 return from_large_h5_file_to_only_cutoff_h5_file(input_h5_file_names, output_h5_file_name);
153 }
154
155 // try
156 // {
157 // IProperties* options = parser.parse (argc, argv);
158 //
159 // std::string input_h5_file_names = options->getStr(STR_URI_INPUT); // h5 files, separated by comma
160 // std::string delimiter = ",";
161 //
162 // // PREPARE FOR WRITING
163 // Storage* storage_out = StorageFactory(STORAGE_HDF5).create (options->getStr(STR_OUT_COVERAGE_FILE_NAME), true, false);
164 // LOCAL (storage_out);
165 // Group& root_out = storage_out->root();
166 // Collection<NativeInt64>& myIntegers_out = root_out.getCollection<NativeInt64> ("cutoffs");
167 //
168 //
169 //
170 //
171 // // PARSE ALL INPUT H5 files, Dump their content in the output h5.
172 // size_t pos = 0;
173 // std::string token;
174 // while ((pos = input_h5_file_names.find(delimiter)) != std::string::npos) {
175 // token = input_h5_file_names.substr(0, pos);
176 // cout<<"reading "<<token<<endl;
177 // // READING
178 // Storage* storage = StorageFactory(STORAGE_HDF5).load (token);
179 // LOCAL (storage);
180 // Group& root = storage->root();
181 //
182 // Group& configGroup = storage->getGroup("configuration");
183 // stringstream ss; ss << configGroup.getProperty ("xml");
184 // Properties props; props.readXML (ss);
185 // int asked_ab_min = props.getInt("abundance_min");
186 //
187 //
188 // if (asked_ab_min != -1) // if asked_ab_min != auto
189 // {
190 // myIntegers_out.insert (asked_ab_min);
191 // }
192 // else // recover the automatic value computed by dsk in the /histogram/cutoff field.
193 // {
194 // Group& histo = root.getGroup("histogram");
195 // Collection<NativeInt64>& myIntegers = histo.getCollection<NativeInt64> ("cutoff");
196 // // We create an iterator for our collection.
197 // Iterator<NativeInt64>* iterInt = myIntegers.iterator();
198 // LOCAL (iterInt);
199 // // WRITING the content of the input h5.
200 // for (iterInt->first(); !iterInt->isDone(); iterInt->next()) {
201 // int n=iterInt->item().toInt();
202 // myIntegers_out.insert (n);
203 // break; // only one value
204 // }
205 // myIntegers.flush();
206 // }
207 // // Get the next asked coverage valoue
208 // input_h5_file_names.erase(0, pos + delimiter.length());
209 // }
210 // // no need to get the last value as entry finishes with a ',' from run_discoSnp++_ML.sh
211 // myIntegers_out.flush();
212 //
213 //
214 // }
215 // catch (OptionFailure& e)
216 // {
217 // return e.displayErrors (std::cerr);
218 // }
219 // catch (Exception& e)
220 // {
221 // std::cerr << "EXCEPTION: " << e.getMessage() << std::endl;
222 // }
223 }