Last active
June 17, 2021 16:03
-
-
Save marco-brandizi/0b147e96660640b410e8e807c109bfc7 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
// Notice the static imports, these makes the syntax below so compact | |
// (and the IDE will auto-define this section) | |
import ondex.fluent.EntityFilter.FluentGraphWrapper | |
import static ondex.fluent.EntityFilter.byType; | |
import static ondex.fluent.EntityFilter.byDoubleAttribute; | |
import static ondex.fluent.ConceptFilter.byAccession; | |
import static ondex.fluent.Filter.byRegEx; | |
import static ondex.fluent.EntityComparator; | |
import static ondex.fluent.ConceptMapper.fromRelations; | |
import static ondex.fluent.RelationMapper.toConcepts; | |
// Also, notice what Java already has for us and the way we combine existing | |
// JDK interfaces and our own ones | |
import java.util.stream.Collectors | |
import static java.util.function.Predicate.or; | |
import static java.util.Comparator.reverseOrder; | |
ONDEXGraph ondexGraph = ... | |
... | |
// The entry point | |
FluentGraphWrapper graphw = FluentGraphWrapper.of ( ondexGraph ); | |
// At the end of chain a concept (node) or a collection of concepts is returned | |
ONDEXConcept concept = graphw. | |
// A Java stream for all the concepts | |
.concepts () | |
// byType returns a java.util.function.Predicate, so that it's compatible with Java streams | |
// this predicate receives a concept as input and compares its type against the "Gene" parameter | |
.filter ( byType ( "Gene" )) | |
// Similar for accession, concept accessions are composed of ID + source name | |
// so, we might want to match both (in addition to one only) | |
.filter ( byAccession ( "P53", "ENSEMBL" ) ) | |
// 'or' comes from Java too, predicates can pass their tested parameter (the concept's name string) to | |
// another predicate (a regular expression matcher in this case) | |
.filter ( or ( byName ( "Apoptosis" ), byName ( byRegEx ( ".*DNA\s+Repairing.*", "i" ) ) )) | |
// switch the chain to a different stream: for all the concepts c matched so far, returns the stream | |
// of edges starting from c, ie, all r such that r(c, y) | |
.map ( fromRelations () ) | |
// Picks all the relations about the genes in the upper stream cited by some publication | |
// Because now we're dealing with a stream of relations, this filters over relation attributes | |
.filter ( byType ( "citedBy" ) ) | |
// Again, restrict on relation attribute | |
.filter ( byDoubleAttribute ( "SCORE", (double) aval -> aval > 10 ) ) | |
// This is Java accepting one of our specialised comparators. This comparator takes two | |
// relations and compares them by the values of the SCORE attributes. | |
// we sort in reverse order cause the best guys here are those having the highest score | |
.sorted ( reverseOrder ( EntityComparator.byDoubleAttribute ( "SCORE" ) ) ) | |
// Stream switch again: returns a stream of concepts y, for all y such that r(c, y) as mentioned above | |
.map ( toConcepts () ) | |
// Because we sorted the down-stream, this will return the concept hit by the highest score | |
// And we know it's a publication-related concept from the relation type | |
.findFirst () | |
.orElseThrow ( () -> new NotFoundException ( "No citing publication concept found" ) ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment