三、jena解析关于基因的go.owl文件(读取信息)

来源:互联网 发布:花生壳域名打不开 编辑:程序博客网 时间:2024/05/29 04:52

1.读取源文件

<owl:Class rdf:about="http://purl.obolibrary.org/obo/GO_0045019">        <rdfs:label rdf:datatype="http://www.w3.org/2001/XMLSchema#string">negative regulation of nitric oxide biosynthetic process</rdfs:label>        <rdfs:subClassOf rdf:resource="http://purl.obolibrary.org/obo/GO_0031327"/>        <rdfs:subClassOf rdf:resource="http://purl.obolibrary.org/obo/GO_0045428"/>        <rdfs:subClassOf rdf:resource="http://purl.obolibrary.org/obo/GO_1903427"/>        <rdfs:subClassOf rdf:resource="http://purl.obolibrary.org/obo/GO_1904406"/>        <obo:IAO_0000115 rdf:datatype="http://www.w3.org/2001/XMLSchema#string">Any process that stops, prevents, or reduces the frequency, rate or extent of the chemical reactions and pathways resulting in the formation of nitric oxide.</obo:IAO_0000115>        <oboInOwl:id rdf:datatype="http://www.w3.org/2001/XMLSchema#string">GO:0045019</oboInOwl:id>        <oboInOwl:hasOBONamespace rdf:datatype="http://www.w3.org/2001/XMLSchema#string">biological_process</oboInOwl:hasOBONamespace>        <oboInOwl:hasExactSynonym rdf:datatype="http://www.w3.org/2001/XMLSchema#string">down regulation of nitric oxide biosynthetic process</oboInOwl:hasExactSynonym>        <oboInOwl:hasExactSynonym rdf:datatype="http://www.w3.org/2001/XMLSchema#string">down-regulation of nitric oxide biosynthetic process</oboInOwl:hasExactSynonym>        <oboInOwl:hasExactSynonym rdf:datatype="http://www.w3.org/2001/XMLSchema#string">downregulation of nitric oxide biosynthetic process</oboInOwl:hasExactSynonym>        <oboInOwl:hasNarrowSynonym rdf:datatype="http://www.w3.org/2001/XMLSchema#string">inhibition of nitric oxide biosynthetic process</oboInOwl:hasNarrowSynonym>        <oboInOwl:hasExactSynonym rdf:datatype="http://www.w3.org/2001/XMLSchema#string">negative regulation of nitric oxide anabolism</oboInOwl:hasExactSynonym>        <oboInOwl:hasExactSynonym rdf:datatype="http://www.w3.org/2001/XMLSchema#string">negative regulation of nitric oxide biosynthesis</oboInOwl:hasExactSynonym>        <oboInOwl:hasExactSynonym rdf:datatype="http://www.w3.org/2001/XMLSchema#string">negative regulation of nitric oxide formation</oboInOwl:hasExactSynonym>        <oboInOwl:hasExactSynonym rdf:datatype="http://www.w3.org/2001/XMLSchema#string">negative regulation of nitric oxide synthesis</oboInOwl:hasExactSynonym>    </owl:Class>

2.实现程序

package test;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.util.Iterator;import org.apache.jena.ontology.AnnotationProperty;import org.apache.jena.ontology.OntClass;import org.apache.jena.ontology.OntModel;import org.apache.jena.ontology.OntModelSpec;import org.apache.jena.rdf.model.ModelFactory;import org.apache.jena.rdf.model.Property;import org.apache.jena.rdf.model.impl.LiteralImpl;public class Query {    public static void main(String args[]) throws FileNotFoundException{            OntModel ontModel = ModelFactory.createOntologyModel(OntModelSpec.OWL_MEM);        ontModel.read(new FileInputStream("f://myowl.owl"), "");        String OBO = "http://purl.obolibrary.org/obo/";        String OBOINOWL = "http://www.geneontology.org/formats/oboInOwl#";        String OWL = "http://www.w3.org/2002/07/owl#";        String RDF = "http://www.w3.org/1999/02/22-rdf-syntax-ns#";        for (Iterator<?> i = ontModel.listClasses(); i.hasNext();) {            OntClass c = (OntClass) i.next(); // 返回类型强制转换            if (!c.isAnon()) { // 如果不是匿名类,则打印类的名字                System.out.print("Class:");                System.out.println("localname:"+c.getLocalName());                //读取标签名                System.out.println("标签名:"+c.getLabel(""));                //迭代显示当前类的直接父类                for (Iterator<?> it = c.listSuperClasses(); it.hasNext();)                {                    OntClass sp = (OntClass) it.next();                    //父类不是匿名类                    if (!sp.isAnon()){                        String str = c.getModel().getGraph().getPrefixMapping().shortForm(c.getURI())+ "'s superClass is " ; // 获取URI                        String strSP = sp.getURI();                        if(strSP != null){                            try{ // 另一种简化处理URI的方法                                str = str + ":" + strSP.substring(strSP.indexOf('#')+1);                                System.out.println("Class" +str);                            }catch( Exception e ){}                        }                    }                } // super class ends                //一些propery                AnnotationProperty p1 = ontModel.createAnnotationProperty(OBO+"IAO_0000115");                Property p2 = ontModel.createProperty(OBOINOWL+"id");                Property p3 = ontModel.createProperty(OBOINOWL+"hasOBONamespace");                Property p4 = ontModel.createProperty(OBOINOWL+"hasExactSynonym");                Property p5 = ontModel.createProperty(OBOINOWL+"hasNarrowSynonym");                System.out.println("IAO_0000115:"+c.getPropertyValue(p1));                System.out.println("id:"+c.getPropertyValue(p2));                System.out.println("hasOBONamespace:"+c.getPropertyValue(p3));                System.out.println("hasNarrowSynonym:"+c.getPropertyValue(p5));                //遍历某个属性p3                for (Iterator<?> j = c.listPropertyValues(p4); j.hasNext();) {                    LiteralImpl p =  (LiteralImpl) j.next();                    System.out.println("hasExactSynonym:"+p.getValue().toString());                }                System.out.println();                ontModel.write(new FileOutputStream("f://go-minus1.owl"));            }        }    }}

3.显示结果
因为只读取这一段,所以父类没有在owl文件中,无法读取。如果是完整的owl文件,上面的代码则没有问题。

Class:localname:GO_0045019标签名:negative regulation of nitric oxide biosynthetic processIAO_0000115:Any process that stops, prevents, or reduces the frequency, rate or extent of the chemical reactions and pathways resulting in the formation of nitric oxide.id:GO:0045019hasOBONamespace:biological_processhasNarrowSynonym:inhibition of nitric oxide biosynthetic processhasExactSynonym:negative regulation of nitric oxide formationhasExactSynonym:negative regulation of nitric oxide biosynthesishasExactSynonym:down-regulation of nitric oxide biosynthetic processhasExactSynonym:negative regulation of nitric oxide anabolismhasExactSynonym:down regulation of nitric oxide biosynthetic processhasExactSynonym:downregulation of nitric oxide biosynthetic processhasExactSynonym:negative regulation of nitric oxide synthesis
原创粉丝点击