{
  "version": "https://jsonfeed.org/version/1.1",
  "title": "chem-bla-ics",
  "description": "Chemblaics (pronounced chem-bla-ics) is the science that uses open science and computers to solve problems in chemistry, biochemistry and related fields.",
  "home_page_url": "https://chem-bla-ics.linkedchemistry.info/",
  "feed_url": "https://chem-bla-ics.linkedchemistry.info/2013/10/09/programming-in-life-sciences-4.json",
  "icon": "https://chem-bla-ics.linkedchemistry.info/assets/images/chem-bla-ics_logo.png",
  "language": "en",
  "authors": [
    {
      "name": "Egon Willighagen",
      "url": "https://orcid.org/0000-0001-7542-0286",
      "_orcid": "0000-0001-7542-0286"
    }
  ],
  "items": [

    {
      "id": "https://doi.org/10.59350/7ex09-4x603",
      "url": "https://chem-bla-ics.linkedchemistry.info/2013/10/09/programming-in-life-sciences-4.html",
      "title": "Programming in the Life Sciences #4: communication from within HTML",
      "content_html": "<p>The purpose of a web service is that you give it a question or task, and that it returns an answer. For example, we can ask the\n<a href=\"http://www.openphacts.org/\">Open PHACTS</a> platform what compounds it knows with aspirin in the name. We pass the question (with the\n<a href=\"http://chem-bla-ics.blogspot.nl/2013/10/programming-in-life-sciences-2-accounts.html\">API key</a>) and get a list of matching compounds.\nNow, this communication is complex: it happens at many levels, which are spelled out in the\n<a href=\"https://en.wikipedia.org/wiki/Internet_model\">Internet Model</a>. There are various variants of the stack of communication layers,\nbut we are interested mostly in the top layers, at the <em>application layer</em>. In fact, for this course this model only serves as\nsupporting information for those who want to learn more.</p>\n\n<p>Practically, what matters here is how to ask the question and how to understand the answer.</p>\n\n<p>We are supported in these practicalities with JavaScript libraries, in particular the <a href=\"https://github.com/openphacts/ops.js\">ops.js</a>\nlibrary and general <a href=\"https://en.wikipedia.org/wiki/JSON\">JSON</a> functionality provided by most browsers (unless the student decided to use\na <em>different</em> programming language, in which there are different libraries). Personally, I have only very limited JavaScript experience,\nand this mostly goes back to the good old <a href=\"http://www.biomedcentral.com/1471-2105/8/487\">Userscript and Greasemonkey days</a> (wow! the\npaper is actually the <a href=\"http://www.altmetric.com/details.php?citation_id=103983\">4th highest scoring BMC Bioinformatics article!</a>).\nBut because my JavaScript knowledge is limited and rusty, I spent a good part of today, to get a basic example running. Very basic,\nand barely exceeding the communication details. That is, this is the output in the browser:</p>\n\n<p><img src=\"/assets/images/mcspils_jsonOutput.png\" alt=\"\" /></p>\n\n<p>So, what does the question look like? The question is actually hardcoded in the HTML source, but the page does take two parameters:\nthe <code class=\"language-plaintext highlighter-rouge\">app_key</code> and <code class=\"language-plaintext highlighter-rouge\">app_id</code> that come <a href=\"http://chem-bla-ics.blogspot.nl/2013/10/programming-in-life-sciences-2-accounts.html\">with your Open PHACTS account</a>.</p>\n\n<p>The ops.js library helps us, and wraps the Open PHACTS LDA methods in JavaScript methods. Thus, rather can crafting special HTTP calls,\nwe use two JavaScript calls:</p>\n\n<div class=\"language-javascript highlighter-rouge\"><div class=\"highlight\"><pre class=\"highlight\"><code><span class=\"kd\">var</span> <span class=\"nx\">searcher</span> <span class=\"o\">=</span> <span class=\"k\">new</span> <span class=\"nx\">Openphacts</span><span class=\"p\">.</span><span class=\"nc\">ConceptWikiSearch</span><span class=\"p\">(</span>\n  <span class=\"dl\">\"</span><span class=\"s2\">https://beta.openphacts.org</span><span class=\"dl\">\"</span><span class=\"p\">,</span>\n  <span class=\"nx\">params</span><span class=\"p\">[</span><span class=\"dl\">\"</span><span class=\"s2\">app_id</span><span class=\"dl\">\"</span><span class=\"p\">],</span> <span class=\"nx\">params</span><span class=\"p\">[</span><span class=\"dl\">\"</span><span class=\"s2\">app_key</span><span class=\"dl\">\"</span><span class=\"p\">]</span>\n<span class=\"p\">);</span>\n<span class=\"nx\">searcher</span><span class=\"p\">.</span><span class=\"nf\">byTag</span><span class=\"p\">(</span>\n  <span class=\"dl\">'</span><span class=\"s1\">Aspirin</span><span class=\"dl\">'</span><span class=\"p\">,</span> <span class=\"dl\">'</span><span class=\"s1\">20</span><span class=\"dl\">'</span><span class=\"p\">,</span> <span class=\"dl\">'</span><span class=\"s1\">4</span><span class=\"dl\">'</span><span class=\"p\">,</span> <span class=\"dl\">'</span><span class=\"s1\">07a84994-e464-4bbf-812a-a4b96fa3d197</span><span class=\"dl\">'</span><span class=\"p\">,</span>\n  <span class=\"nx\">callback</span>\n<span class=\"p\">);</span>\n</code></pre></div></div>\n\n<p>The first statement creates an LDA method object, while the second makes an actual question. I have not defined the callback variable,\nwhich actually is a JavaScript function that looks like:</p>\n\n<div class=\"language-javascript highlighter-rouge\"><div class=\"highlight\"><pre class=\"highlight\"><code><span class=\"kd\">var</span> <span class=\"nx\">callback</span> <span class=\"o\">=</span> <span class=\"kd\">function</span><span class=\"p\">(</span><span class=\"nx\">success</span><span class=\"p\">,</span> <span class=\"nx\">status</span><span class=\"p\">,</span> <span class=\"nx\">response</span><span class=\"p\">){</span>\n  <span class=\"kd\">var</span> <span class=\"nx\">result</span> <span class=\"o\">=</span> <span class=\"nx\">searcher</span><span class=\"p\">.</span><span class=\"nf\">parseResponse</span><span class=\"p\">(</span><span class=\"nx\">response</span><span class=\"p\">);</span>\n  <span class=\"nb\">document</span><span class=\"p\">.</span><span class=\"nf\">getElementById</span><span class=\"p\">(</span><span class=\"dl\">\"</span><span class=\"s2\">output</span><span class=\"dl\">\"</span><span class=\"p\">).</span><span class=\"nx\">innerHTML</span> <span class=\"o\">=</span>\n    <span class=\"dl\">\"</span><span class=\"s2\">Results: </span><span class=\"dl\">\"</span> <span class=\"o\">+</span> <span class=\"nx\">JSON</span><span class=\"p\">.</span><span class=\"nf\">stringify</span><span class=\"p\">(</span><span class=\"nx\">result</span><span class=\"p\">);</span>\n<span class=\"p\">};</span>\n</code></pre></div></div>\n\n<p>When the LDA web service returns data, this method gets called, providing asynchronous functionality to keep the web page responsive.\nBut when called, it first parses the returned data, and then puts the JSON output as text in the HTML. The output that is given in\nthe earlier screenshot.</p>\n\n<p>So, hurdle taken. From here on it’s easier. Regular looping over the results, creating some HTML output, etc. The\n<a href=\"https://gist.github.com/egonw/6902776\">full source code</a> if this example is available as Gist.</p>\n\n<h4>References</h4>\n<div class=\"csl-bib-body\">\n    <div class=\"csl-entry\">Willighagen, E. L., O’Boyle, N. M., Gopalakrishnan, H., Jiao, D., Guha, R., Steinbeck, C., &#38; Wild, D. J. (2007). Userscripts for the Life Sciences. <i>BMC Bioinformatics</i>, <i>8</i>(1). https://doi.org/10.1186/1471-2105-8-487 <a href=\"https://doi.org/10.1186/1471-2105-8-487\">CrossRef</a> <a href=\"https://qlever.scholia.wiki/doi/10.1186/1471-2105-8-487\">Scholia</a></div>\n  </div>",
      "summary": "The purpose of a web service is that you give it a question or task, and that it returns an answer. For example, we can ask the Open PHACTS platform what compounds it knows with aspirin in the name. We pass the question (with the API key) and get a list of matching compounds. Now, this communication is complex: it happens at many levels, which are spelled out in the Internet Model. There are various variants of the stack of communication layers, but we are interested mostly in the top layers, at the application layer. In fact, for this course this model only serves as supporting information for those who want to learn more.",
      "image": "https://chem-bla-ics.linkedchemistry.info/assets/images/mcspils_jsonOutput.png",
      "date_published": "2013-10-09T00:00:00+00:00",
      "date_modified": "2025-02-16T00:00:00+00:00",
      "tags": ["pra3006","javascript","html","openphacts"],
      "_references": [
        
          
          
            { "url": "https://doi.org/10.1186/1471-2105-8-487", "doi": "10.1186/1471-2105-8-487"
             }
            
          
        ],
      
      
      
      
      
      
        "authors": [ { "name": "Egon Willighagen", "url": "https://orcid.org/0000-0001-7542-0286" } ]
      
    }

  ]
}
