In [ ]:
import nmrstarlib

In [ ]:
import nmrstarlib

single_starfile = nmrstarlib.read_files("bmr18569.str")  # single NMR-STAR file
starfiles = nmrstarlib.read_files("bmr18569.str", "bmr336.str") # several NMR-STAR files
dir_starfiles = nmrstarlib.read_files("starfiles_dir")   # directory of NMR-STAR files
arch_starfiles = nmrstarlib.read_files("starfiles.zip")  # archive of NMR-STAR files
url_starfile = nmrstarlib.read_files("18569")            # BMRB id of NMR-STAR file

In [ ]:
for starfile in nmrstarlib.read_files("18569", "15000"):
    print("BMRB id:", starfile.id)          # print BMRB id of StarFile
    print("File source:", starfile.source)  # print source of StarFile
    for saveframe_name in starfile.keys():  # print saveframe names
        print("\t", saveframe_name)

In [ ]:
sf_generator = nmrstarlib.read_files("18569", "15000")

starfile1 = next(sf_generator)
starfile2 = next(sf_generator)

In [ ]:
starfiles_list = list(nmrstarlib.read_files("18569", "15000"))

In [ ]:
import os
os.chdir('_static/nmrstarfiles')

In [ ]:
starfile = next(nmrstarlib.read_files("15000"))

# list StarFile-level keys, i.e. saveframe names
list(starfile.keys())

In [ ]:
# access "data" field 
starfile["data"]

In [ ]:
# access saveframe
starfile["save_entry_information"]

In [ ]:
# list saveframe-level keys
list(starfile["save_entry_information"].keys())

In [ ]:
# access 'key-value' pairs within saveframes
starfile["save_entry_information"]["Entry.Submission_date"]

In [ ]:
# access loops
starfile["save_entry_information"]["loop_0"]

In [ ]:
# list loop-level fields
starfile["save_entry_information"]["loop_0"][0]

In [ ]:
# list loop-level values (list of dictionaries)
starfile["save_entry_information"]["loop_0"][1]

In [ ]:
# every loop entry is accessed by index
starfile["save_entry_information"]["loop_0"][1][0]["Entry_author.Family_name"]

In [ ]:
# check submission date
starfile["save_entry_information"]["Entry.Submission_date"]

In [ ]:
# change submission date
starfile["save_entry_information"]["Entry.Submission_date"] = "2015-07-05"

In [ ]:
# check that submission date is updated
starfile["save_entry_information"]["Entry.Submission_date"]

In [ ]:
starfile = next(nmrstarlib.read_files("bmr15000.str"))

In [ ]:
starfile.print_file(file_format="nmrstar")

In [ ]:
starfile.print_file(file_format="json")

In [ ]:
starfile.print_saveframe("save_entry_information", file_format="nmrstar")

In [ ]:
starfile.print_saveframe("save_entry_information", file_format="json")

In [ ]:
starfile.print_loop("save_entry_information", "loop_0", file_format="nmrstar")

In [ ]:
starfile.print_loop("save_entry_information", "loop_0", file_format="json")

In [ ]:
starfile["save_assigned_chem_shift_list_1"]["loop_0"][0]

In [ ]:
starfile["save_assigned_chem_shift_list_1"]["loop_0"][1][0]["Atom_chem_shift.Seq_ID"]

In [ ]:
starfile["save_assigned_chem_shift_list_1"]["loop_0"][1][0]["Atom_chem_shift.Comp_ID"]

In [ ]:
starfile["save_assigned_chem_shift_list_1"]["loop_0"][1][0]["Atom_chem_shift.Atom_ID"]

In [ ]:
starfile["save_assigned_chem_shift_list_1"]["loop_0"][1][0]["Atom_chem_shift.Val"]

In [ ]:
starfile["save_assigned_chem_shift_list_1"]["loop_0"][1][1]["Atom_chem_shift.Atom_ID"]

In [ ]:
starfile["save_assigned_chem_shift_list_1"]["loop_0"][1][1]["Atom_chem_shift.Val"]

In [ ]:
starfile["save_assigned_chem_shift_list_1"]["loop_0"][1][2]["Atom_chem_shift.Atom_ID"]

In [ ]:
starfile["save_assigned_chem_shift_list_1"]["loop_0"][1][2]["Atom_chem_shift.Val"]

In [ ]:
# access all chemical shifts
starfile.chem_shifts_by_residue()

In [ ]:
# access chemical shifts for "SER" and "GLU" amino acids
starfile.chem_shifts_by_residue(amino_acids=["SER", "GLU"])

In [ ]:
# access chemical shifts for "SER" and "GLU" amino acids for "CB" and "CG" atoms 
starfile.chem_shifts_by_residue(amino_acids=["SER", "GLU"], atoms=["CB", "CG"])

In [ ]:
# acceess chemical shifts for specific amino acid and specific atom
starfile.chem_shifts_by_residue(amino_acids_and_atoms={"SER":["HA", "HB2", "HB3"], "ASP": ["CA", "N"]})

In [ ]:
with open("out/bmr15000_modified.str", "w") as outfile:
    starfile.write(outfile, file_format="nmrstar")

In [ ]:
with open("out/bmr15000_modified.json", "w") as outfile:
    starfile.write(outfile, file_format="json")

In [ ]:
from nmrstarlib.converter import Converter
from nmrstarlib.translator import StarFileToStarFile

# Using valid BMRB id to access file from URL: from_path="18569"
converter = Converter(StarFileToStarFile(from_path="18569", to_path="out/bmr18569.json",
                                         from_format="nmrstar", to_format="json"))
converter.convert()

In [ ]:
from nmrstarlib.converter import Converter
from nmrstarlib.translator import StarFileToStarFile

# Using generated above "bmr18569.json" file
converter = Converter(StarFileToStarFile(from_path="bmr18569.json", to_path="out/bmr18569.str",
                                         from_format="json", to_format="nmrstar"))
converter.convert()

In [ ]:
from nmrstarlib.converter import Converter
from nmrstarlib.translator import StarFileToStarFile

converter = Converter(StarFileToStarFile(from_path="starfiles_dir_nmrstar", to_path="out/starfiles_dir_json",
                                         from_format="nmrstar", to_format="json"))
converter.convert()

In [ ]:
from nmrstarlib.converter import Converter
from nmrstarlib.translator import StarFileToStarFile

converter = Converter(StarFileToStarFile(from_path="starfiles_dir_json", to_path="out/starfiles_dir_nmrstar",
                                         from_format="json", to_format="nmrstar"))
converter.convert()

In [ ]:
from nmrstarlib.converter import Converter
from nmrstarlib.translator import StarFileToPeakList

# Using valid BMRB id to access file from URL: from_path="18569"
converter = Converter(StarFileToPeakList(from_path="18569", to_path="out/18569_HNcoCACB.txt",
                                         from_format="nmrstar", to_format="sparky",
                                         spectrum_name="HNcoCACB"))
converter.convert()

In [ ]:
from nmrstarlib.converter import Converter
from nmrstarlib.translator import StarFileToPeakList

# Using valid BMRB id to access file from URL: from_path="18569"
converter = Converter(StarFileToPeakList(from_path="18569", to_path="out/18569_HNcoCACB.json",
                                         from_format="nmrstar", to_format="json",
                                         spectrum_name="HNcoCACB"))
converter.convert()

In [ ]:
from nmrstarlib.converter import Converter
from nmrstarlib.translator import StarFileToPeakList
from nmrstarlib.noise import NoiseGenerator

# create parameters dictionary for random normal distribution
parameters = {"H_loc": [0], "C_loc": [0], "N_loc": [0],
              "H_scale": [0.001], "C_scale": [0.01], "N_scale": [0.01]}

# create random normal noise generator
random_normal_noise_generator = NoiseGenerator(parameters)

# Using valid BMRB id to access file from URL: from_path="18569"
converter = Converter(StarFileToPeakList(from_path="18569", to_path="out/18569_HNcoCACB_ssv_HCN.txt",
                                         from_format="nmrstar", to_format="sparky",
                                         spectrum_name="HNcoCACB",
                                         noise_generator=random_normal_noise_generator))
converter.convert()

In [ ]:
from nmrstarlib.converter import Converter
from nmrstarlib.translator import StarFileToPeakList
from nmrstarlib.noise import NoiseGenerator

# create parameters dictionary for random normal distribution
parameters = {"H_loc": [0], "C_loc": [None], "N_loc": [0],
              "H_scale": [0.001], "C_scale": [None], "N_scale": [0.01]}

# create random normal noise generator
random_normal_noise_generator = NoiseGenerator(parameters)

# Using valid BMRB id to access file from URL: from_path="18569"
converter = Converter(StarFileToPeakList(from_path="18569", to_path="out/18569_HNcoCACB_ssv_HN.txt",
                                         from_format="nmrstar", to_format="sparky",
                                         spectrum_name="HNcoCACB",
                                         noise_generator=random_normal_noise_generator))
converter.convert()

In [ ]:
from nmrstarlib.converter import Converter
from nmrstarlib.translator import StarFileToPeakList
from nmrstarlib.noise import NoiseGenerator

# create parameters dictionary for random normal distribution
parameters = {"H_loc": [0, 0], "C_loc": [None, None], "N_loc": [0, 0],
              "H_scale": [0.001, 0.005], "C_scale": [None, None], "N_scale": [0.01, 0.05]}

# create random normal noise generator
random_normal_noise_generator = NoiseGenerator(parameters)

# Using valid BMRB id to access file from URL: from_path="18569"
converter = Converter(StarFileToPeakList(from_path="18569", to_path="out/18569_HNcoCACB_tsv_HN.txt",
                                         from_format="nmrstar", to_format="sparky",
                                         spectrum_name="HNcoCACB",
                                         plsplit=(70,30),
                                         noise_generator=random_normal_noise_generator))
converter.convert()

In [ ]:
from nmrstarlib.converter import Converter
from nmrstarlib.translator import StarFileToPeakList
from nmrstarlib.noise import NoiseGenerator

# create parameters dictionary for distribution
parameters = {"H_df": [5], "C_df": [None], "N_df": [5]}

# create chisquare noise generator
chisquare_noise_generator = NoiseGenerator(parameters, distribution_name="chisquare")

# Using valid BMRB id to access file from URL: from_path="18569"
converter = Converter(StarFileToPeakList(from_path="18569", to_path="out/18569_HNcoCACB_ssv_HN_chi2.txt",
                                         from_format="nmrstar", to_format="sparky",
                                         spectrum_name="HNcoCACB",
                                         noise_generator=chisquare_noise_generator))
converter.convert()

In [ ]:
nmrstarlib.nmrstarlib.list_spectrums()

In [ ]:
nmrstarlib.nmrstarlib.list_spectrum_descriptions()

In [ ]:
nmrstarlib.nmrstarlib.list_spectrum_descriptions("HNcoCACB", "NCACX")

In [ ]:
from nmrstarlib.converter import Converter
from nmrstarlib.translator import StarFileToPeakList
from nmrstarlib.noise import NoiseGenerator

# update SPECTRUM_DESCRIPTIONS
nmrstarlib.nmrstarlib.update_constants(spectrum_descriptions_cfg="path/to/custom_spectrum_description.json")

# create parameters dictionary for random normal distribution
parameters = {"H_loc": [None, None], "C_loc": [0, 0], "N_loc": [0, 0],
              "H_scale": [None, None], "C_scale": [0.01, 0.05], "N_scale": [0.01, 0.05]}

# create random normal noise generator
random_normal_noise_generator = NoiseGenerator(parameters)

converter = Converter(StarFileToPeakList(from_path="18569", to_path="out/18569_NCACX_custom.txt",
                                         from_format="nmrstar", to_format="sparky",
                                         spectrum_name="NCACX_custom",
                                         plsplit=(70,30),
                                         noise_generator=random_normal_noise_generator))
converter.convert()

In [ ]:
from nmrstarlib.converter import Converter
from nmrstarlib.translator import StarFileToPeakList
from nmrstarlib.noise import NoiseGenerator

custom_experiment_type = {
    "NCACX_custom": {
        "Labels": ["N", "CA", "CX"],
        "MinNumberPeaksPerSpinSystem": 2,
        "PeakDescriptions": [
            {"fraction": 1, "dimensions": ["N", "CA", "CO"]},
            {"fraction": 1, "dimensions": ["N", "CA", "CA"]},
            {"fraction": 1, "dimensions": ["N", "CA", "CB"]}
        ]
    }
}

# update SPECTRUM_DESCRIPTION
nmrstarlib.nmrstarlib.SPECTRUM_DESCRIPTIONS.update(custom_experiment_type)

# create parameters dictionary for random normal distribution
parameters = {"H_loc": [0, 0], "C_loc": [None, None], "N_loc": [0, 0],
              "H_scale": [0.001, 0.005], "C_scale": [None, None], "N_scale": [0.01, 0.05]}

# create random normal noise generator
random_normal_noise_generator = NoiseGenerator(parameters)

# Using valid BMRB id to access file from URL: from_path="18569"
converter = Converter(StarFileToPeakList(from_path="18569", to_path="out/18569_NCACX_custom.txt",
                                         from_format="nmrstar", to_format="sparky",
                                         spectrum_name="NCACX_custom",
                                         plsplit=(70,30),
                                         noise_generator=random_normal_noise_generator))
converter.convert()

In [ ]:
from nmrstarlib.csviewer import CSViewer

csviewer = CSViewer(from_path="18569", filename="out/18569_chem_shifts_all", csview_format="png")
csviewer.csview(view=False)

In [ ]:
from nmrstarlib.csviewer import CSViewer

csviewer = CSViewer(from_path="18569", amino_acids=["GLU", "THR"], atoms=["CA", "CB", "CG", "CG2"],
                    filename="out/18569_chem_shifts_SER_THR_CA_CB_CG_CG2", csview_format="png")
csviewer.csview(view=False)

In [ ]:
from nmrstarlib.csviewer import CSViewer

csviewer = CSViewer(from_path="18569", amino_acids_and_atoms={"GLU": ["CA", "CB"], "THR": ["HA", "HB"]},
                    filename="out/18569_chem_shifts_GLU_CA_CB_THR_HA_HB", csview_format="png")
csviewer.csview(view=False)

In [ ]:
! python3 -m nmrstarlib --help

In [ ]:
! python3 -m nmrstarlib convert bmr18569.str out/bmr18569.json \
          --from-format=nmrstar --to-format=json

In [ ]:
! python3 -m nmrstarlib convert bmr18569.json out/bmr18569.str \
          --from-format=json --to-format=nmrstar

In [ ]:
! python3 -m nmrstarlib convert bmr18569.str.gz out/bmr18569.json.gz \
          --from-format=nmrstar --to-format=json

In [ ]:
! python3 -m nmrstarlib convert bmr18569.json.gz out/bmr18569.str.gz \
          --from-format=json --to-format=nmrstar

In [ ]:
! python3 -m nmrstarlib convert 18569 out/bmr18569.json.bz2 \
          --from-format=nmrstar --to-format=json

In [ ]:
! python3 -m nmrstarlib convert starfiles_dir_nmrstar out/starfiles_dir_json \
          --from-format=nmrstar --to-format=json

In [ ]:
! python3 -m nmrstarlib convert starfiles_dir_json out/starfiles_dir_nmrstar \
          --from-format=json --to-format=nmrstar

In [ ]:
! python3 -m nmrstarlib convert starfiles_dir_nmrstar out/starfiles_json.zip \
          --from-format=nmrstar --to-format=json

In [ ]:
! python3 -m nmrstarlib convert starfiles_json.tar.gz out/starfiles_dir_nmrstar \
          --from-format=json --to-format=nmrstar

In [ ]:
! python3 -m nmrstarlib convert starfiles_nmrstar.zip out/starfiles_json.tar.bz2 \
          --from-format=nmrstar --to-format=json

In [ ]:
! python3 -m nmrstarlib plsimulate bmr18569.str out/18569_HNcoCACB.txt HNcoCACB \
          --from-format=nmrstar --to-format=sparky

In [ ]:
! python3 -m nmrstarlib plsimulate 18569 out/18569_HNcoCACB_ssv_HCN.txt HNcoCACB \
          --from-format=nmrstar --to-format=sparky \
          --H=0,0.001 --N=0,0.01 --C=0,0.01

In [ ]:
! python3 -m nmrstarlib plsimulate 18569 out/18569_HNcoCACB_ssv_HCN_chi2.txt HNcoCACB \
          --from-format=nmrstar --to-format=sparky \
          --H=5 --N=5 --C=5 --distribution=chisquare

In [ ]:
! python3 -m nmrstarlib plsimulate bmr18569.str.gz out/18569_HNcoCACB_ssv_HN.txt HNcoCACB \
          --from-format=nmrstar --to-format=sparky \
          --H=0,0.001 --N=0,0.01

In [ ]:
! python3 -m nmrstarlib plsimulate 18569 out/18569_HNcoCACB_tsv_HN.txt HNcoCACB \
          --from-format=nmrstar --to-format=sparky \
          --plsplit=70,30 --H=0:0,0.001:0.005 --N=0:0,0.01:0.05

In [ ]:
! python3 -m nmrstarlib plsimulate starfiles_dir_nmrstar out/peaklists_dir HNcoCACB \
          --from-format=nmrstar --to-format=sparky

In [ ]:
! python3 -m nmrstarlib plsimulate starfiles_dir_nmrstar out/peaklists.zip HNcoCACB \
          --from-format=nmrstar --to-format=sparky --H=0,0.001 --N=0,0.01

In [ ]:
! python3 -m nmrstarlib plsimulate starfiles_dir_nmrstar out/peaklists.tar.gz NCACX \
          --from-format=nmrstar --to-format=sparky --plsplit=70,30 \
          --C=0:0,0.01:0.05 --N=0:0,0.01:0.07

In [ ]:
! python3 -m nmrstarlib csview 18569 --csview-outfile=out/18569_chem_shifts_all \
          --csview-format=png

In [ ]:
! python3 -m nmrstarlib csview 18569 --aa=GLU,THR --at=CA,CB,CG,CG2 \
          --csview-outfile=out/18569_chem_shifts_GLU_THR_CA_CB_CG_CG2 \
          --csview-format=png

In [ ]:
! python3 -m nmrstarlib csview 18569 --aa-at=GLU-CA,CB:THR-HA,HB \
          --csview-outfile=out/18569_chem_shifts_GLU_CA_CB_THR_HA_HB \
          --csview-format=png