Knapsack
Knapsack(max_mass, mass_scale, residues, residue_indices, masses, chart)
dataclass
A class that precomputes and stores a knapsack chart.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
max_mass |
float
|
The maximum mass up to which the chart is calculated. |
required |
mass_scale |
int
|
The scale in Daltons at which masses are calculated and rounded off. For example, a scale of 10000 would represent masses at a scale of 1e4 Da. |
required |
residues |
list[str]
|
The list of residues that are considered
in knapsack decoding. The order of this
list is the inverse of |
required |
residue_indices |
dict[str, int]
|
A mapping from residues as strings
to indices in the knapsack chart.
This is the inverse of |
required |
masses |
numpy.ndarray[number of masses]
|
The set of realisable masses in ascending order. |
required |
chart |
torch.BoolTensor[number of masses, number of residues]
|
The chart of realisable masses and residues that
can lead to these masses.
|
required |
construct_knapsack(residue_masses, residue_indices, max_mass, mass_scale)
classmethod
Construct a knapsack chart using depth-first search.
Previous construction algorithms have used dynamic
programming, but its space and time complexity
scale linearly with mass resolution since every
possible
mass is iterated over rather than only the
feasible
masses.
Graph search algorithms only
iterate over feasible
masses which become a
smaller and smaller share of possible masses as the
mass resolution increases. This leads to dramatic
performance improvements.
This implementation uses depth-first search since its agenda is a stack which can be implemented using python lists whose operations have amortized constant time complexity.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
residue_masses |
dict[str, float]
|
A mapping from considered residues to their masses. |
required |
max_mass |
float
|
The maximum mass up to which the chart is calculated. |
required |
mass_scale |
int
|
The scale in Daltons at which masses are calculated and rounded off. For example, a scale of 10000 would represent masses at a scale of 1e4 Da. |
required |
Source code in instanovo/inference/knapsack.py
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 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 |
|
from_file(path)
classmethod
Load a knapsack saved to a directory.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
path |
str
|
The path to the directory. |
required |
Returns:
Name | Type | Description |
---|---|---|
_type_ |
Knapsack
|
description |
Source code in instanovo/inference/knapsack.py
get_feasible_masses(target_mass, tolerance)
Find a set of feasible masses for a given target mass and tolerance using binary search.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
target_mass |
float
|
The masses to be decoded in Daltons. |
required |
tolerance |
float
|
The mass tolerance in Daltons. |
required |
Returns:
Type | Description |
---|---|
list[int]
|
list[int]: A list of feasible masses. |
Source code in instanovo/inference/knapsack.py
save(path)
Save the knapsack file to a directory.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
path |
str
|
The path to the directory. |
required |
Raises:
Type | Description |
---|---|
FileExistsError
|
If the directory |