Created
March 13, 2024 20:35
-
-
Save egonw/ebed221ffd8f41e1cc9665584889605b to your computer and use it in GitHub Desktop.
Code for the new WikiPathways website to convert datanodes.tsv into WikiPathways RDF triples
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Grab(group='org.bridgedb', module='org.bridgedb.bio', version='3.1.1') | |
import java.util.ArrayList | |
import java.util.List | |
import java.util.Map | |
import java.util.HashMap | |
import java.nio.file.Path | |
import java.nio.file.Files | |
import static groovy.io.FileType.FILES | |
import org.bridgedb.Xref | |
import org.bridgedb.DataSource | |
import org.bridgedb.bio.DataSourceTxt | |
DataSourceTxt.init() | |
// extract the BridgDb ID mappings | |
Map<String,List> mappings = new HashMap<>(); | |
def dir = new File("pathways"); | |
def files = []; | |
dir.traverse(type: FILES, maxDepth: 2) { | |
if (it.name.endsWith("-datanodes.tsv")) { | |
files.add(it) | |
} | |
}; | |
files.each { file -> | |
file.eachLine { line -> | |
mappedIDs = new ArrayList<String>() | |
fields = line.split('\t') | |
if (fields.size() > 4) { | |
mainID = fields[2] | |
for (col in 4..(fields.size()-1)) { | |
field = fields[col] | |
if (field != null && field.length() > 0) { | |
mappedIDs.addAll(fields[col].split(';')) | |
} | |
} | |
mappings.put(mainID, mappedIDs) | |
} | |
} | |
} | |
// export Turtle | |
def String asIdentifiersOrg(String compactId) { | |
xref = Xref.fromBioregistryIdentifier(compactId) | |
if (xref != null && "Opt".equals(xref.getDataSource().getSystemCode())) | |
xref = new Xref(xref.getId(), DataSource.getExistingBySystemCode("En")) | |
if (xref == null) return null | |
return xref.getMiriamURN().replace("urn:miriam:", "https://identifiers.org/").replace(":", "/") | |
} | |
mappings.each { id -> | |
//println "== ${id.key}" | |
iri = asIdentifiersOrg(id.key) | |
if (iri != null) { | |
id.value.each { mappedId -> | |
mappedIri = asIdentifiersOrg(mappedId) | |
if (mappedIri != null) { | |
if (mappedId.startsWith("ensembl:")) { println "<${iri} wp:bdbEnsembl <${mappedIri}> ." } | |
else if (mappedId.startsWith("wikidata:")) { println "<${iri} wp:bdbWikidata <${mappedIri}> ." } | |
else if (mappedId.startsWith("wikidata:")) { println "<${iri} wp:bdbUniprot <${mappedIri}> ." } | |
} | |
} | |
} else { | |
// println "NULL xref for ${id}" | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment