<commit_before>import os
import dataset
import pickle
from .mzml_loader import mzml_to_hdf
from IPython.utils.traitlets import (
    HasTraits, CUnicode, CFloat, List, CInt, Bool, Instance)

NERSC_WORKSPACE = '/project/projectdirs/metatlas/workspace'


class Compound(HasTraits):

    """
    name: str
      Name of the compound
    formula : str
        Chemical formula of the intact, neutral form of the molecule.
    adducts : str
        Adduct accounting form the ionization and potential in source degradation.
    mz : float
        Mass-to-charge ratio.
    mz_threshold : float
        Threshold in ppm.
    rt_min : float
        Min retention time (minutes).
    rt_max : float
        Max retention time (minutes).
    rt_peak : float
        Peak retention time (minutes).
    neutral_mass : float
        The intact, neutral mass of the molecule
    pubchem_id : str
        Pubchem ID for compound.
    """
    name = CUnicode()
    formula = CUnicode()
    adducts = CUnicode()
    mz = CFloat()
    mz_threshold = CFloat()
    rt_min = CFloat()
    rt_max = CFloat()
    rt_peak = CFloat()
    neutral_mass = CFloat()
    pubchem_id = CFloat()


class Atlas(HasTraits):

    """
    name : str
        Common name of the atlas.
    compounds : list of strings
        Names of compounds (names of kbase workspace objects).
    sample : str
        Description of sample.
    method : str
        Description of method.
    """
    name = CUnicode()
    compounds = List(Instance(Compound))
    sample = CUnicode()
    method = CUnicode()


class FileSpec(HasTraits):

    """
    polarity : int, optional
        Polarity of ions in file.
    group : str, optional
        Group.
    inclusion_order : int, optional
        Inclusion order.
    normalization_factor : float, optional
        Normalization factor.
    retention_correction : float, optional
        Retention correction factor
    """
    polarity = CInt()
    group = CUnicode()
    inclusion_order = CInt()
    normalization_factor = CFloat()
    retention_correction = CFloat()


class FileInfo(FileSpec):

    """
    mzml_file : str
        Path to input_file.
    hdf_file : str
        Name of the hdf_file.
    polarity : int, optional
        Polarity of ions in file.
    group : str, optional
        Group.
    inclusion_order : int, optional
        Inclusion order.
    normalization_factor : float, optional
        Normalization factor.
    retention_correction : float, optional
        Retention correction factor
    """

    mzml_file = CUnicode()
    hdf_file = CUnicode()

    def parse(self):
        """Parse a file info spec"""
        self.hdf_file = mzml_to_hdf(self.mzml_file, self.hdf_file or None)


class Experiment(HasTraits):

    """
    name : str
        Name of the experiment.
    description : str
        Description of experiment.
    reconstitution_method : str
        Reconstitution method used in experiment.
    quenching_method : str
        Quenching method used in experiment.
    extraction_method : str<commit_msg>Add an app for editing atlases<commit_after>import os
import dataset
import pickle
import random
import pandas as pd
from .mzml_loader import mzml_to_hdf
from IPython.utils.traitlets import (
    HasTraits, CUnicode, CFloat, List, CInt, Bool, Instance)

NERSC_WORKSPACE = '/project/projectdirs/metatlas/workspace'


class _MetatlasObject(HasTraits):
    name = CUnicode()
    unique_id = CInt()

    def _unique_id_default(self):
        return random.randint(0, 1e6)


class Compound(_MetatlasObject):

    """
    name: str
      Name of the compound
    formula : str
        Chemical formula of the intact, neutral form of the molecule.
    adducts : str
        Adduct accounting form the ionization and potential in source degradation.
    mz : float
        Mass-to-charge ratio.
    mz_threshold : float
        Threshold in ppm.
    rt_min : float
        Min retention time (minutes).
    rt_max : float
        Max retention time (minutes).
    rt_peak : float
        Peak retention time (minutes).
    neutral_mass : float
        The intact, neutral mass of the molecule
    pubchem_id : str
        Pubchem ID for compound.
    """
    formula = CUnicode()
    adducts = CUnicode()
    mz = CFloat()
    mz_threshold = CFloat()
    rt_min = CFloat()
    rt_max = CFloat()
    rt_peak = CFloat()
    neutral_mass = CFloat()
    pubchem_id = CUnicode()


class Atlas(_MetatlasObject):

    """
    name : str
        Common name of the atlas.
    compounds : list of strings
        Names of compounds (names of kbase workspace objects).
    sample : str
        Descri 84t4ption of sample.
    method : str
        Description of method.
    """
    name = CUnicode()
    compounds = List(Instance(Compound))
    sample = CUnicode()
    method = CUnicode()

    def edit(self):
        import qgrid
        qgrid.nbinstall(overwrite=False)
        from IPython.html.widgets import Button, HBox
        from IPython.display import display

        # create a visualization for the dataframe
        if not self.compounds:
            self.compounds.append(Compound())
        data = [c._trait_values for c in self.compounds]
        dataframe = pd.DataFrame(data)
        cols = ['name', 'formula', 'adducts', 'mz', 'mz_threshold',
                'rt_min', 'rt_max', 'rt_peak', 'neutral_mass',
                'pubchem_id']
        grid = qgrid.QGridWidget(df=dataframe[cols], remote_js=True)

        add_row = Button(description="Add Row")
        add_row.on_click(grid.add_row)

        rem_row = Button(description="Remove Row")
        rem_row.on_click(grid.remove_row)

        display(HBox((add_row, rem_row)), grid)

        grid.on_msg(self._incoming_msg)

    def _incoming_msg(self, widget, msg):
        if msg['type'] == 'add_row':
            self.compounds.append(Compound())
        elif msg['type'] == 'remove_row':
            self.compounds.pop(msg['row'])
        elif msg['type'] == 'cell_change':
            c = self.compounds[msg['row']]
            setattr(c, msg['column'], msg['value'])


class FileSpec(_MetatlasObject):

    """
    polarity : int, optional
        Polarity of ions in file.
    group : str, optional
        Group.
    inclusion_order : int, optional
        Inclusion order.
    normalization_factor : float, optional
        Normalization factor.
    retention_correction : float, optional
        Retention correction factor
    """
    polarity = CInt()
    group = CUnicode()
    inclusion_order = CInt()
    normalization_factor = CFloat()
    retention_correction = CFloat()


class FileInfo(FileSpec):

    """
    mzml_file : str
        Path to input_file.
    hdf_file : str
        Name of the hdf_file.
    polarity : int, optional
        Polarity of ions in file.
    group : str, optional
        Group.
    inclusion_order : int, optional
        Inclusion order.
    normalization_factor : float, optional
        Normalization factor.
    retention_correction : float, optional
        Retention correction factor
    """

    mzml_file = CUnicode()
    hdf_file = CUnicode()

    def parse(self):
        """Parse a file info spec"""
        self.hdf_file = mzml_to_hdf(self.mzml_file, self.hdf_file or None)


class Experiment(_MetatlasObject):

    """
    name : str
        Name of the experiment.
    description : str
        Description of experiment.
    reconstitution_method : str
        Reconstitution method used in experiment.
    quenching_method : str
        Quenching method used in experiment.
    extraction_method : str
{'fraction_non_alphanumeric': 0.08281600845330868, 'fraction_numerical': 0.0007924976885484084, 'mean_word_length': 2.406207827260459, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3489123', 'n_tokens_mistral': 2292, 'n_tokens_neox': 2240, 'n_words': 660}