{
  "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/23/exercise-what-variable-type-would-you.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/7yb0k-mwq04",
      "url": "https://chem-bla-ics.linkedchemistry.info/2013/10/23/exercise-what-variable-type-would-you.html",
      "title": "Programming in the Life Sciences #7: theory",
      "content_html": "<p>No course, with some good theory. In <a href=\"http://chem-bla-ics.blogspot.nl/2013/10/programming-in-life-sciences-1-six-day.html\">this six-day course</a>,\nI plan to cover this computing theory. It’s very practice oriented:</p>\n\n<p><img src=\"/assets/images/theorySlide.png\" alt=\"\" /></p>\n\n<p>That should give them enough head start to work on something <a href=\"http://chem-bla-ics.blogspot.nl/2013/10/programming-in-life-sciences-5.html\">like this</a>.\nThe material will be more extensive, but I’ll give myself a head start, with some initial content.</p>\n\n<h2 id=\"introduction\">Introduction</h2>\n<p>Programming in the Life Sciences is done to solve problems in the life sciences, but only\nproblems that can be solved with pen and paper too. Programming cannot measure metabolites\nin a cell. For that, you need equipment that gives the things it measured as data as input to\nthe computer.</p>\n\n<p>Instead, the program defines some computation that is done on the computer. For example, noise\nreduction, DNA/RNA/protein sequence alignment, metabolite identification, etc. But all\ncomputation start with input data.</p>\n\n<p>The program tells the computer what it should do, step by step. Get the data from the LC/MS; find\npeaks; group peaks at the same retention time; match that against a metabolite spectral database;\ndetermine the best match; report the best three matches to the user via the screen. Step by step.</p>\n\n<p>The computer consists of input/output devices (to get data; to present results), various kinds of\nmemory (to remember things), and a central processing unit (CPU) that performs the computation\nsteps.</p>\n\n<p>Considering all this, programming is to define what the computer should do, in a (programming)\nlanguage that the computer understands. Note that I say “the computer understands” rather than\n“the CPU understands”. The CPU only speaks one language (machine instructions). But we use a\nhigher level programming language, which is much more compact and easier to read/understand. A\ncompiler translate this higher level language into machine instructions (sometimes more\ncompilers).</p>\n\n<h2 id=\"data-types\">Data Types</h2>\n<p>The programming language says do this, do that. It does not know about data. Fortunately, it\nknows about bit, and bits we can use to store data. That way, we can instruct the CPU to do\nthings like: OK, take the measured LC/MS data, take the MS at retention time 5, then start with\nthe first m/z value, and if that is larger than 10, then… etc. We do not want to hard code the\ndata in our program, so we instruct the CPU to remember it. The computer has various levels of\nmemory that are relevant (ignoring those at a CPU level!): variables stored in the working\nmemory, and data stored on external memory (hard disk, USB disk, LC/MS machine).</p>\n\n<p><em>Exercise: write a program that counts the sum of all numbers starting with 1 up to 50 without\nusing variables.</em></p>\n\n<p>Some programming languages have variables types. This variable is a non-integer number, this\nvariable is a text string. This ensures that you cannot sum up “cat” with 5.3. This is called\nvariable typing. Some programming language have hard typing (types are defined in the source\ncode), while others have dynamic typing (the program figures it out when it is compiling), and\nsome even no typing at all (the computer will complain when it runs).</p>\n\n<p>Example basic variable types include: string, integers, floats, and booleans. Strings can be used\nto remember names; integers are needed for counts and iterations (how many m/z values did I\nalready look at again??), and floats are needed for pretty much all scientific data. A boolean is\na yes/no type, or true/false.</p>\n\n<p>Also, variables do not have units. Remember those high school days? <em>“John, six WHAT??”</em>, <em>“Umm,\nsix mole, sir.”</em> Variables do not have units. Thus while you cannot calculate the sum of “cat”\nand 5.3, a computer has no problem calculating the sum of six mole and three days.</p>\n\n<h2 id=\"complex-types\">Complex Types</h2>\n\n<p><em>Exercise: What variable type would you use for that photo you took last week of that western blot?</em></p>\n\n<p>It is clear that these basic types don’t suffice. This touches on the topic of computer\nrepresentation. How does a computer keep a western blot in memory? That photo you tool with\nyour Android digitized the western blot into a matrix of numbers: if it was a greyscale photo,\nthen a single integer per position.</p>\n\n<p>Programming languages have various complex types, though most even support the definition of even\nmore complicated data structures. But the more basic complex types first: list. A list, vector,\nor array all refer to the same concept: a list of variables, typically of the same type. For\nexample, a mathematical vector is a list of floats (e.g. <code class=\"language-plaintext highlighter-rouge\">float[]</code> in JavaScript, where the\n<code class=\"language-plaintext highlighter-rouge\">[]</code> refers to the list or array nature). A string, actually, which we marked as a “basic”\nvariable type, is really a complex one too: it is a list of characters. That is, the string “cat”\nis a list of three characters. Importantly, each item in the list has an index, and the full list\nhas a length. Depending on the programming language, the first item in the list has index 0 or 1.</p>\n\n<p>As said, a list typically contains variables of the same type, just because it is easier to work\nwith. But the list can contain complex types too. For example, we can create a list of lists (we\nwould write <code class=\"language-plaintext highlighter-rouge\">float[][]</code>). Each element in the top list is a list again; that is, the first\nelement of the outer list is again a list. This matches vary closes the mathematical matrix.</p>\n\n<p>A second complex type important in this course is the map. A map is basically a list of key-value\npairs, where they keys take the role of the index in lists. Instead of asking for the list item\nwith index 7, we ask for the value behind a certain key. And, like we could make a list of lists,\nwe can also make a map of maps, etc. Keep this in mind! We will use this extensively in this\ncourse.</p>\n\n<h2 id=\"automation\">Automation</h2>\n<p>Now that we know how the CPU uses memory, we turn back to what the processor must do, according\nto our program. First, I mentioned the step by step at the start. This is critical: the processor\nhas a linear progression through the steps it must do. I can only go forward, and only step by\nstep. It cannot go back. Yet, that is exactly what we write in a for-loop, like in this four line\nJavaScript example:</p>\n\n<div class=\"language-javascript highlighter-rouge\"><div class=\"highlight\"><pre class=\"highlight\"><code><span class=\"kd\">var</span> <span class=\"nx\">sum</span> <span class=\"o\">=</span> <span class=\"mi\">0</span><span class=\"p\">;</span>\n<span class=\"k\">for </span><span class=\"p\">(</span><span class=\"kd\">var</span> <span class=\"nx\">i</span><span class=\"o\">=</span><span class=\"mi\">1</span><span class=\"p\">;</span> <span class=\"nx\">i</span><span class=\"o\">&lt;</span><span class=\"mi\">50</span><span class=\"p\">;</span> <span class=\"nx\">i</span><span class=\"o\">=</span><span class=\"mi\">1</span><span class=\"o\">+</span><span class=\"mi\">1</span><span class=\"p\">)</span> <span class=\"p\">{</span> \n  <span class=\"nx\">sum</span> <span class=\"o\">=</span> <span class=\"nx\">sum</span> <span class=\"o\">+</span> <span class=\"nx\">i</span><span class=\"p\">;</span>\n<span class=\"p\">}</span>\n</code></pre></div></div>\n\n<p>This code defines the variable sum in the first line, and then starts counting, from 1 to 50, one\nby one, and adding that number to the sum. This loop is only for our convenience. This is how the\ncomputer will run this program (and at a CPU machine instruction level it’s even longer):</p>\n\n<div class=\"language-javascript highlighter-rouge\"><div class=\"highlight\"><pre class=\"highlight\"><code><span class=\"kd\">var</span> <span class=\"nx\">sum</span> <span class=\"o\">=</span> <span class=\"mi\">0</span><span class=\"p\">;</span>\n<span class=\"kd\">var</span> <span class=\"nx\">i</span><span class=\"o\">=</span><span class=\"mi\">1</span><span class=\"p\">;</span>\n<span class=\"nx\">sum</span> <span class=\"o\">=</span> <span class=\"nx\">sum</span> <span class=\"o\">+</span> <span class=\"nx\">i</span><span class=\"p\">;</span>\n<span class=\"nx\">i</span><span class=\"o\">=</span><span class=\"nx\">i</span><span class=\"o\">+</span><span class=\"mi\">1</span><span class=\"p\">;</span>\n<span class=\"nx\">sum</span> <span class=\"o\">=</span> <span class=\"nx\">sum</span> <span class=\"o\">+</span> <span class=\"nx\">i</span><span class=\"p\">;</span>\n<span class=\"nx\">i</span><span class=\"o\">=</span><span class=\"nx\">i</span><span class=\"o\">+</span><span class=\"mi\">1</span><span class=\"p\">;</span>\n<span class=\"nx\">sum</span> <span class=\"o\">=</span> <span class=\"nx\">sum</span> <span class=\"o\">+</span> <span class=\"nx\">i</span><span class=\"p\">;</span>\n<span class=\"nx\">i</span><span class=\"o\">=</span><span class=\"nx\">i</span><span class=\"o\">+</span><span class=\"mi\">1</span><span class=\"p\">;</span>\n<span class=\"nx\">sum</span> <span class=\"o\">=</span> <span class=\"nx\">sum</span> <span class=\"o\">+</span> <span class=\"nx\">i</span><span class=\"p\">;</span>\n<span class=\"nx\">i</span><span class=\"o\">=</span><span class=\"nx\">i</span><span class=\"o\">+</span><span class=\"mi\">1</span><span class=\"p\">;</span>\n<span class=\"nx\">sum</span> <span class=\"o\">=</span> <span class=\"nx\">sum</span> <span class=\"o\">+</span> <span class=\"nx\">i</span><span class=\"p\">;</span>\n<span class=\"nx\">i</span><span class=\"o\">=</span><span class=\"nx\">i</span><span class=\"o\">+</span><span class=\"mi\">1</span><span class=\"p\">;</span>\n<span class=\"nx\">sum</span> <span class=\"o\">=</span> <span class=\"nx\">sum</span> <span class=\"o\">+</span> <span class=\"nx\">i</span><span class=\"p\">;</span>\n<span class=\"nx\">i</span><span class=\"o\">=</span><span class=\"nx\">i</span><span class=\"o\">+</span><span class=\"mi\">1</span><span class=\"p\">;</span>\n<span class=\"nx\">sum</span> <span class=\"o\">=</span> <span class=\"nx\">sum</span> <span class=\"o\">+</span> <span class=\"nx\">i</span><span class=\"p\">;</span>\n<span class=\"nx\">i</span><span class=\"o\">=</span><span class=\"nx\">i</span><span class=\"o\">+</span><span class=\"mi\">1</span><span class=\"p\">;</span>\n<span class=\"nx\">sum</span> <span class=\"o\">=</span> <span class=\"nx\">sum</span> <span class=\"o\">+</span> <span class=\"nx\">i</span><span class=\"p\">;</span>\n<span class=\"c1\">// ...</span>\n</code></pre></div></div>\n\n<p>OK, I won’t give the full sequence of steps the computer takes. I guess you can see the virtues\nof higher level programming languages :) Importantly, it is a linear list of steps it takes.</p>\n\n<p>Another important control structures in programming languages is the if-statement. This gives us\nthe power of making decisions. For example, we can skip the 7 in the above summation:</p>\n\n<div class=\"language-javascript highlighter-rouge\"><div class=\"highlight\"><pre class=\"highlight\"><code><span class=\"kd\">var</span> <span class=\"nx\">sum</span> <span class=\"o\">=</span> <span class=\"mi\">0</span><span class=\"p\">;</span>\n<span class=\"k\">for </span><span class=\"p\">(</span><span class=\"kd\">var</span> <span class=\"nx\">i</span><span class=\"o\">=</span><span class=\"mi\">1</span><span class=\"p\">;</span> <span class=\"nx\">i</span><span class=\"o\">&lt;</span><span class=\"mi\">50</span><span class=\"p\">;</span> <span class=\"nx\">i</span><span class=\"o\">=</span><span class=\"mi\">1</span><span class=\"o\">+</span><span class=\"mi\">1</span><span class=\"p\">)</span> <span class=\"p\">{</span>\n  <span class=\"k\">if </span><span class=\"p\">(</span><span class=\"nx\">i</span> <span class=\"o\">==</span> <span class=\"mi\">7</span><span class=\"p\">)</span> <span class=\"p\">{</span>\n  <span class=\"p\">}</span> <span class=\"k\">else</span> <span class=\"p\">{</span>\n     <span class=\"nx\">sum</span> <span class=\"o\">=</span> <span class=\"nx\">sum</span> <span class=\"o\">+</span> <span class=\"nx\">i</span><span class=\"p\">;</span>\n  <span class=\"p\">}</span>\n<span class=\"p\">}</span>\n</code></pre></div></div>\n\n<p>But I yet did not discuss another important concept: the operator. The operator tells the\ncomputer what operation to perform, and how. This last source code example uses various operators: <code class=\"language-plaintext highlighter-rouge\">=</code>, <code class=\"language-plaintext highlighter-rouge\">&lt;</code>, <code class=\"language-plaintext highlighter-rouge\">+</code>, and <code class=\"language-plaintext highlighter-rouge\">==</code>. The first is an assignment operator: it assigns the value ‘0’\nto the variable sum. This operation does not return anything. The <code class=\"language-plaintext highlighter-rouge\">&lt;</code> operator compares two\nvariable values, or a variable value with a specific value. For example, the above code compares\nthe value behind the ‘i’ variable with 50; indeed, it does not compare 50 with “i”, which is the\nvariable name. The + operator follows the mathematical + operator for floats and integers; for\nstrings the + operator performs a concatenation: <code class=\"language-plaintext highlighter-rouge\">\"cat\" + \"fish\"</code> is not one less fish, but a\n<code class=\"language-plaintext highlighter-rouge\">\"catfish\"</code>. Note that these two operators, &lt; and +, return a new value. The <code class=\"language-plaintext highlighter-rouge\">&lt;</code> returns a\nboolean (yes, it’s smaller; no, it’s not smaller); the <code class=\"language-plaintext highlighter-rouge\">+</code> returns an integer if it was summing\nintegers, or a string when it concatenated two strings. The <code class=\"language-plaintext highlighter-rouge\">==</code> operator also returns a boolean:\ntrue of the two variables are the same (in general). During the course, we will see several more\noperators. Look out for them!</p>\n\n<p>In some way, this brings us to the next topic: functions of parameters. An operator is a special\nkind of function, and that will become more clear if I give an example function:</p>\n\n<div class=\"language-javascript highlighter-rouge\"><div class=\"highlight\"><pre class=\"highlight\"><code><span class=\"kd\">function</span> <span class=\"nf\">add</span><span class=\"p\">(</span><span class=\"nx\">first</span><span class=\"p\">,</span> <span class=\"nx\">second</span><span class=\"p\">)</span> <span class=\"p\">{</span>\n  <span class=\"kd\">var</span> <span class=\"nx\">sum</span> <span class=\"o\">=</span> <span class=\"nx\">first</span> <span class=\"o\">+</span> <span class=\"nx\">second</span><span class=\"p\">;</span>\n  <span class=\"k\">return</span> <span class=\"nx\">sum</span><span class=\"p\">;</span>\n<span class=\"p\">}</span>\n</code></pre></div></div>\n\n<p>Effectively, we just made an alias function “add” which internally just uses the + operator, with\nthe exact same outcome.</p>\n\n<p><em>Exercise: what would be returned by these two function calls? 1. add(1,2); 2. add(“cat”,\n“fish”);</em></p>\n\n<p>This function example is not so interesting, and only makes the code harder to read. However,\nwhen the “body” of the function becomes larger, it allows you to easily replace a complex list\nof steps with one function call. Consider: <code class=\"language-plaintext highlighter-rouge\">sumAllNumbers(1,50)</code>.</p>\n\n<p>Now, if we collect many such functions, pretty much like books, we get a library. So, that one\nwas easy.</p>\n\n<p>That includes this episode of the <a href=\"http://chem-bla-ics.blogspot.nl/search/label/%23mscpils\">Programming in the Life Sciences</a>\nseries. I will continue later with the theory about Web Services and Clients, Serialization\nformats, and Other.</p>",
      "summary": "No course, with some good theory. In this six-day course, I plan to cover this computing theory. It’s very practice oriented:",
      "image": "https://chem-bla-ics.linkedchemistry.info/assets/images/theorySlide.png",
      "date_published": "2013-10-23T00:00:00+00:00",
      "date_modified": "2025-02-15T00:00:00+00:00",
      "tags": ["pra3006"],
      
      
      
      
      
      
        "authors": [ { "name": "Egon Willighagen", "url": "https://orcid.org/0000-0001-7542-0286" } ]
      
    }

  ]
}
