if len(glob.glob('data/heart_l131k/tfrecords/*.tfr')) == 0:
    subprocess.call('curl -o data/heart_l131k.tgz https://storage.googleapis.com/basenji_tutorial_data/heart_l131k.tgz', shell=True)
    subprocess.call('tar -xzvf data/heart_l131k.tgz', shell=True)

"""
Explanation: Precursors
To train a model, you first need to convert your sequences and targets into the input HDF5 format. Check out my tutorials for how to do that; they're linked from the main page.
For this tutorial, grab a small example HDF5 that I constructed here with 10% of the training sequences and only GM12878 targets for various DNase-seq, ChIP-seq, and CAGE experiments.
End of explanation
"""


! basenji_train.py -o models/heart models/params_small.json data/heart_l131k

"""
Explanation: Train
Next, you need to decide what sort of architecture to use. This grammar probably needs work; my goal was to enable hyperparameter searches to write the parameters to file so that I could run parallel training jobs to explore the hyperparameter space. I included an example set of parameters that will work well with this data in models/params_small.txt.
Then, run basenji_train.py to train a model. The program will offer training feedback via stdout and write the model output files to the prefix given by the -s parameter.
The most relevant options here are:
| Option/Argument | Value | Note |
|:---|:---|:---|
| -o | models/heart | Directory to save training logs and model checkpoints. |
| params_file | models/params_small.json | JSON specified parameters to setup the model architecture and optimization. |
| data_dir | data/heart_l131k | Data directory containing the test input and output datasets as generated by basenji_data.py |
If you want to train, uncomment the following line and run it. Depending on your hardware, it may require several hours.
End of explanation
"""


if not os.path.isdir('models/heart'):
    os.mkdir('models/heart')
if not os.path.isfile('models/heart/model_best.h5'):
    subprocess.call('curl -o models/heart/model_best.h5 https://storage.googleapis.com/basenji_tutorial_data/model_best.h5', shell=True)

"""
Explanation: Test
Alternatively, you can just download a trained model.
End of explanation
"""


! basenji_test.py --ai 0,1,2 -o output/heart_test --rc --shifts "1,0,-1" models/params_small.json models/heart/model_best.h5 data/heart_l131k

"""
Explanation: models/heart/model_best.tf will now specify the name of your saved model to be provided to other programs.
To further benchmark the accuracy (e.g. computing significant "peak" accuracy), use basenji_test.py.
The most relevant options here are:
| Option/Argument | Value | Note |
|:---|:---|:---|
| --ai | 0,1,2 | Make accuracy scatter plots for targets 0, 1, and 2. |
| -o | output/heart_test | Output directory. |
| --rc | | Average the forward and reverse complement to form an ensemble predictor. |
| --shifts | | Average sequence shifts to form an ensemble predictor. |
| params_file | models/params_small.json | JSON specified parameters to setup the model architecture and optimization. |
| model_file | models/heart/model_best.h5 | Trained saved model parameters. |
| data_dir | data/heart_l131k | Data directory containing the test input and output datasets as generated by basenji_data.py |
End of explanation
"""


! cat output/heart_test/acc.txt

"""
Explanation: data/heart_test/acc.txt is a table specifiying the Pearson correlation and R2 for each dataset.
End of explanation
"""


IFrame('output/heart_test/pr/t0.pdf', width=600, height=500)

"""
Explanation: The directories pr, roc, violin, and scatter in data/heart_test contain plots for the targets indexed by 0, 1, and 2 as specified by the --ai option above.
E.g.
End of explanation
"""


