Last active
May 7, 2019 18:28
-
-
Save Habush/a2675db9bd91c12e8e4a6bbd1a7603eb to your computer and use it in GitHub Desktop.
Finding the parents of a gene recursively
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
(use-modules (srfi srfi-1)) | |
;;Returns a namespace EvaluationLink of namespaces | |
(define namespace-eval (lambda (nsp) | |
(map (lambda (n) | |
(EvaluationLink | |
(PredicateNode "GO_namespace") | |
(ListLink | |
(VariableNode "$a") | |
(ConceptNode n) | |
) | |
) | |
) | |
nsp | |
) | |
)) | |
;;Given an atom and list of namespaces finds the parents of that atom in the specified namespaces | |
(define find-parent | |
(lambda (node namespaces) | |
(let ([atom (cog-outgoing-atom node 1)]) | |
(cog-outgoing-set (cog-execute! (BindLink | |
(VariableNode "$a") | |
(AndLink | |
(InheritanceLink | |
atom | |
(VariableNode "$a")) | |
(OrLink (namespace-eval namespaces)) | |
) | |
(InheritanceLink | |
atom | |
(VariableNode "$a") | |
) | |
) | |
) | |
) | |
) | |
)) | |
;;Finds Go terms of a gene | |
(define find-memberln | |
(lambda (gene namespaces) | |
(cog-outgoing-set (cog-execute! (BindLink | |
(VariableNode "$a") | |
(AndLink | |
(MemberLink | |
gene | |
(VariableNode "$a")) | |
(OrLink (namespace-eval namespaces)) | |
) | |
(MemberLink | |
gene | |
(VariableNode "$a") | |
) | |
) | |
) | |
)) | |
) | |
;;a helper function to flatten a list, i.e convert a list of lists into a single list | |
(define (flatten x) | |
(cond ((null? x) '()) | |
((pair? x) (append (flatten (car x)) (flatten (cdr x)))) | |
(else (list x)))) | |
;;the main function to find the go terms for a gene with a specification of the parents | |
(define find-go-term | |
(lambda (g namespaces p) | |
(let ( | |
[res (find-memberln (GeneNode g) namespaces)] | |
) | |
(define parents (flatten (let loop ( | |
[i p] | |
[ls res] | |
[acc '()] | |
) | |
(cond | |
[(= i 0) (append ls acc)] | |
[(null? ls) acc] | |
[else (cons (loop (- i 1) (find-parent (car ls) namespaces) (append ls acc)) (loop i (cdr ls) '())) | |
] | |
) | |
))) | |
(if (= 1 p) | |
(cons g (delete-duplicates parents)) | |
(cons g parents) | |
) | |
) | |
)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment