Spaces:
Sleeping
Sleeping
Upload indices.py with huggingface_hub
Browse files- indices.py +22 -5
indices.py
CHANGED
|
@@ -212,13 +212,14 @@ class mRNAStructureAnalyzer:
|
|
| 212 |
"""Convert DNA sequence to RNA (T -> U)."""
|
| 213 |
return dna_seq.upper().replace('T', 'U')
|
| 214 |
|
| 215 |
-
def calculate_mfe(self, sequence: str, is_rna: bool = False) -> float:
|
| 216 |
"""
|
| 217 |
Calculate minimum free energy (MFE) for a sequence.
|
| 218 |
|
| 219 |
Args:
|
| 220 |
sequence: DNA or RNA sequence
|
| 221 |
is_rna: If True, sequence is already RNA
|
|
|
|
| 222 |
|
| 223 |
Returns:
|
| 224 |
MFE in kcal/mol (more negative = more stable structure)
|
|
@@ -232,6 +233,10 @@ class mRNAStructureAnalyzer:
|
|
| 232 |
if len(sequence) < 4:
|
| 233 |
return 0.0
|
| 234 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 235 |
try:
|
| 236 |
return dg(sequence, temp=self.temperature)
|
| 237 |
except Exception:
|
|
@@ -312,12 +317,13 @@ class mRNAStructureAnalyzer:
|
|
| 312 |
except Exception:
|
| 313 |
return ""
|
| 314 |
|
| 315 |
-
def analyze(self, sequence: str) -> dict:
|
| 316 |
"""
|
| 317 |
-
Perform
|
| 318 |
|
| 319 |
Args:
|
| 320 |
sequence: DNA sequence
|
|
|
|
| 321 |
|
| 322 |
Returns:
|
| 323 |
Dictionary with structure metrics
|
|
@@ -332,9 +338,20 @@ class mRNAStructureAnalyzer:
|
|
| 332 |
'structure_penalty': 0.0,
|
| 333 |
}
|
| 334 |
|
| 335 |
-
|
| 336 |
five_prime_mfe = self.calculate_5prime_mfe(sequence)
|
| 337 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 338 |
|
| 339 |
# Calculate structure penalty for optimization
|
| 340 |
# Penalize stable 5' structures (< -30 kcal/mol is problematic)
|
|
|
|
| 212 |
"""Convert DNA sequence to RNA (T -> U)."""
|
| 213 |
return dna_seq.upper().replace('T', 'U')
|
| 214 |
|
| 215 |
+
def calculate_mfe(self, sequence: str, is_rna: bool = False, max_length: int = 200) -> float:
|
| 216 |
"""
|
| 217 |
Calculate minimum free energy (MFE) for a sequence.
|
| 218 |
|
| 219 |
Args:
|
| 220 |
sequence: DNA or RNA sequence
|
| 221 |
is_rna: If True, sequence is already RNA
|
| 222 |
+
max_length: Maximum sequence length to analyze (longer sequences are truncated)
|
| 223 |
|
| 224 |
Returns:
|
| 225 |
MFE in kcal/mol (more negative = more stable structure)
|
|
|
|
| 233 |
if len(sequence) < 4:
|
| 234 |
return 0.0
|
| 235 |
|
| 236 |
+
# Truncate long sequences for performance (RNA folding is O(n³))
|
| 237 |
+
if len(sequence) > max_length:
|
| 238 |
+
sequence = sequence[:max_length]
|
| 239 |
+
|
| 240 |
try:
|
| 241 |
return dg(sequence, temp=self.temperature)
|
| 242 |
except Exception:
|
|
|
|
| 317 |
except Exception:
|
| 318 |
return ""
|
| 319 |
|
| 320 |
+
def analyze(self, sequence: str, fast_mode: bool = True) -> dict:
|
| 321 |
"""
|
| 322 |
+
Perform mRNA structure analysis.
|
| 323 |
|
| 324 |
Args:
|
| 325 |
sequence: DNA sequence
|
| 326 |
+
fast_mode: If True, only analyze critical regions (faster)
|
| 327 |
|
| 328 |
Returns:
|
| 329 |
Dictionary with structure metrics
|
|
|
|
| 338 |
'structure_penalty': 0.0,
|
| 339 |
}
|
| 340 |
|
| 341 |
+
# Always calculate 5' MFE (most important for translation)
|
| 342 |
five_prime_mfe = self.calculate_5prime_mfe(sequence)
|
| 343 |
+
|
| 344 |
+
# For full MFE, only analyze first 200 nt in fast mode
|
| 345 |
+
if fast_mode:
|
| 346 |
+
full_mfe = self.calculate_mfe(sequence[:min(200, len(sequence))])
|
| 347 |
+
# Skip hairpin detection in fast mode for long sequences
|
| 348 |
+
if len(sequence) > 300:
|
| 349 |
+
hairpins = []
|
| 350 |
+
else:
|
| 351 |
+
hairpins = self.find_hairpins(sequence, window_size=40, step=40, threshold=-12.0)
|
| 352 |
+
else:
|
| 353 |
+
full_mfe = self.calculate_mfe(sequence, max_length=500)
|
| 354 |
+
hairpins = self.find_hairpins(sequence)
|
| 355 |
|
| 356 |
# Calculate structure penalty for optimization
|
| 357 |
# Penalize stable 5' structures (< -30 kcal/mol is problematic)
|