Skip to content

Commit

Permalink
VCell Kisao Ontology.
Browse files Browse the repository at this point in the history
Classes creation.
  • Loading branch information
vasilescu authored and vasilescu committed Dec 15, 2020
1 parent ef5bada commit 95551ae
Show file tree
Hide file tree
Showing 3 changed files with 218 additions and 0 deletions.
49 changes: 49 additions & 0 deletions vcell-core/src/main/java/cbit/util/kisao/KisaoOntology.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package cbit.util.kisao;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;


public class KisaoOntology {

private List<KisaoTerm> terms = new ArrayList<>();
private static KisaoOntology instance;


public static KisaoOntology getInstance() {
if (instance == null) {
instance = new KisaoTermParser().parse();
}
return instance;
}

public void addTerm(KisaoTerm curr) {
terms.add(curr);
}
public List<KisaoTerm> getTerms() {
return Collections.unmodifiableList(terms);
}

public KisaoTerm getTermById(String id) {
for (KisaoTerm term : terms) {
if (term.getId().equalsIgnoreCase(id)) {
return term;
}
}
return null;
}

public void createRelations() {
for (KisaoTerm term : terms) {
List<String> isas = term.getIsaRef();
for (String isa : isas) {
KisaoTerm termIsa = getTermById(isa);
if (termIsa != null) {
term.addIsa(termIsa);
}
}
}
}

}
101 changes: 101 additions & 0 deletions vcell-core/src/main/java/cbit/util/kisao/KisaoTerm.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
package cbit.util.kisao;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class KisaoTerm {

private String id, name, def;
private List<KisaoTerm> isaList = new ArrayList<KisaoTerm>();
private List<String> isaRef = new ArrayList<String>(); // used only during parsing, as proxy for still unknown terms

KisaoTerm() {
}

@Override
public String toString() {
return "KisaoTerm [id=" + id + ", name=" + name + ", def=" + def + "]";
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((id == null) ? 0 : id.hashCode());
return result;
}

@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj == null) {
return false;
}
if (getClass() != obj.getClass()) {
return false;
}
KisaoTerm other = (KisaoTerm) obj;
if (id == null) {
if (other.id != null) {
return false;
}
} else if (!id.equals(other.id)) {
return false;
}
return true;
}

public String getId() {
return id;
}
void setId(String id) {
this.id = id;
}

public String getName() {
return name;
}
void setName(String name) {
this.name = name;
}

public String getDefinition() {
return def;
}
void setDef(String def) {
this.def = def;
}

public List<KisaoTerm> getIsa() {
return Collections.unmodifiableList(isaList);
}
void addIsa(KisaoTerm is_a) {
isaList.add(is_a);
}

void addIsaRef(String synonym) {
isaRef.add(synonym);
}
List<String> getIsaRef() {
return isaRef;
}

public boolean is_a(KisaoTerm otherTerm) {
if (this.equals(otherTerm)) {
return true;
}
for (KisaoTerm isa : isaList) {
if (isa.equals(otherTerm)) {
return true;
}
}
for (KisaoTerm isa : isaList) {
return isa.is_a(otherTerm);
}
return false;
}

}

68 changes: 68 additions & 0 deletions vcell-core/src/main/java/cbit/util/kisao/KisaoTermParser.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package cbit.util.kisao;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.regex.Matcher;
import java.util.regex.Pattern;


public class KisaoTermParser {
final String Kisao_OBO = "kisao_2020_12_13.obo";
final String TERM_PATTERN = "\\[Term\\]";
final Pattern ID_PATTERN = Pattern.compile("id:\\s*(.+)");
final Pattern NAME_PATTERN = Pattern.compile("name:\\s*(.+)");
final Pattern DEF_PATTERN = Pattern.compile("def:\\s*(.+)");
final Pattern ISA_PATTERN = Pattern.compile("is_a:\\s*(\\S+)");

KisaoOntology parse() {
InputStream is2 = KisaoTermParser.class.getClassLoader().getResourceAsStream(Kisao_OBO);
BufferedReader isr = new BufferedReader(new InputStreamReader(is2));
String line = null;
boolean inPreamble = true;
boolean inState = false;
KisaoOntology ontology = new KisaoOntology();
KisaoTerm curr = null;

try {
while ((line = isr.readLine()) != null) {

if (line.matches(TERM_PATTERN)) {
inState = true;
curr = new KisaoTerm();
}
if (line.matches("^$") && curr != null) {
inState = false;
inPreamble = false;
ontology.addTerm(curr);
curr = null;
}
if(inState) {
Matcher matcher = ID_PATTERN.matcher(line);
if (matcher.find()) {
curr.setId(matcher.group(1));
}

matcher = NAME_PATTERN.matcher(line);
if (matcher.find()) {
curr.setName(matcher.group(1));
}
matcher = DEF_PATTERN.matcher(line);
if (matcher.find()) {
curr.setDef(matcher.group(1));
}
matcher = ISA_PATTERN.matcher(line);
if (matcher.find()) {
curr.addIsaRef(matcher.group(1));
}
}
}
} catch(Exception e) {
e.printStackTrace();
}

ontology.createRelations();
return ontology;
}
}

0 comments on commit 95551ae

Please sign in to comment.