<jupyter_start><jupyter_text>2. Generating a Sample using MS1 Controller In this notebook, we demonstrate how ViMMS can be used to generate a full-scan mzML file from a single sample. This corresponds to Section 3.1 of the paper.<jupyter_code>%matplotlib inline
%load_ext autoreload
%autoreload 2
import sys
sys.path.append('../..')
from pathlib import Path
from vimms.Chemicals import ChemicalCreator
from vimms.MassSpec import IndependentMassSpectrometer
from vimms.Controller import SimpleMs1Controller
from vimms.Environment import Environment
from vimms.Common import *<jupyter_output><empty_output><jupyter_text>Load previously trained spectral feature database and the list of extracted metabolites, created in **01. Download Data.ipynb**.<jupyter_code>base_dir = os.path.abspath('example_data')
ps = load_obj(Path(base_dir, 'peak_sampler_mz_rt_int_19_beers_fullscan.p'))
hmdb = load_obj(Path(base_dir, 'hmdb_compounds.p'))<jupyter_output><empty_output><jupyter_text>Set ViMMS logging level<jupyter_code>set_log_level_debug()<jupyter_output><empty_output><jupyter_text>Create Chemicals Define an output folder containing our results<jupyter_code>out_dir = Path(base_dir, 'results', 'MS1_single')<jupyter_output><empty_output><jupyter_text>Here we generate the chemical objects that will be used in the sample. The chemical objects are generated by sampling from metabolites in the HMDB database.<jupyter_code># the list of ROI sources created in the previous notebook '01. Download Data.ipynb'
ROI_Sources = [str(Path(base_dir,'DsDA', 'DsDA_Beer', 'beer_t10_simulator_files'))]

# minimum MS1 intensity of chemicals
min_ms1_intensity = 1.75E5

# m/z and RT range of chemicals
rt_range = [(0, 1440)]
mz_range = [(0, 1050)]

# the number of chemicals in the sample
n_chems = 6500

# maximum MS level (we do not generate fragmentation peaks when this value is 1)
ms_level = 1
chems = ChemicalCreator(ps, ROI_Sources, hmdb)
dataset = chems.sample(mz_range, rt_range, min_ms1_intensity, n_chems, ms_level)
save_obj(dataset, Path(out_dir, 'dataset.p'))
for chem in dataset[0:10]:
    print(chem)<jupyter_output>KnownChemical - 'C11H11F3N2O4' rt=262.33 max_intensity=187966.49
KnownChemical - 'C30H50O3' rt=429.48 max_intensity=549845.79
KnownChemical - 'C14H19NO10S2' rt=510.49 max_intensity=904802.52
KnownChemical - 'C14H26N2O3S2' rt=551.37 max_intensity=211304.57
KnownChemical - 'C15H22FN3O6' rt=598.74 max_intensity=375567.61
KnownChemical - 'C7H17N3' rt=892.56 max_intensity=356541.82
KnownChemical - 'C10H8O6' rt=426.60 max_intensity=1425028.33
KnownChemical - 'C21H21O10' rt=430.23 max_intensity=271710.28
KnownChemical - 'C26H20O7' rt=311.66 max_intensity=1456420.83
KnownChemical - 'C2H6O5S' rt=212.72 max_intensity=518452.38<jupyter_text>Run MS1 controller on the samples and generate .mzML files<jupyter_code>set_log_level_warning()
min_rt = rt_range[0][0]
max_rt = rt_range[0][1]
mass_spec = IndependentMassSpectrometer(POSITIVE, dataset, ps)
controller = SimpleMs1Controller()
# create an environment to run both the mass spec and controller
env = Environment(mass_spec, controller, min_rt, max_rt, progress_bar=True)

# set the log level to WARNING so we don't see too many messages when environment is running
set_log_level_warning()

# run the simulation
env.run()<jupyter_output>(1440.911s) ms_level=1: 100%|█████████▉| 1439.5008199999984/1440 [00:59<00:00, 24.17it/s]<jupyter_text>Simulated results are saved to the following .mzML file and can be viewed in tools like [ToppView](https://pubs.acs.org/doi/abs/10.1021/pr900171m) or using other mzML file viewers.<jupyter_code>set_log_level_debug()
mzml_filename = 'ms1_controller.mzML'
env.write_mzML(out_dir, mzml_filename)<jupyter_output>2019-12-12 11:26:31.002 | DEBUG    | vimms.Environment:write_mzML:142 - Writing mzML file to /home/joewandy/git/vimms/examples/example_data/results/MS1_single/ms1_controller.mzML
2019-12-12 11:26:34.475 | DEBUG    | vimms.Environment:write_mzML:149 - mzML file successfully written!
{'fraction_non_alphanumeric': 0.09797043347531947, 'fraction_numerical': 0.08318717113505388, 'mean_word_length': 6.812133072407045, 'pattern_counts': {'":': 0, '<': 27, '<?xml version=': 0, '>': 26, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '7', 'dir': 'jupyter-structured-clean-dedup', 'id': '179689', 'license': 'MIT', 'n_tokens_mistral': 1667, 'n_tokens_neox': 1431, 'n_words': 352, 'path': 'examples/01. vimms (Wandy et al 2019)/02. MS1 Simulations.ipynb', 'repo_name': 'hechth/vimms', 'size': '3991'}