fmod GRAPH-INFO is
  inc COMPONENT-INFO .
****  inc MY-META-ALL .
****  inc META-PETRI .
****  inc NAT-QID .
****  inc PARSE-SEXP-STRING .
****  inc GRAPH2G2D .


  var next : FindResult .
  vars qid modname opname gname kbname opsort clab : Qid .
  vars qidl toks : QidList .
  vars str str' astr opstr field key name fun urlpat : String .
  var sexp : Sexp .  

  vars sl sl' sexpl : SexpL . 
  var sexp? : Sexp? . 
  var atm : SAtom .

  var typel : TypeList . var type : Type .
  var attrs : AttrSet .
  var opdecls : OpDeclSet .
  vars occT thT locT : Term .
  vars nodes nodes' : NodeList .
  var bindings : TBinds .


  vars ispec cspec : Sexp .
  var lsort : [Type] .
  vars  ispecl cspecl : SexpL .
  var vstr : String .

**** mkComponentInfoExpX(ispec,gname,modname,clab,tl)
**** sxv(ispec) := getVal(es,'sxval,'ComponentInfoSpec,sxv([nil]))

  op mkComponentInfoExpX : Sexp Qid Qid Qid Term -> String .
  ceq mkComponentInfoExpX(ispec,gname,modname,clab,thT)
         =  mkCInfoExp(gname,clab,cspecl,sexp)
  if lsort := leastSort([modname],thT)
  /\ cspecl := (if lsort :: Type 
                then getCSpecL(sexp2l(ispec),modname,lsort) 
                else [nil] fi)
  /\ str := getOpMetaData(modname,getBaseName(thT)) 
  /\ ixs(next,sexp?) := readSexp(str,0,length(str)) 
  /\ sexp := (if sexp? :: Sexp then sexp? else [nil] fi) .


  op getCSpecL : SexpL Qid Type -> SexpL .
  eq getCSpecL(nil,modname,lsort) = nil .
  eq getCSpecL(sexp sexpl,modname,lsort) = 
       (if specFor(sexp,modname,lsort) =/= nil
        then specFor(sexp,modname,lsort) 
        else getCSpecL(sexpl,modname,lsort) fi) .

  op specFor : Sexp Qid Type -> SexpL .
  eq specFor([a(str) sexpl],modname,lsort) =
      (if sortLeq([modname], lsort, qid(str)) then sexpl else nil fi) .
  eq specFor(sexp,modname,lsort) = nil [owise] .


  op ComponentInfoTemplate : -> String .
  eq ComponentInfoTemplate =
  "(apply displayComponentInfo %gname %clab (array java.lang.Object  %iarrstr))" .

  op mkCInfoExp : Qid Qid SexpL Sexp -> String .
  eq mkCInfoExp(gname,clab,cspecl,sexp) =
      fillTemplate(ComponentInfoTemplate,
       (("%gname" :~ squote(string(gname))),
        ("%clab" :~ squote(string(clab))),
        ("%iarrstr" :~ mkIArrayStr(cspecl,sexp,""))) ) .

  op mkIArrayStr : SexpL Sexp String -> String .
  eq mkIArrayStr(nil,sexp,str) = str .
  eq mkIArrayStr(cspec cspecl,sexp,str) = 
      mkIArrayStr(cspecl,sexp, mkIItemStr(cspec,sexp,str)) .


  op valInfoTemplate : -> String .
  eq valInfoTemplate =  "(array java.lang.String \"val\" %name %val)" .

  op mkIItemStr : Sexp Sexp String -> String .
  ceq mkIItemStr([a(key) a("val") a(name)],sexp,str) =
    (if vstr == "" 
     then str 
     else  str + "\n" 
           + fillTemplate(valInfoTemplate,
                   (("%name" :~ squote(name)),
                    ("%val" :~ squote(vstr))) ) fi) 
  if vstr := getFieldVal(sexp,key) .

  op linkInfoTemplate : -> String .
  eq linkInfoTemplate =  "(array java.lang.String \"link\" %name %arg %url)" .

  ceq mkIItemStr([a(key) a("link") a(name) a(urlpat)],sexp,str) =
    (if vstr == "" 
     then str 
     else  str + "\n" +
            fillTemplate(linkInfoTemplate,
                (("%name" :~ squote(name)),
                 ("%arg" :~ squote(vstr)),
                 ("%url" :~ squote(fillTemplate(urlpat, ("%arg" :~ vstr))))) ) fi) 
  if vstr := getFieldVal(sexp,key) .

  op listInfoTemplate : -> String .
  eq listInfoTemplate =  
  "(array java.lang.Object \"list\" %name %fun (array java.lang.String %list) )" .

  ceq mkIItemStr([a(key) a("list") a(name) a(fun)],sexp,str) =
    (if sexpl == nil 
     then str 
     else  str + "\n" +
            fillTemplate(listInfoTemplate,
                  (("%name" :~ squote(name)),
                   ("%fun" :~ fun),
                   ("%list" :~ astr)) ) fi)
  if sexpl := getFieldVals(sexp,key) 
  /\   astr := sexpl2astr(sexpl,"") .

  eq mkIItemStr(cspec,sexp,str) = str [owise] .

  op sexpl2astr : SexpL String -> String .
  eq sexpl2astr(nil,str) = str .
  eq sexpl2astr(a(str') sexpl,str) = sexpl2astr(sexpl, str + " " + squote(str')) .
  eq sexpl2astr(sexp sexpl,str) = sexpl2astr(sexpl, str) .



endfm

fmod TEST-COMPONENT-INFO is
  inc GRAPH-INFO .

  op cispec : -> Sexp .
  eq cispec = 
    [ [a("Protein")
        [a("hugosym") a("val") a("HUGO") ]
        [a("spnumber") a("link") a("SwissProt")
          a("http://www.expasy.ch/cgi-bin/niceprot.pl?%arg") ]
        [a("synonyms") a("list") a("Synonyms") a("ident") ]
       ]
       [a("Chemical")
        [a("keggcpd") a("link") a("KEGG")
          a("http://www.genome.jp/dbget-bin/www_bget?compound+%arg") ]
        [a("synonyms") a("list") a("Synonyms") a("ident") ]
       ]
     ] .

  ops mdPIP3 mdPlcg  : -> String .
  eq mdPIP3 = "(\
   (category Chemical)\
   (keggcpd C05981)\
   (synonyms \"Phosphatidylinositol-3,4,5P \" ))" .

  eq mdPlcg = "(\
  (spname PIG1_HUMAN)\
  (spnumber P19174)\
  (hugosym PLCG1)\
  (synonyms \"1-phosphatidylinositol-4,5-bisphosphate phosphodiesterase gamma 1\" \
            \"EC 3.1.4.11 \" \
            \"Phosphoinositide phospholipase C \" \
            \"PLC-gamma-1 \" \
            \"Phospholipase C-gamma-1 \" \
            \"PLC-II,  PLC-148, PLC1 \"))"  .

  op pip3Sexp : -> Sexp .
  ceq pip3Sexp =   (if f:FindResult :: Nat and sexp?:Sexp? :: Sexp
                   then sexp?:Sexp?
                   else [nil] fi)
  if ixs(f:FindResult, sexp?:Sexp?) := readSexp(mdPIP3,0,length(mdPIP3)) .

  op plcgSexp : -> Sexp .
  ceq plcgSexp =   (if f:FindResult :: Nat and sexp?:Sexp? :: Sexp
                   then sexp?:Sexp?
                   else [nil] fi)
  if ixs(f:FindResult, sexp?:Sexp?) := readSexp(mdPlcg,0,length(mdPlcg)) .




endfm



***(

  ceq mkComponentInfoExpX(gname,modname,clab,thT)
         =  mkCInfoExp(gname,clab,cspecl,sexp)
  if sxv(ispec) := getVal(es,'sxval,'ComponentInfoSpec,sxv([nil]))
  /\ lsort := leastSort([modname],thT)
  /\ cspecl := (if lsort :: Type then getCSpecL(sexp2l(ispec),lsort) else [nil] fi)
  /\ str := getOpMetaData(modname,getBaseName(thT)) 
  /\ ixs(next,sexp?) := readSexp(str,0,length(str)) 
  /\ sexp := (if sexp? :: Sexp then sexp? else [nil] fi) .

red getCSpecL(sexp2l(cispec),'QQQ,'Chemical) .
red getCSpecL(sexp2l(cispec),'QQQ,'Protein) .

red mkCInfoExp('g4,'PIP3-CLm,getCSpecL(sexp2l(cispec),'QQQ,'Chemical),pip3Sexp) .

(apply displayComponentInfo "g4" "PIP3-CLm" (array java.lang.Object (array java.lang.String "link" "KEGG" "C05981" "http://www.genome.jp/dbget-bin/www_bget?compound+C05981")(array java.lang.Object "list" "Synonyms" ident (array java.lang.String  "Phosphatidylinositol-3,4,5P ") )))

red mkCInfoExp('g4,'Plcg1-CLc,getCSpecL(sexp2l(cispec),'QQQ,'Protein),plcgSexp) .

(apply displayComponentInfo "g4" "Plcg1-CLc" (array java.lang.Object (array java.lang.String "val" "HUGO" "PLCG1")(array java.lang.String "link" "SwissProt" "P19174" "http://www.expasy.ch/cgi-bin/niceprot.pl?P19174")(array java.lang.Object "list" "Synonyms" ident (array java.lang.String  "1-phosphatidylinositol-4,5-bisphosphate phosphodiesterase gamma 1" "EC 3.1.4.11 " "Phosphoinositide phospholipase C " "PLC-gamma-1 " "Phospholipase C-gamma-1 " "PLC-II,  PLC-148, PLC1 ") )))

red mkComponentInfoExpX(cispec, 'g4,'QQQ,'PIP3-CLm,'PIP3.Chemical) .

red mkComponentInfoExpX(cispec, 'g4,'QQQ,'Plcg1-CLc,'Plcg1.Protein) .

)

