File size: 4,814 Bytes
53e66de | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 | import os
import pickle
import tempfile
import unittest
from CodonTransformer.CodonUtils import (
ProteinConfig,
find_pattern_in_fasta,
get_organism2id_dict,
get_taxonomy_id,
load_pkl_from_url,
load_python_object_from_disk,
save_python_object_to_disk,
sort_amino2codon_skeleton,
)
class TestCodonUtils(unittest.TestCase):
def test_config_manager(self):
with ProteinConfig() as config:
config.set("ambiguous_aminoacid_behavior", "standardize_deterministic")
self.assertEqual(
config.get("ambiguous_aminoacid_behavior"), "standardize_deterministic"
)
config.set("ambiguous_aminoacid_map_override", {"X": ["A", "G"]})
self.assertEqual(
config.get("ambiguous_aminoacid_map_override"), {"X": ["A", "G"]}
)
config.update(
{
"ambiguous_aminoacid_behavior": "raise_error",
"ambiguous_aminoacid_map_override": {"X": ["A", "G"]},
}
)
self.assertEqual(config.get("ambiguous_aminoacid_behavior"), "raise_error")
self.assertEqual(
config.get("ambiguous_aminoacid_map_override"), {"X": ["A", "G"]}
)
try:
config.set("invalid_key", "invalid_value")
self.fail("Expected ValueError")
except ValueError:
pass
with ProteinConfig() as config:
self.assertEqual(
config.get("ambiguous_aminoacid_behavior"), "standardize_random"
)
self.assertEqual(config.get("ambiguous_aminoacid_map_override"), {})
def test_load_python_object_from_disk(self):
test_obj = {"key1": "value1", "key2": 2}
with tempfile.NamedTemporaryFile(suffix=".pkl", delete=False) as temp_file:
temp_file_name = temp_file.name
save_python_object_to_disk(test_obj, temp_file_name)
loaded_obj = load_python_object_from_disk(temp_file_name)
self.assertEqual(test_obj, loaded_obj)
os.remove(temp_file_name)
def test_save_python_object_to_disk(self):
test_obj = [1, 2, 3, 4, 5]
with tempfile.NamedTemporaryFile(suffix=".pkl", delete=False) as temp_file:
temp_file_name = temp_file.name
save_python_object_to_disk(test_obj, temp_file_name)
self.assertTrue(os.path.exists(temp_file_name))
os.remove(temp_file_name)
def test_find_pattern_in_fasta(self):
text = (
">seq1 [keyword=value1]\nATGCGTACGTAGCTAG\n"
">seq2 [keyword=value2]\nGGTACGATCGATCGAT"
)
self.assertEqual(find_pattern_in_fasta("keyword", text), "value1")
self.assertEqual(find_pattern_in_fasta("nonexistent", text), "")
def test_get_organism2id_dict(self):
with tempfile.NamedTemporaryFile(
mode="w", delete=True, suffix=".csv"
) as temp_file:
temp_file.write("0,Escherichia coli\n1,Homo sapiens\n2,Mus musculus")
temp_file.flush()
organism2id = get_organism2id_dict(temp_file.name)
self.assertEqual(
organism2id,
{"Escherichia coli": 0, "Homo sapiens": 1, "Mus musculus": 2},
)
def test_get_taxonomy_id(self):
taxonomy_dict = {
"Escherichia coli": 562,
"Homo sapiens": 9606,
"Mus musculus": 10090,
}
with tempfile.NamedTemporaryFile(suffix=".pkl", delete=True) as temp_file:
temp_file_name = temp_file.name
save_python_object_to_disk(taxonomy_dict, temp_file_name)
self.assertEqual(get_taxonomy_id(temp_file_name, "Escherichia coli"), 562)
self.assertEqual(
get_taxonomy_id(temp_file_name, return_dict=True), taxonomy_dict
)
def test_sort_amino2codon_skeleton(self):
amino2codon = {
"A": (["GCT", "GCC", "GCA", "GCG"], [0.0, 0.0, 0.0, 0.0]),
"C": (["TGT", "TGC"], [0.0, 0.0]),
}
sorted_amino2codon = sort_amino2codon_skeleton(amino2codon)
self.assertEqual(
sorted_amino2codon,
{
"A": (["GCA", "GCC", "GCG", "GCT"], [0.0, 0.0, 0.0, 0.0]),
"C": (["TGC", "TGT"], [0.0, 0.0]),
},
)
def test_load_pkl_from_url(self):
url = "https://example.com/test.pkl"
expected_obj = {"key": "value"}
with unittest.mock.patch("requests.get") as mock_get:
mock_get.return_value.content = pickle.dumps(expected_obj)
loaded_obj = load_pkl_from_url(url)
self.assertEqual(loaded_obj, expected_obj)
if __name__ == "__main__":
unittest.main()
|