charlesreid1.com blog

Learning Bioinformatics with Go and Rosalind

Posted in Computational Biology

permalink

Learning Go with Rosalind

What is Rosalind?

Rosalind.info is a website with programming challenges, similar in spirit to Project Euler, but with a focus on bioinformatics.

Problems in the bioinformatics track are presented grouped by chapter, with several problems per chapter. The problems are designed like a coding competition, with problems providing structured input files and expecting structured output from each calculation. Each time you solve a problem, a unique input is generated, and you have a time limit in which to run your code to solve the problem.

What is Go?

Go is a programming language that is static, compiled, and concurrent. It is essentially intended as a modern replacement to C and C++, and is designed for more modern hardware, networks, and scale of code projects.

Go is a language that was invented in Google. It provides some very powerful features, and its design for concurrency is the primary feature that motivated me to learn Go. See this Go blog post on "Concurrency is not parallelism" for a more in-depth discussion of Go's concurrency design, and how that is different from (and more general and more powerful than) parallelism.

Initial Impression of Go

My initial impression of Go has been positive thus far.

Syntax

The first thing about Go that we will remark on is the syntax - it is unusual because it reverses the order that most programming languages use for variable and type declarations.

For example, in Java, you declare an integer array like this:

public int[5] arr;

while in Go, you would declare an analogous data structure like this:

var arr [5]int

While this reversal is a bit confusing at first, it is different enough from other languages that it easily sticks in your head - which is actually a welcome change from the stew of slightly-different-but-largely-overlapping syntax occurring across common programming languages.

Go also shares many of the features that make Python such an easy language to use, including lack of semicolons (yay!) and a useful selection of built-in methods.

Godoc

One of the handiest features of Go is godoc, a command-line utility that runs a local web server at http:8080 that serves up Go documentation. This includes documentation for the standard Go library, as well as documentation for any libraries found in the Go PATH.

Environment Variables

Speaking of the Go PATH, one confusing thing about getting started with Go is all of the environment variables that must be set for Go to find various things.

Basically, you need to have two directories for Go: the location of your Go tree, and the directory where you store Go packages and executables.

  • $GOROOT refers to the location of your Go tree. For example, this might be the directory ~/go.

  • $GOPATH refers to the location where your Go packages and executables are stored. For example, this might be ~/work.

Both of these variables should be set in your ~/.profile.

See Go/Installing on the charlesreid1 wiki for coverage of getting set up with Go.

Arrays and Slices

The first concept in Go that threw me off was the concept of arrays versus slices. This was principally due to the poor explanation and choice of examples given on the Go blog post on how slices work.

Arrays are chunks of memory that are set aside to store contiguous slots for data. To create an array, its size must be known in advance, making them less useful in practice. To resize an array, a new array must be created, and the old array copied into the new array.

Slices, on the other hand, can be thought of as tiny data structures containing pointers to array data. Slices can easily be resized by changing the data structure, without changing the underlying data.

There are also built-in make() and copy() functions, to allocate slices that have a specified capacity (or none, in which case the slice has a variable size) and copy data from arrays to slices or from slice to slice.

The confusion around such a fundamental data type makes Go more difficult to learn and to intuit about what's going on. While Go is clearly superior to C and Java in many ways, it also has some unfortunate stumbling blocks in its most basic data structure that many early learners are sure to have trouble with.

Learning a New Language

One of the things that stood out to me while learning Go was how different it was from learning a non-language systems technology (library) like Docker or docker-compose, or Ansible.

When you are learning a second or third programming language, you generally have a mental roadmap of concepts, and how they fit together and build upon each other. Furthermore, you already know, before ever starting to learn the language, what that roadmap of concepts generally looks like, from your prior learnings.

(If you pick up and compare any dozen books on various programming languages, you'll find certain core concepts that are repeated from book to book.)

Compare this with a technology like Ansible, an extremely powerful Python library that is used for automation of IT and compute infrastructure. While extremely powerful, Ansible is also extremely complex, and feels bewildering to learn because it requires all users (including seasoned experts in Unix shell scripting), to re-learn an entirely new way of doing things using Ansible's system of modules and YAML syntax.

Ansible has no conceptual roadmap. The Ansible documentation moves somewhat haphazardly through the many topics that must be covered but that don't fit together. Books that cover Ansible often follow the documentation's organization, or have a similarly confused organization of concepts. There is no denying it's a great piece of software, but it is very difficult to reason about and teach compared to a computer language.

Core Concepts

The first few Rosalind problems from Chapter 1 were an excellent warm-up to get to know Go, since the tasks were relatively easy and the main bit to work out was how to use the data structure correctly, rather than the algorithm.

Three data structures that we ended up utilizing to solve the Chapter 1 challenges using Go were:

  • Hash maps
  • Strings
  • Bit vectors

More coverage of how these data structures work in Go, plus details of how we used them to solve Rosalind probelms, to follow in the next blog post.

Tags:    go    golang    rosalind    computational biology    bioinformatics   

First Post of the Fall, Part 2: Flaskadillo

Posted in Python

permalink

Flask + ILLO = Flaskadillo

On October 15, 2018, I had the opportunity to offer an in-lab learning opportunity (ILLO) at the Lab for Data Intensive Biology. The ILLO focused on Flask, a useful Python library for creating and running web servers. This library is useful because it has a very low learning curve, but also has the complexity to handle complicated, real-world projects.

As a part of this in-lab learning opportunity, I created repository with five simple Flask examples to highlight five useful capabilities of Flask.

The repository is called flaskadillo and it is available on git.charlesreid1.com or on github.com.

The five capabilities covered by the examples in flaskadillo are listed below:

  1. hello - hello world flask server

  2. api - a simple API server

  3. jinja - a simple Flask server that makes use of Jinja templates

  4. package - a simple demonstration of how to package flask apps

  5. tests - a simple demonstration of how to write Flask tests

Example 1: Hello World

We'll just cover example 1 here, but similar materials are available for all five examples.

Example 1 consists of a simple flask app, simple.py:

from flask import Flask
app = Flask(__name__)

@app.route("/")
def hello():
    return "Hello World!"

The hello directory of the flaskadillo repo covers how to install the necessary packages and run the Flask application.

There is also a unit test, test_simple.py, which demonstrates how to write tests for Flask applications. To run the unit test, run:

pytest

More Information

For instructions on each of the 5 examples, visit each of the 5 directories in the flaskadillo repository.

Why flaskadillo?

Because armadillo.

Why armadillo?

The word armadillo means "little armoured one" in Spanish.

Armadillos are related to anteaters and sloths (all are in the Xenartha superorder).

The Aztecs called them turtle-rabbits.

Tags:    Github    Software    Python    Flask   

First Post of the Fall, Part 1: Data Commons

Posted in Centillion

permalink

Background: a bit about the Data Commons

It has been a productive but busy summer at the Lab for Data Intensive Biology.

As part of my job, I am supporting a lot of websites and infrastructure for the Data Commons Pilot Phase Consortium (DCPPC), which wrapped up Phase 1 this month.

The Data Commons is a large-scale effort to establish a community-driven set of standards for interoperability for biological data and computation, a massive effort and a broad mandate that has the potential to enable breakthrough research that is currently impossible because data and computations cannot inter-operate between the data, compute resources, and domain expertise that are provided by universities, hospitals, research institutes, companies, nonprofits, and citizen scientists.

Informationally challenged: Data Commons growing pains

An important part of defining a community-driven set of standards is defining a community, and toward that end the collective members of the Data Commons met at monthly face-to-face workshops to iterate tightly on a set of technologies and standards that will allow each institution's different compute platforms or data banks to use other institutions' platforms or data banks. Doing this requirs fostering community and creating the right environment for people to work through the issues.

One of the biggest challenges we faced in fostering a community that could develop and implement a set of standards across such a large and diverse group of experts and institutes was coordinating information. Specifically, making sure that decisions were properly communicated to the appropriate parties, that important documents made their way to the entire consortium, and that documents that were created and edited also be findable and sharable.

This problem began, back in April, as a very small trash fire. People were getting used to the Github workflow and did not know how to find the appropriate repository for the information they needed to contribute, and consortium members were universally annoyed that Google Drive's search functionality was so terrible.

In June we rolled out a trial document-tagging system to the consortium, to deafening silence - no one was impressed or satisfied with the tagging system. The real problem was with search.

Toward that end, I implemented a full-fledged search engine for the Data Commons that utilized various third-party APIs (Github, Google Drive, Groups.io, etc.) to index content related to the project, and make it full-text-searchable.

The result was centillion, the Data Commons search engine. This search engine provides a portal to search for Data Commons-related Google Drive documents, Github issues, Github pull requests, Github files, Groups.io email threads, and more.

Our story picks up with centillion.

Presenting centillion, the Data Commons search engine

One of the tools I have made heavy use of in support of web infrastructure for the DCPPC project is Flask, a Python library for running a web server. Flask is a very powerful library, but it starts with a relatively simple premise: Flask lets you create a web appplication that will bind to a particular port, and you can then add "routes" that are endpoints a user can visit, like /hello/world, and link those routes to Python functions.

On Monday 2018-10-28 the DIB Lab's weekly lab meeting featured yours truly covering the topic of centillion, the Data Commons search engine.

centillion makes use of the Python library whoosh under the hood, to provide search functionality, while the web front-end uses Flask to connect Python functions to a website that users can interact with.

Screen shot of the centillion search engine (2018-10-27).

Screen shot of the centillion search engine (2018-10-27).

centillion architecture: the short version

As of version 1.7, centillion is packaged as a Python package. The centillion package consists of two submodules, corresponding to the Flask frontend and Whoosh backend, respectively: webapp and search.

webapp submodule

centillion.webapp implements the Flask app and defines all routes. When the user runs a search, it passes the query string on to a Search object from the search submodule. The webapp submodule does not know anything about the details of the search engine or search index.

This submodule is located in src/webapp/ in the centillion repo.

search submodule

centillion.search implements a search engine using Whoosh, a programming library for building search engines. Whoosh does not implement any kind of front end, so its role is restricted entirely to the back end.

The search submodule also handles interfacing with the Github, Google, and Groups.io APIs and translating the results of API calls from these services into documents whose contents can be extracted and indexed by Whoosh.

This submodule is located in src/search/ in the centillion repo.

Tags:    DCPPC    Data Commons    Github    Community    Science    Centillion   

November 2026

Baby Mac Crypto

March 2022

How to Read Ulysses

July 2020

Applied Gitflow

September 2019

Mocking AWS in Unit Tests

November 2017

A Hard(y) Math Problem