File size: 5,961 Bytes
907b200
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
<?php

namespace App\Http\Controllers;

use Illuminate\Routing\Controller;
use App\Jobs\RunDockingJob;
use App\Models\DockingJob;
use App\Traits\ApiResponseTrait;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Process;
use Illuminate\Support\Facades\Validator;
use Illuminate\Support\Str;

class DockingTestController extends Controller
{
    use ApiResponseTrait;

    public function testDockingSubmit(Request $request)
    {
        $validator = Validator::make($request->all(), [
            'protein_name' => 'required|string|max:255',
            'ligand_name'  => 'nullable|string|max:255',
            'ligand_file'  => 'nullable|file|required_without:ligand_smiles',
            'ligand_smiles' => 'nullable|string|max:2000|required_without:ligand_file',
            'protein_file' => 'required|file',
            'center_x' => 'required|numeric',
            'center_y' => 'required|numeric',
            'center_z' => 'required|numeric',
            'box_size_x' => 'required|numeric',
            'box_size_y' => 'required|numeric',
            'box_size_z' => 'required|numeric',
            'exhaustiveness' => 'nullable|integer',
            'n_poses' => 'nullable|integer',
        ]);

        $validator->after(function ($validator) use ($request) {
            if ($request->hasFile('ligand_file') && $request->filled('ligand_smiles')) {
                $validator->errors()->add(
                    'ligand_input',
                    'Provide either a ligand file or a SMILES string, not both.'
                );
            }
        });

        if ($validator->fails()) {
            return $this->errorResponse('Validation Error', 422, $validator->errors());
        }

        $isSmiles = $request->filled('ligand_smiles');

        // ---- Handle Ligand ----
        if ($isSmiles) {
            // SMILES → PDBQT conversion (synchronous, fast ~1-3s)
            $conversion = $this->convertSmilesToPdbqt($request->ligand_smiles);

            if ($conversion['status'] === 'error') {
                return $this->errorResponse(
                    'SMILES conversion failed: ' . $conversion['message'],
                    422
                );
            }

            $ligandPath = $conversion['output_file'];
        } else {
            $ligandFilename = Str::random(40) . '.pdbqt';
            $ligandPath = storage_path('app/private/' . $request->file('ligand_file')->storeAs('docking', $ligandFilename));
        }

        // ---- Handle Protein ----
        $proteinFilename = Str::random(40) . '.pdbqt';
        $proteinPath = storage_path('app/private/' . $request->file('protein_file')->storeAs('docking', $proteinFilename));

        // ---- Create Database Record ----
        $job = DockingJob::create([
            'user_id'      => $request->user()->id,
            'input_type'   => $isSmiles ? 'smiles' : 'file',
            'smiles'       => $isSmiles ? $request->ligand_smiles : null,
            'protein_name' => $request->protein_name,
            'ligand_name'  => $request->ligand_name,
            'protein_path' => $proteinPath,
            'ligand_path'  => $ligandPath,
            'status'       => 'pending',
        ]);

        // ---- Dispatch background docking job ----
        RunDockingJob::dispatch($job, [
            'center_x' => $request->center_x,
            'center_y' => $request->center_y,
            'center_z' => $request->center_z,
            'box_size_x' => $request->box_size_x,
            'box_size_y' => $request->box_size_y,
            'box_size_z' => $request->box_size_z,
            'exhaustiveness' => $request->exhaustiveness ?? 8,
            'n_poses' => $request->n_poses ?? 5,
        ]);

        return $this->successResponse('Docking Job Successfully Queued', [
            'job_id' => $job->id,
            'status' => $job->status,
        ]);
    }

    private function convertSmilesToPdbqt(string $smiles): array
    {
        $pythonPath = env('DOCKING_PYTHON_PATH', base_path('vina_env/bin/python'));
        $scriptPath = env('SMILES_SCRIPT_PATH', base_path('scripts/smiles_to_pdbqt.py'));

        // Generate unique output path
        $outputFilename = Str::random(40) . '.pdbqt';
        $outputDir = storage_path('app/private/docking/generated');
        $outputPath = $outputDir . '/' . $outputFilename;

        // Ensure output directory exists
        if (! is_dir($outputDir)) {
            mkdir($outputDir, 0755, true);
        }

        $cmd = sprintf(
            '%s %s %s %s',
            escapeshellarg($pythonPath),
            escapeshellarg($scriptPath),
            escapeshellarg($smiles),
            escapeshellarg($outputPath)
        );

        try {
            $result = Process::timeout(120)->run($cmd);

            $outputData = null;
            $fullOutput = $result->output();

            if (preg_match('/\{"status":\s*"(success|error)".*\}/s', $fullOutput, $matches)) {
                $outputData = json_decode($matches[0], true);
            }

            if (! $result->successful()) {
                $errorMsg = $outputData['message'] ?? $result->errorOutput() ?: 'Unknown conversion error';
                Log::error('SMILES conversion failed', ['smiles' => $smiles, 'error' => $errorMsg]);
                return ['status' => 'error', 'message' => $errorMsg];
            }

            if ($outputData && $outputData['status'] === 'success') {
                return $outputData;
            }

            if ($outputData && $outputData['status'] === 'error') {
                return $outputData;
            }

            return ['status' => 'error', 'message' => 'Unexpected output from conversion script'];

        } catch (\Exception $e) {
            Log::error('SMILES conversion exception', ['smiles' => $smiles, 'error' => $e->getMessage()]);
            return ['status' => 'error', 'message' => $e->getMessage()];
        }
    }
}