File size: 9,623 Bytes
7a92197 |
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 |
"""
BLAST Integration
Sequence alignment and homology searching
"""
from pathlib import Path
from typing import Dict, List, Optional
import subprocess
import yaml
import logging
from Bio import SeqIO
from Bio.Blast import NCBIXML
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)
class BLASTRunner:
"""Run BLAST searches for sequence alignment"""
def __init__(self, config_path: str = "config.yml"):
with open(config_path, 'r') as f:
self.config = yaml.safe_load(f)['pipeline']['blast']
self.database = self.config.get('database', 'nt')
self.evalue = self.config.get('evalue', 0.001)
self.num_threads = self.config.get('num_threads', 4)
self.output_dir = Path(self.config['output_dir'])
self.output_dir.mkdir(parents=True, exist_ok=True)
def run_blastn(
self,
query_file: Path,
output_file: Optional[Path] = None,
max_targets: int = 10
) -> Optional[Path]:
"""
Run BLASTN for nucleotide sequences
Args:
query_file: Input FASTA file with query sequences
output_file: Output XML file
max_targets: Maximum number of target sequences
Returns:
Path to output file or None if failed
"""
if output_file is None:
output_file = self.output_dir / f"{query_file.stem}_blastn.xml"
cmd = [
'blastn',
'-query', str(query_file),
'-db', self.database,
'-out', str(output_file),
'-evalue', str(self.evalue),
'-num_threads', str(self.num_threads),
'-max_target_seqs', str(max_targets),
'-outfmt', '5' # XML format
]
try:
logger.info(f"Running BLASTN on {query_file.name}")
result = subprocess.run(cmd, capture_output=True, text=True, check=True)
logger.info(f"BLASTN completed: {output_file}")
return output_file
except subprocess.CalledProcessError as e:
logger.error(f"BLASTN failed: {e.stderr}")
return None
except FileNotFoundError:
logger.warning("BLASTN not found - creating simulated results")
return self._simulate_blast_results(query_file, output_file)
def run_blastp(
self,
query_file: Path,
output_file: Optional[Path] = None,
max_targets: int = 10
) -> Optional[Path]:
"""
Run BLASTP for protein sequences
Args:
query_file: Input FASTA file with protein sequences
output_file: Output XML file
max_targets: Maximum number of target sequences
"""
if output_file is None:
output_file = self.output_dir / f"{query_file.stem}_blastp.xml"
cmd = [
'blastp',
'-query', str(query_file),
'-db', 'nr', # Non-redundant protein database
'-out', str(output_file),
'-evalue', str(self.evalue),
'-num_threads', str(self.num_threads),
'-max_target_seqs', str(max_targets),
'-outfmt', '5'
]
try:
logger.info(f"Running BLASTP on {query_file.name}")
subprocess.run(cmd, capture_output=True, text=True, check=True)
logger.info(f"BLASTP completed: {output_file}")
return output_file
except subprocess.CalledProcessError as e:
logger.error(f"BLASTP failed: {e.stderr}")
return None
except FileNotFoundError:
logger.warning("BLASTP not found - creating simulated results")
return self._simulate_blast_results(query_file, output_file)
def _simulate_blast_results(self, query_file: Path, output_file: Path) -> Path:
"""Create simulated BLAST results for demo purposes"""
with open(output_file, 'w') as f:
f.write("""<?xml version="1.0"?>
<!DOCTYPE BlastOutput PUBLIC "-//NCBI//NCBI BlastOutput/EN" "http://www.ncbi.nlm.nih.gov/dtd/NCBI_BlastOutput.dtd">
<BlastOutput>
<BlastOutput_program>blastn</BlastOutput_program>
<BlastOutput_version>BLASTN 2.14.0+</BlastOutput_version>
<BlastOutput_reference>Simulated results for demo</BlastOutput_reference>
<BlastOutput_db>nt</BlastOutput_db>
<BlastOutput_query-ID>Query_1</BlastOutput_query-ID>
<BlastOutput_query-def>Sample sequence</BlastOutput_query-def>
<BlastOutput_query-len>100</BlastOutput_query-len>
<BlastOutput_iterations>
<Iteration>
<Iteration_iter-num>1</Iteration_iter-num>
<Iteration_query-ID>Query_1</Iteration_query-ID>
<Iteration_query-def>Sample sequence</Iteration_query-def>
<Iteration_query-len>100</Iteration_query-len>
<Iteration_hits>
</Iteration_hits>
</Iteration>
</BlastOutput_iterations>
</BlastOutput>
""")
return output_file
def parse_results(self, blast_output: Path) -> List[Dict]:
"""
Parse BLAST XML output
Returns:
List of hit dictionaries
"""
hits = []
try:
with open(blast_output, 'r') as f:
blast_records = NCBIXML.parse(f)
for blast_record in blast_records:
for alignment in blast_record.alignments:
for hsp in alignment.hsps:
hit = {
'query': blast_record.query,
'hit_id': alignment.hit_id,
'hit_def': alignment.hit_def,
'length': alignment.length,
'e_value': hsp.expect,
'score': hsp.score,
'identities': hsp.identities,
'positives': hsp.positives,
'gaps': hsp.gaps,
'query_start': hsp.query_start,
'query_end': hsp.query_end,
'hit_start': hsp.sbjct_start,
'hit_end': hsp.sbjct_end,
'alignment_length': hsp.align_length
}
hits.append(hit)
logger.info(f"Parsed {len(hits)} BLAST hits")
return hits
except Exception as e:
logger.error(f"Error parsing BLAST results: {e}")
return []
def filter_hits(
self,
hits: List[Dict],
min_identity: float = 0.9,
max_evalue: float = 0.001
) -> List[Dict]:
"""
Filter BLAST hits by identity and e-value
Args:
hits: List of hit dictionaries
min_identity: Minimum identity percentage (0-1)
max_evalue: Maximum e-value threshold
"""
filtered = []
for hit in hits:
identity_pct = hit['identities'] / hit['alignment_length']
if identity_pct >= min_identity and hit['e_value'] <= max_evalue:
hit['identity_pct'] = identity_pct
filtered.append(hit)
logger.info(f"Filtered to {len(filtered)} high-quality hits")
return filtered
class SequenceAligner:
"""Sequence alignment utilities"""
def __init__(self):
self.blast_runner = BLASTRunner()
def align_to_reference(
self,
query_sequences: Path,
reference_db: str = 'nt'
) -> Dict:
"""
Align query sequences to reference database
Returns:
Alignment results and statistics
"""
# Run BLAST
blast_output = self.blast_runner.run_blastn(query_sequences)
if blast_output is None:
return {'error': 'BLAST search failed'}
# Parse results
hits = self.blast_runner.parse_results(blast_output)
# Calculate statistics
stats = {
'total_queries': 0,
'queries_with_hits': 0,
'total_hits': len(hits),
'avg_identity': 0,
'avg_evalue': 0
}
if hits:
stats['avg_identity'] = sum(h.get('identity_pct', 0) for h in hits) / len(hits)
stats['avg_evalue'] = sum(h['e_value'] for h in hits) / len(hits)
return {
'statistics': stats,
'hits': hits,
'output_file': str(blast_output)
}
def find_homologs(
self,
sequence_file: Path,
min_identity: float = 0.8
) -> List[Dict]:
"""
Find homologous sequences
Args:
sequence_file: Input FASTA file
min_identity: Minimum identity threshold
"""
blast_output = self.blast_runner.run_blastn(sequence_file)
if blast_output:
hits = self.blast_runner.parse_results(blast_output)
return self.blast_runner.filter_hits(hits, min_identity=min_identity)
return []
|