Skip to content

Commit

Permalink
feat: ux/ui updates, text changes and commands added
Browse files Browse the repository at this point in the history
  • Loading branch information
CS76 committed May 23, 2024
1 parent 2bb3144 commit 3b84e5d
Show file tree
Hide file tree
Showing 18 changed files with 529 additions and 59 deletions.
89 changes: 89 additions & 0 deletions app/Console/Commands/ImportCoconutIDs.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
<?php

namespace App\Console\Commands;

use App\Models\Molecule;
use DB;
use Illuminate\Console\Command;

class ImportCoconutIDs extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'app:import-ids {file}';

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

/**
* Execute the console command.
*/
public function handle()
{
$file = storage_path($this->argument('file'));

if (! file_exists($file) || ! is_readable($file)) {
$this->error('File not found or not readable.');

return 1;
}

$batchSize = 1000;
$header = null;
$data = [];
$rowCount = 0;

if (($handle = fopen($file, 'r')) !== false) {
while (($row = fgetcsv($handle, 0, "\t")) !== false) {
if (! $header) {
$header = $row;
} else {
$data[] = array_combine($header, $row);
$rowCount++;

if ($rowCount % $batchSize == 0) {
$this->insertBatch($data);
$data = [];
}
}
}
fclose($handle);

if (! empty($data)) {
$this->insertBatch($data);
}
}

$this->info('IDs imported successfully!');

return 0;
}

/**
* Insert a batch of data into the database.
*
* @return void
*/
private function insertBatch(array $data)
{
DB::transaction(function () use ($data) {
foreach ($data as $row) {
Molecule::updateorCreate(
['id' => $row['id'],
'canonical_smiles' => $row['canonical_smiles'],
],
[
'identifier' => $row['identifier'],
]
);
}
});
}
}
97 changes: 97 additions & 0 deletions app/Console/Commands/ImportPubChemNames.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
<?php

namespace App\Console\Commands;

use App\Models\Molecule;
use DB;
use Illuminate\Console\Command;
use Log;

class ImportPubChemNames extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'app:import-pubchem-data {file}';

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

/**
* Execute the console command.
*/
public function handle()
{
$file = storage_path($this->argument('file'));

if (! file_exists($file) || ! is_readable($file)) {
$this->error('File not found or not readable.');

return 1;
}

$batchSize = 1000;
$header = null;
$data = [];
$rowCount = 0;

if (($handle = fopen($file, 'r')) !== false) {
while (($row = fgetcsv($handle, 0, ',', '"')) !== false) {
if (! $header) {
$header = $row;
} else {
try {
$data[] = array_combine($header, $row);
$rowCount++;
if ($rowCount % $batchSize == 0) {
$this->insertBatch($data);
$data = [];
}
} catch (\ValueError $e) {
Log::info('An error occurred: '.$e->getMessage());
Log::info($rowCount++);
}
}
}
fclose($handle);

if (! empty($data)) {
$this->insertBatch($data);
}
}

$this->info('PubChem data imported successfully!');

return 0;
}

/**
* Insert a batch of data into the database.
*
* @return void
*/
private function insertBatch(array $data)
{
DB::transaction(function () use ($data) {
foreach ($data as $row) {
Molecule::updateorCreate(
[
'id' => $row['id'],
'canonical_smiles' => $row['canonical_smiles'],
],
[
'name' => $row['name'],
'iupac_name' => $row['IUPAC_name'],
'synonyms' => explode('|', $row['synonyms']),
]
);
}
});
}
}
116 changes: 116 additions & 0 deletions app/Console/Commands/MapOrganismNamesToOGG.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
<?php

namespace App\Console\Commands;

use App\Models\Organism;
use GuzzleHttp\Client;
use Illuminate\Console\Command;
use Log;

class MapOrganismNamesToOGG extends Command
{
protected $signature = 'organisms:map-ogg';

protected $description = 'Map organism names to OGG IRIs and update the model';

protected $client;

public function __construct()
{
parent::__construct();
$this->client = new Client([
'base_uri' => 'https://www.ebi.ac.uk/ols4/api/v2/',
]);
}

/**
* Execute the console command.
*/
public function handle()
{
$chunkSize = 100;

Organism::whereNull('iri')->chunk($chunkSize, function ($organisms) {
foreach ($organisms as $organism) {
$name = ucfirst(trim($organism->name));
$data = null;
if ($name && $name != '') {
$data = $this->getOLSIRI($name, 'species');
if ($data) {
$this->updateOrganismModel($name, $data, $organism, 'species');
$this->info("Mapped and updated: $name");
} else {
$data = $this->getOLSIRI(explode(' ', $name)[0], 'genus');
if ($data) {
$this->updateOrganismModel($name, $data, $organism, 'genus');
$this->info("Mapped and updated: $name");
} else {
$data = $this->getOLSIRI(explode(' ', $name)[0], 'family');
if ($data) {
$this->updateOrganismModel($name, $data, $organism, 'genus');
$this->info("Mapped and updated: $name");
} else {
$this->error("Could not map: $name");
}
}
}
}
}
});
}

protected function getOLSIRI($name, $rank)
{
try {
$response = $this->client->get('entities', [
'query' => [
'search' => $name,
'ontologyId' => 'ncbitaxon',
'exactMatch' => true,
'type' => 'class',
],
]);

$data = json_decode($response->getBody(), true);

if (isset($data['elements']) && count($data['elements']) > 0) {
$element = $data['elements'][0];
if (isset($element['iri'], $element['ontologyId']) && $element['isObsolete'] === 'false') {
if ($rank && $rank == 'species') {
if (isset($element['http://purl.obolibrary.org/obo/ncbitaxon#has_rank']) && $element['http://purl.obolibrary.org/obo/ncbitaxon#has_rank'] == 'http://purl.obolibrary.org/obo/NCBITaxon_species') {
return urlencode($element['iri']);
}
} elseif ($rank && $rank == 'genus') {
if (isset($element['http://purl.obolibrary.org/obo/ncbitaxon#has_rank']) && $element['http://purl.obolibrary.org/obo/ncbitaxon#has_rank'] == 'http://purl.obolibrary.org/obo/NCBITaxon_genus') {
return urlencode($element['iri']);
}
} elseif ($rank && $rank == 'family') {
if (isset($element['http://purl.obolibrary.org/obo/ncbitaxon#has_rank']) && $element['http://purl.obolibrary.org/obo/ncbitaxon#has_rank'] == 'http://purl.obolibrary.org/obo/NCBITaxon_family') {
return urlencode($element['iri']);
}
}
}
}
} catch (\Exception $e) {
$this->error("Error fetching IRI for $name: ".$e->getMessage());
Log::error("Error fetching IRI for $name: ".$e->getMessage());
}

return null;
}

protected function updateOrganismModel($name, $iri, $organism = null, $rank = null)
{
if (! $organism) {
$organism = Organism::where('name', $name)->first();
}

if ($organism) {
$organism->iri = $iri;
$organism->rank = $rank;
$organism->save();
} else {
$this->error("Organism not found in the database: $name");
}
}
}
2 changes: 1 addition & 1 deletion app/Filament/Dashboard/Resources/CollectionResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ public static function getWidgets(): array
{
return [
CollectionStats::class,
EntriesOverview::class
EntriesOverview::class,
];
}
}
4 changes: 2 additions & 2 deletions app/Filament/Dashboard/Resources/MoleculeResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@
use App\Filament\Dashboard\Resources\MoleculeResource\RelationManagers\CollectionsRelationManager;
use App\Filament\Dashboard\Resources\MoleculeResource\RelationManagers\GeoLocationRelationManager;
use App\Filament\Dashboard\Resources\MoleculeResource\RelationManagers\MoleculesRelationManager;
use App\Filament\Dashboard\Resources\MoleculeResource\RelationManagers\RelatedRelationManager;
use App\Filament\Dashboard\Resources\MoleculeResource\RelationManagers\OrganismsRelationManager;
use App\Filament\Dashboard\Resources\MoleculeResource\RelationManagers\PropertiesRelationManager;
use App\Filament\Dashboard\Resources\MoleculeResource\RelationManagers\RelatedRelationManager;
use App\Filament\Dashboard\Resources\MoleculeResource\Widgets\MoleculeStats;
use App\Models\Molecule;
use Filament\Forms\Components\TextArea;
Expand Down Expand Up @@ -43,7 +43,7 @@ public static function form(Form $form): Form
TextArea::make('iupac_name'),
TextInput::make('canonical_smiles'),
TextInput::make('murko_framework'),
TextArea::make('synonyms')
TextArea::make('synonyms'),
]);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
use Filament\Forms\Form;
use Filament\Resources\RelationManagers\RelationManager;
use Filament\Tables;
use Filament\Tables\Table;
use Filament\Tables\Columns\ImageColumn;
use Filament\Tables\Table;

class MoleculesRelationManager extends RelationManager
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
use Filament\Forms\Form;
use Filament\Resources\RelationManagers\RelationManager;
use Filament\Tables;
use Filament\Tables\Table;
use Filament\Tables\Columns\ImageColumn;
use Filament\Tables\Table;

class RelatedRelationManager extends RelationManager
{
Expand Down
10 changes: 8 additions & 2 deletions app/Filament/Dashboard/Resources/OrganismResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace App\Filament\Dashboard\Resources;

use App\Filament\Dashboard\Resources\CollectionResource\RelationManagers\MoleculesRelationManager;
use App\Filament\Dashboard\Resources\OrganismResource\Pages;
use App\Filament\Dashboard\Resources\OrganismResource\Widgets\OrganismStats;
use App\Models\Organism;
Expand Down Expand Up @@ -66,9 +67,14 @@ public static function table(Table $table): Table

public static function getRelations(): array
{
return [
//
// $record = static::getOwner();
// dd(static::getOwner());
// dd(static::$model::molecules()->get());
$arr = [
MoleculesRelationManager::class,
];

return $arr;
}

public static function getPages(): array
Expand Down
Loading

0 comments on commit 3b84e5d

Please sign in to comment.