Skip to content

Commit

Permalink
fix: various bug fixes and ux/ui improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
CS76 committed Apr 10, 2024
1 parent 3159085 commit 30768b3
Show file tree
Hide file tree
Showing 20 changed files with 340 additions and 86 deletions.
9 changes: 8 additions & 1 deletion app/Console/Commands/ImportEntries.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@ public function handle()
}

foreach ($collections as $collection) {
$collection->jobs_status = 'PROCESSING';
$collection->job_info = 'Importing entries: Citations, Organism Info and other details';
$collection->save();

$batchJobs = [];
$i = 0;
Entry::select('id')->where('status', 'PASSED')->where('collection_id', $collection->id)->chunk(100, function ($ids) use (&$batchJobs, &$i) {
Expand All @@ -47,7 +51,10 @@ public function handle()
});
$batch = Bus::batch($batchJobs)->then(function (Batch $batch) {
})->catch(function (Batch $batch, Throwable $e) {
})->finally(function (Batch $batch) {
})->finally(function (Batch $batch) use ($collection) {
$collection->jobs_status = 'INCURATION';
$collection->job_info = '';
$collection->save();
})->name('Import Entries '.$collection->id)
->allowFailures(false)
->onConnection('redis')
Expand Down
18 changes: 13 additions & 5 deletions app/Console/Commands/ProcessEntries.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace App\Console\Commands;

use App\Jobs\LoadEntriesBatch;
use App\Models\Collection;
use App\Models\Entry;
use Artisan;
use Illuminate\Bus\Batch;
Expand Down Expand Up @@ -35,18 +36,25 @@ public function handle()
foreach ($collectionIds as $collectionId) {
$batchJobs = [];
$i = 0;
Entry::select('id')->where('status', 'SUBMITTED')->where('collection_id', $collectionId)->chunk(100, function ($ids) use (&$batchJobs, &$i) {

$collection = Collection::whereId($collectionId['collection_id'])->first();
$collection->jobs_status = 'PROCESSING';
$collection->job_info = 'Processing entries using ChEMBL Pipeline.';
$collection->save();

Entry::select('id')->where('status', 'SUBMITTED')->where('collection_id', $collectionId['collection_id'])->chunk(100, function ($ids) use (&$batchJobs, &$i) {
array_push($batchJobs, new LoadEntriesBatch($ids->pluck('id')->toArray()));
$i = $i + 1;
});
$batch = Bus::batch($batchJobs)->then(function (Batch $batch) {
})->catch(function (Batch $batch, Throwable $e) {
})->finally(function (Batch $batch) use ($collectionId) {

})->finally(function (Batch $batch) use ($collection) {
Artisan::call('entries:import', [
'collection_id' => $collectionId['collection_id'],
'collection_id' => $collection->id,
]);

$collection->jobs_status = 'INCURATION';
$collection->job_info = '';
$collection->save();
})->name('Process Entries '.$collectionId['collection_id'])
->allowFailures(false)
->onConnection('redis')
Expand Down
1 change: 1 addition & 0 deletions app/Filament/Dashboard/Imports/EntryImporter.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ public static function getColumns(): array
return [
ImportColumn::make('canonical_smiles'),
ImportColumn::make('reference_id'),
ImportColumn::make('name'),
ImportColumn::make('doi'),
ImportColumn::make('link'),
ImportColumn::make('organism'),
Expand Down
2 changes: 2 additions & 0 deletions app/Filament/Dashboard/Resources/CitationResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ class CitationResource extends Resource

protected static ?string $model = Citation::class;

protected static ?string $recordTitleAttribute = 'title';

protected static ?int $navigationSort = 2;

protected static ?string $navigationIcon = 'heroicon-o-ticket';
Expand Down
24 changes: 11 additions & 13 deletions app/Filament/Dashboard/Resources/CollectionResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,14 @@
use App\Filament\Dashboard\Resources\CollectionResource\Pages;
use App\Filament\Dashboard\Resources\CollectionResource\RelationManagers\CitationsRelationManager;
use App\Filament\Dashboard\Resources\CollectionResource\RelationManagers\EntriesRelationManager;
use App\Livewire\ShowJobStatus;
use App\Models\Collection;
use Filament\Forms\Components\Livewire;
use Filament\Forms\Components\Section;
use Filament\Forms\Components\Select;
use Filament\Forms\Components\SpatieTagsInput;
use Filament\Forms\Components\TextArea;
use Filament\Forms\Components\TextInput;
use Filament\Forms\Components\ToggleButtons;
use Filament\Forms\Form;
use Filament\Forms\Set;
use Filament\Resources\Resource;
Expand All @@ -30,11 +31,16 @@ class CollectionResource extends Resource

protected static ?string $navigationIcon = 'heroicon-o-swatch';

protected static ?string $recordTitleAttribute = 'title';

public static function form(Form $form): Form
{
return $form
->schema(
[

Livewire::make(ShowJobStatus::class),

Section::make('Database details')
->description('Provide details of the database and link to the resource.')
->schema([
Expand All @@ -48,23 +54,15 @@ public static function form(Form $form): Form
Section::make('Meta data')
->schema([
SpatieTagsInput::make('tags')
->type('collections'),
->type('collections'),
TextInput::make('identifier'),
]),
Section::make('Distribution')
->schema([
Select::make('license')
->relationship('license', 'title')
->preload()
->searchable(),
// ToggleButtons::make('status')
// ->options([
// 'DRAFT' => 'Draft',
// 'REVIEW' => 'Review',
// 'EMBARGO' => 'Embargo',
// 'PUBLISHED' => 'Published',
// 'REJECTED' => 'Rejected',
// ])->inline(),
->relationship('license', 'title')
->preload()
->searchable(),
]),
]
)->columns(1);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ public function form(Form $form): Form
Forms\Components\TextInput::make('canonical_smiles')
->required()
->maxLength(255),
Forms\Components\TextInput::make('name')
->required()
->maxLength(255),
Forms\Components\TextInput::make('doi')
->required()
->maxLength(255),
Expand All @@ -42,11 +45,20 @@ public function form(Form $form): Form
Forms\Components\TextInput::make('organism_part')
->required()
->maxLength(255),
Forms\Components\TextInput::make('mol_filename')
->required()
->maxLength(255),
Forms\Components\TextInput::make('molecular_formula')
->required()
->maxLength(255),
Forms\Components\TextArea::make('structural_comments')
->required(),
Forms\Components\TextInput::make('geo_location')
->required()
->maxLength(255),
Forms\Components\TextInput::make('location')
->required()
->maxLength(255),
Forms\Components\TextArea::make('errors')
->required(),
Forms\Components\TextInput::make('standardized_canonical_smiles')
Expand All @@ -66,12 +78,15 @@ public function infolist(Infolist $infolist): Infolist
Section::make()
->schema([
TextEntry::make('reference_id'),
TextEntry::make('name'),
TextEntry::make('doi'),
TextEntry::make('link'),
TextEntry::make('organism'),
TextEntry::make('organism_part'),
TextEntry::make('molecular_formula'),
TextEntry::make('structural_comments'),
TextEntry::make('geo_location'),
TextEntry::make('location'),
TextEntry::make('errors'),
]),
Section::make()
Expand Down Expand Up @@ -122,7 +137,7 @@ public function table(Table $table): Table
->height(200)
->ring(5)
->defaultImageUrl(url('/images/placeholder.png')),
Tables\Columns\TextColumn::make('identifier')->searchable(),
Tables\Columns\TextColumn::make('reference_id')->searchable(),
Tables\Columns\TextColumn::make('status'),
])
->filters([
Expand Down
80 changes: 58 additions & 22 deletions app/Jobs/ImportEntry.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use App\Models\Citation;
use App\Models\Molecule;
use App\Models\Properties;
use App\Models\Ticker;
use Illuminate\Bus\Batchable;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldBeUnique;
Expand All @@ -29,6 +30,14 @@ public function __construct($entry)
$this->entry = $entry;
}

/**
* Get the unique ID for the job.
*/
public function uniqueId(): string
{
return $this->entry->uuid;
}

/**
* Execute the job.
*/
Expand All @@ -41,61 +50,79 @@ public function handle(): void
if ($parent->wasRecentlyCreated) {
$parent->is_parent = true;
$parent->has_variants = true;
$parent->identifier = $this->entry->coconut_id;
$parent->is_placeholder = true;
$parent->variants_count += $parent->variants_count;
$parent = $this->assignData($parent, $data);
$parent->save();

$this->fetchIUPACNameFromPubChem($parent);
$this->attachProperties('parent', $parent);
$this->classify($parent);
// $this->fetchIUPACNameFromPubChem($parent);
// $this->attachProperties('parent', $parent);
// $this->classify($parent);
}
$this->attachCollection($parent);

$data = $this->getRepresentations('standardized');
$molecule = Molecule::firstOrCreate(['standard_inchi' => $data['standard_inchi'], 'standard_inchi_key' => $data['standard_inchikey']]);
if ($molecule->wasRecentlyCreated) {
$molecule->has_stereo = true;
$molecule->parent_id = $parent->id;
$parent->ticker = $parent->ticker + 1;
$molecule->identifier = $this->entry->coconut_id.'.'.$parent->ticker;
$molecule = $this->assignData($molecule, $data);
$this->fetchIUPACNameFromPubChem($molecule);
$this->attachProperties('standardized', $molecule);
$parent->save();
$this->classify($molecule);
$molecule = $this->assignData($molecule, $data);
// $this->fetchIUPACNameFromPubChem($molecule);
// $this->attachProperties('standardized', $molecule);
// $this->classify($molecule);
$molecule->save();
}
$this->entry->molecule_id = $molecule->id;
$this->entry->save();

$this->attachCollection($parent);
$this->attachCollection($molecule);
} else {
$data = $this->getRepresentations('standardized');
$molecule = Molecule::firstOrCreate(['standard_inchi' => $data['standard_inchi'], 'standard_inchi_key' => $data['standard_inchikey']]);
if ($molecule->wasRecentlyCreated) {
$molecule = $this->assignData($molecule, $data);
$molecule->identifier = $this->entry->coconut_id;
$molecule->save();
$this->fetchIUPACNameFromPubChem($molecule);
$this->attachProperties('standardized', $molecule);
$this->classify($molecule);
// $this->fetchIUPACNameFromPubChem($molecule);
// $this->attachProperties('standardized', $molecule);
// $this->classify($molecule);
}
$this->entry->molecule_id = $molecule->id;
$this->entry->save();
$this->attachCollection($molecule);
}

if ($this->entry->doi && $this->entry->doi != '') {
$this->fetchCitation($this->entry->doi, $molecule);
$dois = explode('|', $this->entry->doi);

$doiRegex = '/\b(10[.][0-9]{4,}(?:[.][0-9]+)*)\b/';
foreach ($dois as $doi) {
if (preg_match($doiRegex, $doi)) {
$this->fetchDOICitation($doi, $molecule);
} else {
$this->fetchCitation($doi, $molecule);
}
}
}
}
}

public function classify($molecule)
public function fetchIdentifier()
{
$ticker = Ticker::first();
$identifier = $ticker->index + 1;
$ticker->index = $identifier;
$ticker->save();
$CNP = 'CNP'.$identifier;

while (Molecule::where('identifier', $CNP)->exists()) {
return $this->fetchIdentifier();
}

return $CNP;
}

public function classify($molecule)
{
$properties = $molecule->properties;

if ($properties->chemical_class == null || $properties->chemical_sub_class == null || $properties->chemical_super_class == null || $properties->direct_parent_classification == null) {
Expand Down Expand Up @@ -146,19 +173,29 @@ public function classify($molecule)

}

public function fetchCitation($doi, $molecule)
public function fetchCitation($citation_text, $molecule)
{
$citation = Citation::firstOrCreate(['citation_text' => $citation_text]);
$molecule->citations()->syncWithoutDetaching($citation);
}

public function fetchDOICitation($doi, $molecule)
{

$citation = null;

// check if the doi is valid
$isDOI = preg_match('/\b(10[.][0-9]{4,}(?:[.][0-9]+)*)\b/', $doi);

if ($isDOI) {

echo $doi;
echo '/r/n';

//check if citation already exists
$citation = Citation::where('doi', $doi)->first();
$citation = Citation::firstOrCreate(['doi' => $doi]);
$citationResponse = null;
if (! $citation) {
if ($citation->wasRecentlyCreated) {
// fetch citation from EuropePMC
$europemcUrl = env('EUROPEPMC_WS_API');
$europemcParams = [
Expand Down Expand Up @@ -199,7 +236,6 @@ public function fetchCitation($doi, $molecule)
}
}

// attach citation
$molecule->citations()->syncWithoutDetaching($citation);
}
}
Expand Down
24 changes: 24 additions & 0 deletions app/Livewire/ShowJobStatus.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

namespace App\Livewire;

use Illuminate\Database\Eloquent\Model;
use Livewire\Component;

class ShowJobStatus extends Component
{
public $collection;

public function mount(?Model $record = null)
{
$this->collection = $record;
}

public function render()
{
return view('livewire.show-job-status', [
'status' => $this->collection->jobs_status,
'info' => $this->collection->job_info,
]);
}
}
1 change: 1 addition & 0 deletions app/Models/Collection.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ protected static function booted()
'title',
'slug',
'description',
'jobs_status',
'comments',
'identifier',
'url',
Expand Down
Loading

0 comments on commit 30768b3

Please sign in to comment.