Spaces:
Running on Zero
Running on Zero
File size: 8,512 Bytes
213083c | 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 78 79 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 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 | """
File: CodonEvaluation.py
---------------------------
Includes functions to calculate various evaluation metrics along with helper
functions.
"""
from typing import Dict, List, Tuple
import pandas as pd
from CAI import CAI, relative_adaptiveness
from tqdm import tqdm
def get_CSI_weights(sequences: List[str]) -> Dict[str, float]:
"""
Calculate the Codon Similarity Index (CSI) weights for a list of DNA sequences.
Args:
sequences (List[str]): List of DNA sequences.
Returns:
dict: The CSI weights.
"""
return relative_adaptiveness(sequences=sequences)
def get_CSI_value(dna: str, weights: Dict[str, float]) -> float:
"""
Calculate the Codon Similarity Index (CSI) for a DNA sequence.
Args:
dna (str): The DNA sequence.
weights (dict): The CSI weights from get_CSI_weights.
Returns:
float: The CSI value.
"""
return CAI(dna, weights)
def get_organism_to_CSI_weights(
dataset: pd.DataFrame, organisms: List[str]
) -> Dict[str, dict]:
"""
Calculate the Codon Similarity Index (CSI) weights for a list of organisms.
Args:
dataset (pd.DataFrame): Dataset containing organism and DNA sequence info.
organisms (List[str]): List of organism names.
Returns:
Dict[str, dict]: A dictionary mapping each organism to its CSI weights.
"""
organism2weights = {}
# Iterate through each organism to calculate its CSI weights
for organism in tqdm(organisms, desc="Calculating CSI Weights: ", unit="Organism"):
organism_data = dataset.loc[dataset["organism"] == organism]
sequences = organism_data["dna"].to_list()
weights = get_CSI_weights(sequences)
organism2weights[organism] = weights
return organism2weights
def get_GC_content(dna: str, lower: bool = False) -> float:
"""
Calculate the GC content of a DNA sequence.
Args:
dna (str): The DNA sequence.
lower (bool): If True, converts DNA sequence to lowercase before calculation.
Returns:
float: The GC content as a percentage.
"""
if lower:
dna = dna.lower()
return (dna.count("G") + dna.count("C")) / len(dna) * 100
def get_cfd(
dna: str,
codon_frequencies: Dict[str, Tuple[List[str], List[float]]],
threshold: float = 0.3,
) -> float:
"""
Calculate the codon frequency distribution (CFD) metric for a DNA sequence.
Args:
dna (str): The DNA sequence.
codon_frequencies (Dict[str, Tuple[List[str], List[float]]]): Codon
frequency distribution per amino acid.
threshold (float): Frequency threshold for counting rare codons.
Returns:
float: The CFD metric as a percentage.
"""
# Get a dictionary mapping each codon to its normalized frequency
codon2frequency = {
codon: freq / max(frequencies)
for amino, (codons, frequencies) in codon_frequencies.items()
for codon, freq in zip(codons, frequencies)
}
cfd = 0
# Iterate through the DNA sequence in steps of 3 to process each codon
for i in range(0, len(dna), 3):
codon = dna[i : i + 3]
codon_frequency = codon2frequency[codon]
if codon_frequency < threshold:
cfd += 1
return cfd / (len(dna) / 3) * 100
def get_min_max_percentage(
dna: str,
codon_frequencies: Dict[str, Tuple[List[str], List[float]]],
window_size: int = 18,
) -> List[float]:
"""
Calculate the %MinMax metric for a DNA sequence.
Args:
dna (str): The DNA sequence.
codon_frequencies (Dict[str, Tuple[List[str], List[float]]]): Codon
frequency distribution per amino acid.
window_size (int): Size of the window to calculate %MinMax.
Returns:
List[float]: List of %MinMax values for the sequence.
Credit: https://github.com/chowington/minmax
"""
# Get a dictionary mapping each codon to its respective amino acid
codon2amino = {
codon: amino
for amino, (codons, frequencies) in codon_frequencies.items()
for codon in codons
}
min_max_values = []
codons = [dna[i : i + 3] for i in range(0, len(dna), 3)] # Split DNA into codons
# Iterate through the DNA sequence using the specified window size
for i in range(len(codons) - window_size + 1):
codon_window = codons[i : i + window_size] # Codons in the current window
Actual = 0.0 # Average of the actual codon frequencies
Max = 0.0 # Average of the min codon frequencies
Min = 0.0 # Average of the max codon frequencies
Avg = 0.0 # Average of the averages of all frequencies for each amino acid
# Sum the frequencies for codons in the current window
for codon in codon_window:
aminoacid = codon2amino[codon]
frequencies = codon_frequencies[aminoacid][1]
codon_index = codon_frequencies[aminoacid][0].index(codon)
codon_frequency = codon_frequencies[aminoacid][1][codon_index]
Actual += codon_frequency
Max += max(frequencies)
Min += min(frequencies)
Avg += sum(frequencies) / len(frequencies)
# Divide by the window size to get the averages
Actual = Actual / window_size
Max = Max / window_size
Min = Min / window_size
Avg = Avg / window_size
# Calculate %MinMax
percentMax = ((Actual - Avg) / (Max - Avg)) * 100
percentMin = ((Avg - Actual) / (Avg - Min)) * 100
# Append the appropriate %MinMax value
if percentMax >= 0:
min_max_values.append(percentMax)
else:
min_max_values.append(-percentMin)
# Populate the last floor(window_size / 2) entries of min_max_values with None
for i in range(int(window_size / 2)):
min_max_values.append(None)
return min_max_values
def get_sequence_complexity(dna: str) -> float:
"""
Calculate the sequence complexity score of a DNA sequence.
Args:
dna (str): The DNA sequence.
Returns:
float: The sequence complexity score.
"""
def sum_up_to(x):
"""Recursive function to calculate the sum of integers from 1 to x."""
if x <= 1:
return 1
else:
return x + sum_up_to(x - 1)
def f(x):
"""Returns 4 if x is greater than or equal to 4, else returns x."""
if x >= 4:
return 4
elif x < 4:
return x
unique_subseq_length = []
# Calculate unique subsequences lengths
for i in range(1, len(dna) + 1):
unique_subseq = set()
for j in range(len(dna) - (i - 1)):
unique_subseq.add(dna[j : (j + i)])
unique_subseq_length.append(len(unique_subseq))
# Calculate complexity score
complexity_score = (
sum(unique_subseq_length) / (sum_up_to(len(dna) - 1) + f(len(dna)))
) * 100
return complexity_score
def get_sequence_similarity(
original: str, predicted: str, truncate: bool = True, window_length: int = 1
) -> float:
"""
Calculate the sequence similarity between two sequences.
Args:
original (str): The original sequence.
predicted (str): The predicted sequence.
truncate (bool): If True, truncate the original sequence to match the length
of the predicted sequence.
window_length (int): Length of the window for comparison (1 for amino acids,
3 for codons).
Returns:
float: The sequence similarity as a percentage.
Preconditions:
len(predicted) <= len(original).
"""
if not truncate and len(original) != len(predicted):
raise ValueError(
"Set truncate to True if the length of sequences do not match."
)
identity = 0.0
original = original.strip()
predicted = predicted.strip()
if truncate:
original = original[: len(predicted)]
if window_length == 1:
# Simple comparison for amino acid
for i in range(len(predicted)):
if original[i] == predicted[i]:
identity += 1
else:
# Comparison for substrings based on window_length
for i in range(0, len(original) - window_length + 1, window_length):
if original[i : i + window_length] == predicted[i : i + window_length]:
identity += 1
return (identity / (len(predicted) / window_length)) * 100
|