fmod PNET2SBML-TEMPLATES is
  inc TEMPLATES .

**** id's can contain only letters, underscore, digits 
**** id's and must begin with letter or underscore 
**** names are xml strings, ampersand, dquote and sqoute must be xescapted 

op speciesSBMLTemplate : -> String .
eq speciesSBMLTemplate =
"<species id=%id name=%name compartment=%loc initialConcentration=%init/>" .

  ops reactionSBMLTemplate mreactionSBMLTemplate : -> String .
  eq reactionSBMLTemplate =
"<reaction id=%id name=%name>\n\
  <listOfReactants>\n\
    %reactants\n\
  </listOfReactants>\n\
  <listOfProducts>\n\
    %products\n\
  </listOfProducts>\n\
</reaction>" .


eq mreactionSBMLTemplate =
"<reaction id=%id name=%name>\n\
  <listOfReactants>\n\
    %reactants\n\
  </listOfReactants>\n\
  <listOfProducts>\n\
    %products\n\
  </listOfProducts>\n\
  <listOfModifiers>\n\
    %modifiers\n\
  </listOfModifiers>\n\
</reaction>" .


op pnetSBMLTemplate : -> String .
eq pnetSBMLTemplate =
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n\
<sbml level=\"2\" version=\"1\" xmlns=\"http://www.sbml.org/sbml/level2\">\n\
<model id=%modelid name=%modelname >\n\
  <listOfCompartments>\n\
    <compartment id=\"unknown\" name=\"unrecognized locate\"/>\n\
    <compartment id=\"out\" name=\"outside the cell\"/>\n\
    <compartment id=\"cytosol\" size=\"2.5\"/>\n\
    <compartment id=\"mitochondria\" size=\"0.3\"/>\n\
    <compartment id=\"CLo\" name=\"Cell out\" />\n\
    <compartment id=\"CLm\" name=\"Cell membrane\" />\n\
    <compartment id=\"CLi\" name=\"Cell in\" />\n\
    <compartment id=\"CLc\" name=\"Cell cytosol\" />\n\
    <compartment id=\"NUo\" name=\"Nucleus out\" />\n\
    <compartment id=\"NUm\" name=\"Nucleus membrane\" />\n\
    <compartment id=\"NUi\" name=\"Nucleus in\" />\n\
    <compartment id=\"NUc\" name=\"Nucleus cytosol\" />\n\
    <compartment id=\"MOo\" name=\"Mitochondria Outer out\" />\n\
    <compartment id=\"MOm\" name=\"Mitochondria Outer membrane\" />\n\
    <compartment id=\"MOi\" name=\"Mitochondria Outer in\" />\n\
    <compartment id=\"MOc\" name=\"Mitochondria Outer cytosol\" />\n\
    <compartment id=\"MIo\" name=\"Mitochondria Inner out\" />\n\
    <compartment id=\"MIm\" name=\"Mitochondria Inner membrane\" />\n\
    <compartment id=\"MIi\" name=\"Mitochondria Inner in\" />\n\
    <compartment id=\"MIc\" name=\"Mitochondria Inner cytosol\" />\n\
    <compartment id=\"ERo\" name=\"EndoplasmicReticulum out\" />\n\
    <compartment id=\"ERm\" name=\"EndoplasmicReticulum membrane\" />\n\
    <compartment id=\"ERi\" name=\"EndoplasmicReticulum in\" />\n\
    <compartment id=\"ERc\" name=\"EndoplasmicReticulum cytosol\" />\n\
    <compartment id=\"PXo\" name=\"PeroXisome out\" />\n\
    <compartment id=\"PXm\" name=\"PeroXisome membrane\" />\n\
    <compartment id=\"PXi\" name=\"PeroXisome in\" />\n\
    <compartment id=\"PXc\" name=\"PeroXisome cytosol\" />\n\
    <compartment id=\"GAo\" name=\"Golgi Apparatus out\" />\n\
    <compartment id=\"GAm\" name=\"Golgi Apparatus membrane\" />\n\
    <compartment id=\"GAi\" name=\"Golgi Apparatus in\" />\n\
    <compartment id=\"GAc\" name=\"Golgi Apparatus cytosol\" />\n\
    <compartment id=\"LEo\" name=\"Late Endosome out\" />\n\
    <compartment id=\"LEm\" name=\"Late Endosome membrane\" />\n\
    <compartment id=\"LEi\" name=\"Late Endosome in\" />\n\
    <compartment id=\"LEc\" name=\"Late Endosome cytosol\" />\n\
    <compartment id=\"EEo\" name=\"Early Endosome out\" />\n\
    <compartment id=\"EEm\" name=\"Early Endosome membrane\" />\n\
    <compartment id=\"EEi\" name=\"Early Endosome in\" />\n\
    <compartment id=\"EEc\" name=\"Early Endosome cytosol\" />\n\
    <compartment id=\"LYo\" name=\"Lysosome out\" />\n\
    <compartment id=\"LYm\" name=\"Lysosome membrane\" />\n\
    <compartment id=\"LYi\" name=\"Lysosome in\" />\n\
    <compartment id=\"LYc\" name=\"Lysosome cytosol\" />\n\
    <compartment id=\"CPo\" name=\"Clathrin Coated Pits out\" />\n\
    <compartment id=\"CPm\" name=\"Clathrin Coated Pits membrane\" />\n\
    <compartment id=\"CPi\" name=\"Clathrin Coated Pits in\" />\n\
    <compartment id=\"CPc\" name=\"Clathrin Coated Pits cytosol\"/>\n\
    <compartment id=\"Sig\" name=\"a place for signatures\"/>\n\
  </listOfCompartments>\n\
  <listOfSpecies>\n\
    %specieslist\n\
  </listOfSpecies>\n\
  <listOfReactions>\n\
    %reactionlist\n\
  </listOfReactions>\n\
</model>\n\
</sbml>\n\
</xml>"
.
endfm



fmod GRAPH-SBML is
  inc PNET2SBML-TEMPLATES .
  inc GRAPH .
  inc SVAL .
  inc NAT-QID .

  vars  ?string : String .
  vars id name init loc : String .
  vars  reactions species reaction reactants products modifiers : String .
  vars specieslist reactionlist : String .
  var n : Nat .
  var notes : Notes .
  vars nodes occNodes ruleNodes : NodeList .
  var edges : EdgeList .
  var ns preNats postNats inats onats bnats : NatList .

**** op decls
***(
  op mkSBMLNetStr     : Graph  -> String .

  op mkSBMLSpeciesStr  : NodeList String -> String .
  op mkSpecies : String String String String -> String .

  op mkSBMLReactionsStr : NodeList String -> String .
  op mkReaction : String String String String -> String .
  op mkmReaction : String String String String String -> String .
  op mkSpeciesRef : String -> String .
  op mkmSpeciesRef : String -> String .

  op mkPnetSBML : String String String String -> String  .
  ops mkSBMLSpeciesRefStr mkmSBMLSpeciesRefStr : NatList -> String .
)

  op mkReaction : String String String String -> String .
  eq mkReaction(id,name,reactants,products) =
      fillTemplate(reactionSBMLTemplate,
                   (("%id" :~ squote(id)),
                    ("%name" :~ squote(name)),
                    ("%reactants" :~ reactants),
                    ("%products" :~ products)) ) .

  op mkmReaction : String String String String String -> String .
  eq mkmReaction(id,name,reactants,products,modifiers) =
      fillTemplate(mreactionSBMLTemplate,
		   (("%id" :~ squote(id)),
		    ("%name" :~ squote(name)),
		    ("%reactants" :~ reactants),
		    ("%products" :~ products),
		    ("%modifiers" :~ modifiers)) ) .

  op mkSBMLReactionsStr : NodeList String -> String .
  eq mkSBMLReactionsStr(nil,reactions) = reactions + "\n"  .
  ceq mkSBMLReactionsStr(nd(n,notes) nodes,reactions) =
       mkSBMLReactionsStr(nodes, reactions  + "\n    " + reaction)
  if id := "t" + string(n,10)
  /\ sv(name) := lookupd(notes,"chattylabel",sv(id))
  /\ preNats := str2natl(svalString(lookupd(notes,"pre",sv(""))))
  /\ postNats := str2natl(svalString(lookupd(notes,"post",sv(""))))
  /\ inats := lDiff(preNats,postNats)
  /\ onats := lDiff(postNats,preNats)
  /\ bnats := lDiff(preNats,inats)
  /\ reactants := mkSBMLSpeciesRefStr(inats)
  /\ products := mkSBMLSpeciesRefStr(onats)
  /\ modifiers := mkmSBMLSpeciesRefStr(bnats)
  /\ reaction := (if bnats == nil
                   then mkReaction(id,name,reactants,products)
                   else mkmReaction(id,name,reactants,products,modifiers)
                   fi) .

  op mkSpeciesRef : String -> String .
  eq mkSpeciesRef(id) = "<speciesReference species=" + squote(id) + "/>" .

  op mkmSpeciesRef : String -> String .
  eq mkmSpeciesRef(id) = 
    "<modifierSpeciesReference species=" + squote(id) + "/>" .

  ops mkSBMLSpeciesRefStr mkmSBMLSpeciesRefStr : NatList -> String .
  eq mkSBMLSpeciesRefStr(nil) = "" .
  eq mkSBMLSpeciesRefStr(n ns) = 
        mkSpeciesRef("o" + string(n,10)) + "\n    " +  mkSBMLSpeciesRefStr(ns) .
  eq mkmSBMLSpeciesRefStr(nil) = "" .
  eq mkmSBMLSpeciesRefStr(n ns) =
      mkmSpeciesRef("o" + string(n,10)) + "\n    " +  mkmSBMLSpeciesRefStr(ns) .


  op mkSpecies : String String String String -> String .
  eq mkSpecies(id,name,init,loc) =
      fillTemplate(speciesSBMLTemplate,
                   (("%id" :~ squote(id)),
                    ("%name" :~ squote(name)),
                    ("%init" :~ squote(init)),     **** 0 or 1
                    ("%loc" :~ squote(loc))) ) .

  op mkSBMLSpeciesStr  : NodeList String -> String .
  eq mkSBMLSpeciesStr(nil,species) = species .
  ceq mkSBMLSpeciesStr(nd(n,notes) nodes,species) =
       mkSBMLSpeciesStr(nodes,
           species + "\n    " + mkSpecies(id,name,init,loc)) 
  if id := "o" + string(n,10)
  /\ sv(name) := lookupd(notes,"chattylabel", lookupd(notes,"label",sv(id)))
  /\  init := (if lookupd(notes,"init",sv("false")) == sv("true")
               then "1"
               else "0"
               fi)
  /\ sv(loc) :=  lookupd(notes,"loc",sv("")) .

  op mkPnetSBML : String String String String -> String  .
  eq mkPnetSBML(id,name,specieslist,reactionlist) =
      fillTemplate(pnetSBMLTemplate,
		   (("%modelid" :~ squote(id)),
		    ("%modelname" :~ squote(name)),
		    ("%specieslist" :~ specieslist),
		    ("%reactionlist" :~ reactionlist)) ) .

  op mkSBMLNetStr     : Graph  -> String .
  ceq mkSBMLNetStr(graph(?string,notes,nodes,edges)) =
         mkPnetSBML(?string,?string,species,reactions)
   if occNodes := getNodes(nodes,"type",sv("occ"))
   /\ ruleNodes := getNodes(nodes,"type",sv("rule"))
   /\ species := mkSBMLSpeciesStr(occNodes,"") 
   /\ reactions := mkSBMLReactionsStr(ruleNodes, "")
  .

endfm

fmod TEST-GRAPH-SBML is
  inc GRAPH-SBML .



op tinySbml : -> String .
eq tinySbml = mkPnetSBML("tinySbml","tinySbml",
       mkSpecies("n0","EGF-out","1","out") + "\n    " +
       mkSpecies("n1","EgfR-CLm","1","CLm") + "\n    " +
       mkSpecies("n2","EGF-CLo","0","CLo") + "\n    " +
       mkSpecies("n3","EgfR-act-CLm","0","CLm") + "\n    " +
       mkSpecies("n4","Grb2-CLc","1","CLc") + "\n    " +
       mkSpecies("n5","Grb2-CLi","0","CLc"), 
       mkReaction("t0","1.EgfR.on",
                  mkSpeciesRef("n0") +  "\n    " + mkSpeciesRef("n1"),
                  mkSpeciesRef("n2") +  "\n    " + mkSpeciesRef("n3")) + "\n"
       +
       mkmReaction("t1","761.Grb2.by.EgfR",
                  mkSpeciesRef("n4"),
                  mkSpeciesRef("n5"),
                  mkmSpeciesRef("n3"))  
       )  .

endfm


***(

)
