ccpn.util.Bmrb package

Submodules

ccpn.util.Bmrb.bmrb module

This module provides entry, saveframe, and loop objects. Use python’s built in help function for documentation.

There are five module variables you can set to control our behavior.

*Setting bmrb.verbose to True will print some of what is going on to the terminal. *Setting bmrb.raise_parse_warnings to true will raise an exception if the parser encounters something problematic. Normally warnings are suppressed. *Setting skip_empty_loops to True will suppress the printing of empty loops when calling __str__ methods. *Adding key->value pairs to str_conversion_dict will automatically convert tags whose value matches “key” to the string “value” when printing. This allows you to set the default conversion value for Booleans or other objects. *Setting bmrb.allow_v2_entries will allow parsing of and printing of NMR-STAR version 2.1 entries. Most other methods will not operate correctly on parsed 2.1 entries. This is only to allow you parse and access the data in these entries - nothing else. Only set this if you have a really good reason to.

Some errors will be detected and exceptions raised, but this does not implement a full validator (at least at present).

Call directly (rather than importing) to run a self-test.

class ccpn.util.Bmrb.bmrb.entry(**kargs)[source]

Bases: object

An OO representation of a BMRB entry. You can initialize this object several ways; (e.g. from a file, from the official database, from scratch) see the classmethods.

addSaveframe(frame)[source]

Add a saveframe to the entry.

bmrb_id = 0
compare(other)[source]

Returns the differences between two entries as a list. Otherwise returns 1 if different and 0 if equal. Non-equal entries will always be detected, but specific differences detected depends on order of entries.

frameDict()[source]

Returns a dictionary of saveframe name -> saveframe object

frame_list = []
classmethod fromDatabase(entry_num)[source]

Create an entry corresponding to the most up to date entry on the public BMRB server. (Requires ability to initiate outbound HTTP connections.)

classmethod fromFile(the_file)[source]

Create an entry by loading in a file. If the_file starts with http://, https://, or ftp:// then we will use those protocols to attempt to open the file.

classmethod fromScratch(bmrb_id)[source]

Create an empty entry that you can programatically add to. You must pass a number corresponding to the BMRB ID. If this is not a “real” BMRB entry, use 0 as the BMRB ID.

classmethod fromString(the_string)[source]

Create an entry by parsing a string.

getLoopsByCategory(value)[source]

Allows fetching loops by category.

getSaveframeByName(frame)[source]

Allows fetching a saveframe by name.

getSaveframesByCategory(value)[source]

Allows fetching saveframes by category.

getSaveframesByTagAndValue(tag_name, value)[source]

Allows fetching saveframe(s) by tag and tag value.

getTag(tag, whole_tag=False)[source]

Given a tag (E.g. _Assigned_chem_shift_list.Data_file_name) return a list of all values for that tag. Specify whole_tag=True and the [tag_name, tag_value] pair will be returned.

nefString()[source]

Returns a string representation of the entry in NEF.

printTree()[source]

Prints a summary, tree style, of the frames and loops in the entry.

validate(validation_schema=None)[source]

Validate an entry against a STAR schema. You can pass your own custom schema if desired, otherwise the schema will be fetched from the BMRB servers. Returns a list of errors found. 0-length list indicates no errors found.

class ccpn.util.Bmrb.bmrb.saveframe(**kargs)[source]

Bases: object

A saveframe. Use the classmethod fromScratch to create one.

addLoop(loop)[source]

Add a loop to the saveframe loops.

addTag(name, value, update=False)[source]

Add a tag to the tag list. Does a bit of validation and parsing. Set update to true to update a tag if it exists rather than raise an exception.

addTags(tag_list, update=False)[source]

Adds multiple tags to the list. Input should be a list of tuples that are either [key, value] or [key]. In the latter case the value will be set to ”.”. Set update to true to update a tag if it exists rather than raise an exception.

compare(other)[source]

Returns the differences between two saveframes as a list. Non-equal saveframes will always be detected, but specific differences detected depends on order of saveframes.

deleteTag(tag)[source]

Deletes a tag from the saveframe based on tag name.

classmethod fromFile(the_file, csv=False)[source]

Create a saveframe by loading in a file. Specify csv=True is the file is a CSV file. If the_file starts with http://, https://, or ftp:// then we will use those protocols to attempt to open the file.

classmethod fromScratch(saveframe_name, tag_prefix=None)[source]

Create an empty saveframe that you can programatically add to. You may also pass the tag prefix as the second argument. If you do not pass the tag prefix it will be set the first time you add a tag.

classmethod fromString(the_string, csv=False)[source]

Create a saveframe by parsing a string. Specify csv=True is the string is in CSV format and not NMR-STAR format.

getDataAsCSV(header=True, show_category=True)[source]

Return the data contained in the loops, properly CSVd, as a string. Set header to False omit the header. Set show_category to False to omit the loop category from the headers.

getLoopByCategory(name)[source]

Return a loop based on the loop name (category).

getTag(query, whole_tag=False)[source]

Allows fetching the value of a tag by tag name. Specify whole_tag=True and the [tag_name, tag_value] pair will be returned.

loopDict()[source]

Returns a hash of loop category -> loop.

loopIterator()[source]

Returns an iterator for saveframe loops.

loops = []
name = ''
printTree()[source]

Prints a summary, tree style, of the loops in the saveframe.

setTagPrefix(tag_prefix)[source]

Set the tag prefix for this saveframe.

sortTags(validation_schema=None)[source]

Sort the tags so they are in the same order as a BMRB schema. Will automatically use the standard schema if none is provided.

tagIterator()[source]

Returns an iterator for saveframe tags.

tag_prefix = None
tags = []
validate(validation_schema=None)[source]

Validate a saveframe against a STAR schema. You can pass your own custom schema if desired, otherwise the schema will be fetched from the BMRB servers. Returns a list of errors found. 0-length list indicates no errors found.

class ccpn.util.Bmrb.bmrb.loop(**kargs)[source]

Bases: object

A BMRB loop object.

addColumn(name, ignore_duplicates=False)[source]

Add a column to the column list. Does a bit of validation and parsing. Set ignore_duplicates to true to ignore attempts to add the same tag more than once rather than raise an exception.

You can also pass a list of column names to add more than one column at a time.

addData(the_list)[source]

Add a list to the data field. Items in list can be any type, they will be converted to string and formatted correctly. The list must have the same cardinality as the column names.

addDataByColumn(column_id, value)[source]

Add data to the loop one element at a time, based on column. Useful when adding data from SANS parsers.

category = None
clearData()[source]

Erases all data in this loop. Does not erase the data columns or loop category.

columns = []
compare(other)[source]

Returns the differences between two loops as a list. Otherwise returns 1 if different and 0 if equal. Order of loops being compared does not make a difference on the specific errors detected.

data = []
deleteDataByTagValue(tag, value, index_tag=None)[source]

Deletes all rows which contain the provided value in the provided column. If index_tag is provided, that column is renumbered starting with 1. Returns the deleted rows.

classmethod fromFile(the_file, csv=False)[source]

Create a saveframe by loading in a file. Specify csv=True is the file is a CSV file. If the_file starts with http://, https://, or ftp:// then we will use those protocols to attempt to open the file.

classmethod fromScratch(category=None)[source]

Create an empty saveframe that you can programatically add to. You may also pass the tag prefix as the second argument. If you do not pass the tag prefix it will be set the first time you add a tag.

classmethod fromString(the_string, csv=False)[source]

Create a saveframe by parsing a string. Specify csv=True is the string is in CSV format and not NMR-STAR format.

getDataAsCSV(header=True, show_category=True)[source]

Return the data contained in the loops, properly CSVd, as a string. Set header to False to omit the header. Set show_category to false to omit the loop category from the headers.

getDataByTag(tags=None)[source]

Identical to getTag but wraps the results in a list even if only fetching one tag. Primarily exists for legacy code.

getTag(tags=None, whole_tag=False)[source]

Provided a tag name (or a list of tag names), or ordinals corresponding to columns, return the selected tags by row as a list of lists.

printTree()[source]

Prints a summary, tree style, of the loop.

renumberRows(index_tag, start_value=1, maintain_ordering=False)[source]

Renumber a given column incrementally. Set start_value to initial value if 1 is not acceptable. Set maintain_ordering to preserve sequence with offset. E.g. 2,3,3,5 would become 1,2,2,4.

setCategory(category)[source]

Set the category of the loop. Usefull if you didn’t know the category at loop creation time.

sortRows(tags, key=None)[source]

Sort the data in the rows by their values for a given column or columns. Specify the columns using their names or ordinals. Accepts a list or an int/float. By default we will sort numerically. If that fails we do a string sort. Supply a function as key_func and we will order the elements based on the keys it provides. See the help for sorted() for more details. If you provide multiple columns to sort by, they are interpreted as increasing order of sort priority.

validate(validation_schema=None, category=None)[source]

Validate a loop against a STAR schema. You can pass your own custom schema if desired, otherwise the schema will be fetched from the BMRB servers. Returns a list of errors found. 0-length list indicates no errors found.

class ccpn.util.Bmrb.bmrb.schema(schema_file=None)[source]

Bases: object

A BMRB schema. Used to validate STAR files.

headers = []
schema = {}
schema_file = None
schema_order = []
types = {}
valType(tag, value, category=None, linenum=None)[source]
ccpn.util.Bmrb.bmrb.diff(entry1, entry2)[source]

Prints the differences between two entries. Non-equal entries will always be detected, but specific differences detected depends on order of entries.

ccpn.util.Bmrb.bmrb.validate(entry, schema=None)[source]

Prints a validation report of an entry.

ccpn.util.Bmrb.passthrough module

class ccpn.util.Bmrb.passthrough.Handler[source]

Bases: ccpn.util.Bmrb.sans.handlers.ContentHandler

comment(line, text)[source]
data(tag, tagline, val, valline, delim, inloop)[source]
endData(line, name)[source]
endLoop(line)[source]
endSaveframe(line, name)[source]
startData(line, name)[source]
startLoop(line)[source]
startSaveframe(line, name)[source]

ccpn.util.Bmrb.testNef module

Module Documentation here