2 * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3 * Copyright (C) $$Year-Rel$$ The Jalview Authors
5 * This file is part of Jalview.
7 * Jalview is free software: you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation, either version 3
10 * of the License, or (at your option) any later version.
12 * Jalview is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty
14 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15 * PURPOSE. See the GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with Jalview. If not, see <http://www.gnu.org/licenses/>.
19 * The Jalview Authors are detailed in the 'AUTHORS' file.
21 package jalview.io.gff;
23 import jalview.datamodel.ontology.OntologyBase;
25 import java.util.ArrayList;
26 import java.util.Collections;
27 import java.util.HashMap;
28 import java.util.List;
32 * An implementation of SequenceOntologyI that hard codes terms of interest.
34 * Use this in unit testing by calling SequenceOntology.setInstance(new
35 * SequenceOntologyLite()).
37 * May also become a stand-in for SequenceOntology in the applet if we want to
38 * avoid the additional jars needed for parsing the full SO.
43 public class SequenceOntologyLite extends OntologyBase
44 implements SequenceOntologyI
47 * initial selection of types of interest when processing Ensembl features
48 * NB unlike the full SequenceOntology we don't traverse indirect
49 * child-parent relationships here so e.g. need to list every sub-type
50 * (direct or indirect) that is of interest
53 private final String[][] TERMS = new String[][] {
59 { "ncRNA_gene", "gene" },
60 { "snRNA_gene", "gene" },
61 { "miRNA_gene", "gene" },
62 { "lincRNA_gene", "gene" },
63 { "rRNA_gene", "gene" },
66 * transcript sub-types:
68 { "transcript", "transcript" },
69 { "mature_transcript", "transcript" },
70 { "processed_transcript", "transcript" },
71 { "aberrant_processed_transcript", "transcript" },
72 { "ncRNA", "transcript" },
73 { "snRNA", "transcript" },
74 { "miRNA", "transcript" },
75 { "lincRNA", "transcript" },
76 { "rRNA", "transcript" },
77 { "mRNA", "transcript" },
78 // there are many more sub-types of ncRNA...
81 * sequence_variant sub-types
83 { "sequence_variant", "sequence_variant" },
84 { "structural_variant", "sequence_variant" },
85 { "feature_variant", "sequence_variant" },
86 { "upstream_gene_variant", "sequence_variant" },
87 { "gene_variant", "sequence_variant" },
88 { "transcript_variant", "sequence_variant" },
89 { "non_coding_transcript_variant", "sequence_variant" },
90 { "non_coding_transcript_exon_variant", "sequence_variant" },
91 // NB Ensembl uses NMD_transcript_variant as if a 'transcript'
92 // but we model it here correctly as per the SO
93 { "NMD_transcript_variant", "sequence_variant" },
94 { "missense_variant", "sequence_variant" },
95 { "synonymous_variant", "sequence_variant" },
96 { "frameshift_variant", "sequence_variant" },
97 { "5_prime_UTR_variant", "sequence_variant" },
98 { "3_prime_UTR_variant", "sequence_variant" },
99 { "stop_gained", "sequence_variant" },
100 { "stop_lost", "sequence_variant" },
101 { "inframe_deletion", "sequence_variant" },
102 { "inframe_insertion", "sequence_variant" },
103 { "splice_region_variant", "sequence_variant" },
106 * no sub-types of exon or CDS yet seen in Ensembl
107 * some added here for testing purposes
110 { "coding_exon", "exon" },
112 { "CDS_predicted", "CDS" },
115 * terms used in exonerate or PASA GFF
117 { "protein_match", "protein_match"},
118 { "nucleotide_match", "nucleotide_match"},
119 { "cDNA_match", "nucleotide_match"},
122 * used in InterProScan GFF
124 { "polypeptide", "polypeptide" }
129 * hard-coded list of any parents (direct or indirect)
130 * that we care about for a term
132 private Map<String, List<String>> parents;
134 private List<String> termsFound;
136 private List<String> termsNotFound;
138 public SequenceOntologyLite()
140 termsFound = new ArrayList<>();
141 termsNotFound = new ArrayList<>();
146 * Loads hard-coded data into a lookup table of {term, {list_of_parents}}
148 private void loadStaticData()
150 parents = new HashMap<>();
151 for (String[] pair : TERMS)
153 List<String> p = parents.get(pair[0]);
156 p = new ArrayList<>();
157 parents.put(pair[0], p);
164 * Answers true if 'child' isA 'parent' (including equality). In this
165 * implementation, based only on hard-coded values.
168 public boolean isA(String child, String parent)
170 if (child == null || parent == null)
174 if (child.equals(parent))
180 List<String> p = parents.get(child);
187 if (p.contains(parent))
195 * Records a valid term queried for, for reporting purposes
199 private void termFound(String term)
201 if (!termsFound.contains(term))
203 synchronized (termsFound)
205 termsFound.add(term);
211 * Records an invalid term queried for, for reporting purposes
215 private void termNotFound(String term)
217 synchronized (termsNotFound)
219 if (!termsNotFound.contains(term))
221 // suppress logging here as it reports Uniprot sequence features
222 // (which do not use SO terms) when auto-configuring feature colours
223 // System.out.println("SO term " + term
224 // + " not known - add to model if needed in "
225 // + getClass().getName());
226 termsNotFound.add(term);
232 * Sorts (case-insensitive) and returns the list of valid terms queried for
235 public List<String> termsFound()
237 synchronized (termsFound)
239 Collections.sort(termsFound, String.CASE_INSENSITIVE_ORDER);
245 * Sorts (case-insensitive) and returns the list of invalid terms queried for
248 public List<String> termsNotFound()
250 synchronized (termsNotFound)
252 Collections.sort(termsNotFound, String.CASE_INSENSITIVE_ORDER);
253 return termsNotFound;
258 public List<String> getRootParents(final String term)
261 * check in cache first
263 if (rootParents.containsKey(term))
265 return rootParents.get(term);
268 List<String> top = new ArrayList<>();
269 List<String> query = new ArrayList<>();
272 while (!query.isEmpty())
274 List<String> nextQuery = new ArrayList<>();
275 for (String q : query)
277 List<String> theParents = parents.get(q);
278 if (theParents != null)
280 if (theParents.size() == 1 && theParents.get(0).equals(q))
285 if (!top.contains(q))
292 for (String p : theParents)
305 rootParents.put(term, top);
307 return top.isEmpty() ? null : top;