Matrix Science Mascot Parser toolkit
 
Loading...
Searching...
No Matches
resfile_customquantitation.py

Example program illustrating basic usage of ms_customquantitation.

1#!/usr/bin/python
2
15
16import msparser
17import sys
18import re
19
20def main():
21
22# This example uses fictitious data to illustrate how ms_customquantitation can
23# be used with the Average protocol. In a real situation peptide intensities
24# and the protein-peptide mapping would come from some external source.
25
26 protein_peptide_mapping = {'ALBU_HUMAN': [ [1, 1], [2, 1], [3, 1] ],
27 'ALBU_BOVIN': [ [2, 2], [5, 1], [7, 1] ]}
28
29 peptide_intensities = {'q1p1': 1000, 'q2p1': 500, 'q3p1': 750, 'q2p2': 750, 'q5p1': 2000, 'q7p1': 1000}
30
31 qmethod = msparser.ms_quant_method()
32 average = msparser.ms_quant_average()
33
34 average.setReferenceAccession("ALBU_HUMAN")
35 average.setReferenceAmount("1.0")
36
37# We set the number of top N peptides to 2, so we expect the following
38# peptide intensities to be used:
39# ALBU_HUMAN = 1000, 750
40# ALBU_BOVIN = 2000, 1000
41 average.setNumPeptides(2)
42
43 p = msparser.ms_quant_protocol()
44 p.setAverage(average)
45 qmethod.setProtocol(p)
46
47# Since ms_customquantitation does not use the ratio definitions in the
48# quantitation method, we need not define them at all. Ratios are meaningless
49# in the Average protocol anyway, so we can use any name we want to denote
50# the intensity values.
51
52 rationame = "amount"
53 h = {'normalisation': 'none', 'outliers': 'none', 'protein_ratio_type': 'average', 'min_num_peptides': 2}
54 set_params(qmethod, h)
55
56 customquant = msparser.ms_customquantitation(qmethod)
57
58 if not customquant.isValid():
59 print(customquant.getLastErrorString())
60 sys.exit(1)
61
62 dump_warnings(customquant.getErrorHandler())
63 customquant.clearAllErrors()
64
65 dump_quant_method(qmethod, rationame)
66
67# Create the protein-peptide mapping
68
69 for accession in protein_peptide_mapping:
70 peptides = protein_peptide_mapping[accession]
71 for peptide in peptides:
72 customquant.addPeptideQuantKey(accession, 0, msparser.ms_peptide_quant_key(peptide[0], peptide[1]))
73
74# Add peptide intensities
75
76 for keys in peptide_intensities:
77 match = re.match(r"[a-z](\d+)[a-z](\d+)", keys)
78 q = int(match.group(1))
79 p = int(match.group(2))
80 ratioValue = peptide_intensities[keys]
81 quantKey = msparser.ms_peptide_quant_key(q, p)
82 ratio = msparser.ms_peptide_quant_ratio(quantKey, rationame, ratioValue)
83 customquant.addPeptideRatio(ratio)
84
85 print("\n")
86
87# Print it all out immediately:
88
89 for accession in protein_peptide_mapping:
90 print("%s:" %(accession))
91 ratio = customquant.getProteinRatio(accession, 0, rationame)
92
93 if ratio.isMissing():
94 print("%10s: ---" %(rationame))
95 else:
96 print("%10s = %10g, stderr = %10g, N = %d, normal-p = %6g, hypo-p = %6g" %(rationame, ratio.getValue(), ratio.getStandardError_only(), ratio.getSampleSize(), ratio.getNormalityPvalue_only(), ratio.getHypothesisPvalue_only()))
97
98 print(" %10s %s" %('Peptide', rationame))
99
100 peptides = protein_peptide_mapping[accession]
101 for i in ms_range(1, len(peptides)-1):
102 peptide = peptides[i]
103 for i in range(len(peptide)):
104 q = peptide[0]
105 p = peptide[1]
106
107 values = ''
108
109 ratio = customquant.getPeptideRatio(msparser.ms_peptide_quant_key(q, p), rationame)
110
111 if ratio.isMissing():
112 values = "---"
113 elif ratio.isInfinite():
114 values = "###"
115 else:
116 values = ratio.getValue()
117
118 print(" %-9s %-10s" %("q%d_p%d" %(q, p), str(values)))
119
120 print("\n")
121
122# ms_range function has been added to make the range supplied consistent with other languages such as Perl and C#
123def ms_range(start, stop, step=1):
124 i = start
125 while i <= stop:
126 yield i
127 i += step
128
129def set_params(qmethod, h):
130
131 if qmethod.haveQuality():
132 quality = qmethod.getQuality()
133 else:
134 quality = msparser.ms_quant_quality()
135
136 if qmethod.haveNormalisation():
137 normalisation = qmethod.getNormalisation()
138 else:
139 normalisation = msparser.ms_quant_normalisation()
140
141 if qmethod.haveOutliers():
142 outliers = qmethod.getOutliers()
143 else:
144 outliers = msparser.ms_quant_outliers()
145
146 normalisation.setMethod(h['normalisation'])
147 outliers.setMethod(h['outliers'])
148 qmethod.setMinNumPeptides(h['min_num_peptides'])
149 qmethod.setProteinRatioType(h['protein_ratio_type'])
150 quality.setMinPrecursorCharge(1)
151 quality.setPepThresholdType('maximum expect')
152 quality.setPepThresholdValue('0.05')
153
154 qmethod.setQuality(quality)
155 qmethod.setNormalisation(normalisation)
156 qmethod.setOutliers(outliers)
157
158def dump_quant_method(qmethod, rationames):
159
160 comps = []
161
162 for i in ms_range(1, qmethod.getNumberOfComponents()-1):
163 comp = qmethod.getComponentByNumber(i)
164 comps.append(comp.getName())
165
166 print("Components: %s" %(comps))
167 print("Report ratios: %s" %(rationames))
168
169 print("Protein ratio type = %s" %(qmethod.getProteinRatioType()))
170 print("Min. number of peptides: %d" %(qmethod.getMinNumPeptides()))
171
172 if qmethod.haveQuality():
173 q = qmethod.getQuality()
174 print("Quality: min. precursor charge = %s" %(q.getMinPrecursorCharge()))
175 print("QualityL pep. threshold type = %s" %(q.getPepThresholdType()))
176 print("Quality: pep. threshold value = %s" %(q.getPepThresholdValue()))
177 else:
178 print("Quality: no restrictions")
179
180 if qmethod.haveNormalisation():
181 q = qmethod.getNormalisation()
182 print("Normalisation = %s" %(q.getMethod()))
183 else:
184 print("Normalisation: none")
185
186 if qmethod.haveOutliers():
187 q = qmethod.getOutliers()
188 print("Outliers = %s" %(q.getMethod()))
189 else:
190 print("Outliers: none")
191
192
193def dump_warnings(err):
194 for i in range(1, err.getNumberOfErrors()):
195 print("Warning: %s" %(err.getErrorString(i)))
196
197
198if __name__ == "__main__":
199 main()
200
201'''
202resfile_customquantitation.py
203Will give the following output:
204
205Components: []
206Report ratios: amount
207Protein ratio type = average
208Min. number of peptides: 2
209Quality: min. precursor charge = 1
210QualityL pep. threshold type = maximum expect
211Quality: pep. threshold value = 0.05
212Normalisation = none
213Outliers = none
214
215
216ALBU_HUMAN:
217 amount = 721.125, stderr = 1.22269, N = 3, normal-p = 0.813228, hypo-p = 0.000932101
218 Peptide amount
219 q1_p1 1000.0
220 q2_p1 500.0
221 q3_p1 750.0
222
223
224ALBU_BOVIN:
225 amount = 1144.71, stderr = 1.33789, N = 3, normal-p = 0.552542, hypo-p = 0.00170392
226 Peptide amount
227 q2_p2 750.0
228 q5_p1 2000.0
229 q7_p1 1000.0
230
231'''