Skip to content

Commit

Permalink
feat: various bug fixes, ui improvements, updated commands, php 8.2 m…
Browse files Browse the repository at this point in the history
…igration and rdkit cartridge based postgresql
  • Loading branch information
CS76 committed Nov 9, 2023
1 parent 3c0639b commit 2eed0ed
Show file tree
Hide file tree
Showing 67 changed files with 1,160 additions and 653 deletions.
3 changes: 3 additions & 0 deletions app/Console/Commands/ArchiveData.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace App\Console\Commands;

use App\Jobs\ArchiveProject;
use App\Jobs\ArchiveStudy;
use App\Models\Project;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\DB;
Expand Down Expand Up @@ -35,7 +36,9 @@ public function handle()
])->get();

foreach ($projects as $project) {
// echo($project->identifier);
ArchiveProject::dispatch($project);
ArchiveStudy::dispatch($project);
}
});
}
Expand Down
164 changes: 164 additions & 0 deletions app/Console/Commands/ExtractSpectra.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,164 @@
<?php

namespace App\Console\Commands;

use App\Models\NMRium;
use App\Models\Project;
use Exception;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Http;

class ExtractSpectra extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'nmrxiv:nmrium-import';

/**
* The console command description.
*
* @var string
*/
protected $description = 'extract nmrium json from spectra';

/**
* Execute the console command.
*/
public function handle()
{
$projects = Project::where([
['is_public', true],
])->get();

foreach ($projects as $project) {
echo $project->identifier;
echo "\r\n";
$studies = $project->studies;
foreach ($studies as $study) {
echo $study->identifier;
echo "\r\n";
try {
if (! $study->has_nmrium) {
DB::transaction(function () use ($study) {
$download_url = $study->download_url;
if ($download_url) {
$nmrium_ = $this->processSpectra($download_url);
$parsedSpectra = $nmrium_['data'];
foreach ($parsedSpectra['spectra'] as $spectra) {
unset($spectra['data']);
unset($spectra['meta']);
unset($spectra['originalData']);
unset($spectra['originalInfo']);
}

$version = $parsedSpectra['version'];
unset($parsedSpectra['version']);

$nmriumJSON = [
'data' => $parsedSpectra,
'version' => $version,
];

$nmrium = $study->nmrium;

if ($nmrium) {
$nmrium->nmrium_info = json_encode($nmriumJSON, JSON_UNESCAPED_UNICODE);
$nmrium->save();
} else {
$nmrium = NMRium::create([
'nmrium_info' => json_encode($nmriumJSON, JSON_UNESCAPED_UNICODE),
]);
$study->nmrium()->save($nmrium);
$study->has_nmrium = true;
$study->save();
}
}
});
} else {
$nmriumInfo = json_decode($study->nmrium['nmrium_info'], true);
if (count($nmriumInfo['data']['spectra']) == 0) {
echo '--MISSING SPECTRA INFO (NMRIUM JSON)--';
echo "\r\n";
}
}
} catch (Exception $e) {
echo 'Caught exception: ', $e->getMessage(), "\n";
}

foreach ($study->datasets as $dataset) {
echo $dataset->identifier;
echo "\r\n";
// if(!$dataset->has_nmrium){
$nmriumInfo = json_decode($study->nmrium['nmrium_info'], true);
$_nmriumJSON = $nmriumInfo;
$fsObject = $dataset->fsObject;
$level = -($fsObject->level);
// echo $fsObject->path;
// echo("\r\n");
// echo $study->name;
// echo("\r\n");
$path = $study->name.'/'.$dataset->name;
foreach ($nmriumInfo['data']['spectra'] as $spectra) {
unset($_nmriumJSON['data']['spectra']);
$files = $spectra['sourceSelector']['files'];
// echo($path);
// echo("\r\n");
$pathsMatch = true;
if ($files) {
foreach ($files as $file) {
echo $file;
echo "\r\n";
if (! str_contains($file, $path)) {
$pathsMatch = false;
}
}
}
// echo($pathsMatch);
// echo("\r\n");
if ($pathsMatch) {
$_nmriumJSON['data']['spectra'] = [$spectra];
// dd($_nmriumJSON);
$_nmrium = $dataset->nmrium;
if ($_nmrium) {
$_nmrium->nmrium_info = json_encode($_nmriumJSON, JSON_UNESCAPED_UNICODE);
$dataset->has_nmrium = true;
$_nmrium->save();
} else {
$_nmrium = NMRium::create([
'nmrium_info' => json_encode($_nmriumJSON, JSON_UNESCAPED_UNICODE),
]);
$dataset->nmrium()->save($_nmrium);
$dataset->has_nmrium = true;
}
$experimentDetailsExists = array_key_exists('experiment', $spectra['info']);
if ($experimentDetailsExists) {
$experiment = $spectra['info']['experiment'];
$nucleus = $spectra['info']['nucleus'];
if (is_array($nucleus)) {
$nucleus = implode('-', $nucleus);
}
$dataset->type = $experiment.','.$nucleus;
}
$dataset->save();
}
}
// }
}
}
}
}

public function processSpectra($url)
{
$response = Http::timeout(300)->post('https://nodejsdev.nmrxiv.org/spectra-parser', [
'urls' => [$url],
'snapshot' => false,
]);

return $response->json();
}
}
2 changes: 1 addition & 1 deletion app/Console/Commands/ManageFiles.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public function processFiles($path)
$fsObject->save();
}
} elseif ($item instanceof \League\Flysystem\DirectoryAttributes) {
echo $item->study_id;
// echo $item->study_id;
}
}
}
Expand Down
51 changes: 51 additions & 0 deletions app/Console/Commands/SanitizeMolecules.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php

namespace App\Console\Commands;

use App\Models\Molecule;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Http;

class SanitizeMolecules extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'nmrxiv:molecules-clean';

/**
* The console command description.
*
* @var string
*/
protected $description = 'Sanitize molecules';

/**
* Execute the console command.
*/
public function handle()
{
$molecules = Molecule::whereNull('CANONICAL_SMILES')->get();

foreach ($molecules as $molecule) {
echo $molecule->id;
echo "\r\n";
$molecule->MOL = '
'.$molecule->MOL;
$standardisedMOL = $this->standardizeMolecule($molecule->MOL);
$molecule->CANONICAL_SMILES = $standardisedMOL['canonical_smiles'];
$molecule->STANDARD_INCHI = $standardisedMOL['inchi'];
$molecule->INCHI_KEY = $standardisedMOL['inchikey'];
$molecule->save();
}
}

protected function standardizeMolecule($mol)
{
$response = Http::post('https://dev.api.naturalproducts.net/latest/chem/standardize', $mol);

return $response->json();
}
}
30 changes: 21 additions & 9 deletions app/Console/Commands/SanitizeProjects.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,9 @@ public function cleanProjects()
['draft_id', null],
])->get();

return DB::transaction(function () use ($projects) {
foreach ($projects as $project) {
foreach ($projects as $project) {
return DB::transaction(function () use ($project) {

$user_id = $project->owner->id;
$team_id = $project->team_id;
$id = Str::uuid();
Expand Down Expand Up @@ -107,8 +108,9 @@ public function cleanProjects()
$dataset->save();
}
}
}
});
});

}
}

public function cleanDrafts()
Expand Down Expand Up @@ -307,10 +309,20 @@ public function localise()

public function updateFilesStatus()
{
return DB::transaction(function () {
$fsObjects = FileSystemObject::all();
$drafts = Draft::where('is_deleted', true)->get();
foreach ($drafts as $draft) {
echo $draft->id;
echo "\r\n";
echo 'Deleting associated files';
FileSystemObject::where('draft_id', $draft->id)->delete();
echo 'Deleting draft';
echo "\r\n";
Draft::where('id', $draft->id)->delete();
}

foreach ($fsObjects as $fsObject) {
$fsObjects = FileSystemObject::whereNull('status')->get();
foreach ($fsObjects as $fsObject) {
DB::transaction(function () use ($fsObject) {
if ($fsObject->path) {
$exists = Storage::disk(env('FILESYSTEM_DRIVER'))->exists($fsObject->path);
if (! $exists) {
Expand All @@ -320,8 +332,8 @@ public function updateFilesStatus()
}
$fsObject->save();
}
}
});
});
}
}

public function updateFilePaths()
Expand Down
Loading

0 comments on commit 2eed0ed

Please sign in to comment.