source: annoteimage/trunk/src/main/java/edu/washington/biostr/sig/annoteimage/AnnoteImageTreeNode.java

Last change on this file was 2493, checked in by xorgnz, 7 years ago

AnnoteImage

  • Now built with Maven
  • Auto-fixed extraneous imports
File size: 4.6 KB
Line 
1package edu.washington.biostr.sig.annoteimage;
2
3/*
4 * Jena RDF library
5 * Some code samples at
6 * http://seed.expedita.com.pt/tiki-index.php?page=OWL+to+JTree+using+Jena+API
7 * http://jena.sourceforge.net/tutorial/RDF_API/index.html
8 * http://www.unix.org.ua/orelly/java-ent/jfc/ch03_19.htm#jfcnut-ch-3-ex-tree
9 */
10import com.hp.hpl.jena.rdf.model.*;
11// hey, let's use a really common class name!
12import com.hp.hpl.jena.rdf.model.Statement;
13import javax.swing.tree.DefaultMutableTreeNode;
14
15/**
16 * AnnoteImageTreeNode - extends DefaultMutableTreeNode by
17 * customizing getChildCount() to add children.
18 * Unlike customizing JTree, which is all wonky, this method works.
19 * @author Joshua Daniel Franklin <joshuadf@u>
20 *
21 * Originally from FMATreeNode Created on Oct 4, 2004
22 * @author Todd Detwiler <det@u>
23 *
24 * Replaced Query Manager query to use template query syntax as of 2011 July
25 * @author brinkley <brinkley@u>
26 */
27public class AnnoteImageTreeNode extends DefaultMutableTreeNode {
28
29        private static String qhost = "http://purl.org/sig/";
30        private static String qprefix = "TemplateQuery?qid=95&args=";
31        /*
32        private static String qhost = "http://fma.biostr.washington.edu:8080/FMA_RadLex_Client/QueryHandler?query=";
33        private static String qprefix = "PREFIX+fma_radlex%3A+%3Chttp%3A%2F%2Fbioontology.org%2FFMA_RadLex%23%3E%0D%0APREFIX+rdfs%3A%3Chttp%3A%2F%2Fwww.w3.org%2F2000%2F01%2Frdf-schema%23%3E%0D%0APREFIX+rdf%3A%3Chttp%3A%2F%2Fwww.w3.org%2F1999%2F02%2F22-rdf-syntax-ns%23%3E%0D%0A%0D%0ACONSTRUCT%0D%0A%7B%0D%0A%3Fb+fma_radlex%3APreferred_Name+%3Fname+%0D%0A%7D%0D%0AWHERE%0D%0A%7B%3Fa+fma_radlex%3APreferred_Name+%22";
34        private static String qend = "%22%5E%5E%3Chttp%3A%2F%2Fwww.w3.org%2F2001%2FXMLSchema%23string%3E%0D%0A%7B%3Fa+fma_radlex%3AHas_regional_part+%3Fb%7D+UNION+%7B%3Fa+fma_radlex%3AHas_constitutional_part+%3Fb%7D%0D%0A%3Fb+fma_radlex%3APreferred_Name+%3Fname+.%0D%0A%7D";
35        */
36
37        /*
38         * Hard-coded FMA_RadLex query, see
39         * http://fma.biostr.washington.edu:8080/FMA_RadLex_Client/
40         *
41PREFIX fma_radlex: <http://bioontology.org/FMA_RadLex#>
42PREFIX rdfs:<http://www.w3.org/2000/01/rdf-schema#>
43PREFIX rdf:<http://www.w3.org/1999/02/22-rdf-syntax-ns#>
44
45CONSTRUCT
46{
47  ?b fma_radlex:Preferred_Name ?name
48}
49WHERE
50  {?a fma_radlex:Preferred_Name "abdomen"^^<http://www.w3.org/2001/XMLSchema#string>
51  {?a fma_radlex:Has_regional_part ?b} UNION {?a fma_radlex:Has_constitutional_part ?b}
52  ?b fma_radlex:Preferred_Name ?name .
53}
54         */
55
56        private boolean areChildrenDefined = false;
57
58        public AnnoteImageTreeNode(String name) {
59                super(name);
60        }
61
62        public int getChildCount() {
63                if (!areChildrenDefined)
64                        defineChildNodes();
65                return (super.getChildCount());
66        }
67
68        private void defineChildNodes() {
69                // You must set the flag before defining children if you
70                // use "add" for the new children. Otherwise you get an infinite
71                // recursive loop, since add results in a call to getChildCount.
72                // However, you could use "insert" in such a case.
73                areChildrenDefined = true;
74
75                // iterate over children and add nodes accordingly
76                StmtIterator myIterator = getChildren(this.toString());
77                while (myIterator.hasNext()) {
78                        // jena query call
79                        String currChild = getChildName(myIterator.nextStatement());
80                        // add it to the tree
81                        add(new AnnoteImageTreeNode(currChild));
82                        System.out.println("Adding [" + currChild + "](" + this + ")");
83                }
84        }
85
86        // Parse FMA_RadLex web service call with Jena RDF model
87        private StmtIterator getChildren(String searchTerm) {
88                long b4 = System.currentTimeMillis();
89                Model model = ModelFactory.createDefaultModel();
90                try {
91                        String term = java.net.URLEncoder.encode(searchTerm, "utf-8");
92                        String URI = qprefix + term ;
93                        //                      System.out.println("Checking [" + term + "]: \n" + URI);
94                        model.read(qhost + URI);
95                } catch (Exception e) {
96                        System.err.println("Problem checking [" + searchTerm + "]");
97                        e.printStackTrace();
98                }
99                StmtIterator iter = model.listStatements();
100
101                long after = System.currentTimeMillis() - b4;
102                // FMA SOAP call is about 2280; RadLex is <100
103                System.err.println("Term Query time: " + after + " ms");
104                return iter;
105        }
106
107        private String getChildName (Statement stmt) {
108                RDFNode rdfobject = stmt.getObject(); // get the object
109                // Convert ugly "My_Term^^http..." URI to "My Term"
110                String w3string = "\\^\\^http://www.w3.org/2001/XMLSchema#string";
111                String uri = rdfobject.toString();
112                String rdfName = uri.replaceAll(w3string, "");
113                // Remove @en from "Adding [Viscerocranium@en](Skull)"
114                String foo = rdfName.replaceAll("_"," ");
115                return foo.replaceAll("@en","");
116        }
117
118}
Note: See TracBrowser for help on using the repository browser.