moved files out of trunk/ in prep to track this proj primarily on github
[javacyc.git] / Javacyc.java
blob05b79b9d934719c976a3a70829e03faf2bbd56fb
1 /**
2 Javacyc is a Java interface for the Pathway Tools software. Pathway Tools
3 needs to run a special socket server for Javacyc to work.
5 Javacyc uses J-BUDS for Unix domain sockets.
7 Javacyc accesses Generic Frame Protocol (GFP) and Pathway Tools Internal
8 Lisp (PTIL) functions. These functions either return a boolean, a string,
9 or a list. The lists returned are always an ArrayList of strings unless
10 specified otherwise.
12 @author Thomas Yan
13 Copyright (c) 2003; The Arabidopsis Information Resource
14 See README file for license details
17 import java.io.*;
18 import java.util.*;
20 public class Javacyc
22 /**
23 Constructor for Javacyc.
24 @param organism the name of the organism
26 public Javacyc(String organism)
28 this.organism = organism;
29 socketName = "/tmp/ptools-socket";
33 /**
34 Get a socket connection with Pathway Tools using a Unix domain
35 socket.
37 private void makeSocket() {
38 try {
39 // Create socket and connect to the server
40 uds = new UnixDomainSocket(socketName);
41 out = new PrintWriter(uds.getOutputStream(), true);
42 in = new BufferedReader(
43 new InputStreamReader(uds.getInputStream()));
44 } catch (IOException e) {
45 e.printStackTrace();
46 throw new RuntimeException();
50 /**
51 Close the socket connection with Pathway Tools.
52 @throws IOException if the socket connection cannot be closed
54 private void closeSocket() {
55 try {
56 uds.close();
57 out.close();
58 in.close();
59 } catch (IOException e) {
60 e.printStackTrace();
61 throw new RuntimeException();
66 // Methods that call the GFP Functions
68 /**
69 Calls the GFP function, get-slot-values.
70 @param frame a frame id or object
71 @param slotName a slot name
72 @return an ArrayList of all values of slot of frame
74 public ArrayList getSlotValues(String frame, String slotName)
76 return callFuncArray("get-slot-values '" + frame + " '" + slotName);
79 /**
80 Calls the GFP function get-slot-value.
81 @param frame a frame id or object
82 @param slotName a slot name
83 @return the first value of slot of frame
85 public String getSlotValue(String frame, String slotName)
87 return callFuncString("get-slot-value '" + frame + " '" + slotName);
90 /**
91 Calls the GFP function, get-class-slot-slotvalue.
92 @param className the class
93 @param slotName a slot name
94 @param slotValue a slot value
95 @return an ArrayList with the object names returned by
96 get-class-slot-slotvalue
98 public ArrayList getClassSlotSlotvalue(String className, String slotName,
99 String slotValue)
101 return callFuncArray("get-class-slot-slotvalue '" + className + " '"
102 + slotName + " '" + slotValue);
106 Calls the GFP function, get-class-all-instances
107 @param classFrame a class frame
108 @return an ArrayList of all frames that are direct or indirect instances
109 of classFrame
111 public ArrayList getClassAllInstances(String classFrame)
113 return callFuncArray("get-class-all-instances '" + classFrame);
117 Calls the GFP function, instance-all-instance-of-p
118 @param classFrame a class frame
119 @param instance an instance frame
120 @return true if instance is a direct or indirect child of class
122 public boolean instanceAllInstanceOfP(String classFrame, String instance)
124 return callFuncBool("instance-all-instance-of-p '" + instance + " '"
125 + classFrame);
129 Calls the GFP function, member-slot-value-p
130 @param frame a frame id or object
131 @param slot a slot name
132 @param value a slot value
133 @return true if value is one of the values of slot of frame
135 public boolean memberSlotValueP(String frame, String slot, String value)
137 return callFuncBool("member-slot-value-p '" + frame + " '" + slot
138 + " '" + value);
142 Calls the GFP function, current-kb
143 @return the currently selected KB
145 public String currentKB()
147 return organism;
151 Calls the GFP function, put-slot-values
152 @param frame a frame id or object
153 @param slot a slot name
154 @param values a set of values
155 @return any results from the server in an ArrayList
157 public ArrayList putSlotValues(String frame, String slot, String values)
159 return callFuncArray("put-slot-values '" + frame + " '" + slot
160 + " '" + values);
164 Calls the GFP function, put-slot-value
165 @param frame a frame id or object
166 @param slot a slot name
167 @param value a value
168 @return any results from the server in an ArrayList
170 public ArrayList putSlotValue(String frame, String slot, String value)
172 return callFuncArray("put-slot-value '" + frame + " '" + slot
173 + " '" + value);
177 Calls the GFP function, add-slot-value
178 @param frame a frame id or object
179 @param slot a slot name
180 @param value a value
181 @return any results from the server in an ArrayList
183 public ArrayList addSlotValue(String frame, String slot, String value)
185 return callFuncArray("add-slot-value '" + frame + " '" + slot
186 + " '" + value);
190 Calls the GFP function, replace-slot-value
191 @param frame a frame id or object
192 @param slot a slot name
193 @param oldValue the value to be replaced
194 @param newValue the value to replace oldValue with
195 @return any results from the server in an ArrayList
197 public ArrayList replaceSlotValue(String frame, String slot,
198 String oldValue, String newValue)
200 return callFuncArray("replace-slot-value '" + frame + " '" + slot
201 + " '" + oldValue + " '" + newValue);
205 Calls the GFP function, remove-slot-value
206 @param frame a frame id or object
207 @param slot a slot name
208 @return any results from the server in an ArrayList
210 public ArrayList removeSlotValue(String frame, String slot)
212 return callFuncArray("remove-slot-value '" + frame + " '" + slot);
216 Calls the GFP function, coercible-to-frame-p
217 @param thing a thing
218 @return true if thing is a frame object, the name of a frame in kb, or
219 handle of frame in kb
221 public boolean coercibleToFrameP(String thing)
223 return callFuncBool("coercible-to-frame-p '" + thing);
227 Calls the GFP function, class-all-type-of-p
228 @param classFrame a class frame
229 @param instance an instance
230 @return true if instance is an all-instance of classFrame
232 public boolean classAllTypeOfP(String classFrame, String instance)
234 return callFuncBool("class-all-type-of-p '" + classFrame + " '"
235 + instance);
239 Calls the GFP function, get-instance-direct-types
240 @param instance an instance
241 @return an ArrayList of the direct types of instance
243 public ArrayList getInstanceDirectTypes(String instance)
245 return callFuncArray("get-instance-direct-types '" + instance);
249 Calls the GFP function, get-instance-all-types
250 @param instance an instance
251 @return an ArrayList of all-types of instance
253 public ArrayList getInstanceAllTypes(String instance)
255 return callFuncArray("get-instance-all-types '" + instance);
259 Calls the GFP function, get-frame-slots
260 @param frame a frame id or object
261 @return an ArrayList of instance or template slots associated with frame
263 public ArrayList getFrameSlots(String frame)
265 return callFuncArray("get-frame-slots '" + frame);
269 Calls the GFP function, put-instance-types
270 @param instance an instance
271 @param newTypes the classes that instances becomes an instance of
272 @return the results from the server in an ArrayList
274 public ArrayList putInstanceTypes(String instance, String newTypes)
276 return callFuncArray("put-instance-types '" + instance + " '"
277 + newTypes);
281 Calls the GFP function, save-kb
282 @return the results from the server in an ArrayList
284 public ArrayList saveKB()
286 return callFuncArray("save-kb");
290 Calls the GFP function, revert-kb
291 @return the results from the server in an ArrayList
293 public ArrayList revertKB()
295 return callFuncArray("revert-kb");
299 Calls the GFP function, find-indexed-frame
300 @param datum a datum
301 @param className a class
302 @return the results from the server in an ArrayList. Some of the
303 elements in the returned ArrayList may be ArrayLists themselves.
305 public ArrayList findIndexedFrame(String datum, String className)
307 return callFuncArray("multiple-value-list (find-indexed-frame ' "
308 + datum + " '" + className);
311 // Methods that call Pathway-Tools internal lisp (PTIL) functions
314 Changes the organism. Does not make a call to the select-organism
315 lisp function in Pathway-Tools. Does not make any calls to
316 Pathway-Tools functions. The organism is prefixed to every query
317 sent to the socket server.
318 @param newOrganism the new organism
320 public void selectOrganism(String newOrganism)
322 organism = newOrganism;
326 Calls PTIL function, all-pathways
327 @return an ArrayList containing all pathways in the current organism
329 public ArrayList allPathways()
331 return callFuncArray("all-pathways");
335 Calls PTIL function, all-orgs
336 @return an ArrayList of orgkb-defstructs for all organisms currently
337 known to the Pathway Tools
339 public ArrayList allOrgs()
341 return callFuncArray("all-orgs");
345 Calls PTIL function, all-rxns
346 @return an ArrayList of reactions in the current organism
348 public ArrayList allRxns()
350 return callFuncArray("all-rxns");
354 Calls the PTIL function, genes-of-reaction
355 @param rxn a reaction frame
356 @return an ArrayList of all genes that code for enzymes that catalyze
357 the reaction rxn
359 public ArrayList genesOfReaction(String rxn)
361 return callFuncArray("genes-of-reaction '" + rxn);
365 Calls the PTIL function, substrates-of-reaction
366 @param rxn a reaction frame
367 @return an ArrayList of all substrates of the reaction rxn
369 public ArrayList substratesOfReaction(String rxn)
371 return callFuncArray("substrates-of-reaction '" + rxn);
375 Calls the PTIL function, products-of-reaction
376 This is a hypothetical function that may not exist.
377 @param rxn a reaction frame
378 @return an ArrayList of all products of the reaction rxn
380 public ArrayList productsOfReaction(String rxn)
382 return callFuncArray("products-of-reaction '" + rxn);
386 Calls the PTIL function, enzymes-of-reaction
387 @param rxn a reaction frame
388 @return an ArrayList of all enzymes that catalyze the reaction rxn
390 public ArrayList enzymesOfReaction(String rxn)
392 return callFuncArray("enzymes-of-reaction '" + rxn);
396 Calls the PTIL function, reaction-reactants-and-products
397 @param rxn a reaction frame
398 @param pwy a pathway frame
399 @return an ArrayList containing the reactants of rxn and the products
400 of rxn. Some of the elements of the returned ArrayList may be
401 ArrayLists themselves.
403 public ArrayList reactionReactantsAndProducts(String rxn, String pwy)
405 return callFuncArray("multiple-value-list (" +
406 "reaction-reactants-and-products '" + rxn +
407 " '" + pwy + ")");
411 Calls the PTIL function, get-predecessors
412 @param rxn a reaction frame
413 @param pwy a pathway frame
414 @return an ArrayList of all reactions that are direct predecessors
415 of rxn in pwy
417 public ArrayList getPredecessors(String rxn, String pwy)
419 return callFuncArray("get-predecessors '" + rxn + " '" + pwy);
423 Calls the PTIL function, get-successors
424 @param rxn a reaction frame
425 @param pwy a pathway frame
426 @return an ArrayList of all reactions that are direct successors of
427 rxn in pwy
429 public ArrayList getSuccessors(String rxn, String pwy)
431 return callFuncArray("get-successors '" + rxn + " '" + pwy);
435 Calls the PTIL function, get-reaction-list
436 @param pwy a pathway frame
437 @return an ArrayList of the reactions in pwy
439 public ArrayList getReactionList(String pwy)
441 return callFuncArray("get-reaction-list '" + pwy);
445 Calls the PTIL function, genes-of-pathway
446 @param pwy a pathway frame
447 @return an ArrayList of all genes that code for enzymes that catalyze a
448 reaction in the pathway pwy
450 public ArrayList genesOfPathway(String pwy)
452 return callFuncArray("genes-of-pathway '" + pwy);
456 Calls the PTIL function, enzymes-of-pathway
457 @param pwy a pathway frame
458 @return an ArrayList of all enzymes that catalyze a reaction in pwy
460 public ArrayList enzymesOfPathway(String pwy)
462 return callFuncArray("enzymes-of pathway '" + pwy);
466 Calls the PTIL function, compounds-of-pathway
467 @param pwy a pathway frame
468 @return an ArrayList of of all substrates of reactions of pwy, with
469 duplicates removed
471 public ArrayList compoundsOfPathway(String pwy)
473 return callFuncArray("compounds-of-pathway '" + pwy);
477 Calls the PTIL function, substrates-of-pathway
478 @param pwy a pathway frame
479 @return an ArrayList of ArrayLists that contain the values returned
480 by Pathway Tools
482 public ArrayList substratesOfPathway(String pwy)
484 return callFuncArray("multiple-value-list (substrates-of-pathway '"
485 + pwy + ")");
489 Calls the PTIL function, all-transcription-factors
490 @return all transcription factors in the current organism
492 public ArrayList allTranscriptionFactors()
494 return callFuncArray("all-transcription-factors");
498 Calls the PTIL function, transcription-factor?
499 @param protein a protein
500 @return true if protein is a trascription factor in the current
501 organism
503 public boolean isTranscriptionFactor(String protein)
505 return callFuncBool("transcription-factor? '" + protein);
509 Calls the PTIL function, all-cofactors
510 @return an ArrayList of all cofactors used by enzymes in the current
511 organism
513 public ArrayList allCofactors()
515 return callFuncArray("all-cofactors");
519 Calls the PTIL function, all-modulators
520 @return an ArrayList of all modulators that enzymes in the current
521 organism are sensitive to
523 public ArrayList allModulators()
525 return callFuncArray("all-modulators");
529 Calls the PTIL function, monomers-of-protein
530 @param protein a protein
531 @return an ArrayList of monomers that are subunits of protein
533 public ArrayList monomersOfProtein(String protein)
535 return callFuncArray("monomers-of-protein '" + protein);
539 Calls the PTIL function, components-of-protein
540 @param protein a protein
541 @return an ArrayList of components and their coefficients. Some of
542 the elements in the returned ArrayList may be ArrayLists themselves.
544 public ArrayList componentsOfProtein(String protein)
546 return callFuncArray("multiple-value-list (components-of-protein '"
547 + protein + ")");
551 Calls the PTIL function, genes-of-protein
552 @param protein a protein
553 @return an ArrayList of genes that code for protein and all of the
554 subunits of protein
556 public ArrayList genesOfProtein(String protein)
558 return callFuncArray("genes-of-protein '" + protein);
562 Calls the PTIL function, reactions-of-enzyme
563 @param enzyme an enzyme
564 @return an ArrayList of all reactions that enzyme is linked to via
565 enzymatic reactions
567 public ArrayList reactionsOfEnzyme(String enzyme)
569 return callFuncArray("reactions-of-enzyme '" + enzyme);
573 Calls the PTIL function, enzyme?
574 @param protein a protein
575 @return true if the specified protein is an enzyme
577 public boolean isEnzyme(String protein)
579 return callFuncBool("enzyme? '" + protein);
583 Calls the PTIL function, transporter?
584 @param protein a protein
585 @return true if the specified protein is a transporter
587 public boolean isTransporter(String protein)
589 return callFuncBool("transporter? '" + protein);
593 Calls the PTIL function, containers-of
594 @param protein a protein
595 @return a list of all containers of protein, including itself
597 public ArrayList containersOf(String protein)
599 return callFuncArray("containers-of '" + protein);
603 Calls the PTIL function, modified-forms
604 @param protein a protein
605 @return a list of modified forms of protein, including itself
607 public ArrayList modifiedForms(String protein)
609 return callFuncArray("modified-forms '" + protein);
613 Calls the PTIL function, modified-containers
614 @param protein a protein
615 @return a list of all containers of a protein including itself and all
616 modified forms of a protein
618 public ArrayList modifiedContainers(String protein)
620 return callFuncArray("modified-containers '" + protein);
624 Calls the PTIL function, top-containers
625 @param protein a protein
626 @return a list of all containers of protein that have no containers
628 public ArrayList topContainers(String protein)
630 return callFuncArray("top-containers '" + protein);
634 Calls the PTIL function, reactions-of-protein
635 @param protein a protein
636 @return an ArrayList of all reactions catalyzed by protein or subuinits
637 of protein
639 public ArrayList reactionsOfProtein(String protein)
641 return callFuncArray("reactions-of-protein '" + protein);
645 Calls the PTIL function, regulon-of-protein
646 @param protein a protein
647 @return an ArrayList of transcription units regulated by any modified
648 or unmodified form of protein
650 public ArrayList regulonOfProtein(String protein)
652 return callFuncArray("regulon-of-protein '" + protein);
656 Calls the PTIL function, transcription-units-of-protein
657 @param protein a protein
658 @return an ArrayList of transcripton units activated or inhibited by
659 the supplied protein or modified protein frame
661 public ArrayList transcriptionUnitsOfProtein(String protein)
663 return callFuncArray("transcription-units-of-protein '" + protein);
667 Calls the PTIL function, regulator-proteins-of-transcription-unit
668 @param tu a transcription unit
669 @return an ArrayList of proteins that bind to binding sites within tu
671 public ArrayList regulatorProteinsOfTranscriptionUnit(String tu)
673 return callFuncArray("regulator-proteins-of-transcription-unit '"
674 + tu);
678 Calls the PTIL function, enzymes-of-gene
679 @param gene a gene
680 @return an ArrayList of all enzymes coded for by gene
682 public ArrayList enzymesOfGene(String gene)
684 return callFuncArray("enzymes-of-gene '" + gene);
688 Calls the PTIL function, all-products-of-gene
689 @param gene a gene
690 @return an ArrayList of all gene products of gene including those that
691 are not enzymes
693 public ArrayList allProductsOfGene(String gene)
695 return callFuncArray("all-products-of-gene '" + gene);
699 Calls the PTIL function, reactions-of-gene
700 @param gene a gene
701 @return an ArrayList of all reactions catalyzed by proteins that are
702 products of gene
704 public ArrayList reactionsOfGene(String gene)
706 return callFuncArray("reactions-of-gene '" + gene);
710 Calls the PTIL function, pathways-of-gene
711 @param gene a gene
712 @return an ArrayList of all pathways containing reactions that are
713 catalyzed by proteins that are products of gene
715 public ArrayList pathwaysOfGene(String gene)
717 return callFuncArray("pathways-of-gene '" + gene);
721 Calls the PTIL function, chromosome-of-gene
722 @param gene a gene
723 @return a String containing the chromosome on which gene resides
725 public String chromosomeOfGene(String gene)
727 return callFuncString("chromosome-of-gene '" + gene);
731 Calls the PTIL function, transcription-units-of-gene
732 @param gene a gene
733 @return an ArrayList of all transcription units that form the operon
734 containing gene
736 public ArrayList transcriptionUnitsOfGene(String gene)
738 return callFuncArray("transcription-units-of-gene '" + gene);
742 Calls the PTIL function, transcription-unit-promoter
743 @param tu a transcription unit
744 @return a string containing the promoter of tu
746 public String transcriptionUnitPromoter(String tu)
748 return callFuncString("transcription-unit-promoter '" + tu);
752 Calls the PTIL function, transcription-unit-genes
753 @param tu a transcription unit
754 @return an ArrayList of genes within the transcription unit
756 public ArrayList transcriptionUnitGenes(String tu)
758 return callFuncArray("transcription-unit-genes '" + tu);
762 Calls the PTIL function, transcription-unit-binding-sites
763 @param tu a transcription unit
764 @return an ArrayList of DNA binding sites within the transcriptional
765 unit
767 public ArrayList transcriptionUnitBindingSites(String tu)
769 return callFuncArray("transcriptional-unit-binding-sites '" + tu);
773 Calls the PTIL function, transcription-unit-transcription-factors
774 @param tu a transcription unit
775 @return an ArrayList of the transcription factors that control the
776 transcription unit tu
778 public ArrayList transcriptionUnitTranscriptionFactors(String tu)
780 return callFuncArray("transcription-unit-transcription-factors '"
781 + tu);
785 Calls the PTIL function, transcription-unit-terminators
786 @param tu a transcription unit
787 @return an ArrayList of the transcription terminators(s) within the
788 transcription unit
790 public ArrayList transcriptionUnitTerminators(String tu)
792 return callFuncArray("transcription-unit-terminators '" + tu);
796 Calls the PTIL function, all-transported-chemicals
797 @return an ArrayList of chemicals that are transported by the set of all
798 defined transport reactions in current organism
800 public ArrayList allTransportedChemicals()
802 return callFuncArray("all-transported-chemicals");
806 Calls the PTIL function, reactions-of-compound
807 @param cpd a chemical
808 @return an ArrayList of the reactions in which cpd occurs as a
809 reactant or a product
811 public ArrayList reactionsOfCompound(String cpd)
813 return callFuncArray("reactions-of-compound '" + cpd);
817 Calls the PTIL function, full-enzyme-name
818 @param enzyme an enzyme
819 @return the full name of the enzyme
821 public String fullEnzymeName(String enzyme)
823 return callFuncString("full-enzyme-name '" + enzyme);
827 Calls the PTIL function, enzyme-activity-name
828 @param enzyme an enzyme
829 @return the enzyme activity name
831 public String enzymeActivityName(String enzyme)
833 return callFuncString("enzyme-activity-name '" + enzyme);
836 // Private methods for querying, retrieving results, calling functions,
837 // and lisp list parsing
840 Private method to call a Pathway Tools function that returns a list.
841 @param func the Pathway Tools function to call
842 @return an ArrayList representation of the lisp list returned by
843 Pathway Tools
845 private ArrayList callFuncArray(String func)
847 makeSocket();
848 try {
849 String query = wrapQuery(func);
850 sendQuery(query);
851 ArrayList results = retrieveResultsArray();
852 // try
853 // {
854 // close();
855 // }
856 // catch (IOException e)
857 // {
858 // e.printStackTrace();
859 // }
860 return results;
861 } finally {
862 closeSocket();
867 Private method to call a Pathway Tools function that returns a string.
868 @param func the Pathway Tools function to call
869 @return string returned by Pathway Tools function call
871 private String callFuncString(String func)
873 makeSocket();
874 try {
875 String query = "(with-organism (:org-id '" + organism +
876 ") (object-name (" + func + ")))";
877 sendQuery(query);
878 String results = retrieveResultsString();
879 // try
880 // {
881 // close();
882 // }
883 // catch (IOException e)
884 // {
885 // e.printStackTrace();
886 // }
887 return results;
888 } finally {
889 closeSocket();
894 Private method to call a Pathway Tools function that returns a boolean.
895 @param func the Pathway Tools function to call
896 @return true if the result of the function call is true
898 private boolean callFuncBool(String func)
900 String result = callFuncString(func);
901 if (result.equals("NIL"))
903 return false;
905 else
907 return true;
912 Private method that wraps a query.
913 @param func the function call to wrap in a query
914 @return a query
916 private String wrapQuery(String func)
918 return "(with-organism (:org-id '" + organism +
919 ") (mapcar #'object-name (" + func + ")))";
923 Private method to send a query to Pathway Tools.
924 @param query the query to send to Pathway Tools
926 private void sendQuery(String query)
928 out.println(query);
932 Private method to retrieve a string result.
933 @return the string result
935 private String retrieveResultsString()
939 ArrayList results = new ArrayList();
940 String readStr = in.readLine();
941 while (readStr != null)
943 // DEBUG ONLY
944 //System.out.println(readStr);
946 results.add(readStr);
947 readStr = in.readLine();
949 String retStr = (String)results.get(0);
951 // DEBUG
952 //System.out.println("0th element: " + (String)results.get(0));
954 // If retStr is surrounded by quotation marks, remove them
955 if ((retStr.startsWith("\"")) && (retStr.endsWith("\"")))
957 int endIndex = retStr.length() - 1;
958 return retStr.substring(1, endIndex);
960 else
962 return retStr;
965 catch (IOException e)
967 e.printStackTrace();
969 return null; // if an IOException has occured
973 Private method to retrieve an ArrayList result.
974 This method is like retrieve_results in perlcyc, not perlcyc's
975 retrieve_results_array subroutine.
976 @return the ArrayList result
978 private ArrayList retrieveResultsArray()
980 LinkedList tokens = tokenize();
981 return parseExpr(tokens);
985 Private method to tokenize a lisp expression.
986 @return an LinkedList containing the tokens of the lisp expression
988 private LinkedList tokenize()
990 LinkedList tokens = new LinkedList();
993 StreamTokenizer tokenizer = new StreamTokenizer(in);
994 tokenizer.resetSyntax();
995 tokenizer.wordChars('a', 'z');
996 tokenizer.wordChars('A', 'Z');
997 tokenizer.wordChars('\u00A0', '\u00FF');
998 tokenizer.wordChars('0', '9');
999 tokenizer.wordChars('.', '.');
1000 tokenizer.wordChars('-', '-');
1001 tokenizer.wordChars('/', '/');
1002 tokenizer.wordChars('\'', '\'');
1003 tokenizer.whitespaceChars(' ', ' ');
1004 tokenizer.whitespaceChars('\n', '\n');
1005 tokenizer.quoteChar('"');
1006 int type = tokenizer.nextToken();
1007 while (type != StreamTokenizer.TT_EOF)
1009 if (type == StreamTokenizer.TT_NUMBER)
1011 tokens.add(Double.toString(tokenizer.nval));
1013 else if (type == StreamTokenizer.TT_WORD)
1015 tokens.add(tokenizer.sval);
1017 else if (type == '(')
1019 tokens.add("(");
1021 else if (type == '"')
1023 tokens.add(tokenizer.sval);
1025 else if (type == ')')
1027 tokens.add(")");
1029 else if (type == '|')
1031 StringBuffer buffer = new StringBuffer();
1032 buffer.append("|");
1033 int typeLastTok = type;
1034 type = tokenizer.nextToken();
1035 while (type != '|')
1037 if (type == StreamTokenizer.TT_WORD)
1039 if ((typeLastTok == StreamTokenizer.TT_WORD) ||
1040 (typeLastTok == StreamTokenizer.TT_NUMBER) ||
1041 (typeLastTok == '"'))
1043 buffer.append(" ");
1045 buffer.append(tokenizer.sval);
1047 else if (type == StreamTokenizer.TT_NUMBER)
1049 if ((typeLastTok == StreamTokenizer.TT_WORD) ||
1050 (typeLastTok == StreamTokenizer.TT_NUMBER) ||
1051 (typeLastTok == '"'))
1053 buffer.append(" ");
1055 buffer.append(Double.toString(tokenizer.nval));
1057 else if (type == '"')
1059 if ((typeLastTok == StreamTokenizer.TT_WORD) ||
1060 (typeLastTok == StreamTokenizer.TT_NUMBER) ||
1061 (typeLastTok == '"'))
1063 buffer.append(" ");
1065 buffer.append("\"");
1066 buffer.append(tokenizer.sval);
1067 buffer.append("\"");
1069 else
1071 buffer.append((char)tokenizer.ttype);
1073 typeLastTok = type;
1074 type = tokenizer.nextToken();
1076 buffer.append("|");
1077 tokens.add(buffer.toString());
1079 else
1081 Character c = new Character((char)tokenizer.ttype);
1082 tokens.add(c.toString());
1084 type = tokenizer.nextToken();
1087 catch (IOException e)
1089 e.printStackTrace();
1091 return tokens;
1095 Private parser method.
1096 @param tokens a LinkedList containing the tokens of a lisp expression
1097 @return an ArrayList representation of the lisp list
1099 private ArrayList parseExpr(LinkedList tokens)
1101 String first = (String)tokens.getFirst();
1102 if (first.equals("("))
1104 tokens.removeFirst();
1105 ArrayList listElements = new ArrayList();
1106 while (!((String)tokens.getFirst()).equals(")"))
1108 String temp = (String)tokens.getFirst();
1109 if (temp.equals("("))
1111 listElements.add(parseExpr(tokens)); // add an inner list
1113 else if (temp.equals("NIL"))
1115 listElements.add("NIL");
1116 tokens.removeFirst();
1118 else
1120 listElements.add(temp);
1121 tokens.removeFirst();
1124 tokens.removeFirst(); // remove a )
1125 return listElements;
1127 else // return empty ArrayList
1129 return new ArrayList();
1133 private UnixDomainSocket uds; // J-BUDS Unix domain socket
1134 private String socketName; // name of the socket
1135 private String organism; // name of the organism
1136 private PrintWriter out; // output to the Pathway Tools server
1137 private BufferedReader in; // input from the Pathway Tools server