1# 2# @file translateMath.R 3# @brief Translates infix formulas into MathML and vice-versa 4# @author Frank Bergmann 5# 6# 7# <!-------------------------------------------------------------------------- 8# This sample program is distributed under a different license than the rest 9# of libSBML. This program uses the open-source MIT license, as follows: 10# 11# Copyright (c) 2013-2018 by the California Institute of Technology 12# (California, USA), the European Bioinformatics Institute (EMBL-EBI, UK) 13# and the University of Heidelberg (Germany), with support from the National 14# Institutes of Health (USA) under grant R01GM070923. All rights reserved. 15# 16# Permission is hereby granted, free of charge, to any person obtaining a 17# copy of this software and associated documentation files (the "Software"), 18# to deal in the Software without restriction, including without limitation 19# the rights to use, copy, modify, merge, publish, distribute, sublicense, 20# and/or sell copies of the Software, and to permit persons to whom the 21# Software is furnished to do so, subject to the following conditions: 22# 23# The above copyright notice and this permission notice shall be included in 24# all copies or substantial portions of the Software. 25# 26# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 27# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 28# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 29# THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 30# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 31# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 32# DEALINGS IN THE SOFTWARE. 33# 34# Neither the name of the California Institute of Technology (Caltech), nor 35# of the European Bioinformatics Institute (EMBL-EBI), nor of the University 36# of Heidelberg, nor the names of any contributors, may be used to endorse 37# or promote products derived from this software without specific prior 38# written permission. 39# ------------------------------------------------------------------------ --> 40# 41# Usage: R --slave -f translateMath.R 42# 43# 44 45library(libSBML) 46 47# Utility function to read a line from stdin 48# 49getline <- function() { 50 f <- file("stdin") 51 open(f) 52 line = readLines(f,n=1) 53 close(f) 54 return (line) 55} 56 57# Utility function to trim the string 58trim <- function(str) { 59 return(gsub("(^ +)|( +$)", "", str)) 60} 61 62# 63# Translates the given infix formula into MathML. 64# 65# @return the MathML as a string. The caller owns the memory and is 66# responsible for freeing it. 67# 68translateInfix <- function(formula) { 69 70 math = parseFormula(formula); 71 result = writeMathMLToString(math); 72 73 return (result); 74} 75 76 77# 78# Translates the given MathML into an infix formula. The MathML must 79# contain no leading whitespace, but an XML header is optional. 80# 81# @return the infix formula as a string. The caller owns the memory and 82# is responsible for freeing it. 83# 84translateMathML <- function(xml) { 85 # 86 # Prepend an XML header if not already present. 87 # 88 if (substring(trim(xml),1, 2) != '<?') { 89 header = "<?xml version='1.0' encoding='UTF-8'?>\n"; 90 math = readMathMLFromString(paste(header, xml)); 91 return( formulaToString(math)) 92 } else { 93 math = readMathMLFromString(xml); 94 return( formulaToString(math)) 95 } 96} 97 98 99 100 101cat( "\n" ); 102cat( "This program translates infix formulas into MathML and\n" ); 103cat( "vice-versa. An 'enter' or a 'return' on an empty line\n" ); 104cat( "triggers translation. Ctrl-C quits\n" ); 105cat( "\n" ); 106 107while (TRUE) { 108 cat( "Enter an infix formula or MathML expression (Ctrl-C to quit):\n" ); 109 cat( "\n" ); 110 cat( "> " ); 111 112 buffer = "" 113 114 repeat { 115 line = trim(getline()); 116 len = nchar(line); 117 118 if (len > 0) { 119 buffer = paste(buffer, line ,"\n", sep=""); 120 } else { 121 if(substring(trim(buffer), 1,1) == "<") { 122 result = translateMathML(buffer) 123 } else { 124 result = translateInfix(buffer) 125 } 126 cat("Result:\n\n",result,"\n\n\n"); 127 128 buffer = "" 129 break; 130 } 131 } 132} 133 134q(status=0); 135 136 137