# Path: metatlas/datastructures/metatlas_objects.py
# class LcmsRun(MetatlasObject):
#     """An LCMS run is the reference to a file prepared with liquid
#     chromatography and mass spectrometry.
# 
#     The msconvert program is used to convert raw data (centroided is prefered)
#     to mzML.
# 
#     Note: These objects are not intented to be created directly, but by putting
#     the files in /project/projectdirs/metatlas/raw_data/<username> or by
#     running `load_lcms_files()`.
#     """
#     method = MetInstance(Method)
#     experiment = MetUnicode(help='The name of the experiment')
#     hdf5_file = MetUnicode(help='Path to the HDF5 file at NERSC')
#     mzml_file = MetUnicode(help='Path to the MZML file at NERSC')
#     acquisition_time = MetInt(help='Unix timestamp when data was acquired creation')
#     injection_volume = MetFloat()
#     injection_volume_units = MetEnum(('uL', 'nL'), 'uL')
#     pass_qc = MetBool(help= 'True/False for if the LCMS Run has passed a quality control assessment')
#     sample = MetInstance(Sample)
# 
# def retrieve(object_type, **kwargs):
#     """Get objects from the Metatlas object database.
# 
#     This will automatically select only objects created by the current
#     user unless `username` is provided. Use `username='*'` to search
#     against all users.
# 
#     Parameters
#     ----------
#     object_type: string
#       The type of object to search for (i.e. "Groups").
#     **kwargs
#       Specific search queries (i.e. name="Sargasso").
#       Use '%' for wildcard patterns (i.e. description='Hello%').
#       If you want to match a '%' character, use '%%'.
# 
#     Returns
#     -------
#     objects: list
#       List of Metatlas Objects meeting the criteria.  Will return the
#       latest version of each object.
#     """
#     workspace = Workspace.get_instance()
#     out = workspace.retrieve(object_type, **kwargs)
#     workspace.close_connection()
#     return out
# 
# def store(objects, **kwargs):
#     """Store Metatlas objects in the database.
# 
#     Parameters
#     ----------
#     objects: Metatlas object or list of Metatlas Objects
#         Object(s) to store in the database.
#     """
#     workspace = Workspace.get_instance()
#     workspace.save_objects(objects, **kwargs)
#     workspace.close_connection()


import os
import fcntl
import pwd
import re
import shutil
import sys
import time
import random
import smtplib
import traceback
import time
    import argparse
from collections import defaultdict
from subprocess import check_output
from datetime import datetime, time as dtime
from metatlas.mzml_loader import VERSION_TIMESTAMP
from metatlas.datastructures.metatlas_objects import LcmsRun, retrieve, store
from metatlas.io.mzml_loader import mzml_to_hdf
    from metatlas.metatlas_objects import find_invalid_runs
                os.makedirs(dname)
            try:
                shutil.copy(fname, pasteur_path)
            except IOError as e:
                readonly_files[username].add(dirname)
                continue

        # Get a lock on the mzml file to prevent interference.
        try:
            fid = open(fname, 'r')
            fcntl.flock(fid, fcntl.LOCK_EX | fcntl.LOCK_NB)
        except IOError:
            fid.close()
            msg = '%s already converting in another process\n' % fname
            sys.stderr.write(msg)
            sys.stderr.flush()
            continue

        # Convert to HDF and store the entry in the database.
        try:
            hdf5_file = fname.replace('mzML', 'h5')

            #Get Acquisition Time Here
            acquisition_time = get_acqtime_from_mzml(fname)
            mzml_to_hdf(fname, hdf5_file, True)
            os.chmod(hdf5_file, 0o660)
            description = info['experiment'] + ' ' + info['path']
            ctime = os.stat(fname).st_ctime
            # Add this to the database unless it is already there
            try:

# Path: metatlas/datastructures/metatlas_objects.py
# class LcmsRun(MetatlasObject):
#     """An LCMS run is the reference to a file prepared with liquid
#     chromatography and mass spectrometry.
# 
#     The msconvert program is used to convert raw data (centroided is prefered)
#     to mzML.
# 
#     Note: These objects are not intented to be created directly, but by putting
#     the files in /project/projectdirs/metatlas/raw_data/<username> or by
#     running `load_lcms_files()`.
#     """
#     method = MetInstance(Method)
#     experiment = MetUnicode(help='The name of the experiment')
#     hdf5_file = MetUnicode(help='Path to the HDF5 file at NERSC')
#     mzml_file = MetUnicode(help='Path to the MZML file at NERSC')
#     acquisition_time = MetInt(help='Unix timestamp when data was acquired creation')
#     injection_volume = MetFloat()
#     injection_volume_units = MetEnum(('uL', 'nL'), 'uL')
#     pass_qc = MetBool(help= 'True/False for if the LCMS Run has passed a quality control assessment')
#     sample = MetInstance(Sample)
# 
# def retrieve(object_type, **kwargs):
#     """Get objects from the Metatlas object database.
# 
#     This will automatically select only objects created by the current
#     user unless `username` is provided. Use `username='*'` to search
#     against all users.
# 
#     Parameters
#     ----------
#     object_type: string
#       The type of object to search for (i.e. "Groups").
#     **kwargs
#       Specific search queries (i.e. name="Sargasso").
#       Use '%' for wildcard patterns (i.e. description='Hello%').
#       If you want to match a '%' character, use '%%'.
# 
#     Returns
#     -------
#     objects: list
#       List of Metatlas Objects meeting the criteria.  Will return the
#       latest version of each object.
#     """
#     workspace = Workspace.get_instance()
#     out = workspace.retrieve(object_type, **kwargs)
#     workspace.close_connection()
#     return out
# 
# def store(objects, **kwargs):
#     """Store Metatlas objects in the database.
# 
#     Parameters
#     ----------
#     objects: Metatlas object or list of Metatlas Objects
#         Object(s) to store in the database.
#     """
#     workspace = Workspace.get_instance()
#     workspace.save_objects(objects, **kwargs)
#     workspace.close_connection()


# Path: metatlas/io/directory_watcher.py
import os
import fcntl
import pwd
import re
import shutil
import sys
import time
import random
import smtplib
import traceback
import time
    import argparse
from collections import defaultdict
from subprocess import check_output
from datetime import datetime, time as dtime
from metatlas.mzml_loader import VERSION_TIMESTAMP
from metatlas.datastructures.metatlas_objects import LcmsRun, retrieve, store
from metatlas.io.mzml_loader import mzml_to_hdf
    from metatlas.metatlas_objects import find_invalid_runs

                os.makedirs(dname)
            try:
                shutil.copy(fname, pasteur_path)
            except IOError as e:
                readonly_files[username].add(dirname)
                continue

        # Get a lock on the mzml file to prevent interference.
        try:
            fid = open(fname, 'r')
            fcntl.flock(fid, fcntl.LOCK_EX | fcntl.LOCK_NB)
        except IOError:
            fid.close()
            msg = '%s already converting in another process\n' % fname
            sys.stderr.write(msg)
            sys.stderr.flush()
            continue

        # Convert to HDF and store the entry in the database.
        try:
            hdf5_file = fname.replace('mzML', 'h5')

            #Get Acquisition Time Here
            acquisition_time = get_acqtime_from_mzml(fname)
            mzml_to_hdf(fname, hdf5_file, True)
            os.chmod(hdf5_file, 0o660)
            description = info['experiment'] + ' ' + info['path']
            ctime = os.stat(fname).st_ctime
            # Add this to the database unless it is already there
            try:

                runs = retrieve('lcmsrun', username='*', mzml_file=fname)