Coding with FileReader objects#

Here, you will find tutorials and explanations for using AaronTools’ functions, classes, and subroutines pertaining to our object for reading files: AaronTools.fileIO.FileReader().

Creation#

A FileReader object can be created by passing the path to a file. To begin, let’s import FileReader:

from AaronTools.fileIO import FileReader

fr = FileReader("benzene.log")

The file “benzene.log” will be read as a Gaussian output file because of the .log extension. Creating a Geometry from this FileReader is as simple as passing it to Geometry:

from AaronTools.geometry import Geometry

geom = Geometry(fr)

If the file has an unexpected extension, we should specify the format for AaronTools:

fr = FileReader(("benzene.gout", "log", None))

Here, we’ve replaced the filename with a tuple containing

  1. the path to the file

  2. the format AaronTools should read the file as

  3. the file content

Because we haven’t read anything from the file yet, we specify None in our tuple. The content can also be a string containing the contents of a file of the specified format or a file-like object (e.g. sys.stdin).

Below is a table containing AaronTools’ expectations for file extensions:

Expected File Extensions#

file type

extension

Gaussian input

com or gjf

Gaussian output

log

ORCA output

out

XYZ

xyz

structure data file

sd or mol

Psi4 output

dat or out

Getting More Information#

FileReader initialization accepts two keywords: get_all and just_geom. Let’s look at what those do.

Multiple Structures#

The get_all keyword tells FileReader to store all the structures it finds while reading the file (e.g. all steps in an optimization or an XYZ trajectory). These structures are stored in the FileReader’s all_geom attribute. This will be a list of dictionaries, with one of the keys being atoms and the other being either comment or data, depending on the file format. The data key is used for QM output files, and stores whatever data has been parsed at the time the corresponding structure was read. The comment key is used for files that do not have computed data to parse (e.g., XYZ or Mol files). With get_all=False, the FileReader’s only structure will be the last one in the file.

Below is sample code for turning each structure in a FileReader into a Geometry:

from AaronTools.atoms import Atom
from AaronTools.geometry import Geometry
from AaronTools.fileIO import FileReader

fr = FileReader('benzene.log', get_all=True)

geom_list = []
for struc in fr.all_geom:
    from item in struc:
        geom_list.append(Geometry(item["atoms"]))

Calculation Information

The just_geom keyword controls whether the FileReader grabs more than the file’s structure. Additional information will be stored as a dictionary in the FileReader’s other attribute. This data can also be accessed by using the FileReader as if it was a dictionary (i.e. fr["energy"] instead of fr.other["energy"]). A list of dictionary keys, what they are, and which of our file parsers can grab them can be found on the this page.