Comment on page
Stata in DXJupyterLab
Using Stata via DXJupyterlab, working with project files, and creating datasets with Spark.
Note also that DXJupyterLab is accessible to all users of the UK Biobank Research Analysis Platform and the Our Future Health Trusted Research Environment. A license is required to access DXJupyterLab on the DNAnexus Platform. Contact DNAnexus Sales for more information.
To use Stata on the DNAnexus Platform, you need a valid Stata license. Before launching Stata in a project, you must save your license details according to the instructions below in a plain text file with the extension
.json, then upload this file to the project’s root directory. You only need to do this once per project.
Start by creating the file in a text editor, including all the fields shown here, where
<user>is your DNAnexus username, and
<organization>is the org of which you're a member:
"serialNumber": "<Serial number from Stata>",
"code": "<Code from Stata>",
"authorization": "<Authorization from Stata>",
"user": "<Registered user line 1>",
"organization": "<Registered user line 2>"
Save the file according to the following format, where
<username>is your DNAnexus username:
Some operating systems may not support the naming of files with a "." as the first character. If this is the case, you can rename the
.jsonfile after uploading it to your project by hovering over the name of your file and clicking the pencil icon that appears.
Open the project in which you want to use Stata. Upload the Stata license details file to the project’s root directory by going to your project's Manage tab, clicking on the Add button on the upper right, and then selecting the Upload data option.
When working in a shared project, you can take an additional step to avoid exposing your Stata license details to project collaborators.
Create a private project. Then create and save a Stata license details file in that project’s root directory, as per the instructions above.
Within the shared project, create and save a Stata license details project in this format, where
project-yyyyis the name of the private project, and
file-xxxxis the license details file name, in that private project:
- 1.Open the project in which you want to use Stata. From within the project's Manage tab, click the Start Analysis button.
- 2.Select the app DXJupyterLab with Python, R, Stata, ML.
- 3.Click the Run Selected button. Note that if you haven't run this app before, you'll be prompted to install it. Next, you’ll be taken to the Run Analysis screen.
- 4.On the Run Analysis screen, open the Analysis Inputs tab and click the Stata settings file button.
- 5.Add your Stata settings file as an input. This is the
.jsonfile you created, containing your Stata license details.
- 6.In the Common section at the bottom of the Analysis Inputs pane, open the Feature dropdown menu and select Stata.
- 7.Click the Start Analysis button at the top right corner of the screen. This will launch the DXJupyterLab app, and take you to the project's Monitor tab, where you can monitor the app's status as it loads.
Note: The app can take some time to load and start running.
Once the analysis starts, you’ll see the notification "Running" appear under the name of the app.
Click the Monitor tab heading. This will open a list of running and past jobs. Jobs are shown in reverse chronological order, with the most recently launched at the top. The topmost row should show the job you’ve just launched. To open the job and enter the JupyterLab interface, click on the URL shown under Worker URL.
If you do not see the worker URL, click on the name of the job in the Monitor page.
Within the JupyterLab interface, open the DNAnexus tab shown at the left edge of the screen.
The location of the DNAnexus tab within the JupyterLab interface.
Open a new Stata notebook by clicking the Stata tile in the Notebooks section.
You can download DNAnexus data files to DXJupyterLab container from Stata notebook with:
!dx download project-xxxx:file-yyy
Data files in the current project can also be accessed using a
/mnt/projectfolder from a Stata notebook as follows: To load a DTA file:
To load a CSV file:
import delimited /mnt/project/<path>/data_in.csv
To write a DTA file to the DXJupyterLab container:
To write a CSV file to the DXJupyterLab container:
export delimited data_out.csv
To upload a data file from the DXJupyterLab container to the project, use the following command in a Stata notebook:
!dx upload <file> --destination=<destination>
Alternatively, open a new Launcher tab, open Terminal, and run:
dx upload <file> --destination=<destination>
/mnt/projectdirectory is read-only, so trying to write to it results in an error.
pandas_df = spark_df.toPandas()
Pandas dataframe can be exported to CSV or Stata DTA files in the JupyterLab container with:
To upload a data file from the JupyterLab container to the DNAnexus project in the DXJupyterLab spark cluster app, use
dx upload <file>
Once saved to the project, data files can be used in a DXJupyterLab Stata session using the instructions above.