# ----------------------------------------------------------------------------
# Copyright (c) 2016-2018, QIIME 2 development team.
#
# Distributed under the terms of the Modified BSD License.
#
# The full license is in the file LICENSE, distributed with this software.
# ----------------------------------------------------------------------------

from unittest import TestCase, main
import qiime2
import os
from q2_qemistree import MGFDirFmt, SiriusDirFmt, ZodiacDirFmt, OutputDirs
from q2_qemistree import (compute_fragmentation_trees,
                          rerank_molecular_formulas,
                          predict_fingerprints)
from q2_qemistree._fingerprint import artifactory


class FingerprintTests(TestCase):
    def setUp(self):
        THIS_DIR = os.path.dirname(os.path.abspath(__file__))
        self.badsirpath = os.path.join(THIS_DIR, 'data/foo/bin')
        self.goodsirpath = os.path.join(THIS_DIR, 'data/'
                                        'sirius-linux64-headless-4.0.1/bin')
        # MassSpectrometryFeatures
        self.ions = qiime2.Artifact.load(os.path.join(THIS_DIR,
                                                      'data/sirius.mgf.qza'))
        # SiriusFolder
        self.sirout = qiime2.Artifact.load(os.path.join(THIS_DIR,
                                                        'data/sirFolder.qza'))
        # ZodiacFolder
        self.zodout = qiime2.Artifact.load(os.path.join(THIS_DIR,
                                                        'data/zodFolder.qza'))

    def test_artifactory(self):
        # everything is working fine
        obs = os.environ.get('_JAVA_OPTIONS', '')
        res = artifactory(self.goodsirpath, ['--help'],
                          constructor=OutputDirs, java_flags='-Xms2G')
        self.assertEqual(obs, os.environ.get('_JAVA_OPTIONS'))
        self.assertTrue(isinstance(res, OutputDirs))
        # exceptions are raised
        with self.assertRaises(OSError):
            res = artifactory(self.badsirpath, ['--help'],
                              constructor=OutputDirs)

    def test_fragmentation_trees(self):
        ions = self.ions.view(MGFDirFmt)
        result = compute_fragmentation_trees(sirius_path=self.goodsirpath,
                                             features=ions,
                                             ppm_max=15, profile='orbitrap')
        contents = os.listdir(result.get_path())
        self.assertTrue(('version.txt' in contents))

    def test_fragmentation_trees_negative_ionization(self):
        ions = self.ions.view(MGFDirFmt)
        result = compute_fragmentation_trees(sirius_path=self.goodsirpath,
                                             features=ions,
                                             ppm_max=15, profile='orbitrap',
                                             ionization_mode='negative')
        contents = os.listdir(result.get_path())
        self.assertTrue(('version.txt' in contents))

    def test_fragmentation_trees_exception(self):
        ions = self.ions.view(MGFDirFmt)
        with self.assertRaises(ValueError):
            compute_fragmentation_trees(sirius_path=self.goodsirpath,
                                        features=ions,
                                        ppm_max=15,
                                        profile='orbitrap',
                                        ionization_mode='n3gativ3')

    def test_reranking(self):
        ions = self.ions.view(MGFDirFmt)
        sirout = self.sirout.view(SiriusDirFmt)
        result = rerank_molecular_formulas(sirius_path=self.goodsirpath,
                                           fragmentation_trees=sirout,
                                           features=ions)
        contents = os.listdir(result.get_path())
        self.assertTrue(('zodiac_summary.csv' in contents))

    def test_fingerid(self):
        zodout = self.zodout.view(ZodiacDirFmt)
        result = predict_fingerprints(sirius_path=self.goodsirpath,
                                      molecular_formulas=zodout, ppm_max=15)
        contents = os.listdir(result.get_path())
        self.assertTrue(('summary_csi_fingerid.csv' in contents))


if __name__ == '__main__':
    main()

[((91, 4, 91, 10), 'unittest.main', 'main', ({}, {}), '()', False, 'from unittest import TestCase, main\n'), ((22, 26, 22, 64), 'os.path.join', 'os.path.join', ({(22, 39, 22, 47): 'THIS_DIR', (22, 49, 22, 63): '"""data/foo/bin"""'}, {}), "(THIS_DIR, 'data/foo/bin')", False, 'import os\n'), ((23, 27, 24, 76), 'os.path.join', 'os.path.join', ({(23, 40, 23, 48): 'THIS_DIR', (23, 50, 24, 75): '"""data/sirius-linux64-headless-4.0.1/bin"""'}, {}), "(THIS_DIR, 'data/sirius-linux64-headless-4.0.1/bin')", False, 'import os\n'), ((37, 14, 37, 49), 'os.environ.get', 'os.environ.get', ({(37, 29, 37, 44): '"""_JAVA_OPTIONS"""', (37, 46, 37, 48): '""""""'}, {}), "('_JAVA_OPTIONS', '')", False, 'import os\n'), ((38, 14, 39, 70), 'q2_qemistree._fingerprint.artifactory', 'artifactory', (), '', False, 'from q2_qemistree._fingerprint import artifactory\n'), ((49, 17, 51, 76), 'q2_qemistree.compute_fragmentation_trees', 'compute_fragmentation_trees', (), '', False, 'from q2_qemistree import compute_fragmentation_trees, rerank_molecular_formulas, predict_fingerprints\n'), ((57, 17, 60, 72), 'q2_qemistree.compute_fragmentation_trees', 'compute_fragmentation_trees', (), '', False, 'from q2_qemistree import compute_fragmentation_trees, rerank_molecular_formulas, predict_fingerprints\n'), ((76, 17, 78, 57), 'q2_qemistree.rerank_molecular_formulas', 'rerank_molecular_formulas', (), '', False, 'from q2_qemistree import compute_fragmentation_trees, rerank_molecular_formulas, predict_fingerprints\n'), ((84, 17, 85, 76), 'q2_qemistree.predict_fingerprints', 'predict_fingerprints', (), '', False, 'from q2_qemistree import compute_fragmentation_trees, rerank_molecular_formulas, predict_fingerprints\n'), ((21, 35, 21, 60), 'os.path.abspath', 'os.path.abspath', ({(21, 51, 21, 59): '__file__'}, {}), '(__file__)', False, 'import os\n'), ((26, 41, 27, 76), 'os.path.join', 'os.path.join', ({(26, 54, 26, 62): 'THIS_DIR', (27, 54, 27, 75): '"""data/sirius.mgf.qza"""'}, {}), "(THIS_DIR, 'data/sirius.mgf.qza')", False, 'import os\n'), ((29, 43, 30, 77), 'os.path.join', 'os.path.join', ({(29, 56, 29, 64): 'THIS_DIR', (30, 56, 30, 76): '"""data/sirFolder.qza"""'}, {}), "(THIS_DIR, 'data/sirFolder.qza')", False, 'import os\n'), ((32, 43, 33, 77), 'os.path.join', 'os.path.join', ({(32, 56, 32, 64): 'THIS_DIR', (33, 56, 33, 76): '"""data/zodFolder.qza"""'}, {}), "(THIS_DIR, 'data/zodFolder.qza')", False, 'import os\n'), ((40, 30, 40, 61), 'os.environ.get', 'os.environ.get', ({(40, 45, 40, 60): '"""_JAVA_OPTIONS"""'}, {}), "('_JAVA_OPTIONS')", False, 'import os\n'), ((44, 18, 45, 53), 'q2_qemistree._fingerprint.artifactory', 'artifactory', (), '', False, 'from q2_qemistree._fingerprint import artifactory\n'), ((67, 12, 71, 67), 'q2_qemistree.compute_fragmentation_trees', 'compute_fragmentation_trees', (), '', False, 'from q2_qemistree import compute_fragmentation_trees, rerank_molecular_formulas, predict_fingerprints\n')]