#!/usr/bin/env python
#-*- coding: utf-8 -*-
import os
from unittest import TestCase, main
from wrappers.FARNA.FARNA import FARNA
from Config import FARNA_DB_PATH

class FARNATests(TestCase):
    """ Tests for Farna """
           
    ## def test_0(self):
    ##     """FARNA: first test"""
    ##     farna = FARNA('')
    ##     try:
    ##         farna._prepare_stderr_stdout()
    ##         result = farna.run(os.path.dirname(__file__) + os.sep + 'test.pdb')
    ##     finally:
    ##         farna.cleanup()
    ##     correct = -114.443
    ##     self.assertAlmostEqual(result, correct)
             
    ## def test_1(self):
    ##     """FARNA: second test"""
    ##     farna = FARNA('')
    ##     try:
    ##         farna._prepare_stderr_stdout()
    ##         result = farna.run(os.path.dirname(__file__) + os.sep + 'test2.pdb')
    ##     finally:
    ##         farna.cleanup()
    ##     correct = -78.073
    ##     self.assertAlmostEqual(result, correct)

    ## def test_local_scores(self):
    ##     """FARNA: test local scores"""
    ##     correct = {
    ##             'N_BS': [17.0, -0.70039, -0.720981, -0.685238, -0.734146,
    ##                 -0.701071, -0.723381, -0.711617, -0.713064, -0.698462,
    ##                 -0.759616, -0.717377, -0.730964, -0.698075, -0.724274,
    ##                 -0.72524, -0.697026, -0.714249, -0.7278, -0.725997,
    ##                 -0.686557, -0.691155, -0.742009, -0.727605, -0.710005], 
    ##             'atom_pair_constraint': [0.0, -0.764688, -0.773833, -0.757914,
    ##                 -0.771429, -0.736677, -0.783028, -0.757879, -0.734886,
    ##                 -0.787879, -0.742389, -0.733271, -0.778352, -0.752749,
    ##                 -0.776026, -0.782631, -0.732163, -0.832465, -0.776058,
    ##                 -0.776968, -0.788664, -0.749612, -0.752012, -0.736119,
    ##                 -0.812041], 'score': [-114.443, 0.0, -68.595, -68.645,
    ##                 -75.657, -91.722, 156.259, -30.652, 164.79, -64.282,
    ##                 -56.935, -92.382, -102.201, 82.919, -60.911, -158.731,
    ##                 -169.534, -76.137, -86.608, -82.019, -76.089, -71.758,
    ##                 -164.341, -65.639, -70.664],
    ##             'N_WC': [0.0, -1.722081, -1.561713, -1.668795, -1.572366,
    ##                 -1.725206, -1.663495, -1.87509, -1.740313, -1.612779,
    ##                 -1.637651, -1.703152, -1.919718, -1.733393, -1.605748,
    ##                 -1.971868, -1.815194, -1.446888, -1.53608, -1.570019,
    ##                 -1.626897, -1.708298, -1.623953, -1.674689, -1.61063],
    ##             'hbond_sr_bb_sc': [-0.291, -146.937, -143.636, -144.362,
    ##                 -143.714, -21.479, 99.955, -139.889, -142.198, 142.005,
    ##                 99.941, -130.981, -141.204, 3.326, -140.486, -147.416,
    ##                 145.052, 128.79, 99.97, -142.794, -20.039, 100.029,
    ##                 -140.458, 99.992, -141.08],
    ##             'N_NWC': [4.0, -1.882012, -1.845311, -1.85536, -1.84574,
    ##                 -1.848706, -1.889638, -1.889743, -1.868197, -1.862973,
    ##                 -1.834916, -1.845558, -1.931075, -1.880485, -1.857567,
    ##                 -1.929501, -1.873637, -1.842361, -1.846748, -1.848119,
    ##                 -1.86504, -1.857772, -1.848206, -1.848734, -1.889312],
    ##             'linear_chainbreak': [0.0, -1.412355, -1.574619, -1.469062,
    ##                 -1.568086, -1.410884, -1.48779, -1.235153, -1.375147,
    ##                 -1.532957, -1.526007, -1.441046, -1.096362, -1.388187,
    ##                 -1.541883, -1.001851, -1.308701, -1.664601, -1.596681,
    ##                 -1.573258, -1.501321, -1.429853, -1.534871, -1.473195,
    ##                 -1.538419],
    ##             'fa_intra_rep': [0.547, 84.997, 77.526, 81.948, 78.49, 83.437,
    ##                 84.373, 93.228, 82.518, 81.656, 81.991, 82.977, 150.29,
    ##                 86.382, 82.141, 153.059, 87.57, 77.643, 77.718, 79.098,
    ##                 82.2, 83.068, 79.103, 82.916, 84.672],
    ##             'rna_sugar_close': [2.296, 90.384, 94.496, 92.375, 92.477,
    ##                 87.064, 91.701, 82.584, 83.22, 96.236, 86.032, 86.727,
    ##                 157.574, 91.124, 93.152, 155.448, 84.047, 104.061,
    ##                 95.003, 94.49, 92.691, 89.624, 88.865, 86.292, 100.129],
    ##             'hbond_sc': [-56.329, -1.333415, -1.409565, -1.362553,
    ##                 -1.410262, -1.35406, -1.365699, -1.256811, -1.355524,
    ##                 -1.379441, -1.403565, -1.366813, -0.13183, -1.31484,
    ##                 -1.392441, -0.058186, -1.302584, -1.419136, -1.416135,
    ##                 -1.402605, -1.388554, -1.353765, -1.40306, -1.384852,
    ##                 -1.353816],
    ##             'lk_nonpolar': [5.718, -160.256, -159.848, -161.36, -158.106,
    ##                     -156.49, -136.452, -150.759, -149.573, -140.935,
    ##                     -154.128, -169.121, -68.264, -136.307, -173.49,
    ##                     -86.743, -168.898, -132.382, -167.004, -154.88,
    ##                     -149.064, -166.046, -159.264, -167.866, 0.0],
    ##             'fa_atr': [-52.73, -171.566, -169.592, 176.583, 176.369,
    ##                     -176.792, 139.661, 163.459, 145.316, 165.209, 164.561,
    ##                     176.285, 117.764, -108.904, 161.33, 161.673, -170.953,
    ##                     -179.758, -170.892, -178.109, 174.411, 178.637,
    ##                     160.631, 173.013, 177.971],
    ##             'hack_elec_rna_phos_phos': [1.305, -78.631, -65.735, -55.538,
    ##                     -41.26, -53.022, -85.946, -50.212, -54.924, -72.601,
    ##                     -36.178, -101.064, 78.824, -70.667, -119.222, 150.335,
    ##                     -54.705, -33.737, -49.967, -57.41, -39.759, -70.692,
    ##                     -61.725, -60.801, 0.0],
    ##             'ch_bond': [-39.337, 71.999, 83.983, 80.541, 72.215, 74.495,
    ##                     64.543, 85.027, 79.819, 52.873, 77.511, 60.352, 152.69,
    ##                     66.345, 77.824, -53.102, 91.156, 69.52, 96.623, 80.018,
    ##                     78.359, 95.036, 77.531, 83.13, 80.417],
    ##             'fa_rep': [7.611, 41.818, 48.088, 54.738, 60.397, 60.07,
    ##                     64.811, 25.813, 165.45, 89.594, 52.445, 70.906,
    ##                     149.118, -132.973, 55.686, -166.952, 13.703, 63.802,
    ##                     38.069, 59.707, 60.369, 48.753, 48.167, 54.282,
    ##                     63.608],
    ##             'hbond_lr_bb_sc': [-27.986, 0.324895, 0.302459, 0.318922,
    ##                     0.29719, 0.313176, 0.313424, 0.32974, 0.297874,
    ##                     0.31585, 0.284924, 0.307462, 0.319941, 0.343701,
    ##                     0.305437, 0.320274, 0.326073, 0.311046, 0.299317,
    ##                     0.305259, 0.28717, 0.318006, 0.296335, 0.296982,
    ##                     0.340488],
    ##             'geom_sol': [43.362, -0.651053, -0.486617, -0.589947,
    ##                     -0.487386, -0.618294, -0.599353, -0.797609, -0.613571,
    ##                     -0.563833, -0.510375, -0.586522, -1.486358, -0.676191,
    ##                     -0.535181, -1.49027, -0.71046, -0.456984, -0.466517,
    ##                     -0.499242, -0.548941, -0.615352, -0.51353, -0.547852,
    ##                     -0.617501],
    ##             'rna_torsion': [1.393, 38.176, 39.788, 39.167, 40.349, 40.962,
    ##                     38.011, 37.917, 42.603, 37.458, 42.696, 41.617,
    ##                     -37.964, 37.251, 39.08, -37.901, 40.1, 35.2, 39.739,
    ##                     39.251, 39.856, 39.882, 41.52, 42.442, 33.427]
    ##             }
    ##     farna = FARNA('')
    ##     try:
    ##         farna._prepare_stderr_stdout()
    ##         result = farna.run(os.path.dirname(__file__) + os.sep + 'test.pdb',
    ##                 global_energy_score=False)
    ##     finally:
    ##         farna.cleanup()
    ##     self.assertEqual(set(result.keys()), set(correct.keys()))
    ##     for k in correct.keys():
    ##         for i in xrange(len(result[k])):
    ##             self.assertAlmostEqual(result[k][i], correct[k][i])

    def test_2(self):
        """FARNA: database is readable"""
        f = open(FARNA_DB_PATH + 'chemical/residue_type_sets/rna/residue_types.txt')

    def test_3(self):
        """FARNA: minimize off, global energy score"""
        farna = FARNA('')
        try:
            farna._prepare_stderr_stdout()
            result = farna.run(os.path.dirname(__file__) + os.sep + 'test.pdb',
                    global_energy_score=True)
        finally:
            farna.cleanup()
        correct = -20.507
        self.assertAlmostEqual(result, correct)

if __name__ == '__main__':
    main()
