parmed.amber.amberformat module

This is a generalization of the readparm.AmberParm class to handle similar Amber-style files with %FLAG/%FORMAT tags

class parmed.amber.amberformat.AmberFormat(fname=None)[source]

Bases: object

A class that can parse and print files stored in the Amber topology or MDL format. In particular, these files have the general form:

` %VERSION VERSION_STAMP = V00001.000  DATE = XX/XX/XX  XX:XX:XX %FLAG <FLAG_NAME> %COMMENT <comments> %FORMAT(<Fortran_Format>) ... data corresponding to that Fortran Format %FLAG <FLAG_NAME2> %COMMENT <comments> %FORMAT(<Fortran_Format>) ... data corresponding to that Fortran Format `

where the %COMMENT sections are entirely optional

Parameters
fnamestr=None

If provided, this file is parsed and the data structures will be loaded from the data in this file

Attributes
parm_datadict {str

A dictionary that maps FLAG names to all of the data contained in that section of the Amber file.

formatsdict {str

A dictionary that maps FLAG names to the FortranFormat instance in which the data is stored in that section

parm_commentsdict {str

A dictionary that maps FLAG names to the list of COMMENT lines that were stored in the original file

flag_listlist

An ordered list of all FLAG names. This must be kept synchronized with parm_data, formats, and parm_comments such that every item in flag_list is a key to those 3 dicts and no other keys exist

charge_flagstr=’CHARGE’

The name of the name of the FLAG that describes partial atomic charge data. If this flag is found, then its data are multiplied by the ELECTROSTATIC_CONSTANT to convert back to fractions of electrons

versionstr

The VERSION string from the Amber file

namestr

The file name of the originally parsed file (set to the fname parameter)

Methods

add_flag(flag_name, flag_format[, data, …])

Adds a new flag with the given flag name and Fortran format string and initializes the array with the values given, or as an array of 0s of length num_items

delete_flag(flag_name)

Removes a flag from the topology file

id_format(filename)

Identifies the file type as either Amber-format file (like prmtop) or an old-style topology file.

parse(filename, *args, **kwargs)

Meant for use with the automatic file loader, this will automatically return a subclass of AmberFormat corresponding to what the information in the prmtop file contains (i.e., either an AmberParm, ChamberParm, AmoebaParm, or AmberFormat)

rdparm(fname[, slow])

Parses the Amber format file

rdparm_old(prmtop_lines[, check])

This reads an old-style topology file and stores the results in the same data structures as a new-style topology file

rdparm_slow(fname)

Parses the Amber format file.

set_version()

Sets the version string

view_as(cls)

Returns a view of the current object as another object.

write_parm(name)

Writes the current data in parm_data into a new topology file with the given name

add_flag(flag_name, flag_format, data=None, num_items=- 1, comments=None, after=None)[source]

Adds a new flag with the given flag name and Fortran format string and initializes the array with the values given, or as an array of 0s of length num_items

Parameters
flag_namestr

Name of the flag to insert. It is converted to all upper case

flag_formatstr

Fortran format string representing how the data in this section should be written and read. Do not enclose in ()

datalist=None

Sequence with data for the new flag. If None, a list of zeros of length num_items (see below) is given as a holder

num_itemsint=-1

Number of items in the section. This variable is ignored if a set of data are given in data

commentslist of str=None

List of comments to add to this section

afterstr=None

If provided, the added flag will be added after the one with the name given to after. If this flag does not exist, IndexError will be raised

Raises
AmberError if flag already exists
IndexError if the after flag does not exist
delete_flag(flag_name)[source]

Removes a flag from the topology file

static id_format(filename)[source]

Identifies the file type as either Amber-format file (like prmtop) or an old-style topology file.

Parameters
filenamestr

Name of the file to check format for

Returns
is_fmtbool

True if it is an Amber-style format, False otherwise

static parse(filename, *args, **kwargs)[source]

Meant for use with the automatic file loader, this will automatically return a subclass of AmberFormat corresponding to what the information in the prmtop file contains (i.e., either an AmberParm, ChamberParm, AmoebaParm, or AmberFormat)

rdparm(fname, slow=False)[source]

Parses the Amber format file

rdparm_old(prmtop_lines, check=False)[source]

This reads an old-style topology file and stores the results in the same data structures as a new-style topology file

Parameters
prmtop_lineslist of str

List of all lines in the prmtop file

checkbool, optional

If True, only the first couple sections will be read to determine if this is, in fact, an old-style topology file

rdparm_slow(fname)[source]

Parses the Amber format file. This parser is written in pure Python and is therefore slower than the C++-optimized version

set_version()[source]

Sets the version string

view_as(cls)[source]

Returns a view of the current object as another object.

Parameters
clstype

Class definition of an AmberParm subclass for the current object to be converted into

Returns
instance of cls initialized from data in this object. This is NOT a deep
copy, so modifying the original object may modify this. The copy
function will create a deep copy of any AmberFormat-derived object
write_parm(name)[source]

Writes the current data in parm_data into a new topology file with the given name

Parameters
namestr or file-like

Name of the file to write the topology file to or file-like object to write

class parmed.amber.amberformat.FortranFormat(format_string, strip_strings=True)[source]

Bases: object

Processes Fortran format strings according to the Fortran specification for such formats. This object handles reading and writing data with any valid Fortran format. It does this by using the fortranformat project [https://bitbucket.org/brendanarnold/py-fortranformat].

However, while fortranformat is very general and adheres well to the standard, it is very slow. As a result, simple, common format strings have been optimized and processes reads and writes between 3 and 5 times faster. The format strings (case-insensitive) of the following form (where # can be replaced by any number) are optimized:

  • #E#.#

  • #D#.#

  • #F#.#

  • #(F#.#)

  • #a#

  • #I#

Parameters
format_stringstr

The Fortran Format string to process

strip_stringsbool=True

If True, strings are stripped before being processed by stripping (only) trailing whitespace

Methods

read(line)

Reads the line and returns the converted data

write(items, dest)

Writes an iterable of data (or a single item) to the passed file-like object

floatre = re.compile('(\\d+)?[edf](\\d+)\\.(\\d+)$', re.IGNORECASE)
floatre2 = re.compile('(\\d+)?\\([edf](\\d+)\\.(\\d+)\\)$', re.IGNORECASE)
intre = re.compile('(\\d+)?i(\\d+)$', re.IGNORECASE)
read(line)[source]

Reads the line and returns the converted data

strre = re.compile('(\\d+)?a(\\d+)$', re.IGNORECASE)
write(items, dest)[source]

Writes an iterable of data (or a single item) to the passed file-like object

Parameters
itemsiterable or single float/str/int

These are the objects to write in this format. The types of each item should match the type specified in this Format for that argument

destfile or file-like

This is the file to write the data to. It must have a write method or an AttributeError will be raised

Notes

This method may be replaced with _write_string (for #a#-style formats) or _write_ffwriter in the class initializer if no optimization is provided for this format, but the call signatures and behavior are the same for each of those functions.