ASReml · Simulations Using Python

This page was last modified on 07 May 2007, at 13:28 NZST

As I mention in the Simulations Using SAS page, I would rather use Python and NumPy for breeding strategy simulation. Once the data are generated (which I should explain how one day; summary: Cholesky decomposition), data can be saved and there are two additional steps:

  1. Call AS Reml from Python, and
  2. Read the results of the analysis. This provides two types of files:
    1. Nicely column formatted (easy piecy) and
    2. Free form, with the prime example being the .asr file.

The first part is handled in the same way as in SAS (or almost any language with the exception of R and S+), calling a .bat file (do you remember DOS?). Let’s say that analysis.bat contains the following commands:

asreml -ls5 dotasr.as
exit

then we can call the .bat file from Python using a function like:

import os
os.system('analysis.bat')

The second part, particularly reading the .asr file uses a function like the one below:

def readAsr(fileName, componentsList):
    inputFile = open(fileName)
    outputDic = {}
    outputDic['LogLStatus'] = 'Unknown'

    for line in inputFile:
        record = string.split(line)
        if len(record) > 0:
            if record[0] in componentsList:
                outputDic[record[0]] = record[4]
            if 'LogL=' in record:
                outputDic['LogL'] = record[2]
            if 'Converged' in record:
                outputDic['LogLStatus'] = 'Converged'

    inputFile.close()
    return outputDic

A simple way to call the function would be:

asrFile = 'dotasr.asr'
componentsList = ['Variance', 'treeID', 'plot']
simuResults = readAsr(asrFile, componentsList)

You can throw everything in a loop and you are in business.