void CVMappingFile::load(const String & filename, CVMappings & cv_mappings, bool strip_namespaces) { //File name for error messages in XMLHandler file_ = filename; strip_namespaces_ = strip_namespaces; parse_(filename, this); cv_mappings.setCVReferences(cv_references_); cv_mappings.setMappingRules(rules_); cv_references_.clear(); rules_.clear(); return; }
TEST_EQUAL(CVMappings(cvm).getMappingRules() == rules, true) CVReference ref1, ref2; ref1.setIdentifier("Ref1"); ref2.setIdentifier("Ref2"); vector<CVReference> refs; refs.push_back(ref1); refs.push_back(ref2); cvm.setCVReferences(refs); TEST_EQUAL(CVMappings(cvm).getCVReferences() == refs, true) } END_SECTION START_SECTION((CVMappings& operator=(const CVMappings &rhs))) { CVMappings cvm, cvm_copy; CVMappingRule r1, r2; vector<CVMappingRule> rules; rules.push_back(r1); rules.push_back(r2); cvm.setMappingRules(rules); cvm_copy = cvm; TEST_EQUAL(cvm_copy.getMappingRules() == rules, true) CVReference ref1, ref2; ref1.setIdentifier("Ref1"); ref2.setIdentifier("Ref2"); vector<CVReference> refs; refs.push_back(ref1); refs.push_back(ref2); cvm.setCVReferences(refs);
START_SECTION(CVMappingFile()) { ptr = new CVMappingFile(); TEST_NOT_EQUAL(ptr, nullPointer) } END_SECTION START_SECTION(virtual ~CVMappingFile()) { delete ptr; } END_SECTION START_SECTION((void load(const String &filename, CVMappings &cv_mappings, bool strip_namespaces=false))) { CVMappings mappings; CVMappingFile().load(OPENMS_GET_TEST_DATA_PATH("cv_mapping_test_file.xml"), mappings); TEST_EQUAL(mappings.getMappingRules().size(), 9) vector<CVMappingRule> rules = mappings.getMappingRules(); TEST_STRING_EQUAL(rules[0].getIdentifier(), "0") TEST_STRING_EQUAL(rules[1].getIdentifier(), "1") TEST_STRING_EQUAL(rules[2].getIdentifier(), "2") TEST_STRING_EQUAL(rules[3].getIdentifier(), "3") TEST_STRING_EQUAL(rules[4].getIdentifier(), "4") TEST_STRING_EQUAL(rules[5].getIdentifier(), "5") TEST_STRING_EQUAL(rules[6].getIdentifier(), "6") TEST_STRING_EQUAL(rules[7].getIdentifier(), "7") TEST_STRING_EQUAL(rules[8].getIdentifier(), "8")
ExitCodes main_(int, const char**) { StringList cv_files = getStringList_("cv_files"); StringList cv_names = getStringList_("cv_names"); if (cv_files.size() != cv_names.size()) { cerr << "Error: You have to specify an identifier for each CV file. Aborting!" << endl; return ILLEGAL_PARAMETERS; } // load cv terms ControlledVocabulary cv; for (Size i = 0; i < cv_files.size(); ++i) { cv.loadFromOBO(cv_names[i], cv_files[i]); } Map<String, ControlledVocabulary::CVTerm> terms = cv.getTerms(); // load mappings from mapping file String mapping_file = getStringOption_("mapping_file"); CVMappings mappings; CVMappingFile().load(mapping_file, mappings); //store HTML version of mapping and CV if (getStringOption_("html") != "") { TextFile file; file.addLine("<HTML>"); file.addLine(" <HEAD>"); file.addLine(" <TITLE>CV mapping file</TITLE>"); file.addLine(" <SCRIPT language=javascript type='text/javascript'>"); file.addLine(" function toggleDiv(layer_ref,force_state) "); file.addLine(" {"); file.addLine(" if (document.getElementById(layer_ref).style.display=='none' || force_state=='true')"); file.addLine(" {"); file.addLine(" document.getElementById(layer_ref).style.display = 'block';"); file.addLine(" }"); file.addLine(" else if (document.getElementById(layer_ref).style.display=='block' || force_state=='false')"); file.addLine(" {"); file.addLine(" document.getElementById(layer_ref).style.display = 'none';"); file.addLine(" }"); file.addLine(" }"); file.addLine(" </SCRIPT>"); file.addLine(" </HEAD>"); file.addLine(" <BODY>"); //count the number of terms and add button to expend/collaps all terms Int term_count = 0; for (vector<CVMappingRule>::const_iterator it = mappings.getMappingRules().begin(); it != mappings.getMappingRules().end(); ++it) { for (vector<CVMappingTerm>::const_iterator tit = it->getCVTerms().begin(); tit != it->getCVTerms().end(); ++tit) { ++term_count; } } String expand_all = " <a href=\"javascript:toggleDiv('div0','true')"; String collapse_all = " <a href=\"javascript:toggleDiv('div0','false')"; for (Int i = 1; i < term_count; ++i) { expand_all += String(";toggleDiv('div") + i + "','true')"; collapse_all += String(";toggleDiv('div") + i + "','false')"; } file.addLine(expand_all + "\">Expand all</a><BR>"); file.addLine(collapse_all + "\">Collapse all</a>"); file.addLine(" <TABLE width=100% border=0>"); term_count = -1; for (vector<CVMappingRule>::const_iterator it = mappings.getMappingRules().begin(); it != mappings.getMappingRules().end(); ++it) { //create rule line file.addLine(" <TR><TD colspan=\"2\"><HR></TD></TR>"); file.addLine(String(" <TR><TD>Identifier:</TD><TD><B>") + it->getIdentifier() + "</B></TD></TR>"); file.addLine(String(" <TR><TD>Element:</TD><TD><B>") + it->getElementPath() + "</B></TD></TR>"); if (it->getRequirementLevel() == CVMappingRule::MUST) { file.addLine(" <TR><TD>Requirement level:</TD><TD><FONT color=\"red\">MUST</FONT></TD></TR>"); } else if (it->getRequirementLevel() == CVMappingRule::SHOULD) { file.addLine(" <TR><TD>Requirement level:</TD><TD><FONT color=\"orange\">SHOULD</FONT></TD></TR>"); } else if (it->getRequirementLevel() == CVMappingRule::MAY) { file.addLine(" <TR><TD>Requirement level:</TD><TD><FONT color=\"green\">MAY</FONT></TD></TR>"); } if (it->getCombinationsLogic() == CVMappingRule::AND) { file.addLine(" <TR><TD>Combination logic:</TD><TD><FONT color=\"red\">AND</FONT></TD></TR>"); } else if (it->getCombinationsLogic() == CVMappingRule::XOR) { file.addLine(" <TR><TD>Combination logic:</TD><TD><FONT color=\"orange\">XOR</FONT></TD></TR>"); } else if (it->getCombinationsLogic() == CVMappingRule::OR) { file.addLine(" <TR><TD>Combination logic:</TD><TD><FONT color=\"green\">OR</FONT></TD></TR>"); } //create table with terms for (vector<CVMappingTerm>::const_iterator tit = it->getCVTerms().begin(); tit != it->getCVTerms().end(); ++tit) { //create term line String term_line = String(" <TR><TD valign=\"top\">Term:</TD><TD>"); if (tit->getAllowChildren()) { ++term_count; term_line += String("<a href=\"javascript:toggleDiv('div") + term_count + "','')\" style=\"text-decoration:none\" >+</a> "; } else { term_line += String(" "); } //add Term accession, name and description (as popup) if (cv.exists(tit->getAccession())) { const ControlledVocabulary::CVTerm& child_term = cv.getTerm(tit->getAccession()); String description = child_term.description; if (child_term.synonyms.size() != 0) { description += String(" -- Synonyms: '") + ListUtils::concatenate(child_term.synonyms, ", ") + "'"; } term_line += "<span title=\"" + description + "\">"; } term_line += tit->getAccession() + " ! " + tit->getTermName(); if (cv.exists(tit->getAccession())) { term_line += "</span>"; //check if term accession and term name correspond to the CV const ControlledVocabulary::CVTerm& main_term = cv.getTerm(tit->getAccession()); if (main_term.name != tit->getTermName()) { cerr << "Warning: Accession '" << tit->getAccession() << "' and name '" << tit->getTermName() << "' do not match. Name should be '" << main_term.name << "'." << endl; } } //tags StringList tags; if (!tit->getUseTerm()) { tags.push_back("children only"); } if (tit->getIsRepeatable()) { tags.push_back("repeatable"); } if (cv.exists(tit->getAccession())) { const ControlledVocabulary::CVTerm& term = cv.getTerm(tit->getAccession()); if (term.obsolete) { tags.push_back("<font color=darkred>obsolete</font>"); } if (term.xref_type != ControlledVocabulary::CVTerm::NONE) { tags.push_back("value-type=" + ControlledVocabulary::CVTerm::getXRefTypeName(term.xref_type)); } if (term.units.size() > 0) { StringList units; for (set<String>::const_iterator u_it = term.units.begin(); u_it != term.units.end(); ++u_it) { units.push_back(*u_it + "!" + cv.getTerm(*u_it).name); } tags.push_back(String("units=") + ListUtils::concatenate(units, ",")); } if (term.xref_binary.size() > 0) { StringList types; for (StringList::const_iterator u_it = term.xref_binary.begin(); u_it != term.xref_binary.end(); ++u_it) { types.push_back(*u_it + "!" + cv.getTerm(*u_it).name); } tags.push_back(String("binary-array-types=") + ListUtils::concatenate(types, ",")); } } if (tags.size() != 0) { term_line += String("<FONT color=\"grey\"> (") + ListUtils::concatenate(tags, ", ") + ")</FONT>"; } file.addLine(term_line); // check whether we need the whole tree, or just the term itself if (tit->getAllowChildren()) { file.addLine(String(" <div id=\"div") + term_count + "\" style=\"display: none\">"); if (cv.exists(tit->getAccession())) { writeTermTree_(tit->getAccession(), cv, file, 1); //BEGIN - THIS IS NEEDED FOR WRITING PARSERS ONLY /* set<String> allowed_terms; cv.getAllChildTerms(allowed_terms, tit->getAccession()); for (set<String>::const_iterator atit=allowed_terms.begin(); atit!=allowed_terms.end(); ++atit) { const ControlledVocabulary::CVTerm& child_term = cv.getTerm(*atit); String parser_string = String("os << \"<cvParam cvRef=\\\"MS\\\" accession=\\\"") + child_term.id + "\\\" name=\\\"" + child_term.name + "\\\""; for (Size i=0; i<child_term.unparsed.size(); ++i) { //TODO this does not work anymore. The type is now stored as a member if (child_term.unparsed[i].hasSubstring("value-type:xsd\\:int") || child_term.unparsed[i].hasSubstring("value-type:xsd\\:float") || child_term.unparsed[i].hasSubstring("value-type:xsd\\:string")) { parser_string += " value=\\\"\" << << \"\\\""; } } parser_string += "/>\\n\";<BR>"; file.push_back(parser_string); }*/ } else { file.addLine(" - Missing terms, CV not loaded..."); cerr << "Warning: no child terms for " << tit->getAccession() << " found!" << endl; } file.addLine(" </div>"); file.addLine(" </TD></TD></TR>"); } } } file.addLine(" </TABLE>"); file.addLine(" </BODY>"); file.addLine("</HTML>"); file.store(getStringOption_("html")); return EXECUTION_OK; } // iterator over all mapping rules and store the mentioned terms StringList ignore_namespaces = getStringList_("ignore_cv"); set<String> ignore_cv_list; for (StringList::const_iterator it = ignore_namespaces.begin(); it != ignore_namespaces.end(); ++it) { ignore_cv_list.insert(*it); } set<String> used_terms; for (vector<CVMappingRule>::const_iterator it = mappings.getMappingRules().begin(); it != mappings.getMappingRules().end(); ++it) { set<String> allowed_terms; // iterate over all allowed terms for (vector<CVMappingTerm>::const_iterator tit = it->getCVTerms().begin(); tit != it->getCVTerms().end(); ++tit) { // check whether the term itself it allowed, or only its children if (tit->getUseTerm()) { allowed_terms.insert(tit->getAccession()); } // check whether we need the whole tree, or just the term itself if (tit->getAllowChildren()) { // check whether we want to ignore this term if (!(tit->getAccession().has(':') && ignore_cv_list.find(tit->getAccession().prefix(':')) != ignore_cv_list.end())) { cv.getAllChildTerms(allowed_terms, tit->getAccession()); } // also add the term itself to the used_terms, because all the children are allowed used_terms.insert(tit->getAccession()); } } // print the allowed terms for the rule cout << "MappingRule: id=" << it->getIdentifier() << ", elementPath=" << it->getElementPath() << ", #terms=" << it->getCVTerms().size() << endl; for (set<String>::const_iterator ait = allowed_terms.begin(); ait != allowed_terms.end(); ++ait) { cout << *ait << " " << terms[*ait].name << endl; } used_terms.insert(allowed_terms.begin(), allowed_terms.end()); } // find unused terms, which CANNOT be used in the XML due to the mapping file set<String> unused_terms; for (Map<String, ControlledVocabulary::CVTerm>::ConstIterator it = terms.begin(); it != terms.end(); ++it) { if (used_terms.find(it->first) == used_terms.end()) { unused_terms.insert(it->first); } } cout << "\n\nCVTerms which are unused in the mapping file and therefore MUST NOT be used in an instance document" << endl; for (set<String>::const_iterator it = unused_terms.begin(); it != unused_terms.end(); ++it) { cout << *it << " " << terms[*it].name; // print also parent names for (set<String>::const_iterator pit = terms[*it].parents.begin(); pit != terms[*it].parents.end(); ++pit) { cout << " " << terms[*pit].id << " " << terms[*pit].name; } cout << endl; } return EXECUTION_OK; }