Mercurial > repos > galaxyp > maxquant
comparison test_mqparam.py @ 4:dcd39bcc7481 draft
"planemo upload for repository https://github.com/galaxyproteomics/tools-galaxyp/tree/master/tools/maxquant commit da342a782ccc391b87fb4fead956b7b3cbd21258"
author | galaxyp |
---|---|
date | Sat, 11 Apr 2020 11:49:19 -0400 |
parents | |
children | 37d669de2828 |
comparison
equal
deleted
inserted
replaced
3:175e062b6a17 | 4:dcd39bcc7481 |
---|---|
1 """Tests for mqparam class. If testing a new MaxQuant version, | |
2 create a new parameter file using '<MAXQUANT_CMD> -c ./mqpar.xml' | |
3 """ | |
4 | |
5 import pytest | |
6 import xml.etree.ElementTree as ET | |
7 from mqparam import MQParam, ParamGroup | |
8 | |
9 TEMPLATE_PATH = './test-data/template.xml' | |
10 | |
11 | |
12 def mk_pg_root(): | |
13 mqpar = ET.parse(TEMPLATE_PATH).getroot() | |
14 return mqpar.find('.parameterGroups/parameterGroup') | |
15 | |
16 | |
17 class TestParamGroup: | |
18 def test_list_param(self): | |
19 t = ParamGroup(mk_pg_root()) | |
20 t.set_list_param('enzymes', ('test 1', 'test 2')) | |
21 assert len(t._root.find('enzymes')) == 2 | |
22 | |
23 t.set_list_param('variableModifications', ('Oxidation (M)', )) | |
24 assert t._root.find('variableModifications')[0].text == 'Oxidation (M)' | |
25 | |
26 with pytest.raises(ValueError): | |
27 t.set_list_param('foo', []) | |
28 | |
29 def test_simple_params(self): | |
30 t = ParamGroup(mk_pg_root()) | |
31 t.set_simple_param('fastLfq', False) | |
32 assert t._root.find('.fastLfq').text == 'False' | |
33 | |
34 with pytest.raises(ValueError): | |
35 t.set_simple_param('foo', 2) | |
36 | |
37 def test_silac(self): | |
38 t = ParamGroup(mk_pg_root()) | |
39 t.set_silac(None, None, ('Arg10', 'Lys4')) | |
40 assert t._root.find('.maxLabeledAa').text == '2' | |
41 assert t._root.find('.multiplicity').text == '2' | |
42 assert t._root.find('.labelMods')[1].text == 'Arg10;Lys4' | |
43 assert t._root.find('.labelMods')[0].text == '' | |
44 | |
45 def test_isobaric_label(self): | |
46 t = ParamGroup(mk_pg_root()) | |
47 t.set_isobaric_label('iTRAQ4plex-Lys114', 'iTRAQ4plex-Nter114', 0.3, 1, 1.2, 0, True) | |
48 | |
49 assert len(t._root.find('isobaricLabels')) == 1 | |
50 assert len(t._root.find('isobaricLabels')[0]) == 7 | |
51 | |
52 t.set_isobaric_label('iTRAQ4plex-Lys115', 'iTRAQ4plex-Nter115', 0.3, 1.0, 1.2, 0, True) | |
53 | |
54 assert len(t._root.find('isobaricLabels')) == 2 | |
55 | |
56 tag_list = [el.tag for el in t._root.find('isobaricLabels')[1]] | |
57 assert tag_list == ['internalLabel', 'terminalLabel', 'correctionFactorM2', | |
58 'correctionFactorM1', 'correctionFactorP1', 'correctionFactorP2', | |
59 'tmtLike'] | |
60 | |
61 text_list = [el.text for el in t._root.find('isobaricLabels')[1]] | |
62 assert text_list == ['iTRAQ4plex-Lys115', 'iTRAQ4plex-Nter115', | |
63 '0.3', '1', '1.2', '0', 'True'] | |
64 | |
65 | |
66 class TestMQParam: | |
67 | |
68 def test_version(self): | |
69 t = MQParam(TEMPLATE_PATH) | |
70 assert t._root.find('maxQuantVersion').text == '1.6.10.43' | |
71 | |
72 def test_validity_check(self): | |
73 design = {'Name': ['Test1', 'Test2'], | |
74 'Fraction': ['2', 32767], | |
75 'PTM': ['False', 'False'], | |
76 'Experiment': ['e1', 'e1'], | |
77 'paramGroup': [0, 0]} | |
78 | |
79 assert MQParam._check_validity(design, 2) is None | |
80 | |
81 design['Name'][0] = None | |
82 with pytest.raises(Exception): | |
83 MQParam._check_validity(design, 2) | |
84 design['Name'][0] = 'Test1' | |
85 | |
86 design['Experiment'][0] = '' | |
87 with pytest.raises(ValueError): | |
88 MQParam._check_validity(design, 2) | |
89 design['Experiment'][0] = 'e1' | |
90 | |
91 design['Fraction'][0] = 'foo' | |
92 with pytest.raises(ValueError): | |
93 MQParam._check_validity(design, 2) | |
94 | |
95 def test_exp_design(self, tmpdir): | |
96 # default experimental design when None is specified | |
97 t = MQParam(TEMPLATE_PATH) | |
98 design = t._make_exp_design((0, 0), ('./Test1.mzXML', './Test2.mzXML')) | |
99 assert design['Name'] == ('./Test1.mzXML', './Test2.mzXML') | |
100 assert design['Fraction'] == ('32767', '32767') | |
101 | |
102 # valid experimental design | |
103 e1 = tmpdir / "e1.txt" | |
104 e1.write('Name\tExperiment\tFraction\tPTM\nTest1\te1\nTest2\te1\t\tfalse') | |
105 t.exp_design = str(e1) | |
106 design = t._make_exp_design((0, 0), ('./Test1.mzXML', './Test2.mzXML')) | |
107 | |
108 assert design == {'Name': ['./Test1.mzXML', './Test2.mzXML'], | |
109 'Experiment': ['e1', 'e1'], | |
110 'Fraction': ['32767', '32767'], | |
111 'PTM': ['False', 'false'], | |
112 'paramGroup': (0, 0)} | |
113 | |
114 # invalid header | |
115 e2 = tmpdir / "e2.txt" | |
116 e2.write('Name\tExperiment\tFraction\tPTM\tparamGroup\n') | |
117 t.exp_design = str(e2) | |
118 | |
119 with pytest.raises(Exception): | |
120 design = t._make_exp_design(('./Test2.mzXML',), (0,)) | |
121 | |
122 def test_add_infiles(self): | |
123 t = MQParam(TEMPLATE_PATH) | |
124 t.add_infiles([('/path/Test1.mzXML', '/path/Test2.mzXML'), | |
125 ('/path/Test3.mzXML', '/path/Test4.mzXML')]) | |
126 | |
127 assert [e.text for e in t._root.find('filePaths')] == ['/path/Test1.mzXML', | |
128 '/path/Test2.mzXML', | |
129 '/path/Test3.mzXML', | |
130 '/path/Test4.mzXML'] | |
131 | |
132 assert [e.text for e in t._root.find('paramGroupIndices')] == ['0', '0', '1', '1'] | |
133 assert t[1] | |
134 | |
135 def test_translate(self): | |
136 t = MQParam(TEMPLATE_PATH) | |
137 t.add_infiles([('/posix/path/to/Test1.mzXML', | |
138 '/posix/path/to/Test2.mzXML'), | |
139 ('/path/dummy.mzXML',)]) # mqparam is not designed for windows | |
140 | |
141 t._root.find('filePaths')[2].text = r'D:\Windows\Path\Test3.mzXML' | |
142 | |
143 t.translate(('/galaxy/working/Test3.mzXML', | |
144 '/galaxy/working/Test1.mzXML', | |
145 '/galaxy/working/Test2.mzXML', | |
146 '/galaxy/working/Test4.mzXML')) | |
147 | |
148 assert [e.text for e in t._root.find('filePaths')] == ['/galaxy/working/Test1.mzXML', | |
149 '/galaxy/working/Test2.mzXML', | |
150 '/galaxy/working/Test3.mzXML'] | |
151 | |
152 def test_fasta_files(self): | |
153 t = MQParam(TEMPLATE_PATH) | |
154 t.add_fasta_files(('test1', 'test2'), | |
155 parse_rules={'identifierParseRule': r'>([^\s]*)'}) | |
156 assert len(t._root.find('fastaFiles')) == 2 | |
157 assert t._root.find('fastaFiles')[0].find("fastaFilePath").text == 'test1' | |
158 assert t._root.find('fastaFiles')[0].find("identifierParseRule").text == '>([^\\s]*)' | |
159 | |
160 def test_simple_param(self): | |
161 t = MQParam(TEMPLATE_PATH) | |
162 t.set_simple_param('minUniquePeptides', 4) | |
163 assert t._root.find('.minUniquePeptides').text == '4' | |
164 | |
165 with pytest.raises(ValueError): | |
166 t.set_simple_param('foo', 3) | |
167 | |
168 def test_from_yaml(self, tmpdir): | |
169 conf1 = tmpdir / "conf1.yml" | |
170 conf1.write(r""" | |
171 numThreads: 4 | |
172 fastaFiles: [test1.fasta,test2.fasta] | |
173 parseRules: | |
174 identifierParseRule: ^>.*\|(.*)\|.*$ | |
175 paramGroups: | |
176 - files: [Test1.mzXML,Test2.mzXML] # paramGroup 0 | |
177 fixedModifications: [mod1,mod2] | |
178 lfqMode: 1 | |
179 - files: [Test3.mzXML,Test4.mzXML] # paramGroup 1 | |
180 labelMods: | |
181 - [] | |
182 - [] | |
183 - [label1,label2] | |
184 """) | |
185 | |
186 t = MQParam(TEMPLATE_PATH) | |
187 t._from_yaml(str(conf1)) | |
188 assert t['numThreads'] == '4' | |
189 assert [child.text for child in t[1]._root.find('labelMods')] == ['', 'label1;label2'] | |
190 | |
191 def test_write(self, tmpdir): | |
192 yaml_conf = tmpdir / "conf.yml" | |
193 yaml_conf.write(r""" | |
194 numThreads: 4 | |
195 fastaFiles: [test1.fasta,test2.fasta] | |
196 parseRules: | |
197 identifierParseRule: ^>.*\|(.*)\|.*$ | |
198 paramGroups: | |
199 - files: [Test1.mzXML,Test2.mzXML] # paramGroup 0 | |
200 fixedModifications: [mod1] | |
201 variableModifications: [mod2,mod3] | |
202 maxMissedCleavages: 1 | |
203 """) | |
204 mqpar_out = tmpdir / "mqpar.xml" | |
205 | |
206 t = MQParam(TEMPLATE_PATH, yaml=str(yaml_conf)) | |
207 t.write(str(mqpar_out)) | |
208 | |
209 test = ET.parse(str(mqpar_out)).getroot() | |
210 assert test.find('numThreads').text == '4' | |
211 assert test.find('fastaFiles')[1].find('identifierParseRule').text == '^>.*\\|(.*)\\|.*$' | |
212 assert [el.text for el in test.find('parameterGroups')[0].find('variableModifications')] == ['mod2', 'mod3'] |