# Path: metatlas/io/update_lcmsfiles_in_lims.py
# EXTENSIONS = {'mzml':'.mzML',
#         'hdf5':'.h5',
#         'spectralhits':'_spectral-hits.tab.gz',
#         'pactolus':'.pactolus.gz',
#         'raw':'.raw'}


import numpy as np
import sys
import json
import pandas as pd
import re
import glob as glob
import os
import six
import time
import sys
import os
import pathlib
import argparse
import hashlib
import requests
import json
import pandas as pd
import numpy as np
import re
import time
import math
import collections
import xmltodict
import zipfile
    import gspread
    import subprocess
import numpy as np
import scipy.sparse as sp
    import subprocess
    import os.path
from collections import defaultdict
from xml.etree import cElementTree as ET
from io import StringIO
from metatlas.io.update_lcmsfiles_in_lims import EXTENSIONS
from collections import Mapping
from pathlib2 import PurePath
from subprocess import call
from labkey.api_wrapper import APIWrapper
from datetime import datetime, time as dtime
from subprocess import check_output
from ast import literal_eval
from copy import deepcopy
    from oauth2client.service_account import ServiceAccountCredentials
from pyteomics import mgf
    if lcmsruns is None:
        lcmsruns = get_lcmsrun_matrix()

    done_input_files = lcmsruns.loc[pd.notna(lcmsruns['%s_filename'%input_type]),'%s_filename'%input_type]
    done_output_files = lcmsruns.loc[pd.notna(lcmsruns['%s_filename'%output_type]),'%s_filename'%output_type]

    task_idx = file_conversion_tasks['task']==task
    inputfile_idx = file_conversion_tasks['input_file'].isin(done_input_files)
    outputfile_idx = file_conversion_tasks['output_file'].isin(done_output_files)

    # This finds where output file exists
    done_tasks_idx = (task_idx) & (outputfile_idx)
    if sum(done_tasks_idx)>0:
        update_table_in_lims(file_conversion_tasks.loc[done_tasks_idx,['Key']],'file_conversion_task',method='delete')#,labkey_server=labkey_server,project_name=project_name)
        print(('%s: There are %d tasks where output file exist and will be removed'%(task,file_conversion_tasks[done_tasks_idx].shape[0])))
    
    
    # This finds where input file is missing
    done_tasks_idx = (task_idx) & (~inputfile_idx)
    if sum(done_tasks_idx)>0:
        update_table_in_lims(file_conversion_tasks.loc[done_tasks_idx,['Key']],'file_conversion_task',method='delete')#,labkey_server=labkey_server,project_name=project_name)
        print(('%s: There are %d tasks where input file is missing and will be removed'%(task,file_conversion_tasks[done_tasks_idx].shape[0])))

    right_now_str = datetime.now().strftime("%Y%m%d %H:%M:%S")

    idx = (pd.notna(lcmsruns['%s_filename'%input_type])) & (pd.isna(lcmsruns['%s_filename'%output_type]))

    temp = pd.DataFrame()

    temp['input_file'] = lcmsruns.loc[idx,'%s_filename'%input_type]

# Path: metatlas/io/update_lcmsfiles_in_lims.py
# EXTENSIONS = {'mzml':'.mzML',
#         'hdf5':'.h5',
#         'spectralhits':'_spectral-hits.tab.gz',
#         'pactolus':'.pactolus.gz',
#         'raw':'.raw'}


# Path: metatlas/untargeted/tools.py
import numpy as np
import sys
import json
import pandas as pd
import re
import glob as glob
import os
import six
import time
import sys
import os
import pathlib
import argparse
import hashlib
import requests
import json
import pandas as pd
import numpy as np
import re
import time
import math
import collections
import xmltodict
import zipfile
    import gspread
    import subprocess
import numpy as np
import scipy.sparse as sp
    import subprocess
    import os.path
from collections import defaultdict
from xml.etree import cElementTree as ET
from io import StringIO
from metatlas.io.update_lcmsfiles_in_lims import EXTENSIONS
from collections import Mapping
from pathlib2 import PurePath
from subprocess import call
from labkey.api_wrapper import APIWrapper
from datetime import datetime, time as dtime
from subprocess import check_output
from ast import literal_eval
from copy import deepcopy
    from oauth2client.service_account import ServiceAccountCredentials
from pyteomics import mgf

    if lcmsruns is None:
        lcmsruns = get_lcmsrun_matrix()

    done_input_files = lcmsruns.loc[pd.notna(lcmsruns['%s_filename'%input_type]),'%s_filename'%input_type]
    done_output_files = lcmsruns.loc[pd.notna(lcmsruns['%s_filename'%output_type]),'%s_filename'%output_type]

    task_idx = file_conversion_tasks['task']==task
    inputfile_idx = file_conversion_tasks['input_file'].isin(done_input_files)
    outputfile_idx = file_conversion_tasks['output_file'].isin(done_output_files)

    # This finds where output file exists
    done_tasks_idx = (task_idx) & (outputfile_idx)
    if sum(done_tasks_idx)>0:
        update_table_in_lims(file_conversion_tasks.loc[done_tasks_idx,['Key']],'file_conversion_task',method='delete')#,labkey_server=labkey_server,project_name=project_name)
        print(('%s: There are %d tasks where output file exist and will be removed'%(task,file_conversion_tasks[done_tasks_idx].shape[0])))
    
    
    # This finds where input file is missing
    done_tasks_idx = (task_idx) & (~inputfile_idx)
    if sum(done_tasks_idx)>0:
        update_table_in_lims(file_conversion_tasks.loc[done_tasks_idx,['Key']],'file_conversion_task',method='delete')#,labkey_server=labkey_server,project_name=project_name)
        print(('%s: There are %d tasks where input file is missing and will be removed'%(task,file_conversion_tasks[done_tasks_idx].shape[0])))

    right_now_str = datetime.now().strftime("%Y%m%d %H:%M:%S")

    idx = (pd.notna(lcmsruns['%s_filename'%input_type])) & (pd.isna(lcmsruns['%s_filename'%output_type]))

    temp = pd.DataFrame()

    temp['input_file'] = lcmsruns.loc[idx,'%s_filename'%input_type]

    temp['output_file'] = temp['input_file'].apply(lambda x: re.sub('\%s$'%EXTENSIONS[input_type],'%s'%EXTENSIONS[output_type],x))