# Path: metatlas/io/h5_query.py
# def get_data(h5file, **kwargs):
# def get_chromatogram(h5file, min_mz, max_mz, aggregator=np.sum, **kwargs):
# def get_heatmap(h5file, mz_bins, **kwargs):
# def get_spectrogram(h5file, min_rt, max_rt, bins=2000, **kwargs):
# def get_info(h5file):


import sys
import qgrid
import pandas as pd
import os
import tables
import pickle
import h5py
import dill
import numpy as np
import os.path
import glob as glob
import json
import six
    import pickle
    import getpass
    import glob
    import getpass
from metatlas import metatlas_objects as metob
from metatlas.io import h5_query as h5q
from metatlas.helpers import metatlas_get_data_helper_fun as mgd
from matplotlib import pyplot as plt
from matplotlib import colors as matcolors
from matplotlib.widgets import RadioButtons, CheckButtons
from requests import Session
from rdkit import Chem
from rdkit.Chem import AllChem
from rdkit.Chem import Draw
from rdkit.Chem import Descriptors
from rdkit.Chem import rdMolDescriptors
from rdkit.Chem import AllChem
from rdkit.Chem import Draw
from rdkit.Chem import rdDepictor
from rdkit.Chem.Draw import rdMolDraw2D
from rdkit.Chem.Draw import IPythonConsole
from IPython.display import SVG,display
from PIL import Image
from six.moves import range
from six.moves import zip
    from requests import Session
        from copy import deepcopy
    # In item 1/2 I first fill the array with [0,i,0] values to define unique x/y locations 
    # for each spectrum and in the second line I then create the last column with start index
    # of the spectra which is just the cumulative-sum of the length of the spectra.
    # when you create the start stop locations you will need to:
    # prepend [0] to the cummulative sums (the first spectrum starts at 0 not its length).
    # remove the last entry to make sure the array has the correct length
    # That is why I did the following:
    # np.cumsum([0] + [ ri['m/z array'].shape[0] for ri in good_list ])[:-1]
    if not os.path.exists(target_directory):
        try:
            os.makedirs(target_directory)
        except OSError as exc: # Guard against race condition
            if exc.errno != errno.EEXIST:
                raise
    for myfile in myfiles:
        finfo = h5q.get_info(myfile)
        with tables.open_file(myfile) as fid:    
            num_pos_data = finfo['ms1_pos']['nrows'] + finfo['ms2_pos']['nrows']
            num_neg_data = finfo['ms1_neg']['nrows'] + finfo['ms2_neg']['nrows']
            do_polarity = []
            if num_pos_data > 0:
                do_polarity.append(1)
            if num_neg_data > 0:
                do_polarity.append(0)
            scan_polarity = []
            for my_polarity in do_polarity:
                container_file = os.path.join(target_directory,'container_file_polarity_%d.h5'%(my_polarity))
                if not os.path.isfile(container_file):
                    make_container=True
                if make_container:

# Path: metatlas/io/h5_query.py
# def get_data(h5file, **kwargs):
# def get_chromatogram(h5file, min_mz, max_mz, aggregator=np.sum, **kwargs):
# def get_heatmap(h5file, mz_bins, **kwargs):
# def get_spectrogram(h5file, min_rt, max_rt, bins=2000, **kwargs):
# def get_info(h5file):


# Path: metatlas/interfaces/pactolus_tools.py
import sys
import qgrid
import pandas as pd
import os
import tables
import pickle
import h5py
import dill
import numpy as np
import os.path
import glob as glob
import json
import six
    import pickle
    import getpass
    import glob
    import getpass
from metatlas import metatlas_objects as metob
from metatlas.io import h5_query as h5q
from metatlas.helpers import metatlas_get_data_helper_fun as mgd
from matplotlib import pyplot as plt
from matplotlib import colors as matcolors
from matplotlib.widgets import RadioButtons, CheckButtons
from requests import Session
from rdkit import Chem
from rdkit.Chem import AllChem
from rdkit.Chem import Draw
from rdkit.Chem import Descriptors
from rdkit.Chem import rdMolDescriptors
from rdkit.Chem import AllChem
from rdkit.Chem import Draw
from rdkit.Chem import rdDepictor
from rdkit.Chem.Draw import rdMolDraw2D
from rdkit.Chem.Draw import IPythonConsole
from IPython.display import SVG,display
from PIL import Image
from six.moves import range
from six.moves import zip
    from requests import Session
        from copy import deepcopy

    # In item 1/2 I first fill the array with [0,i,0] values to define unique x/y locations 
    # for each spectrum and in the second line I then create the last column with start index
    # of the spectra which is just the cumulative-sum of the length of the spectra.
    # when you create the start stop locations you will need to:
    # prepend [0] to the cummulative sums (the first spectrum starts at 0 not its length).
    # remove the last entry to make sure the array has the correct length
    # That is why I did the following:
    # np.cumsum([0] + [ ri['m/z array'].shape[0] for ri in good_list ])[:-1]
    if not os.path.exists(target_directory):
        try:
            os.makedirs(target_directory)
        except OSError as exc: # Guard against race condition
            if exc.errno != errno.EEXIST:
                raise
    for myfile in myfiles:
        finfo = h5q.get_info(myfile)
        with tables.open_file(myfile) as fid:    
            num_pos_data = finfo['ms1_pos']['nrows'] + finfo['ms2_pos']['nrows']
            num_neg_data = finfo['ms1_neg']['nrows'] + finfo['ms2_neg']['nrows']
            do_polarity = []
            if num_pos_data > 0:
                do_polarity.append(1)
            if num_neg_data > 0:
                do_polarity.append(0)
            scan_polarity = []
            for my_polarity in do_polarity:
                container_file = os.path.join(target_directory,'container_file_polarity_%d.h5'%(my_polarity))
                if not os.path.isfile(container_file):
                    make_container=True
                if make_container:

                    data = h5q.get_data(fid,ms_level=2,polarity=my_polarity,min_rt = min_rt,max_rt=max_rt,min_precursor_intensity=min_intensity)#TODO: filter by intensity,)