public static void main(String[] args) throws Exception { URIFactoryMemory factory = URIFactoryMemory.getSingleton(); G graph = new GraphMemory(factory.getURI("http://mesh")); GraphLoader_MESH_XML loader = new GraphLoader_MESH_XML(); loader.populate(new GDataConf(GFormat.MESH_XML, "/data/mesh/desc2013.xml"), graph); URI dna_barcoding = factory.getURI("http://D058893"); System.out.println(graph); System.out.println(graph.containsVertex(dna_barcoding)); }
/** * Apply the given action to the graph. * * @param action the action to perform * @param g the graph on which the action must be performed * @throws SLIB_Ex_Critic */ public static void applyAction(GAction action, G g) throws SLIB_Ex_Critic { applyAction(URIFactoryMemory.getSingleton(), action, g); }
/** * MeSH Loader. This class permits to load the MeSH trees into a Directed Acyclic Graph * http://www.nlm.nih.gov/mesh/trees.html * * <p>The loader was designed for the 2013 XML version of the MeSH, coherency with prior or older * version hasn't been tested. * * @author Sébastien Harispe <*****@*****.**> */ public class GraphLoader_MESH_XML implements GraphLoader { Logger logger = LoggerFactory.getLogger(this.getClass()); Map<String, MeshConcept> idToConcepts = new HashMap<String, MeshConcept>(); Set<MeshConcept> concepts = new HashSet<MeshConcept>(); G graph; URIFactory factory = URIFactoryMemory.getSingleton(); int conceptIgnored = 0; /** */ public static final String ARG_PREFIX = "prefix"; String default_namespace; /** * Return parent ID i.e. giving C10.228.140.300.275.500 will return C10.228.140.300.275 * * @return a String associated to the parent ID. */ private String getParentId(String id) { String[] data = id.split("\\."); String idParent = null; for (int i = data.length - 2; i >= 0; i--) { if (idParent == null) { idParent = data[i]; } else { idParent = data[i] + "." + idParent; } } return idParent; } void addConcept(MeshConcept concept) { if (!concept.treeNumberList.isEmpty()) { for (String s : concept.treeNumberList) { idToConcepts.put(s, concept); } concepts.add(concept); } else { logger.info( "Warning: no tree number associated to " + concept.descriptorUI + " concept ignored..."); conceptIgnored++; } } @Override public void populate(GDataConf conf, G g) throws SLIB_Exception { this.graph = g; default_namespace = (String) conf.getParameter(ARG_PREFIX); if (default_namespace == null) { default_namespace = graph.getURI().getNamespace(); } try { logger.info("-------------------------------------"); logger.info("Loading Mesh XML"); logger.info("-------------------------------------"); idToConcepts = new HashMap<String, MeshConcept>(); SAXParserFactory parserfactory = SAXParserFactory.newInstance(); SAXParser saxParser; saxParser = parserfactory.newSAXParser(); saxParser.parse(conf.getLoc(), new MeshXMLHandler(this)); logger.info( "Number of descriptor loaded " + concepts.size() + " (ignored " + conceptIgnored + ")"); logger.info("Loading relationships "); // Create universal root if required URI universalRoot = OWL.THING; if (!graph.containsVertex(universalRoot)) { graph.addV(universalRoot); } // create relationships and roots of each tree for (Entry<String, MeshConcept> e : idToConcepts.entrySet()) { MeshConcept c = e.getValue(); URI vConcept = getOrCreateVertex(c.descriptorUI); for (String treeNumber : c.treeNumberList) { String parentId = getParentId(treeNumber); if (parentId != null) { MeshConcept parent = idToConcepts.get(parentId); if (parent == null) { throw new SLIB_Ex_Critic( "Cannot locate parent identified by TreeNumber " + treeNumber + "\nError occured processing\n" + c); } else { // System.out.println("\t" + parentId + "\t" + parent.descriptorUI); URI vParent = getOrCreateVertex(parent.descriptorUI); E edge = new Edge(vConcept, RDFS.SUBCLASSOF, vParent); g.addE(edge); } } else { /* Those vertices are the inner roots of each trees, * i.e. Psychiatry and Psychology [F] tree has for inner roots: * - Behavior and Behavior Mechanisms [F01] * - Psychological Phenomena and Processes [F02] * - Mental Disorders [F03] * - Behavioral Disciplines and Activities [F04] * A vertex has already been created for each inner root (e.g. F01, F02, F03, F04) * , we therefore create a vertex for the tree root (e.g. F). * Finally all the tree roots are rooted by a global root which do not * correspond to a concept specified into the mesh. * * More information about MeSH trees at http://www.nlm.nih.gov/mesh/trees.html */ // we link the tree inner root to the root tree char localNameTreeRoot = treeNumber.charAt(0); // id of the tree root URI rootTree = getOrCreateVertex(localNameTreeRoot + ""); // e.g. F E treeInnerRootToTreeRoot = new Edge(vConcept, RDFS.SUBCLASSOF, rootTree); g.addE(treeInnerRootToTreeRoot); // logger.debug("Creating Edge : " + treeInnerRootToTreeRoot); // we link the tree root to the universal root E treeRootToUniversalRoot = new Edge(rootTree, RDFS.SUBCLASSOF, universalRoot); g.addE(treeRootToUniversalRoot); // logger.debug("Creating Edge : " + treeRootToUniversalRoot); } } } } catch (IOException ex) { throw new SLIB_Ex_Critic(ex.getMessage()); } catch (ParserConfigurationException ex) { throw new SLIB_Ex_Critic(ex.getMessage()); } catch (SAXException ex) { throw new SLIB_Ex_Critic(ex.getMessage()); } catch (SLIB_Ex_Critic ex) { throw new SLIB_Ex_Critic(ex.getMessage()); } logger.info("MESH loader - process performed"); logger.info("-------------------------------------"); } private URI getOrCreateVertex(String descriptorUI) { String uriConceptAsString = default_namespace + descriptorUI; URI uriConcept = factory.getURI(uriConceptAsString); if (!graph.containsVertex(uriConcept)) { graph.addV(uriConcept); } return uriConcept; } public static void main(String[] args) throws Exception { URIFactoryMemory factory = URIFactoryMemory.getSingleton(); G graph = new GraphMemory(factory.getURI("http://mesh")); GraphLoader_MESH_XML loader = new GraphLoader_MESH_XML(); loader.populate(new GDataConf(GFormat.MESH_XML, "/data/mesh/desc2013.xml"), graph); URI dna_barcoding = factory.getURI("http://D058893"); System.out.println(graph); System.out.println(graph.containsVertex(dna_barcoding)); } }