pepdata 1.3.7__tar.gz → 1.3.10__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (57) hide show
  1. {pepdata-1.3.7/pepdata.egg-info → pepdata-1.3.10}/PKG-INFO +1 -1
  2. pepdata-1.3.10/pepdata/amino_acid_properties.py +218 -0
  3. pepdata-1.3.10/pepdata/blosum.py +78 -0
  4. {pepdata-1.3.7 → pepdata-1.3.10}/pepdata/chou_fasman.py +2 -2
  5. {pepdata-1.3.7 → pepdata-1.3.10}/pepdata/iedb/alleles.py +1 -1
  6. {pepdata-1.3.7 → pepdata-1.3.10}/pepdata/iedb/columns.py +1 -1
  7. {pepdata-1.3.7 → pepdata-1.3.10}/pepdata/iedb/tcell.py +1 -4
  8. pepdata-1.3.10/pepdata/pairwise_table.py +167 -0
  9. {pepdata-1.3.7 → pepdata-1.3.10}/pepdata/peptide_vectorizer.py +2 -1
  10. pepdata-1.3.10/pepdata/pmbec.py +78 -0
  11. pepdata-1.3.10/pepdata/residue_contact_energies.py +96 -0
  12. {pepdata-1.3.7 → pepdata-1.3.10}/pepdata/version.py +1 -1
  13. {pepdata-1.3.7 → pepdata-1.3.10/pepdata.egg-info}/PKG-INFO +1 -1
  14. {pepdata-1.3.7 → pepdata-1.3.10}/pepdata.egg-info/SOURCES.txt +6 -1
  15. {pepdata-1.3.7 → pepdata-1.3.10}/pyproject.toml +1 -1
  16. pepdata-1.3.10/tests/test_amino_acid_properties.py +109 -0
  17. pepdata-1.3.10/tests/test_chou_fasman.py +36 -0
  18. pepdata-1.3.10/tests/test_pairwise_table.py +171 -0
  19. pepdata-1.3.10/tests/test_public_tables_unchanged.py +84 -0
  20. pepdata-1.3.7/pepdata/amino_acid_properties.py +0 -357
  21. pepdata-1.3.7/pepdata/blosum.py +0 -73
  22. pepdata-1.3.7/pepdata/pmbec.py +0 -87
  23. pepdata-1.3.7/pepdata/residue_contact_energies.py +0 -74
  24. {pepdata-1.3.7 → pepdata-1.3.10}/LICENSE.txt +0 -0
  25. {pepdata-1.3.7 → pepdata-1.3.10}/MANIFEST.in +0 -0
  26. {pepdata-1.3.7 → pepdata-1.3.10}/README.md +0 -0
  27. {pepdata-1.3.7 → pepdata-1.3.10}/pepdata/__init__.py +0 -0
  28. {pepdata-1.3.7 → pepdata-1.3.10}/pepdata/amino_acid.py +0 -0
  29. {pepdata-1.3.7 → pepdata-1.3.10}/pepdata/amino_acid_alphabet.py +0 -0
  30. {pepdata-1.3.7 → pepdata-1.3.10}/pepdata/common.py +0 -0
  31. {pepdata-1.3.7 → pepdata-1.3.10}/pepdata/iedb/__init__.py +0 -0
  32. {pepdata-1.3.7 → pepdata-1.3.10}/pepdata/iedb/common.py +0 -0
  33. {pepdata-1.3.7 → pepdata-1.3.10}/pepdata/iedb/mhc.py +0 -0
  34. {pepdata-1.3.7 → pepdata-1.3.10}/pepdata/matrices/BLOSUM30 +0 -0
  35. {pepdata-1.3.7 → pepdata-1.3.10}/pepdata/matrices/BLOSUM50 +0 -0
  36. {pepdata-1.3.7 → pepdata-1.3.10}/pepdata/matrices/BLOSUM62 +0 -0
  37. {pepdata-1.3.7 → pepdata-1.3.10}/pepdata/matrices/__init__.py +0 -0
  38. {pepdata-1.3.7 → pepdata-1.3.10}/pepdata/matrices/amino_acid_properties.txt +0 -0
  39. {pepdata-1.3.7 → pepdata-1.3.10}/pepdata/matrices/helix_vs_coil.txt +0 -0
  40. {pepdata-1.3.7 → pepdata-1.3.10}/pepdata/matrices/helix_vs_strand.txt +0 -0
  41. {pepdata-1.3.7 → pepdata-1.3.10}/pepdata/matrices/pmbec.mat +0 -0
  42. {pepdata-1.3.7 → pepdata-1.3.10}/pepdata/matrices/strand_vs_coil.txt +0 -0
  43. {pepdata-1.3.7 → pepdata-1.3.10}/pepdata/reduced_alphabet.py +0 -0
  44. {pepdata-1.3.7 → pepdata-1.3.10}/pepdata/static_data.py +0 -0
  45. {pepdata-1.3.7 → pepdata-1.3.10}/pepdata.egg-info/dependency_links.txt +0 -0
  46. {pepdata-1.3.7 → pepdata-1.3.10}/pepdata.egg-info/requires.txt +0 -0
  47. {pepdata-1.3.7 → pepdata-1.3.10}/pepdata.egg-info/top_level.txt +0 -0
  48. {pepdata-1.3.7 → pepdata-1.3.10}/requirements.txt +0 -0
  49. {pepdata-1.3.7 → pepdata-1.3.10}/setup.cfg +0 -0
  50. {pepdata-1.3.7 → pepdata-1.3.10}/tests/test_amino_acids.py +0 -0
  51. {pepdata-1.3.7 → pepdata-1.3.10}/tests/test_blosum.py +0 -0
  52. {pepdata-1.3.7 → pepdata-1.3.10}/tests/test_iedb_alleles.py +0 -0
  53. {pepdata-1.3.7 → pepdata-1.3.10}/tests/test_iedb_cache_lifecycle.py +0 -0
  54. {pepdata-1.3.7 → pepdata-1.3.10}/tests/test_iedb_mhc.py +0 -0
  55. {pepdata-1.3.7 → pepdata-1.3.10}/tests/test_iedb_tcell.py +0 -0
  56. {pepdata-1.3.7 → pepdata-1.3.10}/tests/test_ngram.py +0 -0
  57. {pepdata-1.3.7 → pepdata-1.3.10}/tests/test_pmbec.py +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: pepdata
3
- Version: 1.3.7
3
+ Version: 1.3.10
4
4
  Summary: Immunological peptide datasets and amino acid properties
5
5
  Author-email: Alex Rubinsteyn <alex.rubinsteyn@mssm.edu>
6
6
  Project-URL: Homepage, https://github.com/openvax/pepdata
@@ -0,0 +1,218 @@
1
+ # Licensed under the Apache License, Version 2.0 (the "License");
2
+ # you may not use this file except in compliance with the License.
3
+ # You may obtain a copy of the License at
4
+ #
5
+ # http://www.apache.org/licenses/LICENSE-2.0
6
+ #
7
+ # Unless required by applicable law or agreed to in writing, software
8
+ # distributed under the License is distributed on an "AS IS" BASIS,
9
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10
+ # See the License for the specific language governing permissions and
11
+ # limitations under the License.
12
+
13
+ """
14
+ Quantify amino acids by their physical/chemical properties
15
+ """
16
+
17
+ import re
18
+ from os.path import join
19
+
20
+ from .amino_acid_alphabet import canonical_amino_acid_letters, letter_to_index
21
+ from .static_data import MATRIX_DIR
22
+
23
+ # Copy of the CRASP amino-acid property page,
24
+ # http://www.sgi.sscc.ru/mgs/programs/crasp/texts/AA_Properties.htm
25
+ CRASP_PROPERTIES_PATH = join(MATRIX_DIR, "amino_acid_properties.txt")
26
+
27
+
28
+ def aa_dict_to_positional_list(aa_property_dict):
29
+ value_list = [None] * 20
30
+ for letter, value in aa_property_dict.items():
31
+ idx = letter_to_index(letter)
32
+ assert idx >= 0
33
+ assert idx < 20
34
+ value_list[idx] = value
35
+ assert all(elt is not None for elt in value_list), \
36
+ "Missing amino acids in:\n%s" % aa_property_dict.keys()
37
+ return value_list
38
+
39
+
40
+ def parse_property_table(table_string):
41
+ """
42
+ Parse lines like `1.80000 A ALA` into a residue property dictionary.
43
+
44
+ Parameters
45
+ ----------
46
+ table_string : str
47
+ One line per residue: the value, the one-letter code and, optionally,
48
+ further fields that are ignored. Blank lines are skipped.
49
+
50
+ Returns
51
+ -------
52
+ dict of str to float
53
+ Property value for each one-letter residue code, in line order.
54
+
55
+ Raises
56
+ ------
57
+ ValueError
58
+ If a line has fewer than two fields, a value is not a number, or a
59
+ residue is repeated.
60
+ """
61
+ value_dict = {}
62
+ for line in table_string.splitlines():
63
+ fields = line.split()
64
+ if not fields:
65
+ continue
66
+ if len(fields) < 2:
67
+ raise ValueError("Expected a value and a residue in '%s'" % line)
68
+ value, letter = fields[:2]
69
+ if letter in value_dict:
70
+ raise ValueError("Repeated amino acid in '%s'" % line)
71
+ value_dict[letter] = float(value)
72
+ return value_dict
73
+
74
+
75
+ def read_crasp_properties(path=CRASP_PROPERTIES_PATH):
76
+ """
77
+ Read every table of the CRASP amino-acid property file.
78
+
79
+ The file is a sequence of blocks separated by blank lines. Each block is
80
+ a numbered header such as `19. Hydropathy (Kyte and Doolittle 1982)`, a
81
+ line with the literature citation, and one `value letter code` line per
82
+ canonical residue. Blocks without a numbered header, such as the source
83
+ URL on the first line, are skipped.
84
+
85
+ Parameters
86
+ ----------
87
+ path : str or path-like
88
+ Location of the property file; defaults to the copy shipped with
89
+ pepdata.
90
+
91
+ Returns
92
+ -------
93
+ dict of str to dict of str to float
94
+ Residue property dictionaries keyed by their header, written as
95
+ `"<number>. <title>"` with a single space after the period.
96
+
97
+ Raises
98
+ ------
99
+ ValueError
100
+ If a header is repeated, a table is malformed, or a table does not
101
+ cover exactly the 20 canonical residues.
102
+ """
103
+ with open(path, "r") as f:
104
+ blocks = re.split(r"\n\s*\n", f.read())
105
+ properties = {}
106
+ for block in blocks:
107
+ lines = block.strip().splitlines()
108
+ header = re.fullmatch(r"(\d+)\.\s*(.+)", lines[0].strip()) if lines else None
109
+ if header is None:
110
+ continue
111
+ name = "%s. %s" % (header.group(1), header.group(2))
112
+ if name in properties:
113
+ raise ValueError("Repeated property table '%s'" % name)
114
+ table = parse_property_table("\n".join(lines[2:]))
115
+ if sorted(table) != sorted(canonical_amino_acid_letters):
116
+ raise ValueError(
117
+ "Property table '%s' does not cover the 20 canonical "
118
+ "amino acids: %s" % (name, "".join(table)))
119
+ properties[name] = table
120
+ return properties
121
+
122
+
123
+ # All 36 tables of the CRASP property file, keyed by their numbered header
124
+ crasp_properties = read_crasp_properties()
125
+
126
+ # The names below are the CRASP tables this module has always exported.
127
+ # Each comment gives the literature citation as written in the CRASP file.
128
+
129
+ # Kyte and Doolittle, J. Mol. Biol. (1982) 157 105-132
130
+ hydropathy = crasp_properties["19. Hydropathy (Kyte and Doolittle 1982)"]
131
+
132
+ # Chothia, Annu. Rev. Biochem. (1984) 53 537-572
133
+ volume = crasp_properties["7. Volume (Chothia 1984)"]
134
+
135
+ # Ponnuswamy et al., Biochim.Biophys.Acta(1980) 623 301-316
136
+ polarity = crasp_properties["32. Polarity (Ponnuswamy et al. 1980)"]
137
+
138
+ # White A,Handler P,Smith EL,Hill RL,Lehman IR;Principles of Biochemistry;1978
139
+ pK_side_chain = crasp_properties["35. pK of side chain (White et al., 1978)"]
140
+
141
+ # Janin et al., J. Mol. Biol. (1978) 125 357-386
142
+ prct_exposed_residues = crasp_properties[
143
+ "27. Percentage of exposed residues (Janin et al. 1978)"]
144
+
145
+ # Hopp and Woods, Proc. Natl. Acad. Sci. USA (1981) 78 3824-3828
146
+ hydrophilicity = crasp_properties["18. Hydrophilicity (Hopp and Woods 1981)"]
147
+
148
+ # Janin et al., J. Mol. Biol. (1978) 125 357-386
149
+ accessible_surface_area = crasp_properties[
150
+ "12. Average accessibility surface area (Janin et al. 1978)"]
151
+
152
+ # Ragone et al., Prot. Engineering(1989) 2 497-504
153
+ local_flexibility = crasp_properties["8. Local flexibility (Ragone et al. 1989)"]
154
+
155
+ # Rose et al., Science (1975) 229 834-838
156
+ accessible_surface_area_folded = crasp_properties[
157
+ "16. Average accessible surface area in folded proteins (Rose et al. 1985)"]
158
+
159
+ # Jones, J.theor.Biol.(1975) 50 167-183
160
+ refractivity = crasp_properties["22. Refractivity (Jones 1975)"]
161
+
162
+ # Biemann, Meth.Enzimol. (1990) v.193, p.888
163
+ mass = crasp_properties["33. Average isotopic mass (Biemann, 1990)"]
164
+
165
+ ###
166
+ # Values copied from:
167
+ # "Solvent accessibility of AA in known protein structures"
168
+ # http://prowl.rockefeller.edu/aainfo/access.htm
169
+ ###
170
+ """
171
+ Solvent accessibility of AA in known protein structures
172
+
173
+ Figure 1.
174
+
175
+ S 0.70 0.20 0.10
176
+ T 0.71 0.16 0.13
177
+ A 0.48 0.35 0.17
178
+ G 0.51 0.36 0.13
179
+ P 0.78 0.13 0.09
180
+ C 0.32 0.54 0.14
181
+ D 0.81 0.09 0.10
182
+ E 0.93 0.04 0.03
183
+ Q 0.81 0.10 0.09
184
+ N 0.82 0.10 0.08
185
+ L 0.41 0.49 0.10
186
+ I 0.39 0.47 0.14
187
+ V 0.40 0.50 0.10
188
+ M 0.44 0.20 0.36
189
+ F 0.42 0.42 0.16
190
+ Y 0.67 0.20 0.13
191
+ W 0.49 0.44 0.07
192
+ K 0.93 0.02 0.05
193
+ R 0.84 0.05 0.11
194
+ H 0.66 0.19 0.15
195
+ """
196
+
197
+ solvent_exposed_area = dict(
198
+ S=0.70,
199
+ T=0.71,
200
+ A=0.48,
201
+ G=0.51,
202
+ P=0.78,
203
+ C=0.32,
204
+ D=0.81,
205
+ E=0.93,
206
+ Q=0.81,
207
+ N=0.82,
208
+ L=0.41,
209
+ I=0.39,
210
+ V=0.40,
211
+ M=0.44,
212
+ F=0.42,
213
+ Y=0.67,
214
+ W=0.49,
215
+ K=0.93,
216
+ R=0.84,
217
+ H=0.66,
218
+ )
@@ -0,0 +1,78 @@
1
+ # Licensed under the Apache License, Version 2.0 (the "License");
2
+ # you may not use this file except in compliance with the License.
3
+ # You may obtain a copy of the License at
4
+ #
5
+ # http://www.apache.org/licenses/LICENSE-2.0
6
+ #
7
+ # Unless required by applicable law or agreed to in writing, software
8
+ # distributed under the License is distributed on an "AS IS" BASIS,
9
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10
+ # See the License for the specific language governing permissions and
11
+ # limitations under the License.
12
+
13
+ import warnings
14
+ from os.path import join
15
+
16
+ from .static_data import MATRIX_DIR
17
+
18
+ from .amino_acid_alphabet import dict_to_amino_acid_matrix
19
+ from .pairwise_table import (
20
+ parse_pairwise_table,
21
+ read_pairwise_table,
22
+ rekey_pairwise_table,
23
+ )
24
+
25
+
26
+ def parse_blosum_table(table, coeff_type=int, key_type='row'):
27
+ """
28
+ Parse a table of pairwise amino acid coefficient (e.g. BLOSUM50)
29
+
30
+ Deprecated since 1.3.8: use
31
+ `pepdata.pairwise_table.parse_pairwise_table` or
32
+ `pepdata.pairwise_table.read_pairwise_table`. This wrapper will be
33
+ removed in a future release.
34
+
35
+ Parameters
36
+ ----------
37
+ table : str
38
+ Contents of a table with a header line of column labels.
39
+
40
+ coeff_type : callable, default int
41
+ Converts each coefficient string to a number.
42
+
43
+ key_type : {'row', 'pair', 'pair_string'}
44
+ Layout of the returned dictionary; see
45
+ `pepdata.pairwise_table.rekey_pairwise_table`.
46
+
47
+ Returns
48
+ -------
49
+ dict
50
+ Coefficients in the requested layout.
51
+
52
+ Raises
53
+ ------
54
+ ValueError
55
+ If the table is malformed, has fewer than 20 column labels, or
56
+ `key_type` is unknown.
57
+ """
58
+ warnings.warn(
59
+ "parse_blosum_table is deprecated; use "
60
+ "pepdata.pairwise_table.parse_pairwise_table instead",
61
+ DeprecationWarning,
62
+ stacklevel=2)
63
+ coeffs = parse_pairwise_table(table, coeff_type=coeff_type)
64
+ n_labels = len(next(iter(coeffs.values())))
65
+ if n_labels < 20:
66
+ raise ValueError(
67
+ "Expected 20+ amino acids but header has %d fields" % n_labels)
68
+ return rekey_pairwise_table(coeffs, key_type=key_type)
69
+
70
+
71
+ blosum30_dict = read_pairwise_table(join(MATRIX_DIR, 'BLOSUM30'), coeff_type=int)
72
+ blosum30_matrix = dict_to_amino_acid_matrix(blosum30_dict)
73
+
74
+ blosum50_dict = read_pairwise_table(join(MATRIX_DIR, 'BLOSUM50'), coeff_type=int)
75
+ blosum50_matrix = dict_to_amino_acid_matrix(blosum50_dict)
76
+
77
+ blosum62_dict = read_pairwise_table(join(MATRIX_DIR, 'BLOSUM62'), coeff_type=int)
78
+ blosum62_matrix = dict_to_amino_acid_matrix(blosum62_dict)
@@ -10,7 +10,7 @@
10
10
  # See the License for the specific language governing permissions and
11
11
  # limitations under the License.
12
12
 
13
- from .amino_acid_alphabet import amino_acid_name_indices
13
+ from .amino_acid_alphabet import amino_acid_name_indices, index_to_letter
14
14
 
15
15
  # Chou-Fasman of structural properties from
16
16
  # http://prowl.rockefeller.edu/aainfo/chou.htm
@@ -55,7 +55,7 @@ def parse_chou_fasman(table):
55
55
  name = fields[0]
56
56
 
57
57
  assert name in amino_acid_name_indices, "Invalid amino acid name %s" % name
58
- letter = amino_acid_name_indices[name]
58
+ letter = index_to_letter(amino_acid_name_indices[name])
59
59
  alpha = int(fields[1])
60
60
  beta = int(fields[2])
61
61
  turn = int(fields[3])
@@ -17,7 +17,7 @@ import xml.etree.ElementTree
17
17
  from .common import cache
18
18
 
19
19
  ALLELE_XML_FILENAME = "MhcAlleleNames.xml"
20
- ALLELE_XML_URL = "http://www.iedb.org/doc/MhcAlleleNameList.zip"
20
+ ALLELE_XML_URL = "https://www.iedb.org/doc/MhcAlleleNameList.zip"
21
21
  ALLELE_XML_DECOMPRESS = True
22
22
 
23
23
  def local_path(force_download=False):
@@ -72,7 +72,7 @@ def get_mhc_class(
72
72
 
73
73
 
74
74
  def get_mhc_assay(
75
- df : pd.Series,
75
+ df : pd.DataFrame,
76
76
  group_candidates : list[str] = ASSAY_GROUP_CANDIDATES,
77
77
  column_candidates : list[str] =["method"]) -> pd.Series | None:
78
78
  return find(df, group_candidates, column_candidates)
@@ -27,13 +27,11 @@ from .columns import (
27
27
  get_assay_method,
28
28
  get_host_name,
29
29
  get_mhc_allele,
30
- get_mhc_class,
31
30
  get_epitope_name,
32
-
33
31
  )
34
32
 
35
33
  TCELL_COMPACT_FILENAME = "tcell_full.csv"
36
- TCELL_COMPACT_URL = "http://www.iedb.org/downloader.php?file_name=doc/tcell_full_v3.zip"
34
+ TCELL_COMPACT_URL = "https://www.iedb.org/downloader.php?file_name=doc/tcell_full_v3.zip"
37
35
  TCELL_COMPACT_DECOMPRESS = True
38
36
 
39
37
  def download(force=False):
@@ -118,7 +116,6 @@ def load_dataframe(
118
116
  encoding="latin-1")
119
117
 
120
118
  mhc = get_mhc_allele(df)
121
- mhc_class_series = get_mhc_class(df)
122
119
  epitopes = get_epitope_name(df)
123
120
  organism = get_host_name(df)
124
121
  assay_method_series = get_assay_method(df)
@@ -0,0 +1,167 @@
1
+ # Licensed under the Apache License, Version 2.0 (the "License");
2
+ # you may not use this file except in compliance with the License.
3
+ # You may obtain a copy of the License at
4
+ #
5
+ # http://www.apache.org/licenses/LICENSE-2.0
6
+ #
7
+ # Unless required by applicable law or agreed to in writing, software
8
+ # distributed under the License is distributed on an "AS IS" BASIS,
9
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10
+ # See the License for the specific language governing permissions and
11
+ # limitations under the License.
12
+
13
+ """
14
+ Read whitespace-delimited tables of amino-acid pair coefficients.
15
+
16
+ One parser serves the BLOSUM substitution matrices, the PMBEC matrix and the
17
+ residue contact energy tables that ship in `pepdata/matrices`.
18
+ """
19
+
20
+
21
+ def parse_pairwise_table(text, column_order=None, coeff_type=float):
22
+ """
23
+ Parse the text of a table of pairwise amino-acid coefficients.
24
+
25
+ Blank lines and lines starting with `#` are ignored.
26
+
27
+ Parameters
28
+ ----------
29
+ text : str
30
+ Contents of the table.
31
+
32
+ column_order : sequence of str, optional
33
+ Residue labels of a table that has no header line and no row labels,
34
+ such as the residue contact energy tables. The same labels name the
35
+ rows and the columns, so the table must be square. When omitted, the
36
+ first line holds the column labels and every later line starts with
37
+ its row label.
38
+
39
+ coeff_type : callable, default float
40
+ Converts each coefficient string to a number, e.g. `int` for
41
+ BLOSUM scores.
42
+
43
+ Returns
44
+ -------
45
+ dict of str to dict of str to number
46
+ Coefficients accessed like `table[row_label][column_label]`.
47
+
48
+ Raises
49
+ ------
50
+ ValueError
51
+ If the table is empty, a label is repeated, a row has the wrong
52
+ number of fields, or a coefficient is not a number.
53
+ """
54
+ rows = [
55
+ line.split()
56
+ for line in text.splitlines()
57
+ if line.strip() and not line.strip().startswith("#")
58
+ ]
59
+ if column_order is None:
60
+ if not rows:
61
+ raise ValueError("Pairwise table has no header line")
62
+ column_labels = rows[0]
63
+ labeled_rows = [(fields[0], fields[1:]) for fields in rows[1:]]
64
+ else:
65
+ column_labels = list(column_order)
66
+ if len(rows) != len(column_labels):
67
+ raise ValueError(
68
+ "Expected %d rows in pairwise table but found %d" % (
69
+ len(column_labels), len(rows)))
70
+ labeled_rows = list(zip(column_labels, rows))
71
+
72
+ if len(set(column_labels)) != len(column_labels):
73
+ raise ValueError(
74
+ "Repeated column label in pairwise table: %s" % (column_labels,))
75
+ if not labeled_rows:
76
+ raise ValueError("Pairwise table has no rows")
77
+
78
+ table = {}
79
+ for row_label, coeff_strings in labeled_rows:
80
+ if row_label in table:
81
+ raise ValueError(
82
+ "Repeated row label '%s' in pairwise table" % row_label)
83
+ if len(coeff_strings) != len(column_labels):
84
+ raise ValueError(
85
+ "Expected %d coefficients in row '%s' but found %d" % (
86
+ len(column_labels), row_label, len(coeff_strings)))
87
+ table[row_label] = {
88
+ column_label: coeff_type(coeff_string)
89
+ for column_label, coeff_string in zip(column_labels, coeff_strings)
90
+ }
91
+ return table
92
+
93
+
94
+ def read_pairwise_table(path, column_order=None, coeff_type=float):
95
+ """
96
+ Read a file holding a table of pairwise amino-acid coefficients.
97
+
98
+ Parameters
99
+ ----------
100
+ path : str or path-like
101
+ Location of the table.
102
+
103
+ column_order : sequence of str, optional
104
+ Residue labels of a table without a header line or row labels; see
105
+ `parse_pairwise_table`.
106
+
107
+ coeff_type : callable, default float
108
+ Converts each coefficient string to a number.
109
+
110
+ Returns
111
+ -------
112
+ dict of str to dict of str to number
113
+ Coefficients accessed like `table[row_label][column_label]`.
114
+
115
+ Raises
116
+ ------
117
+ ValueError
118
+ If the table is malformed; see `parse_pairwise_table`.
119
+ """
120
+ with open(path, "r") as f:
121
+ return parse_pairwise_table(
122
+ f.read(), column_order=column_order, coeff_type=coeff_type)
123
+
124
+
125
+ def rekey_pairwise_table(table, key_type="row"):
126
+ """
127
+ Convert a nested pairwise table to one of the legacy key layouts.
128
+
129
+ Exists to support the deprecated `key_type` argument of
130
+ `pepdata.blosum.parse_blosum_table` and
131
+ `pepdata.pmbec.read_pmbec_coefficients`.
132
+
133
+ Parameters
134
+ ----------
135
+ table : dict of str to dict of str to number
136
+ Coefficients accessed like `table[row_label][column_label]`.
137
+
138
+ key_type : {'row', 'pair', 'pair_string'}
139
+ `'row'` returns the nested layout unchanged, `'pair'` keys each
140
+ coefficient by a `(row_label, column_label)` tuple and
141
+ `'pair_string'` by the two labels joined into one string.
142
+
143
+ Returns
144
+ -------
145
+ dict
146
+ The coefficients in the requested layout.
147
+
148
+ Raises
149
+ ------
150
+ ValueError
151
+ If `key_type` is not one of the three layouts.
152
+ """
153
+ if key_type == "row":
154
+ return table
155
+ if key_type == "pair":
156
+ return {
157
+ (row_label, column_label): coeff
158
+ for row_label, row in table.items()
159
+ for column_label, coeff in row.items()
160
+ }
161
+ if key_type == "pair_string":
162
+ return {
163
+ row_label + column_label: coeff
164
+ for row_label, row in table.items()
165
+ for column_label, coeff in row.items()
166
+ }
167
+ raise ValueError("Unknown key type: %s" % key_type)
@@ -23,7 +23,8 @@ def make_count_vectorizer(reduced_alphabet, max_ngram):
23
23
  if reduced_alphabet is None:
24
24
  preprocessor = None
25
25
  else:
26
- preprocessor = lambda s: "".join([reduced_alphabet[si] for si in s])
26
+ def preprocessor(s):
27
+ return "".join([reduced_alphabet[si] for si in s])
27
28
 
28
29
  return CountVectorizer(
29
30
  analyzer='char',
@@ -0,0 +1,78 @@
1
+ # Copyright (c) 2014-2016. Mount Sinai School of Medicine
2
+ #
3
+ # Licensed under the Apache License, Version 2.0 (the "License");
4
+ # you may not use this file except in compliance with the License.
5
+ # You may obtain a copy of the License at
6
+ #
7
+ # http://www.apache.org/licenses/LICENSE-2.0
8
+ #
9
+ # Unless required by applicable law or agreed to in writing, software
10
+ # distributed under the License is distributed on an "AS IS" BASIS,
11
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ # See the License for the specific language governing permissions and
13
+ # limitations under the License.
14
+
15
+ import warnings
16
+ from os.path import join
17
+
18
+ from .static_data import MATRIX_DIR
19
+
20
+ from .amino_acid_alphabet import dict_to_amino_acid_matrix
21
+ from .pairwise_table import read_pairwise_table, rekey_pairwise_table
22
+
23
+ PMBEC_PATH = join(MATRIX_DIR, 'pmbec.mat')
24
+
25
+
26
+ def read_pmbec_coefficients(
27
+ key_type='row',
28
+ verbose=False,
29
+ filename=PMBEC_PATH):
30
+ """
31
+ Read the PMBEC coefficient matrix.
32
+
33
+ Deprecated since 1.3.8: use
34
+ `pepdata.pairwise_table.read_pairwise_table`. This wrapper will be
35
+ removed in a future release.
36
+
37
+ Parameters
38
+ ----------
39
+ key_type : str
40
+ 'row' : every key is a single amino acid,
41
+ which maps to a dictionary for that row
42
+ 'pair' : every key is a tuple of amino acids
43
+ 'pair_string' : every key is a string of two amino acid characters
44
+
45
+ verbose : bool
46
+ Ignored. Earlier releases printed the header and residue list to
47
+ stdout by default; nothing is printed any more.
48
+
49
+ filename : str
50
+ Location of PMBEC coefficient matrix
51
+
52
+ Returns
53
+ -------
54
+ dict
55
+ Coefficients in the requested layout.
56
+
57
+ Raises
58
+ ------
59
+ ValueError
60
+ If the table is malformed, does not have exactly 20 columns, or
61
+ `key_type` is unknown.
62
+ """
63
+ warnings.warn(
64
+ "read_pmbec_coefficients is deprecated; use "
65
+ "pepdata.pairwise_table.read_pairwise_table instead",
66
+ DeprecationWarning,
67
+ stacklevel=2)
68
+ coeffs = read_pairwise_table(filename)
69
+ n_labels = len(next(iter(coeffs.values())))
70
+ if n_labels != 20:
71
+ raise ValueError(
72
+ "Expected 20 amino acids but header has %d fields" % n_labels)
73
+ return rekey_pairwise_table(coeffs, key_type=key_type)
74
+
75
+
76
+ # dictionary of PMBEC coefficient accessed like pmbec_dict["V"]["R"]
77
+ pmbec_dict = read_pairwise_table(PMBEC_PATH)
78
+ pmbec_matrix = dict_to_amino_acid_matrix(pmbec_dict)