-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdata_processor.py
More file actions
270 lines (227 loc) · 9.11 KB
/
Copy pathdata_processor.py
File metadata and controls
270 lines (227 loc) · 9.11 KB
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
"""Data processing with batch operations and parallel execution."""
import concurrent.futures
from pathlib import Path
from dataclasses import asdict
from typing import Any, Dict, List, Optional
import pandas as pd
from tqdm import tqdm
from binding_data_processor import BindingDataProcessor
from chemical_properties import ChemicalProperties
from web_enrichment import WebEnrichment
from api_client import PubChemClient, PubMedClient
from cache_manager import CacheManager
from config import BATCH_SIZE, DATA_SOURCES, IDENTIFIER_FIELDS, MAX_WORKERS
from logger import LogManager
from models import CompoundData, ValidationError
class DataProcessor:
"""Handles batch processing and parallel execution of data collection."""
def __init__(self):
"""Initialize data processor."""
self.logger = LogManager().get_logger("data_processor")
self.cache = CacheManager()
self.pubchem = PubChemClient()
self.pubmed = PubMedClient()
# Initialize specialized processors
self.binding_processor = BindingDataProcessor(self.pubmed)
self.chemical_properties = ChemicalProperties()
self.web_enrichment = WebEnrichment()
def validate_compound(self, compound: CompoundData) -> List[str]:
"""
Validate compound data.
Args:
compound: Compound data to validate
Returns:
List of validation error messages (empty if valid)
"""
errors = []
# Check that at least one identifier is present
has_identifier = False
for field in IDENTIFIER_FIELDS:
value = getattr(compound, field)
if value is not None and value != "N/A" and value != "":
has_identifier = True
break
if not has_identifier:
errors.append("At least one identifier (CAS, name, or SMILES) is required")
# Validate structure if SMILES present
if compound.smiles != "N/A":
is_valid, error = self.chemical_properties.validate_structure(compound.smiles)
if not is_valid:
errors.append(f"Invalid structure: {error}")
return errors
def process_batch(
self,
compounds: List[CompoundData],
sources: Optional[List[str]] = None
) -> List[CompoundData]:
"""
Process a batch of compounds.
Args:
compounds: List of compounds to process
sources: Optional list of data sources to use
Returns:
List of processed compounds
"""
if sources is None:
sources = [
source for source, config in DATA_SOURCES.items()
if config['enabled']
]
processed = []
errors = []
# Process compounds in parallel
with concurrent.futures.ThreadPoolExecutor(max_workers=MAX_WORKERS) as executor:
future_to_compound = {
executor.submit(
self._process_single_compound, compound, sources
): compound
for compound in compounds
}
for future in concurrent.futures.as_completed(future_to_compound):
compound = future_to_compound[future]
try:
result = future.result()
if result:
processed.append(result)
except Exception as e:
self.logger.error(
f"Error processing compound {compound.name}: {str(e)}"
)
errors.append((compound, str(e)))
# Log errors summary
if errors:
self.logger.warning(
f"Failed to process {len(errors)} compounds:"
f"\n" + "\n".join(
f"- {c.name}: {e}" for c, e in errors
)
)
return processed
def _process_single_compound(
self,
compound: CompoundData,
sources: List[str]
) -> Optional[CompoundData]:
"""
Process a single compound using specified data sources.
Args:
compound: Compound to process
sources: List of data sources to use
Returns:
Processed compound or None if processing failed
"""
try:
# Validate input
errors = self.validate_compound(compound)
if errors:
raise ValidationError(
f"Validation failed for {compound.name}: {', '.join(errors)}"
)
# Calculate chemical properties if SMILES available
if compound.smiles != "N/A":
props = self.chemical_properties.calculate_properties(compound.smiles)
if props:
for key, value in props.items():
setattr(compound, key, value)
# Rank common names by search results
common_names = self.web_enrichment.get_common_names(compound.name)
for i, name_data in enumerate(common_names, 1):
setattr(compound, f'common_name_{i}', name_data['name'])
setattr(compound, f'common_name_{i}_source', name_data['source'])
setattr(compound, f'common_name_{i}_relevance', name_data['relevance'])
# Sort and enrich binding data
self.binding_processor.sort_binding_data(compound)
# Get legal status
legal_status = self.web_enrichment.get_legal_status(compound.name)
if legal_status:
compound.scheduling.update(
{s['jurisdiction']: s['schedule'] for s in legal_status['scheduling']}
)
compound.data_sources.update(legal_status['sources'])
# Get pharmacology
pharm_info = self.web_enrichment.get_pharmacology(compound.name)
if pharm_info:
if pharm_info['mechanism_of_action']:
compound.mechanism_of_action = '; '.join(pharm_info['mechanism_of_action'])
if pharm_info['toxicity']:
compound.toxicity = '; '.join(pharm_info['toxicity'])
compound.data_sources.update(pharm_info['sources'])
# Get reference URLs
urls = self.web_enrichment.get_reference_urls(compound.name)
for key, value in urls.items():
setattr(compound, key, value)
return compound
except Exception as e:
self.logger.error(
f"Error processing compound {compound.name}: {str(e)}"
)
return None
def process_file(
self,
input_file: str,
output_file: str,
sources: Optional[List[str]] = None
) -> Dict[str, Any]:
"""
Process compounds from input file.
Args:
input_file: Input TSV/CSV file path
output_file: Output TSV/CSV file path
sources: Optional list of data sources to use
Returns:
Processing statistics
"""
# Determine file format from extension
input_ext = Path(input_file).suffix.lower()
output_ext = Path(output_file).suffix.lower()
# Read input file
if input_ext == '.tsv':
df = pd.read_csv(input_file, sep='\t')
else:
df = pd.read_csv(input_file)
compounds = [
CompoundData(**row)
for _, row in df.iterrows()
]
# Process in batches
processed = []
for i in tqdm(
range(0, len(compounds), BATCH_SIZE),
desc="Processing compounds"
):
batch = compounds[i:i + BATCH_SIZE]
processed.extend(self.process_batch(batch, sources))
# Save results
result_df = pd.DataFrame([
asdict(compound) for compound in processed
])
# Save in appropriate format
if output_ext == '.tsv':
result_df.to_csv(output_file, sep='\t', index=False)
else:
result_df.to_csv(output_file, index=False)
# Calculate statistics
stats = {
'total_compounds': len(compounds),
'processed_compounds': len(processed),
'success_rate': len(processed) / len(compounds) * 100,
'sources_used': set().union(*(
c.data_sources for c in processed
)),
'missing_identifiers': sum(
1 for c in processed
if any(
getattr(c, f) == "N/A"
for f in IDENTIFIER_FIELDS
)
)
}
self.logger.info(
f"Processing completed:\n"
f"- Total compounds: {stats['total_compounds']}\n"
f"- Successfully processed: {stats['processed_compounds']}\n"
f"- Success rate: {stats['success_rate']:.1f}%\n"
f"- Sources used: {', '.join(stats['sources_used'])}\n"
f"- Missing identifiers: {stats['missing_identifiers']}"
)
return stats