Skip to content

Commit

Permalink
Documentation update.
Browse files Browse the repository at this point in the history
  • Loading branch information
smelandr committed Jan 12, 2017
1 parent 72abe66 commit 2e85f76
Show file tree
Hide file tree
Showing 2 changed files with 122 additions and 121 deletions.
190 changes: 95 additions & 95 deletions docs/tutorial.rst
Original file line number Diff line number Diff line change
Expand Up @@ -187,101 +187,101 @@ using bracket accessors.

* Printing a :class:`~nmrstarlib.nmrstarlib.StarFile` and its components (`saveframe` and `loop` data):

>>> starfile.print_starfile(format="nmrstar")
data_18569
save_entry_information
_Entry.Sf_category entry_information
_Entry.Sf_framecode entry_information
_Entry.ID 18569
...
>>>
>>> starfile.print_starfile(format="json")
{
"data": "18569",
"save_entry_information": {
"Entry.Sf_category": "entry_information",
"Entry.Sf_framecode": "entry_information",
"Entry.ID": "18569",
...
}
>>>
>>> starfile.print_saveframe("save_entry_information", format="nmrstar")
_Entry.Sf_category entry_information
_Entry.Sf_framecode entry_information
_Entry.ID 18569
_Entry.Title
;
13C, 15N and 1H backbone and sidechain assignments of the
ENA-VASP homology 1 (EVH1) domain of the human
vasodilator-stimulated phosphoprotein (VASP)
;
_Entry.Type .
_Entry.Version_type original
_Entry.Submission_date 2012-07-05
_Entry.Accession_date 2012-07-05
_Entry.Last_release_date 2012-07-18
_Entry.Original_release_date 2012-07-18
_Entry.Origination author
_Entry.NMR_STAR_version 3.1.1.61
_Entry.Original_NMR_STAR_version 3.1
_Entry.Experimental_method NMR
_Entry.Experimental_method_subtype solution
_Entry.Details 'ANSIG v3.3 exported crosspeaks file'
_Entry.BMRB_internal_directory_name .
...
>>>
>>> starfile.print_saveframe("save_entry_information", format="json")
{
"Entry.Sf_category": "entry_information",
"Entry.Sf_framecode": "entry_information",
"Entry.ID": "18569",
"Entry.Title": ";\n13C, 15N and 1H backbone and sidechain assignments of the
ENA-VASP homology 1 (EVH1) domain of the human
vasodilator-stimulated phosphoprotein (VASP)\n;",
"Entry.Type": ".",
"Entry.Version_type": "original",
"Entry.Submission_date": "2012-07-05",
"Entry.Accession_date": "2012-07-05",
"Entry.Last_release_date": "2012-07-18",
"Entry.Original_release_date": "2012-07-18",
"Entry.Origination": "author",
"Entry.NMR_STAR_version": "3.1.1.61",
"Entry.Original_NMR_STAR_version": "3.1",
"Entry.Experimental_method": "NMR",
"Entry.Experimental_method_subtype": "solution",
"Entry.Details": "'ANSIG v3.3 exported crosspeaks file'",
"Entry.BMRB_internal_directory_name": ".",
>>> starfile.print_starfile(file_format="nmrstar")
data_18569
save_entry_information
_Entry.Sf_category entry_information
_Entry.Sf_framecode entry_information
_Entry.ID 18569
...
>>>
>>> starfile.print_starfile(file_format="json")
{
"data": "18569",
"save_entry_information": {
"Entry.Sf_category": "entry_information",
"Entry.Sf_framecode": "entry_information",
"Entry.ID": "18569",
...
}
>>>
>>> starfile.print_loop("save_entry_information", "loop_1", format="nmrstar")
_Data_set.Type
_Data_set.Count
_Data_set.Entry_ID
assigned_chemical_shifts 1 18569
spectral_peak_list 1 18569
>>>
>>> starfile.print_loop("save_entry_information", "loop_1", format="json")
[
[
"Data_set.Type",
"Data_set.Count",
"Data_set.Entry_ID"
],
[
{
"Data_set.Type": "assigned_chemical_shifts",
"Data_set.Count": "1",
"Data_set.Entry_ID": "18569"
},
{
"Data_set.Type": "spectral_peak_list",
"Data_set.Count": "1",
"Data_set.Entry_ID": "18569"
}
]
]
>>>
}
>>>
>>> starfile.print_saveframe("save_entry_information", file_format="nmrstar")
_Entry.Sf_category entry_information
_Entry.Sf_framecode entry_information
_Entry.ID 18569
_Entry.Title
;
13C, 15N and 1H backbone and sidechain assignments of the
ENA-VASP homology 1 (EVH1) domain of the human
vasodilator-stimulated phosphoprotein (VASP)
;
_Entry.Type .
_Entry.Version_type original
_Entry.Submission_date 2012-07-05
_Entry.Accession_date 2012-07-05
_Entry.Last_release_date 2012-07-18
_Entry.Original_release_date 2012-07-18
_Entry.Origination author
_Entry.NMR_STAR_version 3.1.1.61
_Entry.Original_NMR_STAR_version 3.1
_Entry.Experimental_method NMR
_Entry.Experimental_method_subtype solution
_Entry.Details 'ANSIG v3.3 exported crosspeaks file'
_Entry.BMRB_internal_directory_name .
...
>>>
>>> starfile.print_saveframe("save_entry_information", file_format="json")
{
"Entry.Sf_category": "entry_information",
"Entry.Sf_framecode": "entry_information",
"Entry.ID": "18569",
"Entry.Title": ";\n13C, 15N and 1H backbone and sidechain assignments of the
ENA-VASP homology 1 (EVH1) domain of the human
vasodilator-stimulated phosphoprotein (VASP)\n;",
"Entry.Type": ".",
"Entry.Version_type": "original",
"Entry.Submission_date": "2012-07-05",
"Entry.Accession_date": "2012-07-05",
"Entry.Last_release_date": "2012-07-18",
"Entry.Original_release_date": "2012-07-18",
"Entry.Origination": "author",
"Entry.NMR_STAR_version": "3.1.1.61",
"Entry.Original_NMR_STAR_version": "3.1",
"Entry.Experimental_method": "NMR",
"Entry.Experimental_method_subtype": "solution",
"Entry.Details": "'ANSIG v3.3 exported crosspeaks file'",
"Entry.BMRB_internal_directory_name": ".",
...
}
>>>
>>> starfile.print_loop("save_entry_information", "loop_1", file_format="nmrstar")
_Data_set.Type
_Data_set.Count
_Data_set.Entry_ID
assigned_chemical_shifts 1 18569
spectral_peak_list 1 18569
>>>
>>> starfile.print_loop("save_entry_information", "loop_1", file_format="json")
[
[
"Data_set.Type",
"Data_set.Count",
"Data_set.Entry_ID"
],
[
{
"Data_set.Type": "assigned_chemical_shifts",
"Data_set.Count": "1",
"Data_set.Entry_ID": "18569"
},
{
"Data_set.Type": "spectral_peak_list",
"Data_set.Count": "1",
"Data_set.Entry_ID": "18569"
}
]
]
>>>

* Accessing chemical shift data:

Expand Down Expand Up @@ -401,7 +401,7 @@ or in equivalent JSON format using :meth:`~nmrstarlib.nmrstarlib.StarFile.write(
* Writing into a NMR-STAR formatted file:

>>> with open("bmr18569_modified.str", "w") as outfile:
... starfile.write(outfile, fileformat="nmrstar")
... starfile.write(outfile, file_format="nmrstar")
>>>

* Writing into a JSONized NMR-STAR formatted file:
Expand Down
53 changes: 27 additions & 26 deletions nmrstarlib/nmrstarlib.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,20 +104,20 @@ def read(self, filehandle):
raise TypeError("Unknown file format")
filehandle.close()

def write(self, filehandle, fileformat):
def write(self, filehandle, file_format):
"""Write :class:`~nmrstarlib.nmrstarlib.StarFile` data into file.
:param filehandle: file-like object.
:type filehandle: :py:class:`io.TextIOWrapper`
:param str fileformat: Format to use to write data: `nmrstar` or `json`.
:param str file_format: Format to use to write data: `nmrstar` or `json`.
:return: None
:rtype: :py:obj:`None`
"""
try:
if fileformat == "json":
if file_format == "json":
json_str = self._to_json()
filehandle.write(json_str)
elif fileformat == "nmrstar":
elif file_format == "nmrstar":
nmrstar_str = self._to_nmrstar()
filehandle.write(nmrstar_str)
else:
Expand All @@ -126,18 +126,18 @@ def write(self, filehandle, fileformat):
raise IOError('"filehandle" parameter must be writable.')
filehandle.close()

def writestr(self, fileformat):
def writestr(self, file_format):
"""Write :class:`~nmrstarlib.nmrstarlib.StarFile` data into string.
:param str fileformat: Format to use to write data: `nmrstar` or `json`.
:param str file_format: Format to use to write data: `nmrstar` or `json`.
:return: String representing the :class:`~nmrstarlib.nmrstarlib.StarFile` instance.
:rtype: :py:class:`str`
"""
try:
if fileformat == "json":
if file_format == "json":
json_str = self._to_json()
return json_str
elif fileformat == "nmrstar":
elif file_format == "nmrstar":
nmrstar_str = self._to_nmrstar()
return nmrstar_str
else:
Expand Down Expand Up @@ -266,42 +266,42 @@ def _build_loop(self, lexer):
values = [OrderedDict(zip(fields, values[i:i + len(fields)])) for i in range(0, len(values), len(fields))]
return fields, values

def print_starfile(self, f=sys.stdout, format="nmrstar", tw=3):
def print_starfile(self, f=sys.stdout, file_format="nmrstar", tw=3):
"""Print :class:`~nmrstarlib.nmrstarlib.StarFile` into a file or stdout.
:param io.StringIO f: writable file-like stream.
:param str format: Format to use: `nmrstar` or `json`.
:param str file_format: Format to use: `nmrstar` or `json`.
:param int tw: Tab width.
:return: None
:rtype: :py:obj:`None`
"""
if format is "nmrstar":
if file_format is "nmrstar":
for saveframe in self.keys():
if saveframe == u"data":
print(u"{}_{}\n".format(saveframe, self[saveframe]), file=f)
elif saveframe.startswith(u"comment"):
print(u"{}".format(self[saveframe]), file=f)
else:
print(u"{}".format(saveframe), file=f)
self.print_saveframe(saveframe, f, format, tw)
self.print_saveframe(saveframe, f, file_format, tw)
print(u"\nsave_\n\n", file=f)

elif format is "json":
elif file_format is "json":
print(self._to_json(), file=f)

def print_saveframe(self, sf, f=sys.stdout, format="nmrstar", tw=3):
def print_saveframe(self, sf, f=sys.stdout, file_format="nmrstar", tw=3):
"""Print saveframe into a file or stdout.
We need to keep track of how far over everything is tabbed. The "tab width"
variable tw does this for us.
:param str sf: Saveframe name.
:param io.StringIO f: writable file-like stream.
:param str format: Format to use: `nmrstar` or `json`.
:param str file_format: Format to use: `nmrstar` or `json`.
:param int tw: Tab width.
:return: None
:rtype: :py:obj:`None`
"""
if format is "nmrstar":
if file_format is "nmrstar":
if sf == u"data":
print(u"{}_{}\n".format(sf, self[sf]), file=f)
else:
Expand All @@ -314,26 +314,26 @@ def print_saveframe(self, sf, f=sys.stdout, format="nmrstar", tw=3):
# handle loops
elif sftag[:5] == "loop_":
print(u"\n{}loop_".format(tw * u" "), file=f)
self.print_loop(sf, sftag, f, format, tw * 2)
self.print_loop(sf, sftag, f, file_format, tw * 2)
print(u"\n{}stop_".format(tw * u" "), file=f)
else:
print(u"{}_{}\t {}".format(tw * u" ", sftag, self[sf][sftag]), file=f)

elif format is "json":
elif file_format is "json":
print(json.dumps(self[sf], sort_keys=False, indent=4), file=f)

def print_loop(self, sf, sftag, f=sys.stdout, format="nmrstar", tw=3):
def print_loop(self, sf, sftag, f=sys.stdout, file_format="nmrstar", tw=3):
"""Print loop into a file or stdout.
:param str sf: Saveframe name.
:param str sftag: Saveframe tag, i.e. field name.
:param io.StringIO f: writable file-like stream.
:param str format: Format to use: `nmrstar` or `json`.
:param str file_format: Format to use: `nmrstar` or `json`.
:param int tw: Tab width.
:return: None
:rtype: :py:obj:`None`
"""
if format is "nmrstar":
if file_format is "nmrstar":
# First print the fields
for field in self[sf][sftag][0]:
print(u"{}_{}".format(tw * u" ", field), file=f)
Expand All @@ -344,7 +344,7 @@ def print_loop(self, sf, sftag, f=sys.stdout, format="nmrstar", tw=3):
for valuesdict in self[sf][sftag][1]:
print(u"{}{}".format(tw * u" ", u" ".join(valuesdict.values())), file=f)

elif format is "json":
elif file_format is "json":
print(json.dumps(self[sf][sftag], sort_keys=False, indent=4), file=f)

def _to_json(self):
Expand Down Expand Up @@ -478,7 +478,8 @@ def chem_shifts_by_residue(self, amino_acids=None, atoms=None, nmrstar_version="
def update_constants(filehandle):
"""Update constants related to NMR-STAR format, e.g. field names.
:param str filehandle: JSON file that contains information about NMR-STAR format.
:param filehandle: JSON file that contains information about NMR-STAR format.
:type filehandle: :py:class:`io.TextIOWrapper`
:return: None
:rtype: :py:obj:`None`
"""
Expand All @@ -489,7 +490,7 @@ def update_constants(filehandle):
def _generate_filenames(sources):
"""Generate filenames.
:param list sources: List of strings representing path to file(s).
:param tuple sources: Sequence of strings representing path to file(s).
:return: Path to file(s).
:rtype: :py:class:`str`
"""
Expand Down Expand Up @@ -532,7 +533,7 @@ def _generate_handles(filenames):
def read_files(*sources):
"""Construct a generator that yields :class:`~nmrstarlib.nmrstarlib.StarFile` instances.
:param str sources: One or more strings representing path to file(s).
:param sources: One or more strings representing path to file(s).
:return: :class:`~nmrstarlib.nmrstarlib.StarFile` instance(s).
:rtype: :class:`~nmrstarlib.nmrstarlib.StarFile`
"""
Expand Down Expand Up @@ -661,4 +662,4 @@ def __init__(self):
pass

def __str__(self):
return "Skipping save frame"
return "Skipping save frame"

0 comments on commit 2e85f76

Please sign in to comment.