Language notes

This page collects information on Visual Studio Code, C, C++, Fortran, Java, Perl, python and R.


It is still possible to reflect the old language from here,


There have been multiple instances for suggesting migration fromtempnam to mkstemp (/usr/include/stdlib.h under Bash but not Windows) and mktemp -- the following code illustrates its use under both Bash and Windows, Nevertheless these are not standard routines, one still needs to add char *mktemp(char *) for instance.

#include <stdio.h>
#include <fcntl.h>      /* for open flags */
#include <limits.h>     /* for PATH_MAX */

int main(void)
        static char template[] = "/tmp/myfileXXXXXX";
        char fname[PATH_MAX];
        static char mesg[] =
                "Here's lookin' at you, kid!\n";        /* beats "hello, world" */
        int fd;

        strcpy(fname, template);


        printf("Filename is %s\n", fname);


        fd = open(fname, O_CREAT|O_RDWR|O_TRUNC, 0600);
        write(fd, mesg, strlen(mesg));

        /* unlink(fname); */

        return 0;

A script for testing UTF-8 support by PCRE,

#include <pcre.h>
#include <stdio.h>
#include <stdlib.h>

int main() {
    int supports_utf8;
    if (pcre_config (PCRE_CONFIG_UTF8, &supports_utf8)) {
        fprintf(stderr, "pcre_config() failed\n");
    printf("UTF-8 is supported: %s\n", supports_utf8 ? "yes" : "no");
// gcc $(pkg-config --cflags --libs libpcre) pcreutf.c
// ./a.out
// pcretest -C

The following is Timsort implementation,

#include <stdio.h>

#define MIN_RUN 32

// 插入排序算法
void insertionSort(int arr[], int left, int right) {
    for (int i = left + 1; i <= right; i++) {
        int key = arr[i];
        int j = i - 1;
        while (j >= left && arr[j] > key) {
            arr[j + 1] = arr[j];
        arr[j + 1] = key;

// 归并函数
void merge(int arr[], int left, int mid, int right) {
    int len1 = mid - left + 1;
    int len2 = right - mid;
    int L[len1], R[len2];

    for (int i = 0; i < len1; i++)
        L[i] = arr[left + i];
    for (int j = 0; j < len2; j++)
        R[j] = arr[mid + 1 + j];

    int i = 0, j = 0, k = left;

    while (i < len1 && j < len2) {
        if (L[i] <= R[j])
            arr[k++] = L[i++];
            arr[k++] = R[j++];

    while (i < len1)
        arr[k++] = L[i++];
    while (j < len2)
        arr[k++] = R[j++];

// Timsort 算法
void timSort(int arr[], int n) {
    for (int i = 0; i < n; i += MIN_RUN)
        insertionSort(arr, i, (i + MIN_RUN - 1) < n ? (i + MIN_RUN - 1)
                      : (n - 1));

    for (int size = MIN_RUN; size < n; size *= 2) {
        for (int left = 0; left < n; left += 2 * size) {
            int mid = left + size - 1;
            int right = (left + 2 * size - 1) < (n - 1) ?
              (left + 2 * size - 1) : (n - 1);
            merge(arr, left, mid, right);

int main() {
    int arr[] = {12, 11, 13, 5, 6, 7};
    int n = sizeof(arr) / sizeof(arr[0]);
    printf("Original array: ");
    for (int i = 0; i < n; i++)
        printf("%d ", arr[i]);

    timSort(arr, n);

    printf("\nSorted array: ");
    for (int i = 0; i < n; i++)
        printf("%d ", arr[i]);
    return 0;


The use of Google Test is noted here,


wget -qO- | \
tar xvfz -
cd googletest-release-1.11.0
mkdir build && cd build
build ..
# amending set(CMAKE_INSTALL_PREFIX "/rds/user/jhz22/hpc-work") in `cmake_install.cmake`
make install

Now it is possible to compile R/glmnet 4.1-3, i.e., find_package(GTest 1.11 CONFIG REQUIRED) of src/glmnetpp/CMakeLists.txt.


Information on modernising Fortran could be very useful in foreign language calls (e.g., R),

Debugging Fortran code

  • gdb
  • valgrind
program segfault1
  implicit none
  real, dimension(10) :: a
  integer :: i
  a = 0.
  do i = 1, 12
     a(i) = i
  end do
end program segfault1

! gfortran -g -Wall -Wextra -Wimplicit-interface -fPIC -fmax-errors=1 -fcheck=all -fbacktrace segfault1.f90 -o segfault1
! valgrind  --leak-check=full --dsymutil=yes --track-origins=yes ./segfault1
! MacOS --dsymutil=yes:
! valgrind  --leak-check=full --dsymutil=yes --track-origins=yes ./segfault1


The IDE of choice is NetBeans (e.g., DEPICT and JAM); however 8.1 from apt install under Ubuntu 18.04 crashes so it is suggested to download directly from To enable JDK it is helpful to specify --javahome option.

sudo ./ --javahome /usr/lib/jvm/java-8-oracale

or start with netbeans --javahome /usr/lib/jvm/java-8-oracle (more convenient to set alias netbeans='netbeans --javahome /usr/lib/jvm/java-8-oracle' at .bashrc).

NetBeans 9.0 is currently available from; the .zip file can be downloaded and unpacked for use.

For software such as cutadapt cython is required,

sudo apt install cython


The mermaid diagram is illustrated with mermaid.html using code available from here,

The hello world example with plotly.js is and the 3D diagram is with 3d-scatter.html based on

The base64 encode/decode is with


sudo perl -MCPAN -e shell
install DBI

for instance, as used in VEP.

Another notable example is circos, and its Google group,

wget -qO- | \
tar xvfz -
cd circos-0.69-9
bin/circos --modules
wget -qO- | \
tar xvfz -
wget -qO- | \
tar xvfz -

The following required modules can be installed

Config::General (v2.50 or later)
Set::IntSpan (v1.16 or later)

and we can enter the example/ directory to run its script. The CircosAPI module requires namespace::autoclean, Moose, JSON::PP and String::Util.


To disable upgrade of pip, add

disable-pip-version-check = True

option to $HOME/.config/pip/pip.conf

To install a particular version of package, e.g.,

sudo -H pip install pandas==0.20.1

which is required by DEPICT's Other pip options include uninstall.

The python programs in agotron_detector requires MySQL and can be installed as follows,

sudo apt-get install python-dev libmysqlclient-dev
sudo pip install MySQL-python

It is necessary to use --user option without super-user privilege.

PyStan is available with pip install pystan which uses matplotlib, and Tkinter, established with sudo apt install python-tk or sudo apt install python3-tk.

import pystan

schools_code = """
data {
    int<lower=0> J; // number of schools
    real y[J]; // estimated treatment effects
    real<lower=0> sigma[J]; // s.e. of effect estimates
parameters {
    real mu;
    real<lower=0> tau;
    real eta[J];
transformed parameters {
    real theta[J];
    for (j in 1:J)
    theta[j] = mu + tau * eta[j];
model {
    eta ~ normal(0, 1);
    y ~ normal(theta, sigma);

schools_dat = {'J': 8,
               'y': [28,  8, -3,  7, -1,  1, 18, 12],
               'sigma': [15, 10, 16, 11,  9, 11, 10, 18]}

sm = pystan.StanModel(model_code=schools_code)
fit = sm.sampling(data=schools_dat, iter=1000, chains=4)

import matplotlib.pyplot as plt
def plotGraph():
      fig = fit.plot()
      # use the save button or the following command,
      # f.savefig("foo.pdf", bbox_inches='tight')
      return fig

from matplotlib.backends.backend_pdf import PdfPages
pp = PdfPages('foo.pdf')
f = plotGraph()

To install jupyter-book,

module load python/2.7.10
python -m pip install jupyter-book --user

and we can check for $HOME/.local/lib/python2.7/site-packages and start from /home/jhz22/.local/bin. We can install notebook similarly.

python -m pip install notebook --user

Owing to recent changes, it is more appropriate to use python3

module load python/3.5
export PATH=$PATH:$HOME/.local/bin
export PYTHONPATH=/usr/local/Cluster-Apps/python/3.5.1/lib/python3.5/site-packages:$HOME/.local/lib/python3.5/site-packages
python3 -m pip install jupyter-book --user

To convert from parquet to csv is done as follows,

import pandas as pd
import pyspark
import pyarrow
import sys
import os

fn = sys.argv[1]

df = pd.read_parquet(fn)

outfn = "".join("GTEx_Analysis_v8_EUR_eQTL_all_associations_csv/" + os.path.splitext(os.path.basename(fn))[0] + ".csv") 



Information on R and RStudio can be seen from installation section of this,

The use of multi-byte string needs specific handling, e.g.,

# on Bash
iconv myfile -f UTF-8 -t ISO-8859-1 -c


# in R
Sys.setlocale("LC_ALL", "C")



It requires a number of software,

sudo dnf install udunits2-devel
sudo dnf install cairo-devel
sudo dnf install gdal gdal-devel
sudo dnf install proj-devel proj-static
sudo dnf install geos geos-devel

to be followed by


Calls from R

  • Basic examples using OpenMP with R, for C, C++, F77, and Fortran 2003 using Romp,
  • RFI: R to Modern Fortran Interface,
  • Stanford Utility Tools for R packages using Fortran,

Package examples for Fortran, * * *

Documentation * * *

R packages



Web:, Shiny examples

The hello world version is as follows,

ui <- fluidPage(
  "Hello, world!"
server <- function(input, output, session) {
shinyApp(ui, server)

Suppose our a directory (called shinyapps here) contains files ui.R and server.R (or combined in app.R).

Go the web site, and register an account with email address. Login from and the following information is available:

Step 1 – Install rsconnect

The rsconnect package can be installed directly from CRAN. To make sure you have the latest version run following code in your R console:


Step 2 – Authorize Account

The rsconnect package must be authorized to your account using a token and secret. To do this, click the copy button below and we'll copy the whole command you need to your clipboard. Just paste it into your console to authorize your account. Once you've entered the command successfully in R, that computer is now authorized to deploy applications to your account.

rsconnect::setAccountInfo(name='your-account', token='your token', secret='your secret')

In the future, you can manage your tokens from the Tokens page the settings menu.

Step 3 – Deploy

Once the rsconnect package has been configured, you're ready to deploy your first application. If you haven't written any applications yet, you can also checkout the Getting Started Guide for instructions on how to deploy our demo application. Run the following code in your R console.


The shiny page is then up as


It is most convient to convert Tex/LaTex formulas into MicroSoft Word equtions via pandoc, i.e., pandoc -o README.docx.

See and

For Chinese language support, try

sudo apt-get install texlive-latex-base
sudo apt-get install latex-cjk-all
sudo apt-get install texlive-latex-extra
sudo apt-get install texmaker
sudo apt-get install texlive-xetex
sudo apt-get install texlive-publishers

Now change latex to xelatex from Texmaker.

How are you?你好吗?


First, create hello.ts with two lines,

#!/usr/bin/env ts-node
console.log('Hello world!');

and set up the environment,

npm install -g npm
npm install typescript ts-node -g
chmod +x hello.ts

Visual Studio Code

It is available from, so we could download a .tar.gz file and unpack.

There is a pointer from to Once downloaded, it can be installed with

sudo dpkg -i code_1.23.1-1525968403_amd64.deb

but it requires libgconf-2-4; when failed to install use sudo apt --fix-broken install.

See for the hello world example.


We need sign up/in for an API key from, e.g., via your MicrsoSoft account.

From Visual Studio Code, add extension CodeGPT; Ctrl-Shit-p to saarch for CodeGPT: Set API Key and enter the key given above from

Then we create a file, start with a comment, e.g., '# to draw a forest plot' and Ctrl-Shift-i to obtain the code.

Here is one of the results given,

# First, create a dataset with the relevant values for your forest plot
study_names <- c("Study A", "Study B", "Study C", "Study D")
odds_ratios <- c(1.2, 1.5, 0.8, 0.6)
lo_ci <- c(0.9, 1.0, 0.5, 0.3)
hi_ci <- c(1.5, 2.0, 1.2, 0.9)

# Combine data into a data frame
df <- data.frame(study_names, odds_ratios, lo_ci, hi_ci)

# Load the 'meta' package for the forest plot function

# Create the forest plot
forest(df$odds_ratios, = df$lo_ci, ci.ub = df$hi_ci,
       slab = df$study_names, xlab = "Odds Ratio",
       main = "Forest Plot Example")