AILIXIR Bot commited on
Commit
e4cbc93
·
1 Parent(s): aa77d38

Auto-sync: 20f162778391e038b0e34e79dd850d3018583411

Browse files
API.md CHANGED
@@ -1401,7 +1401,7 @@ curl -X POST "{base_url}/api/docking/submit" \\
1401
  - Path parameter:
1402
  - `id` (integer)
1403
 
1404
- #### Success response (pending or completed)
1405
 
1406
  ```json
1407
  {
@@ -1417,12 +1417,30 @@ curl -X POST "{base_url}/api/docking/submit" \\
1417
  {"affinity": 0, "inter": 0, "intra": -2.031, "torsions": 0, "unbound": -2.031},
1418
  {"affinity": 0, "inter": 0, "intra": -2.031, "torsions": 0, "unbound": -2.031},
1419
  {"affinity": 0.001, "inter": 0, "intra": -2.031, "torsions": 0, "unbound": -2.031}
1420
- ]
 
1421
  }
1422
  ```
1423
 
1424
  If the job is not completed, `scores` will be an empty array.
1425
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1426
  ---
1427
 
1428
  ### GET `/api/docking/history`
@@ -1449,7 +1467,8 @@ If the job is not completed, `scores` will be an empty array.
1449
  {"affinity": 0, "inter": 0, "intra": -2.031, "torsions": 0, "unbound": -2.031},
1450
  {"affinity": 0, "inter": 0, "intra": -2.031, "torsions": 0, "unbound": -2.031},
1451
  {"affinity": 0.001, "inter": 0, "intra": -2.031, "torsions": 0, "unbound": -2.031}
1452
- ]
 
1453
  }
1454
  ],
1455
  "pagination": {
 
1401
  - Path parameter:
1402
  - `id` (integer)
1403
 
1404
+ #### Response (completed)
1405
 
1406
  ```json
1407
  {
 
1417
  {"affinity": 0, "inter": 0, "intra": -2.031, "torsions": 0, "unbound": -2.031},
1418
  {"affinity": 0, "inter": 0, "intra": -2.031, "torsions": 0, "unbound": -2.031},
1419
  {"affinity": 0.001, "inter": 0, "intra": -2.031, "torsions": 0, "unbound": -2.031}
1420
+ ],
1421
+ "error": null
1422
  }
1423
  ```
1424
 
1425
  If the job is not completed, `scores` will be an empty array.
1426
 
1427
+ #### Response (failed)
1428
+
1429
+ ```json
1430
+ {
1431
+ "success": true,
1432
+ "message": "Job details retrieved successfully",
1433
+ "id": 7,
1434
+ "status": "failed",
1435
+ "protein": "EGFR",
1436
+ "ligand": "Erlotinib",
1437
+ "created_at": "2026-06-17T05:45:31+00:00",
1438
+ "download_url": "{base_url}/api/docking/download/7",
1439
+ "scores": [],
1440
+ "error": "\n\nPDBQT parsing error: Unknown or inappropriate tag found in flex residue or ligand.\n > ATOM 1 N UNL 1 8.304 191.693 26.328 0.00 0.00 +0.000 N \n"
1441
+ }
1442
+ ```
1443
+
1444
  ---
1445
 
1446
  ### GET `/api/docking/history`
 
1467
  {"affinity": 0, "inter": 0, "intra": -2.031, "torsions": 0, "unbound": -2.031},
1468
  {"affinity": 0, "inter": 0, "intra": -2.031, "torsions": 0, "unbound": -2.031},
1469
  {"affinity": 0.001, "inter": 0, "intra": -2.031, "torsions": 0, "unbound": -2.031}
1470
+ ],
1471
+ "error": null
1472
  }
1473
  ],
1474
  "pagination": {
app/Http/Controllers/Api/DockingController.php CHANGED
@@ -26,10 +26,10 @@ class DockingController
26
  if ($isSmiles) {
27
  $ligandPath = null;
28
  } else {
29
- $ligandPath = $this->storeAsPdbqt($request->file('ligand_file'));
30
  }
31
 
32
- $proteinPath = $this->storeAsPdbqt($request->file('protein_file'));
33
 
34
  $job = DockingJob::create([
35
  'user_id' => $request->user()->id,
@@ -83,10 +83,13 @@ class DockingController
83
  'created_at' => $job->created_at->toIso8601String(),
84
  'download_url' => url('/api/docking/download/'.$job->id),
85
  'scores' => $job->vina_scores ?? [],
 
86
  ];
87
  });
88
 
89
  return response()->json([
 
 
90
  'results' => $items,
91
  'pagination' => [
92
  'current_page' => $paginator->currentPage(),
@@ -117,6 +120,7 @@ class DockingController
117
  'created_at' => $job->created_at->toIso8601String(),
118
  'download_url' => url('/api/docking/download/'.$job->id),
119
  'scores' => $job->vina_scores ?? [],
 
120
  ];
121
 
122
  return response()->json(array_merge(
@@ -129,12 +133,12 @@ class DockingController
129
  {
130
  $token = $request->bearerToken() ?? $request->query('token');
131
 
132
- if (!$token) {
133
  return $this->errorResponse('Unauthenticated', 401);
134
  }
135
 
136
  $accessToken = PersonalAccessToken::findToken($token);
137
- if (!$accessToken || !$accessToken->tokenable) {
138
  return $this->errorResponse('Invalid token', 401);
139
  }
140
 
@@ -166,7 +170,7 @@ class DockingController
166
  ]);
167
  }
168
 
169
- private function storeAsPdbqt(UploadedFile $file): string
170
  {
171
  $ext = strtolower($file->getClientOriginalExtension());
172
  $filename = Str::random(40).'.pdbqt';
@@ -175,7 +179,7 @@ class DockingController
175
  if ($ext === 'pdbqt') {
176
  $file->storeAs('docking', $filename);
177
  } elseif ($ext === 'pdb') {
178
- $this->convertPdbToPdbqt($file->getRealPath(), $destPath);
179
  } else {
180
  abort(422, 'Unsupported file format: .'.$ext.'. Only .pdb and .pdbqt files are accepted.');
181
  }
@@ -183,7 +187,7 @@ class DockingController
183
  return $destPath;
184
  }
185
 
186
- private function convertPdbToPdbqt(string $source, string $dest): void
187
  {
188
  $pythonPath = env('DOCKING_PYTHON_PATH');
189
  $obabel = $pythonPath ? dirname($pythonPath).'/obabel' : 'obabel';
@@ -201,10 +205,22 @@ class DockingController
201
  if (! $result->successful()) {
202
  Log::error('PDB-to-PDBQT conversion failed', [
203
  'source' => $source,
204
- 'dest' => $dest,
205
- 'error' => $result->errorOutput(),
206
  ]);
207
  abort(500, 'Failed to convert PDB file to PDBQT format.');
208
  }
 
 
 
 
 
 
 
 
 
 
 
 
209
  }
210
  }
 
26
  if ($isSmiles) {
27
  $ligandPath = null;
28
  } else {
29
+ $ligandPath = $this->storeAsPdbqt($request->file('ligand_file'), true);
30
  }
31
 
32
+ $proteinPath = $this->storeAsPdbqt($request->file('protein_file'), false);
33
 
34
  $job = DockingJob::create([
35
  'user_id' => $request->user()->id,
 
83
  'created_at' => $job->created_at->toIso8601String(),
84
  'download_url' => url('/api/docking/download/'.$job->id),
85
  'scores' => $job->vina_scores ?? [],
86
+ 'error' => $job->status === 'failed' ? ($job->result_data['error'] ?? null) : null,
87
  ];
88
  });
89
 
90
  return response()->json([
91
+ 'success' => true,
92
+ 'message' => 'Docking history retrieved successfully',
93
  'results' => $items,
94
  'pagination' => [
95
  'current_page' => $paginator->currentPage(),
 
120
  'created_at' => $job->created_at->toIso8601String(),
121
  'download_url' => url('/api/docking/download/'.$job->id),
122
  'scores' => $job->vina_scores ?? [],
123
+ 'error' => $job->status === 'failed' ? ($job->result_data['error'] ?? null) : null,
124
  ];
125
 
126
  return response()->json(array_merge(
 
133
  {
134
  $token = $request->bearerToken() ?? $request->query('token');
135
 
136
+ if (! $token) {
137
  return $this->errorResponse('Unauthenticated', 401);
138
  }
139
 
140
  $accessToken = PersonalAccessToken::findToken($token);
141
+ if (! $accessToken || ! $accessToken->tokenable) {
142
  return $this->errorResponse('Invalid token', 401);
143
  }
144
 
 
170
  ]);
171
  }
172
 
173
+ private function storeAsPdbqt(UploadedFile $file, bool $isLigand): string
174
  {
175
  $ext = strtolower($file->getClientOriginalExtension());
176
  $filename = Str::random(40).'.pdbqt';
 
179
  if ($ext === 'pdbqt') {
180
  $file->storeAs('docking', $filename);
181
  } elseif ($ext === 'pdb') {
182
+ $this->convertPdbToPdbqt($file->getRealPath(), $destPath, $isLigand);
183
  } else {
184
  abort(422, 'Unsupported file format: .'.$ext.'. Only .pdb and .pdbqt files are accepted.');
185
  }
 
187
  return $destPath;
188
  }
189
 
190
+ private function convertPdbToPdbqt(string $source, string $dest, bool $isLigand): void
191
  {
192
  $pythonPath = env('DOCKING_PYTHON_PATH');
193
  $obabel = $pythonPath ? dirname($pythonPath).'/obabel' : 'obabel';
 
205
  if (! $result->successful()) {
206
  Log::error('PDB-to-PDBQT conversion failed', [
207
  'source' => $source,
208
+ 'dest' => $dest,
209
+ 'error' => $result->errorOutput(),
210
  ]);
211
  abort(500, 'Failed to convert PDB file to PDBQT format.');
212
  }
213
+
214
+ $content = file_get_contents($dest);
215
+ $lines = explode("\n", $content);
216
+
217
+ if ($isLigand) {
218
+ $lines = array_map(fn ($line) => str_starts_with($line, 'ATOM') ? 'HETATM'.substr($line, 6) : $line, $lines);
219
+ } else {
220
+ $lines = array_filter($lines, fn ($line) => preg_match('/^(ATOM|HETATM)/', $line));
221
+ $lines = array_values($lines);
222
+ }
223
+
224
+ file_put_contents($dest, implode("\n", $lines));
225
  }
226
  }
app/Jobs/RunDockingJob.php CHANGED
@@ -85,22 +85,22 @@ class RunDockingJob implements ShouldQueue
85
  if (empty(trim($errorMessage))) {
86
  $errorMessage = trim($fullOutput) ?: 'Unknown Error / Empty Output';
87
  }
88
- throw new \Exception('Process Failed: '.$errorMessage);
89
  }
90
 
91
  if (isset($outputData['status']) && $outputData['status'] === 'error') {
92
- throw new \Exception('Python Error: '.($outputData['message'] ?? 'Unknown script error'));
93
  }
94
 
95
  $vinaScores = [];
96
  if ($outputData && isset($outputData['energies'])) {
97
  foreach ($outputData['energies'] as $pose) {
98
  $vinaScores[] = [
99
- 'affinity' => (float) ($pose[0] ?? 0.0),
100
- 'inter' => (float) ($pose[1] ?? 0.0),
101
- 'intra' => (float) ($pose[2] ?? 0.0),
102
  'torsions' => (float) ($pose[3] ?? 0.0),
103
- 'unbound' => (float) ($pose[4] ?? 0.0),
104
  ];
105
  }
106
  unset($outputData['energies']);
 
85
  if (empty(trim($errorMessage))) {
86
  $errorMessage = trim($fullOutput) ?: 'Unknown Error / Empty Output';
87
  }
88
+ throw new \Exception(trim($errorMessage));
89
  }
90
 
91
  if (isset($outputData['status']) && $outputData['status'] === 'error') {
92
+ throw new \Exception($outputData['message'] ?? 'Unknown script error');
93
  }
94
 
95
  $vinaScores = [];
96
  if ($outputData && isset($outputData['energies'])) {
97
  foreach ($outputData['energies'] as $pose) {
98
  $vinaScores[] = [
99
+ 'affinity' => (float) ($pose[0] ?? 0.0),
100
+ 'inter' => (float) ($pose[1] ?? 0.0),
101
+ 'intra' => (float) ($pose[2] ?? 0.0),
102
  'torsions' => (float) ($pose[3] ?? 0.0),
103
+ 'unbound' => (float) ($pose[4] ?? 0.0),
104
  ];
105
  }
106
  unset($outputData['energies']);