def singleThermo(sbml_paths,
                 pathway_id,
                 tmpOutputFolder,
                 ph=7.0,
                 ionic_strength=200,
                 pMg=10.0,
                 temp_k=298.15,
                 stdev_factor=1.96):
    """Given a list of rpSBML input files, perform thermodynamics analysis. Less memory effecient than the _hdd method but faster

    :param sbml_paths: The list of rpSBML paths passed to calculate thermodynamics
    :param pathway_id: The id of the heterologous pathway of interest
    :param tmpOutputFolder: The path to the output folder to write the result rpSBML file
    :param ph: The pH of the host organism (Default: 7.0)
    :param ionic_strength: Ionic strenght of the host organism (Default: 200.0)
    :param pMg: The pMg of the host organism (Default: 10.0)
    :param temp_k: The temperature of the host organism in Kelvin (Default: 298.15)
    :param stdev_factor: The standard deviation factor to calculate MDF (Default: 1.96)

    :type sbml_paths: str
    :type pathway_id: str
    :type tmpOutputFolder: str
    :type ph: float
    :type ionic_strength: float
    :type pMg: float
    :type temp_k: float
    :type stdev_factor: float

    :rtype: bool
    :return: Success or failure of the function
    """
    rpequilibrator = rpEquilibrator.rpEquilibrator(
        ph=ph,
        ionic_strength=ionic_strength,
        pMg=pMg,
        temp_k=temp_k,
        stdev_factor=stdev_factor)
    for sbml_path in sbml_paths:
        logging.debug('Calculating the thermodynamics of the pathway ' +
                      str(pathway_id) + ' for the file: ' + str(sbml_path))
        file_name = sbml_path.split('/')[-1].replace('.sbml', '').replace(
            '.xml', '').replace('.rpsbml', '').replace('_rpsbml', '')
        rpsbml = rpSBML.rpSBML(file_name, path=sbml_path)
        rpequilibrator.rpsbml = rpsbml
        res = rpequilibrator.pathway(
            pathway_id, True)  #ignore the results because written in SBML file
        rpsbml.writeSBML(tmpOutputFolder)
        rpsbml = None
    return True
def runEqSBtab_hdd(inputTar,
                   outputTar,
                   pathway_id='rp_pathway',
                   fba_id=None,
                   thermo_id='dfG_prime_o',
                   ph=7.0,
                   ionic_strength=200,
                   pMg=10.0,
                   temp_k=298.15,
                   stdev_factor=1.96):
    """Given a tar input file, perform MDF analysis for each rpSBML file.

    :param inputTar: The path to the input TAR file
    :param outputTar: The path to the output TAR file
    :param pathway_id: The id of the heterologous pathway of interest (Default: rp_pathway)
    :param fba_id: The id of the FBA value. Default sets all FBA values to 1.0 and if specified (Default: None)
    :param thermo_id: The id of the thermodynamics id (Default: dfG_prime_o)
    :param ph: The pH of the host organism (Default: 7.0)
    :param ionic_strength: Ionic strenght of the host organism (Default: 200.0)
    :param pMg: The pMg of the host organism (Default: 10.0)
    :param temp_k: The temperature of the host organism in Kelvin (Default: 298.15)
    :param stdev_factor: The standard deviation factor to calculate MDF (Default: 1.96)

    :type inputTar: str
    :type outputTar: str
    :type pathway_id: str
    :type tmpOutputFolder: str
    :type ph: float
    :type ionic_strength: float
    :type pMg: float
    :type temp_k: float
    :type stdev_factor: float

    :rtype: bool
    :return: Success or failure of the function
    """
    rpequilibrator = rpEquilibrator.rpEquilibrator(
        ph=ph, ionic_strength=ionic_strength, pMg=pMg, temp_k=temp_k)
    with tempfile.TemporaryDirectory() as tmpInputFolder:
        with tempfile.TemporaryDirectory() as tmpOutputFolder:
            tar = tarfile.open(inputTar, mode='r')
            tar.extractall(path=tmpInputFolder)
            tar.close()
            if len(glob.glob(tmpInputFolder + '/*')) == 0:
                logging.error('Input file is empty')
                return False
            for sbml_path in glob.glob(tmpInputFolder + '/*'):
                logging.debug('=========== ' + str(sbml_path) +
                              ' ============')
                fileName = sbml_path.split('/')[-1].replace(
                    '.sbml',
                    '').replace('.xml', '').replace('.rpsbml',
                                                    '').replace('_rpsbml', '')
                rpsbml = rpSBML.rpSBML(fileName, path=sbml_path)
                rpequilibrator.rpsbml = rpsbml
                status = rpequilibrator.toNetworkSBtab(
                    os.path.join(tmpOutputFolder, fileName + '.tsv'),
                    pathway_id, thermo_id, fba_id, stdev_factor)
                rpsbml = None
            if len(glob.glob(tmpOutputFolder + '/*')) == 0:
                logging.error('rpThermo has not produced any results')
                return False
            with tarfile.open(outputTar, mode='w:gz') as ot:
                for sbml_path in glob.glob(tmpOutputFolder + '/*'):
                    fileName = str(sbml_path.split('/')[-1])
                    info = tarfile.TarInfo(fileName)
                    info.size = os.path.getsize(sbml_path)
                    ot.addfile(tarinfo=info, fileobj=open(sbml_path, 'rb'))
    return True
 def test_MDF(self):
     rpsbml = rpSBML.rpSBML('test',
                            path=os.path.join('data', 'rpsbml_pathway.xml'))
     rpeq = rpEquilibrator.rpEquilibrator(rpsbml)
     mdf = rpeq.MDF()
     self.assertAlmostEqual(mdf, 205.53445861091302)
def runThermo_hdd(inputTar,
                  outputTar,
                  pathway_id='rp_pathway',
                  ph=7.0,
                  ionic_strength=200,
                  pMg=10.0,
                  temp_k=298.15):
    """Given a tar input file, perform thermodynamics analysis for each rpSBML file.

    :param inputTar: The path to the input TAR file
    :param outputTar: The path to the output TAR file
    :param pathway_id: The id of the heterologous pathway of interest
    :param ph: The pH of the host organism (Default: 7.0)
    :param ionic_strength: Ionic strenght of the host organism (Default: 200.0)
    :param pMg: The pMg of the host organism (Default: 10.0)
    :param temp_k: The temperature of the host organism in Kelvin (Default: 298.15)
    :param stdev_factor: The standard deviation factor to calculate MDF (Default: 1.96)

    :type inputTar: str
    :type outputTar: str
    :type pathway_id: str
    :type tmpOutputFolder: str
    :type ph: float
    :type ionic_strength: float
    :type pMg: float
    :type temp_k: float
    :type stdev_factor: float

    :rtype: bool
    :return: Success or failure of the function
    """
    rpequilibrator = rpEquilibrator.rpEquilibrator(
        ph=ph, ionic_strength=ionic_strength, pMg=pMg, temp_k=temp_k)
    with tempfile.TemporaryDirectory() as tmpOutputFolder:
        with tempfile.TemporaryDirectory() as tmpInputFolder:
            tar = tarfile.open(inputTar, mode='r')
            tar.extractall(path=tmpInputFolder)
            tar.close()
            if len(glob.glob(tmpInputFolder + '/*')) == 0:
                logging.error('Input file is empty')
                return False
            for sbml_path in glob.glob(tmpInputFolder + '/*'):
                logging.debug('Passing the sbml file: ' + str(sbml_path))
                fileName = sbml_path.split('/')[-1].replace(
                    '.sbml',
                    '').replace('.xml', '').replace('.rpsbml',
                                                    '').replace('_rpsbml', '')
                rpsbml = rpSBML.rpSBML(fileName, path=sbml_path)
                rpequilibrator.rpsbml = rpsbml
                res = rpequilibrator.pathway(
                    pathway_id,
                    True)  #ignore the results since written to SBML file
                rpsbml.writeSBML(tmpOutputFolder)
                rpsbml = None
            if len(glob.glob(tmpOutputFolder + '/*')) == 0:
                logging.error('rpThermo has not produced any results')
                return False
            with tarfile.open(outputTar, mode='w:gz') as ot:
                for sbml_path in glob.glob(tmpOutputFolder + '/*'):
                    fileName = str(
                        sbml_path.split('/')[-1].replace('.sbml', '').replace(
                            '.xml', '').replace('.rpsbml',
                                                '').replace('_rpsbml', ''))
                    fileName += '.sbml.xml'
                    info = tarfile.TarInfo(fileName)
                    info.size = os.path.getsize(sbml_path)
                    ot.addfile(tarinfo=info, fileobj=open(sbml_path, 'rb'))
    return True
 def setUpClass(self):
     self.rpsbml = rpSBML.rpSBML('test',
                                 path=os.path.join('data', 'rpsbml.xml'))
     self.rpeq = rpEquilibrator.rpEquilibrator(self.rpsbml)