Developer Tutorials/Quick Start

This tutorial gives a quick intro to the DNAnexus developer experience, and works its way to building a fully functional, useful app on the platform. For a more in-depth discussion of the platform, take a look at Intro to Building Apps.

The steps below require the DNAnexus SDK. Please download and install it if you have not done so already.

1. Build a simple app

Every DNAnexus app starts with 2 files:

  • dxapp.json a file containing the app's metadata: its inputs and outputs, how the app will be run, etc.
  • a script that will be executed in the cloud when the app is run

Let's start by creating a file called dxapp.json with the following text:

{ "name": "coolapp",
  "runSpec": {
    "file": "code.py",
    "interpreter": "python2.7"
  }
}

Above, we've specified the name for our app (coolapp), the type of interpreter (python2.7) to run our script with, and a path (code.py) to the script that we will create next.

Next, we create our script in a file called code.py with the following text:

import dxpy

@dxpy.entry_point('main')
def main(**kwargs):
    print "Hello, DNAnexus!"
    return {}

That's all we need. To build the app, first log in to DNAnexus and start a project with dx login. In the directory with the two files above, run:

$ dx login ... $ dx build -a

Now, run the app and watch the output:

$ dx run coolapp --watch

That's it! You have just made and run your first DNAnexus applet. Applets are lightweight apps that live in your project, and are not visible in the App Library. When you typed "dx run", the app ran on its own Linux instance in the cloud. You have exclusive, secure access to the CPU, storage, and memory on the instance. The DNAnexus API lets your app read and write data on the platform, as well as launch other apps.

The app is now available in the DNAnexus web interface, as part of the project that you started. It can be configured and run in the Workflow Builder, or shared with other users by sharing the project.

2. Run BLAST

Next, we'll make our app do something a bit more interesting: take in two files with FASTA-formatted DNA, run the BLAST tool to compare them, and output the result.

In the cloud, your app will run on Ubuntu Linux 12.04, where BLAST is available as an APT package, blast2. You can request that the DNAnexus Execution Environment install it before your script is run by listing blast2 in the execDepends field of your dxapp.json like this:

{ "name": "coolapp",
  "runSpec": {
    "file": "code.py",
    "interpreter": "python2.7",
    "execDepends": [ {"name": "blast2"} ]
  }
}

Next, let's update code.py to run BLAST:

import dxpy, subprocess

@dxpy.entry_point('main')
def main(seq1, seq2):
    dxpy.download_dxfile(seq1, "seq1.fasta")
    dxpy.download_dxfile(seq2, "seq2.fasta")
    
    subprocess.call("blast2 -p blastn -i seq1.fasta -j seq2.fasta > report.txt", shell=True)
    
    report = dxpy.upload_local_file("report.txt")
    return {"blast_result": report}

We're now ready to rebuild the app and test it on some real data. You can use some demo inputs available in the Demo Data project, or you can upload your own data with dx upload or via the website.

Rebuild the app with dx build -a, and run it like this:

$ dx run coolapp -i seq1="Demo Data:/Developer Quickstart/NC_000868.fasta" -i seq2="Demo Data:/Developer Quickstart/NC_001422.fasta" --watch

Once the job is done, you can examine the output with dx head report.txt, download it with dx download, or view it on the website.

3. Provide an input/output spec

Workflows are a powerful way to visually connect, configure, and run multiple apps in pipelines. To add our app to a workflow and be able to connect its inputs and/or outputs to other apps, our app will need both input and output specifications. Let's update our dxapp.json as follows:

{ "name": "coolapp",
  "runSpec": {
    "file": "code.py",
    "interpreter": "python2.7",
    "execDepends": [ {"name": "blast2"} ]
  },
  "inputSpec": [
    {"name": "seq1", "class": "file"},
    {"name": "seq2", "class": "file"}
  ],
  "outputSpec": [
    {"name": "blast_result", "class": "file"}
  ]
}

Rebuild the app with dx build -a. You can run it in the same way as before, but now we can add the applet to a workflow. Click "New Workflow" while looking at your project on the website, and click on coolapp once to add it to the workflow. You'll see inputs and outputs appear on the workflow stage which can be connected to other stages in the workflow.

Also, if you now go back to the command line and run dx run coolapp with no input arguments, it will prompt you for the input values for seq1 and seq2.

4. Configure app settings

In addition to specifying input files, the I/O specification can also be used to configure settings that we want the app to use. For example, we can configure the E-value setting and other BLAST settings with this code and dxapp.json:

code.py

import dxpy, subprocess

@dxpy.entry_point('main')
def main(seq1, seq2, evalue, blast_args):
    dxpy.download_dxfile(seq1, "seq1.fasta")
    dxpy.download_dxfile(seq2, "seq2.fasta")
    
    command = "blast2 -p blastn -i seq1.fasta -j seq2.fasta -e {e} {args} > report.txt".format(e=evalue, args=blast_args)
    subprocess.call(command, shell=True)
    
    report = dxpy.upload_local_file("report.txt")
    return {"blast_result": report}
dxapp.json
{ "name": "coolapp",
  "runSpec": {
    "file": "code.py",
    "interpreter": "python2.7",
    "execDepends": [ {"name": "blast2"} ]
  },
  "inputSpec": [
    {"name": "seq1", "class": "file"},
    {"name": "seq2", "class": "file"},
    {"name": "evalue", "class": "float", "default": 0.01},
    {"name": "blast_args", "class": "string", "default": ""}
  ],
  "outputSpec": [
    {"name": "blast_result", "class": "file"}
  ]
}
Rebuild the app again and add it in the workflow builder. You should now see the evalue and blast_args settings available when you click the gear button on the stage. See the I/O specification docs for information on more options. Also, after building and configuring a workflow, you can run the workflow itself with dx run workflowname.

5. Use SDK tools

One of the utilities provided in the SDK is dx-app-wizard. This tool will prompt you with a series of questions with which it will create the basic files needed for a new app. It also gives you the option of writing your app as a bash shell script instead of Python. Just run dx-app-wizard to try it out.

Next steps

This is it for the Quick Start! Check Intro to Building Apps for a more in-depth look at the structure of apps on the platform, or continue browsing the Developer Portal.

Last edited by Thanh-Ha Nguyen, 2016-02-22 22:54:29

 Feedback