Changeset 506


Ignore:
Timestamp:
01/19/12 17:21:46 (16 months ago)
Author:
tadamusiak
Message:

added xrefs to annotations returned from ols

Location:
trunk/ontoCAT
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/ontoCAT/dist/release_notes.txt

    r504 r506  
    44Version 0.9.9.2 / / / 
    55        Added <userIds> <obsoleteParent> fields to Bioportal's OntologyBean 
    6         FIX for the inability of using encoded slashes in the URL proper when querying Bioportal.  
     6        FIX for the inability of using encoded slashes in the URL proper when querying Bioportal 
    77        Now passing them as string parameters, q=? pattern 
     8        Added xrefs to annotations returned from OLS 
    89 
    910Version 0.9.9.1 30/9/2011 
  • trunk/ontoCAT/src/uk/ac/ebi/ontocat/ols/OlsOntologyService.java

    r489 r506  
    3333import java.util.Set; 
    3434import java.util.TreeMap; 
     35import java.util.regex.Matcher; 
     36import java.util.regex.Pattern; 
    3537 
    3638import javax.xml.rpc.ServiceException; 
     
    7072@SuppressWarnings("unchecked") 
    7173public class OlsOntologyService extends AbstractOntologyService implements 
    72 OntologyService { 
     74                OntologyService { 
    7375        Logger logger = Logger.getLogger(OlsOntologyService.class); 
    7476        // Webservice API 
     
    8890                // Try to make connections to the database and the webservice 
    8991                try { 
    90                         this.locator = new QueryServiceLocator(); 
    91                         this.qs = locator.getOntologyQuery(); 
     92                        locator = new QueryServiceLocator(); 
     93                        qs = locator.getOntologyQuery(); 
    9294                } catch (ServiceException e) { 
    9395                        throw new OntologyServiceException(e); 
     
    142144                                                if (ops.contains(SearchOptions.EXACT) 
    143145                                                                && dh.getAnnotationStringValue() 
    144                                                                 .equalsIgnoreCase(query)) { 
     146                                                                                .equalsIgnoreCase(query)) { 
    145147                                                        continue; 
    146148                                                } 
     
    155157                                String label = terms.get(key); 
    156158                                URI uri = URI 
    157                                 .create("http://www.ebi.ac.uk/ontology-lookup/?termId=" + key); 
     159                                                .create("http://www.ebi.ac.uk/ontology-lookup/?termId=" 
     160                                                                + key); 
    158161                                result.add(new OntologyTerm(ontAccession, key, label, uri)); 
    159162                        } 
     
    166169        @Override 
    167170        public List<OntologyTerm> searchAll(String query, SearchOptions... options) 
    168         throws OntologyServiceException { 
     171                        throws OntologyServiceException { 
    169172                List<SearchOptions> ops = new ArrayList<SearchOptions>( 
    170173                                Arrays.asList(options)); 
     
    185188                                String label = entry.getValue().split(":")[1]; 
    186189                                URI uri = URI 
    187                                 .create("http://www.ebi.ac.uk/ontology-lookup/?termId=" + termAccession); 
     190                                                .create("http://www.ebi.ac.uk/ontology-lookup/?termId=" 
     191                                                                + termAccession); 
    188192                                // filter out non-exact terms if necessary 
    189193                                if (ops.contains(SearchOptions.EXACT) 
     
    205209        @Override 
    206210        public Ontology getOntology(String ontologyAccession) 
    207         throws OntologyServiceException { 
     211                        throws OntologyServiceException { 
    208212                Ontology o = new Ontology(ontologyAccession); 
    209213                try { 
    210214                        String label = (String) qs.getOntologyNames() 
    211                         .get(ontologyAccession); 
     215                                        .get(ontologyAccession); 
    212216                        if (label == null) { 
    213217                                return null; 
     
    225229        @Override 
    226230        public OntologyTerm getTerm(String ontologyAccession, String termAccession) 
    227         throws OntologyServiceException { 
     231                        throws OntologyServiceException { 
    228232                String label = null; 
    229233                try { 
     
    235239                        throw new OntologyServiceException(e); 
    236240                } 
    237                 URI uri = URI 
    238                                 .create("http://www.ebi.ac.uk/ontology-lookup/?termId=" + termAccession); 
     241                URI uri = URI.create("http://www.ebi.ac.uk/ontology-lookup/?termId=" 
     242                                + termAccession); 
    239243                return new OntologyTerm(ontologyAccession, termAccession, label, uri); 
    240244        } 
     
    242246        @Override 
    243247        public OntologyTerm getTerm(String termAccession) 
    244         throws OntologyServiceException { 
     248                        throws OntologyServiceException { 
    245249 
    246250                return getTerm(getOntologyAccFromTerm(termAccession), termAccession); 
     
    260264        @Override 
    261265        public List<OntologyTerm> getRootTerms(String ontologyAccession) 
    262         throws OntologyServiceException { 
     266                        throws OntologyServiceException { 
    263267                // Make sure the ontology exists 
    264268                getOntology(ontologyAccession); 
     
    276280                // Make sure the term exists 
    277281                getTerm(ontologyAccession, termAccession); 
    278                 String key = ontologyAccession + ":" + termAccession; 
    279                 if (!annotationCache.containsKey(key)) { 
     282                String termKey = ontologyAccession + ":" + termAccession; 
     283                if (!annotationCache.containsKey(termKey)) { 
    280284                        try { 
    281                                 Map result = qs.getTermMetadata(termAccession, 
     285                                Map<String, List<String>> result = new HashMap<String, List<String>>(); 
     286                                Map<String, Object> annots = qs.getTermMetadata(termAccession, 
    282287                                                ontologyAccession); 
    283                                 // clean out the String from String[] 
    284                                 for (Object metadataKey : result.keySet()) { 
    285                                         // logger.debug("getting annotation "+metadataKey); 
    286                                         if (result.get(metadataKey) instanceof String) { 
    287                                                 result.put(metadataKey, Arrays 
    288                                                                 .asList(new String[] { (String) result 
    289                                                                                 .get(metadataKey) })); 
    290                                         } else if (result.get(metadataKey) instanceof String[]) { 
    291                                                 result.put(metadataKey, Arrays.asList((String[]) result 
    292                                                                 .get(metadataKey))); 
     288                                // dump xrefs into the annotation list 
     289                                annots.putAll(qs.getTermXrefs(termAccession, ontologyAccession)); 
     290 
     291                                for (String annotsKey : annots.keySet()) { 
     292                                        Object values = annots.get(annotsKey); 
     293                                        String cleanedKey = cleanupAnnotsKey(annotsKey); 
     294 
     295                                        List<String> temp = result.get(cleanedKey); 
     296                                        if (temp == null) { 
     297                                                temp = new ArrayList<String>(); 
     298                                        } 
     299 
     300                                        // convert String[] to List<String> if necessary 
     301                                        if (values instanceof String) { 
     302                                                temp.add((String) values); 
     303                                        } else if (values instanceof String[]) { 
     304                                                temp.addAll(Arrays.asList((String[]) values)); 
    293305                                        } else { 
    294306                                                throw new OntologyServiceException("annotation error: " 
    295                                                                 + metadataKey); 
     307                                                                + annotsKey); 
    296308                                        } 
     309                                        result.put(cleanedKey, temp); 
    297310                                } 
    298                                 annotationCache.put(key, result); 
     311                                annotationCache.put(termKey, result); 
    299312                        } catch (RemoteException e) { 
    300313                                throw new OntologyServiceException(e); 
    301314                        } 
    302315                } 
    303                 return annotationCache.get(key); 
     316                return annotationCache.get(termKey); 
     317        } 
     318 
     319        /** 
     320         * Removes the unnecessary index from OLS annotation key, e.g., 
     321         * exact_synonym_4 becomes exact_synonym 
     322         */ 
     323        private String cleanupAnnotsKey(String indexedKey) { 
     324                Pattern p = Pattern.compile("(.*)_\\d+"); 
     325                Matcher m = p.matcher(indexedKey); 
     326                if (m.find()) { 
     327                        return m.group(1); 
     328                } else { 
     329                        return indexedKey; 
     330                } 
    304331        } 
    305332 
     
    308335                        String termAccession) throws OntologyServiceException { 
    309336                List<String> result = getAnnotations(ontologyAccession, termAccession) 
    310                 .get("exact_synonym"); 
     337                                .get("exact_synonym"); 
    311338                if (result == null) { 
    312339                        return Collections.EMPTY_LIST; 
     
    328355        // helper methods 
    329356        protected List<OntologyTerm> fetchFullTerms(Map<String, String> terms) 
    330         throws OntologyServiceException { 
     357                        throws OntologyServiceException { 
    331358                List<OntologyTerm> result = new ArrayList<OntologyTerm>(); 
    332359                for (String termAccession : terms.keySet()) { 
     
    341368                        String termAccession) throws OntologyServiceException { 
    342369                List<String> result = getAnnotations(ontologyAccession, termAccession) 
    343                 .get("definition"); 
     370                                .get("definition"); 
    344371                if (result == null) { 
    345372                        return Collections.EMPTY_LIST; 
     
    397424                                throw new OntologyServiceException( 
    398425                                                "findSearchTermPath(): TOO MANY ITERATIONS (" 
    399                                                 + iteration + "x)"); 
     426                                                                + iteration + "x)"); 
    400427                        } 
    401428                } 
Note: See TracChangeset for help on using the changeset viewer.