Read from a JSON file into a RibonucleotideDB
| 210 | |
| 211 | // Read from a JSON file into a RibonucleotideDB |
| 212 | void RibonucleotideDB::readFromJSON_(const std::string& path) |
| 213 | { |
| 214 | using json = nlohmann::json; |
| 215 | |
| 216 | String full_path = File::find(path); |
| 217 | |
| 218 | // the input file is Unicode encoded, so we need Qt to read it: |
| 219 | QFile file(full_path.toQString()); |
| 220 | if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) |
| 221 | { |
| 222 | throw Exception::FileNotReadable(__FILE__, __LINE__, OPENMS_PRETTY_FUNCTION, full_path); |
| 223 | } |
| 224 | |
| 225 | QTextStream source(&file); |
| 226 | source.setCodec("UTF-8"); |
| 227 | Size line_count = 0; |
| 228 | json mod_obj; |
| 229 | try |
| 230 | { |
| 231 | mod_obj = json::parse(String(source.readAll())); |
| 232 | } |
| 233 | catch (Exception::ParseError& e) |
| 234 | { |
| 235 | OPENMS_LOG_ERROR << "Error: Failed to parse Modomics JSON. Reason:\n" << e.getName() << " - " << e.what() << endl; |
| 236 | throw; |
| 237 | } |
| 238 | for (auto& element : mod_obj) |
| 239 | { |
| 240 | line_count++; |
| 241 | try |
| 242 | { |
| 243 | // Throw an exception if we are straight up missing necessary elements of the JSON |
| 244 | entryIsWellFormed_(element); |
| 245 | |
| 246 | ParsedEntry_ entry = parseEntry_(element); |
| 247 | |
| 248 | unique_ptr<Ribonucleotide> ribo = std::move(entry.ribo); |
| 249 | if (entry.isAmbiguous()) // Handle the ambiguity map |
| 250 | { |
| 251 | ambiguity_map_[ribo->getCode()] = make_pair(getRibonucleotide(entry.alternative_1), getRibonucleotide(entry.alternative_2)); |
| 252 | } |
| 253 | // there are some weird exotic mods in modomics that don't have codes. We ignore them |
| 254 | if (ribo->getCode() != "") |
| 255 | { |
| 256 | code_map_[ribo->getCode()] = ribonucleotides_.size(); |
| 257 | max_code_length_ = max(max_code_length_, ribo->getCode().size()); |
| 258 | ribonucleotides_.push_back(std::move(ribo)); |
| 259 | } |
| 260 | } |
| 261 | catch (Exception::BaseException& e) |
| 262 | { |
| 263 | OPENMS_LOG_ERROR << "Error: Failed to parse input element " << line_count << ". Reason:\n" << e.getName() << " - " << e.what() << "\nSkipping this line." << endl; |
| 264 | } |
| 265 | } |
| 266 | } |
| 267 | |
| 268 | // Read entries from a TSV file |
| 269 | void RibonucleotideDB::readFromFile_(const std::string& path) |
nothing calls this directly
no test coverage detected