sirnaforge 0.2.0__py3-none-any.whl
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.
- sirnaforge/__init__.py +77 -0
- sirnaforge/cli.py +837 -0
- sirnaforge/core/__init__.py +1 -0
- sirnaforge/core/design.py +470 -0
- sirnaforge/core/off_target.py +713 -0
- sirnaforge/core/thermodynamics.py +188 -0
- sirnaforge/data/__init__.py +21 -0
- sirnaforge/data/base.py +901 -0
- sirnaforge/data/gene_search.py +254 -0
- sirnaforge/data/mirna_manager.py +634 -0
- sirnaforge/data/orf_analysis.py +448 -0
- sirnaforge/models/__init__.py +19 -0
- sirnaforge/models/schemas.py +280 -0
- sirnaforge/models/sirna.py +337 -0
- sirnaforge/pipeline/__init__.py +17 -0
- sirnaforge/pipeline/nextflow/__init__.py +6 -0
- sirnaforge/pipeline/nextflow/config.py +487 -0
- sirnaforge/pipeline/nextflow/runner.py +462 -0
- sirnaforge/pipeline/nextflow/workflows/main.nf +134 -0
- sirnaforge/pipeline/nextflow/workflows/modules/local/aggregate_results.nf +99 -0
- sirnaforge/pipeline/nextflow/workflows/modules/local/build_bowtie_index.nf +58 -0
- sirnaforge/pipeline/nextflow/workflows/modules/local/build_bwa_index.nf +57 -0
- sirnaforge/pipeline/nextflow/workflows/modules/local/environment.yml +11 -0
- sirnaforge/pipeline/nextflow/workflows/modules/local/offtarget_analysis.nf +73 -0
- sirnaforge/pipeline/nextflow/workflows/modules/local/prepare_candidates.nf +65 -0
- sirnaforge/pipeline/nextflow/workflows/modules/local/resolve_indices.nf +65 -0
- sirnaforge/pipeline/nextflow/workflows/modules/local/sirnaforge_analysis.nf +0 -0
- sirnaforge/pipeline/nextflow/workflows/modules/local/split_candidates.nf +67 -0
- sirnaforge/pipeline/nextflow/workflows/nextflow.config +218 -0
- sirnaforge/pipeline/nextflow/workflows/subworkflows/local/sirna_offtarget_analysis.nf +147 -0
- sirnaforge/pipeline/resources/__init__.py +5 -0
- sirnaforge/pipeline/resources/genomes.yaml +28 -0
- sirnaforge/pipeline/resources/resources.py +177 -0
- sirnaforge/pipeline/resources/test_data/genomes.yaml +28 -0
- sirnaforge/pipeline/resources/test_data/test_candidates.fasta +10 -0
- sirnaforge/pipeline/resources/test_data/test_mirna_seeds.fasta +14 -0
- sirnaforge/pipeline/resources/test_data/test_transcriptome.fasta +6 -0
- sirnaforge/utils/__init__.py +1 -0
- sirnaforge/utils/logging_utils.py +73 -0
- sirnaforge/validation/__init__.py +31 -0
- sirnaforge/validation/config.py +110 -0
- sirnaforge/validation/middleware.py +302 -0
- sirnaforge/validation/utils.py +285 -0
- sirnaforge/workflow.py +1157 -0
- sirnaforge-0.2.0.dist-info/METADATA +938 -0
- sirnaforge-0.2.0.dist-info/RECORD +49 -0
- sirnaforge-0.2.0.dist-info/WHEEL +4 -0
- sirnaforge-0.2.0.dist-info/entry_points.txt +2 -0
- sirnaforge-0.2.0.dist-info/licenses/LICENSE +21 -0
|
@@ -0,0 +1,254 @@
|
|
|
1
|
+
"""Gene search and sequence retrieval from multiple databases."""
|
|
2
|
+
|
|
3
|
+
import asyncio
|
|
4
|
+
from pathlib import Path
|
|
5
|
+
from typing import Optional, Union
|
|
6
|
+
|
|
7
|
+
from pydantic import BaseModel, ConfigDict, Field
|
|
8
|
+
|
|
9
|
+
from sirnaforge.data.base import (
|
|
10
|
+
AbstractDatabaseClient,
|
|
11
|
+
DatabaseAccessError,
|
|
12
|
+
DatabaseType,
|
|
13
|
+
EnsemblClient,
|
|
14
|
+
FastaUtils,
|
|
15
|
+
GencodeClient,
|
|
16
|
+
GeneInfo,
|
|
17
|
+
GeneNotFoundError,
|
|
18
|
+
RefSeqClient,
|
|
19
|
+
TranscriptInfo,
|
|
20
|
+
get_database_display_name,
|
|
21
|
+
)
|
|
22
|
+
from sirnaforge.utils.logging_utils import get_logger
|
|
23
|
+
|
|
24
|
+
logger = get_logger(__name__)
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
class GeneSearchResult(BaseModel):
|
|
28
|
+
"""Complete gene search result."""
|
|
29
|
+
|
|
30
|
+
query: str
|
|
31
|
+
database: DatabaseType
|
|
32
|
+
gene_info: Optional[GeneInfo] = None
|
|
33
|
+
transcripts: list[TranscriptInfo] = Field(default_factory=list)
|
|
34
|
+
error: Optional[str] = None
|
|
35
|
+
is_access_error: bool = False # True if error was due to access/network issues
|
|
36
|
+
|
|
37
|
+
model_config = ConfigDict(use_enum_values=True)
|
|
38
|
+
|
|
39
|
+
@property
|
|
40
|
+
def success(self) -> bool:
|
|
41
|
+
"""Check if search was successful."""
|
|
42
|
+
return self.gene_info is not None and len(self.transcripts) > 0
|
|
43
|
+
|
|
44
|
+
|
|
45
|
+
class GeneSearcher:
|
|
46
|
+
"""Search genes and retrieve sequences from genomic databases using multiple clients."""
|
|
47
|
+
|
|
48
|
+
def __init__(self, timeout: int = 30, max_retries: int = 3):
|
|
49
|
+
"""
|
|
50
|
+
Initialize gene searcher with database clients.
|
|
51
|
+
|
|
52
|
+
Args:
|
|
53
|
+
timeout: Request timeout in seconds
|
|
54
|
+
max_retries: Maximum retry attempts
|
|
55
|
+
"""
|
|
56
|
+
self.timeout = timeout
|
|
57
|
+
self.max_retries = max_retries
|
|
58
|
+
|
|
59
|
+
# Initialize database clients
|
|
60
|
+
self.clients: dict[DatabaseType, AbstractDatabaseClient] = {
|
|
61
|
+
DatabaseType.ENSEMBL: EnsemblClient(timeout=timeout),
|
|
62
|
+
DatabaseType.REFSEQ: RefSeqClient(timeout=timeout),
|
|
63
|
+
DatabaseType.GENCODE: GencodeClient(timeout=timeout),
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
def get_client(self, database: DatabaseType) -> AbstractDatabaseClient:
|
|
67
|
+
"""Get the client for a specific database."""
|
|
68
|
+
return self.clients[database]
|
|
69
|
+
|
|
70
|
+
async def search_gene_with_fallback(self, query: str, include_sequence: bool = True) -> GeneSearchResult:
|
|
71
|
+
"""
|
|
72
|
+
Search for a gene with automatic fallback to other databases.
|
|
73
|
+
|
|
74
|
+
Tries databases in order: Ensembl -> RefSeq -> GENCODE
|
|
75
|
+
Falls back to next database only if access is blocked (not if gene is not found).
|
|
76
|
+
|
|
77
|
+
Args:
|
|
78
|
+
query: Gene ID, gene name, or transcript ID
|
|
79
|
+
include_sequence: Whether to fetch transcript sequences
|
|
80
|
+
|
|
81
|
+
Returns:
|
|
82
|
+
GeneSearchResult from the first accessible database
|
|
83
|
+
"""
|
|
84
|
+
databases_to_try = [DatabaseType.ENSEMBL, DatabaseType.REFSEQ, DatabaseType.GENCODE]
|
|
85
|
+
|
|
86
|
+
for db in databases_to_try:
|
|
87
|
+
logger.info(f"Searching for '{query}' in {get_database_display_name(db)}")
|
|
88
|
+
|
|
89
|
+
try:
|
|
90
|
+
result = await self.search_gene(query, db, include_sequence)
|
|
91
|
+
if result.success:
|
|
92
|
+
logger.info(f"Successfully found '{query}' in {get_database_display_name(db)}")
|
|
93
|
+
return result
|
|
94
|
+
if result.is_access_error:
|
|
95
|
+
# Access error - try next database
|
|
96
|
+
logger.warning(f"Access blocked to {get_database_display_name(db)}: {result.error}")
|
|
97
|
+
continue
|
|
98
|
+
if result.error and "not yet implemented" in result.error:
|
|
99
|
+
# Implementation missing - try next database
|
|
100
|
+
logger.info(f"{get_database_display_name(db)} search not implemented, trying next database")
|
|
101
|
+
continue
|
|
102
|
+
# Gene not found in this database, but database is accessible
|
|
103
|
+
# Continue to next database to see if gene exists there
|
|
104
|
+
logger.info(f"Gene '{query}' not found in {get_database_display_name(db)}, trying next database")
|
|
105
|
+
continue
|
|
106
|
+
|
|
107
|
+
except DatabaseAccessError as e:
|
|
108
|
+
logger.warning(f"Access blocked to {get_database_display_name(db)}: {e}")
|
|
109
|
+
# Try next database
|
|
110
|
+
continue
|
|
111
|
+
except Exception as e:
|
|
112
|
+
logger.error(f"Unexpected error searching {get_database_display_name(db)}: {e}")
|
|
113
|
+
# Try next database
|
|
114
|
+
continue
|
|
115
|
+
|
|
116
|
+
# If we get here, all databases failed or gene not found anywhere
|
|
117
|
+
return GeneSearchResult(
|
|
118
|
+
query=query,
|
|
119
|
+
database=DatabaseType.ENSEMBL, # Default to first attempted
|
|
120
|
+
error=f"Gene '{query}' not found in any accessible database",
|
|
121
|
+
)
|
|
122
|
+
|
|
123
|
+
async def search_gene(
|
|
124
|
+
self, query: str, database: Optional[DatabaseType] = None, include_sequence: bool = True
|
|
125
|
+
) -> GeneSearchResult:
|
|
126
|
+
"""
|
|
127
|
+
Search for a gene and retrieve its isoforms.
|
|
128
|
+
|
|
129
|
+
Args:
|
|
130
|
+
query: Gene ID, gene name, or transcript ID
|
|
131
|
+
database: Database to search (defaults to Ensembl)
|
|
132
|
+
include_sequence: Whether to fetch transcript sequences
|
|
133
|
+
|
|
134
|
+
Returns:
|
|
135
|
+
GeneSearchResult with gene info and transcripts
|
|
136
|
+
"""
|
|
137
|
+
db = database or DatabaseType.ENSEMBL
|
|
138
|
+
|
|
139
|
+
logger.info(f"Searching for '{query}' in {get_database_display_name(db)}")
|
|
140
|
+
|
|
141
|
+
try:
|
|
142
|
+
client = self.get_client(db)
|
|
143
|
+
gene_info, transcripts = await client.search_gene(query, include_sequence)
|
|
144
|
+
|
|
145
|
+
return GeneSearchResult(query=query, database=db, gene_info=gene_info, transcripts=transcripts)
|
|
146
|
+
|
|
147
|
+
except DatabaseAccessError as e:
|
|
148
|
+
logger.warning(f"Access error for '{query}' in {get_database_display_name(db)}: {e}")
|
|
149
|
+
return GeneSearchResult(query=query, database=db, error=str(e), is_access_error=True)
|
|
150
|
+
except GeneNotFoundError as e:
|
|
151
|
+
logger.info(f"Gene '{query}' not found in {get_database_display_name(db)}")
|
|
152
|
+
return GeneSearchResult(query=query, database=db, error=str(e), is_access_error=False)
|
|
153
|
+
except Exception as e:
|
|
154
|
+
logger.error(f"Search failed for '{query}' in {get_database_display_name(db)}: {e}")
|
|
155
|
+
return GeneSearchResult(query=query, database=db, error=str(e), is_access_error=False)
|
|
156
|
+
|
|
157
|
+
async def search_multiple_databases(
|
|
158
|
+
self, query: str, databases: Optional[list[DatabaseType]] = None, include_sequence: bool = True
|
|
159
|
+
) -> list[GeneSearchResult]:
|
|
160
|
+
"""
|
|
161
|
+
Search across multiple databases.
|
|
162
|
+
|
|
163
|
+
Args:
|
|
164
|
+
query: Gene ID, gene name, or transcript ID
|
|
165
|
+
databases: List of databases to search
|
|
166
|
+
include_sequence: Whether to fetch sequences
|
|
167
|
+
|
|
168
|
+
Returns:
|
|
169
|
+
List of search results from each database
|
|
170
|
+
"""
|
|
171
|
+
if databases is None:
|
|
172
|
+
databases = list(DatabaseType)
|
|
173
|
+
|
|
174
|
+
tasks = [self.search_gene(query, db, include_sequence) for db in databases]
|
|
175
|
+
|
|
176
|
+
results = await asyncio.gather(*tasks, return_exceptions=True)
|
|
177
|
+
|
|
178
|
+
# Convert exceptions to error results
|
|
179
|
+
processed_results = []
|
|
180
|
+
for i, result in enumerate(results):
|
|
181
|
+
if isinstance(result, Exception):
|
|
182
|
+
processed_results.append(GeneSearchResult(query=query, database=databases[i], error=str(result)))
|
|
183
|
+
elif isinstance(result, GeneSearchResult):
|
|
184
|
+
processed_results.append(result)
|
|
185
|
+
else:
|
|
186
|
+
# Handle unexpected result type
|
|
187
|
+
processed_results.append(
|
|
188
|
+
GeneSearchResult(query=query, database=databases[i], error="Unexpected result type")
|
|
189
|
+
)
|
|
190
|
+
|
|
191
|
+
return processed_results
|
|
192
|
+
|
|
193
|
+
def save_transcripts_fasta(
|
|
194
|
+
self, transcripts: list[TranscriptInfo], output_path: Union[str, Path], include_metadata: bool = True
|
|
195
|
+
) -> None:
|
|
196
|
+
"""
|
|
197
|
+
Save transcripts to FASTA format using shared utility.
|
|
198
|
+
|
|
199
|
+
Args:
|
|
200
|
+
transcripts: List of transcript information
|
|
201
|
+
output_path: Output file path
|
|
202
|
+
include_metadata: Include metadata in FASTA headers
|
|
203
|
+
"""
|
|
204
|
+
# Convert transcripts to (header, sequence) tuples
|
|
205
|
+
fasta_sequences = []
|
|
206
|
+
|
|
207
|
+
for transcript in transcripts:
|
|
208
|
+
if not transcript.sequence:
|
|
209
|
+
logger.warning(f"No sequence for transcript {transcript.transcript_id}")
|
|
210
|
+
continue
|
|
211
|
+
|
|
212
|
+
# Create FASTA header
|
|
213
|
+
header = transcript.transcript_id
|
|
214
|
+
if include_metadata:
|
|
215
|
+
metadata = []
|
|
216
|
+
if transcript.gene_name:
|
|
217
|
+
metadata.append(f"gene_name:{transcript.gene_name}")
|
|
218
|
+
if transcript.transcript_type:
|
|
219
|
+
metadata.append(f"type:{transcript.transcript_type}")
|
|
220
|
+
if transcript.is_canonical:
|
|
221
|
+
metadata.append("canonical:true")
|
|
222
|
+
if transcript.length:
|
|
223
|
+
metadata.append(f"length:{transcript.length}")
|
|
224
|
+
|
|
225
|
+
if metadata:
|
|
226
|
+
header += " " + " ".join(metadata)
|
|
227
|
+
|
|
228
|
+
fasta_sequences.append((header, transcript.sequence))
|
|
229
|
+
|
|
230
|
+
# Use shared FastaUtils to save
|
|
231
|
+
FastaUtils.save_sequences_fasta(fasta_sequences, output_path)
|
|
232
|
+
|
|
233
|
+
|
|
234
|
+
# Convenience functions for synchronous usage
|
|
235
|
+
def search_gene_sync(
|
|
236
|
+
query: str, database: DatabaseType = DatabaseType.ENSEMBL, include_sequence: bool = True
|
|
237
|
+
) -> GeneSearchResult:
|
|
238
|
+
"""Synchronous wrapper for gene search."""
|
|
239
|
+
searcher = GeneSearcher()
|
|
240
|
+
return asyncio.run(searcher.search_gene(query, database, include_sequence))
|
|
241
|
+
|
|
242
|
+
|
|
243
|
+
def search_gene_with_fallback_sync(query: str, include_sequence: bool = True) -> GeneSearchResult:
|
|
244
|
+
"""Synchronous wrapper for gene search with fallback."""
|
|
245
|
+
searcher = GeneSearcher()
|
|
246
|
+
return asyncio.run(searcher.search_gene_with_fallback(query, include_sequence))
|
|
247
|
+
|
|
248
|
+
|
|
249
|
+
def search_multiple_databases_sync(
|
|
250
|
+
query: str, databases: Optional[list[DatabaseType]] = None, include_sequence: bool = True
|
|
251
|
+
) -> list[GeneSearchResult]:
|
|
252
|
+
"""Synchronous wrapper for multi-database search."""
|
|
253
|
+
searcher = GeneSearcher()
|
|
254
|
+
return asyncio.run(searcher.search_multiple_databases(query, databases, include_sequence))
|