180 likes | 315 Views
Introduction to LISP Programming of Pathway Tools Queries and Updates. Myths and Facts About Lisp. Myth: Lisp runs interpreted only Fact: All major Lisp implementations have compilers Myth: Lisp uses huge amounts of memory Fact: Baseline Lisp installation requires 8-10MB
E N D
Introduction to LISP Programming of Pathway Tools Queries and Updates
Myths and Facts About Lisp • Myth: Lisp runs interpreted only • Fact: All major Lisp implementations have compilers • Myth: Lisp uses huge amounts of memory • Fact: Baseline Lisp installation requires 8-10MB • Myth: Lisp is complicated • Fact: Lisp is much simpler and more elegant than Perl
LISP and GFP References • Lisp on the web: ALU.org • ANSI Common LISP • Paul Graham • Common LISP, the Language -- The standard reference • Guy L. Steele • On Common LISP • Paul Graham • The Art of the Metaobject Protocol • Kiczales, Rivieres, Bobrow • Information on writing Pathway Tools queries: • http://bioinformatics.ai.sri.com/ptools/ptools-resources.html • http://www.ai.sri.com/pkarp/loop.html • http://bioinformatics.ai.sri.com/ptools/debugger.html
Accessing Lisp through the Pathway Tools • Starting Pathway Tools for Lisp work: • pathway-tools –lisp • (select-organism :org-id ‘XXX) • Lisp expressions can be typed at any time to the Pathway Tools listener • Command: (get-slot-value ‘trp ‘common-name) -> “L-tryptophan” • Invoking the Navigator from Lisp: • (eco)
LISP Syntax • Prefix notation • Simple and clean syntax • Expressions are delimited by parentheses • The same syntax is used for programs and for data • (1 2 3 4 5 10 “10”) • (a b c d e f) • (+ 1 2) • (subseq “abcdefg” 0 2)
LISP Expressions and Evaluation • (+ 3 4 5) • ‘+’ is a function • (+ 3 4 5) is a function call with 3 arguments • Arguments are evaluated: • Numbers evaluate to themselves • If any of the args are themselves expressions, they are evaled in the same way • (+ 1 (+ 3 4)) • The values of the args are passed to the function • Because of prefix notation, variable number of args • (+) ---> 0 • (+ 1) ---> 1 • (+ 2 3 1 3 4 5 6) ----> 24 • (+ (* 3 4) 6) --> 18 • Turning off evaluation with Quote • ’(+ 1 3) ----> (+ 1 3)
LISP Listener • Also called “top level” and “read-eval-print loop” • Expressions typed in listener are evaluated interactively • Uses a three-step process • Read • Reader converts elements outside “” and || to uppercase • Evaluate • Print • Useful forms in listener: • Previous Results: *, **, *** • DO NOT use in programs (+ 1 2) -> 3 (+ 3 *) -> 6 ** -> 3
LISP Data Types • Usual types in other languages: • Numbers -- 2, 312, 1.45, -222e2 • Strings -- “sky”, “this is a lisp intro” • Characters - #\D, #\space • Hashtables • True/False T / NIL • Fundamental LISP data types • Symbols - BLUE, :CONT • Lists - (1 2 3) (“a” “b” “c”) (“a” 2 “X”)
Lisp Variables • Global variable values can be set and used during a session • Declarations not needed (setq x 5) -> 5 x -> 5 (+ 3 x) -> 8 (setq y “atgc”) -> “atgc”
Examples (select-organism :org-id ‘ecoli) -> ECOLI (setq genes (get-class-all-instances ‘|Genes|)) -> (……………) (setq monomers (get-class-all-instances ‘|Polypeptides|)) -> (…………….) (setq genes2 genes) -> (…………….)
LISP Lists • Fundamental to LISP ::: LISt Processing • Zero or more elements enclosed by parentheses • Typing a list to the listener: • ’(this is a list) => (THIS IS A LIST) • Creating a list with functions : • (list ’so ’is ’this) => (SO IS THIS) • Examples: • (1 3 5 7), ((2 4 6) 10 (0 8)), ’(1 this T NIL “that”) • The empty list: nil ()
List Examples (length genes) -> 4316 (first genes) -> XXX (subseq genes 0 50) -> (……………) (nth 3 genes) -> XXX
Functions for Operating on Lists • Length • (length x) • Returns the number of elements in the list X • First • (first x) • Returns the first element in the list X • Subseq • (subseq x j k) • Returns a newly created list containing a subsequence of list X, starting at element number J and ending before element number K • Nth • (nth j x) • Returns the Jth element of list X (element 0 is the first element)
Defining Functions • Put function definitions in a file • Reload the file when definitions change • (defun <name> (<arguments>) … code for function …) • Creates a new operation called <name> • Examples: • (defun square (x) (* x x)) • (defun message () (print “Hello”)) • (defun test-fn () 1 2 3 4)
Arglist Keywords • Are markers in arglist • Not themselves argument names, but flag that following arguments are different somehow • Most common are: • &optional • &rest • &key • Examples: • (defun plus5 (x &optional (y 5)) (+ x y) ) • (plus5 3) ==> 8 (plus5 4 4) ==> 8 • (defun embed (x &key (y “<<<“) (z “>>>”)) (concatenate ‘string y x z) ) • (embed “foo” :z “]]]”) ==> “<<<foo]]]” • (defun listall (&rest rest-of-args) (sort (copy-seq rest-of-args) #’<))
Problems • all-substrates • enzymes-of-reaction • genes-of-reaction • genes-of-pathway • monomers-of-protein • genes-of-enzyme
Example Session (setq x ‘trp) => trp (get-slot-value x ‘common-name) => “L-tryptophan” (setq aas (get-class-all-instances ‘|Amino-Acids|)) => (……..) (loop for x in aas count x) => 20
Example Session (loop for x in genes for name = (get-slot-value x ‘common-name) when (and name (search “trp” name)) collect x)) -> (…) (setq rxns (get-class-all-instances ‘|Reactions|)) -> (…) (loop for x in rxns when (member-slot-value-p x ‘substrates ‘trp) collect x) -> (…) (replace-answer-list *)