test_core.py 9.06 KB
Newer Older
Nicolas  MAILLET's avatar
Nicolas MAILLET committed
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
"""Tests for core.py"""
import pytest
from .context import rpg
from rpg import core
from rpg import rule
from rpg import enzyme
from rpg import digest
from rpg import sequence

def test_handle_errors(capsys):
    """Test function 'handle_errors(msg="", err=1, error_type="")'"""
    # Error, then exit
    message = "This is a test"
    error = 0
    error_type = "Test "
    with pytest.raises(SystemExit) as pytest_wrapped_e:
        core.handle_errors(message, error, error_type)
    _, err = capsys.readouterr()
    assert err == "Test Error: This is a test\n"
    assert pytest_wrapped_e.type == SystemExit
    assert pytest_wrapped_e.value.code == 1

    # Warning test
    message = "This is a test"
    error = 1
    error_type = "Test "
    core.handle_errors(message, error, error_type)
    _, err = capsys.readouterr()
    assert err == "Test Warning: This is a test\n"

    # Writing in stderr test
    message = "This is a test"
    error = 2
    error_type = "Test "
    core.handle_errors(message, error, error_type)
    _, err = capsys.readouterr()
    assert err == "Test This is a test\n"

    # Default parameters
    message = "This is a test"
    core.handle_errors(message)
    _, err = capsys.readouterr()
    assert err == "Warning: This is a test\n"

def test_get_header():
    """Test function 'get_header(fmt="fasta")'"""
    res_fasta = None
    res_csv = "Original_header,No_peptide,Enzyme,Cleaving_pos,Peptide_size,Pe"\
              "ptide_mass,pI,Sequence"
    res_tsv = "Original_header\tNo_peptide\tEnzyme\tCleaving_pos\tPeptide_siz"\
              "e\tPeptide_mass\tpI\tSequence"
    assert core.get_header() == res_fasta
    assert core.get_header("csv") == res_csv
    assert core.get_header("tsv") == res_tsv
    assert core.get_header("pwet") == res_fasta

def test_output_results(capsys, tmpdir):
    """Test function 'output_results(output_file, all_seq_digested,
                                     fmt, quiet, verbose)'"""
    # Test file not found
    with pytest.raises(SystemExit) as pytest_wrapped_e:
        core.output_results("/tmp", None, "csv", False, False)
    _, err = capsys.readouterr()
    assert err == "File Error: /tmp can't be open in 'w' mode\n"
    assert pytest_wrapped_e.type == SystemExit
    assert pytest_wrapped_e.value.code == 1

    a_rule = {}
    rule_txt = "(S)(D,)"
    a_rule[rule_txt] = True
    all_rules = rule.create_rules(a_rule)
    enz1 = enzyme.Enzyme(-1, "fake_enzyme1", all_rules)
    enzymes = [enz1]
    mode = "sequential"

    # CSV output
    seq = "WQSDESDFZQSDESDF"
78
79
    aa_pka = core.AA_PKA_IPC
    all_seq_digested = digest.digest_from_input(seq, enzymes, mode, aa_pka)
Nicolas  MAILLET's avatar
Nicolas MAILLET committed
80
81
82
83
    output_file = tmpdir.join("test_result.csv")
    fmt = "csv"
    quiet = False
    verbose = 0
84
    core.output_results(str(output_file), all_seq_digested, fmt, quiet, verbose)
Nicolas  MAILLET's avatar
Nicolas MAILLET committed
85
86
87
88
    out, err = capsys.readouterr()
    assert out == output_file.read()
    assert output_file.read() == "Original_header,No_peptide,Enzyme,Cleaving_"\
                                 "pos,Peptide_size,Peptide_mass,pI,Sequence\n"\
89
90
91
                                 "Input,0,fake_enzyme1,4,4,534.52598,3.14,WQSD"\
                                 "\nInput,1,fake_enzyme1,7,3,349.29758,3.04,ES"\
                                 "D\nInput,2,fake_enzyme1,12,5,495.48938,3.14,"\
Nicolas  MAILLET's avatar
Nicolas MAILLET committed
92
                                 "FZQSD\nInput,3,fake_enzyme1,15,3,349.29758,"\
93
94
                                 "3.04,ESD\nInput,4,fake_enzyme1,16,1,165.1918"\
                                 "8,5.97,F\n"
Nicolas  MAILLET's avatar
Nicolas MAILLET committed
95
96
97
98
99
100

    # TSV output
    output_file = tmpdir.join("test_result.tsv")
    fmt = "tsv"
    quiet = False
    verbose = 0
101
    core.output_results(str(output_file), all_seq_digested, fmt, quiet, verbose)
Nicolas  MAILLET's avatar
Nicolas MAILLET committed
102
103
104
105
106
    out, err = capsys.readouterr()
    assert out == output_file.read()
    assert output_file.read() == "Original_header\tNo_peptide\tEnzyme\tCleavi"\
                                 "ng_pos\tPeptide_size\tPeptide_mass\tpI\tSeq"\
                                 "uence\nInput\t0\tfake_enzyme1\t4\t4\t534.52"\
107
108
109
110
                                 "598\t3.14\tWQSD\nInput\t1\tfake_enzyme1\t7\t"\
                                 "3\t349.29758\t3.04\tESD\nInput\t2\tfake_enzy"\
                                 "me1\t12\t5\t495.48938\t3.14\tFZQSD\nInput\t3"\
                                 "\tfake_enzyme1\t15\t3\t349.29758\t3.04\tESD"\
Nicolas  MAILLET's avatar
Nicolas MAILLET committed
111
                                 "\nInput\t4\tfake_enzyme1\t16\t1\t165.19188"\
112
                                 "\t5.97\tF\n"
Nicolas  MAILLET's avatar
Nicolas MAILLET committed
113
114
115
116
117
118

    # Fasta output
    output_file = tmpdir.join("test_result.fasta")
    fmt = "fasta"
    quiet = False
    verbose = 0
119
    core.output_results(str(output_file), all_seq_digested, fmt, quiet, verbose)
Nicolas  MAILLET's avatar
Nicolas MAILLET committed
120
121
    out, err = capsys.readouterr()
    assert out == output_file.read()
122
123
    assert output_file.read() == ">Input_0_fake_enzyme1_4_4_534.52598_3.14\nWQ"\
                                 "SD\n>Input_1_fake_enzyme1_7_3_349.29758_3.04"\
Nicolas  MAILLET's avatar
Nicolas MAILLET committed
124
                                 "\nESD\n>Input_2_fake_enzyme1_12_5_495.48938"\
125
126
127
                                 "_3.14\nFZQSD\n>Input_3_fake_enzyme1_15_3_349"\
                                 ".29758_3.04\nESD\n>Input_4_fake_enzyme1_16_1"\
                                 "_165.19188_5.97\nF\n"
Nicolas  MAILLET's avatar
Nicolas MAILLET committed
128
129
130

    # CSV output in quiet
    seq = "WQSDESDFZQSDESDF"
131
    all_seq_digested = digest.digest_from_input(seq, enzymes, mode, aa_pka)
Nicolas  MAILLET's avatar
Nicolas MAILLET committed
132
133
134
135
    output_file = tmpdir.join("test_result.csv")
    fmt = "csv"
    quiet = True
    verbose = 0
Nicolas  MAILLET's avatar
PEP8    
Nicolas MAILLET committed
136
137
    core.output_results(str(output_file), all_seq_digested, fmt, quiet,
                        verbose)
Nicolas  MAILLET's avatar
Nicolas MAILLET committed
138
139
140
141
142
    out, err = capsys.readouterr()
    # Quiet
    assert out == ""
    assert output_file.read() == "Original_header,No_peptide,Enzyme,Cleaving_"\
                                 "pos,Peptide_size,Peptide_mass,pI,Sequence\n"\
143
144
145
                                 "Input,0,fake_enzyme1,4,4,534.52598,3.14,WQSD"\
                                 "\nInput,1,fake_enzyme1,7,3,349.29758,3.04,ES"\
                                 "D\nInput,2,fake_enzyme1,12,5,495.48938,3.14,"\
Nicolas  MAILLET's avatar
Nicolas MAILLET committed
146
                                 "FZQSD\nInput,3,fake_enzyme1,15,3,349.29758,"\
147
148
                                 "3.04,ESD\nInput,4,fake_enzyme1,16,1,165.1918"\
                                 "8,5.97,F\n"
Nicolas  MAILLET's avatar
Nicolas MAILLET committed
149
150
151

    # CSV output in verbose > 2
    seq = "WQSDESDFZQSDESDF"
152
    all_seq_digested = digest.digest_from_input(seq, enzymes, mode, aa_pka)
Nicolas  MAILLET's avatar
Nicolas MAILLET committed
153
154
155
156
    output_file = tmpdir.join("test_result.csv")
    fmt = "csv"
    quiet = False
    verbose = 3
Nicolas  MAILLET's avatar
PEP8    
Nicolas MAILLET committed
157
158
    core.output_results(str(output_file), all_seq_digested, fmt, quiet,
                        verbose)
Nicolas  MAILLET's avatar
Nicolas MAILLET committed
159
160
161
    out, err = capsys.readouterr()
    assert output_file.read() == "Original_header,No_peptide,Enzyme,Cleaving_"\
                                 "pos,Peptide_size,Peptide_mass,pI,Sequence\n"\
162
163
164
                                 "Input,0,fake_enzyme1,4,4,534.52598,3.14,WQSD"\
                                 "\nInput,1,fake_enzyme1,7,3,349.29758,3.04,ES"\
                                 "D\nInput,2,fake_enzyme1,12,5,495.48938,3.14,"\
Nicolas  MAILLET's avatar
Nicolas MAILLET committed
165
                                 "FZQSD\nInput,3,fake_enzyme1,15,3,349.29758,"\
166
167
                                 "3.04,ESD\nInput,4,fake_enzyme1,16,1,165.1918"\
                                 "8,5.97,F\n"
Nicolas  MAILLET's avatar
Nicolas MAILLET committed
168
169
    # Verbose > 2
    assert out == "\nNumber of cleavage: 4\nCleavage position: 4, 7, 12, 15\n"\
170
171
                  "Number of miscleavage: 0\nmiscleavage position: \nmis"\
                  "cleavage ratio: 0.00%\nSmallest peptide size: 1\nN termin"\
Nicolas  MAILLET's avatar
Nicolas MAILLET committed
172
173
174
175
176
177
178
179
                  "al peptide: WQSD\nC terminal peptide: F\n" + \
                  output_file.read()

def test_peptide():
    """Test class 'Peptide'"""
    header = "Test"
    seq = "QWSDESDF"
    enz_name = "fake_enzyme"
180
181
    aa_pka = core.AA_PKA_IPC
    pep0 = sequence.Peptide(header, seq, enz_name, aa_pka, 1, 3)
Nicolas  MAILLET's avatar
Nicolas MAILLET committed
182
183
184
185
    # Test function '__repr__()'
    print_res = pep0.__repr__()
    assert print_res == "Original header: Test\nNo. peptide: 1\nEnzyme: fake_"\
                        "enzyme\nCleav. pos: 3\nPep. size: 8\nPep. mass: 1012"\
186
187
                        ".98488\npKa values from: IPC\nPep. pI: 2.91\nSequenc"\
                        "e: QWSDESDF\n"
Nicolas  MAILLET's avatar
Nicolas MAILLET committed
188
189

    # Test function '__eq__()'
190
    pep1 = sequence.Peptide(header, seq, enz_name, aa_pka, 1, 3)
Nicolas  MAILLET's avatar
Nicolas MAILLET committed
191
192
193
    assert pep0 == pep1

    # Test function '__ne__()'
194
195
196
197
198
    pep2 = sequence.Peptide(header, seq, enz_name, aa_pka, 1, 2)
    pep3 = sequence.Peptide(header, seq, enz_name, aa_pka, 2, 3)
    pep4 = sequence.Peptide(header, seq, enz_name + "A", aa_pka, 1, 3)
    pep5 = sequence.Peptide(header, seq + "A", enz_name, aa_pka, 1, 3)
    pep6 = sequence.Peptide(header + "A", seq, enz_name, aa_pka, 1, 3)
Nicolas  MAILLET's avatar
Nicolas MAILLET committed
199
200
201
202
203
204
205
206
    assert pep0 != pep2
    assert pep0 != pep3
    assert pep0 != pep4
    assert pep0 != pep5
    assert pep0 != pep6

    # Test function '__format__()'
    format_res = pep0.__format__("csv")
207
    assert format_res == "Test,1,fake_enzyme,3,8,1012.98488,2.91,QWSDESDF"
Nicolas  MAILLET's avatar
Nicolas MAILLET committed
208
    format_res = pep2.__format__("tsv")
209
    assert format_res == "Test\t1\tfake_enzyme\t2\t8\t1012.98488\t2.91\tQWSD"\
Nicolas  MAILLET's avatar
PEP8    
Nicolas MAILLET committed
210
                         "ESDF"
Nicolas  MAILLET's avatar
Nicolas MAILLET committed
211
    format_res = pep3.__format__("fasta")
212
    assert format_res == ">Test_2_fake_enzyme_3_8_1012.98488_2.91\nQWSDESDF"
Nicolas  MAILLET's avatar
Nicolas MAILLET committed
213
214

    # Test function 'def get_isoelectric_point():'
215
    assert pep3.get_isoelectric_point() == 2.91