Skip to content

Commit

Permalink
feat: doi fetching in citation (wip)
Browse files Browse the repository at this point in the history
  • Loading branch information
sriramkanakam87 committed Apr 30, 2024
1 parent f7d6d54 commit 8f43650
Show file tree
Hide file tree
Showing 6 changed files with 227 additions and 13 deletions.
66 changes: 61 additions & 5 deletions app/Filament/Dashboard/Resources/CitationResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,16 @@
use Filament\Tables;
use Filament\Tables\Columns\TextColumn;
use Filament\Tables\Table;
use Illuminate\Support\Str;
use Filament\Notifications\Notification;
use Closure;
use Filament\Forms\Set;
use Filament\Forms\Get;
use Illuminate\Support\HtmlString;
use Filament\Forms\Components\Section;
use Filament\Forms\Components\ViewField;
use App\Livewire\ShowStatus;
use Filament\Forms\Components\Livewire;

class CitationResource extends Resource
{
Expand All @@ -30,11 +40,57 @@ public static function form(Form $form): Form
{
return $form
->schema([
TextInput::make('doi'),
TextInput::make('title'),
TextInput::make('authors'),
TextArea::make('citation_text'),
])->columns(1);
Section::make()
->schema([
TextInput::make('failMessage')
->default('hello'),
Livewire::make(ShowStatus::class, function(Get $get) {
return ['status' => $get('failMessage')];
})->live(),
TextInput::make('doi')
->label('DOI')
->live(onBlur: true)
->afterStateUpdated(function ($set, $state) {
$set('failMessage', 'Fetching');
if(doiRegxMatch($state)) {
$citationDetails = fetchDOICitation($state);
if($citationDetails) {
$set('title', $citationDetails['title']);
$set('authors', $citationDetails['authors']);
$set('citation_text', $citationDetails['citation_text']);
$set('failMessage', 'Successful');
} else {
$set('failMessage', 'No citation found');
}
} else {
$set('failMessage', 'Invalid DOI');
}
})
->required()
->unique()
->rules([
fn (Get $get): Closure => function (string $attribute, $value, Closure $fail) use ($get) {
if ($get('failMessage')) {
$fail($get('failMessage'));
}
},
])
->validationMessages([
'unique' => 'The DOI already exists.',
]),
]),

Section::make()
->schema([

// ViewField::make('forms.loading'),
TextInput::make('title'),
TextInput::make('authors'),
TextArea::make('citation_text')
->label('Citation text / URL'),
])->columns(1)
])
;
}

public static function table(Table $table): Table
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,4 @@
class CreateGeoLocation extends CreateRecord
{
protected static string $resource = GeoLocationResource::class;

protected function beforeCreate(): void
{
// $molecule = Molecule::where('identifier', $this->data['molecule_id'])->get();
// $this->data['molecule_id'] = $molecule[0]->id;
// dd($this->data);
// $this->data->molecules()->attach($molecule);
}
}
138 changes: 138 additions & 0 deletions app/Helper.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
<?php

use App\Models\Citation;


function npScore($old_value)
{
$old_min = -4.5;
Expand All @@ -9,3 +12,138 @@ function npScore($old_value)

return ($old_value - $old_min) / ($old_max - $old_min) * ($new_max - $new_min) + $new_min;
}

function doiRegxMatch($doi)
{
$doiRegex = '/\b(10[.][0-9]{4,}(?:[.][0-9]+)*)\b/';
return preg_match($doiRegex, $doi);
}

function fetchDOICitation($doi)
{
$citationResponse = null;
$europemcUrl = env('EUROPEPMC_WS_API');
$europemcParams = [
'query' => 'DOI:'.$doi,
'format' => 'json',
'pageSize' => '1',
'resulttype' => 'core',
'synonym' => 'true',
];
$europemcResponse = makeRequest($europemcUrl, $europemcParams);

if ($europemcResponse && isset($europemcResponse['resultList']['result']) && count($europemcResponse['resultList']['result']) > 0) {
$citationResponse = formatCitationResponse($europemcResponse['resultList']['result'][0], 'europemc');
} else {
// fetch citation from CrossRef
$crossrefUrl = env('CROSSREF_WS_API').$doi;
$crossrefResponse = makeRequest($crossrefUrl);
if ($crossrefResponse && isset($crossrefResponse['message'])) {
$citationResponse = formatCitationResponse($crossrefResponse['message'], 'crossref');
} else {
// fetch citation from DataCite
$dataciteUrl = env('DATACITE_WS_API').$doi;
$dataciteResponse = makeRequest($dataciteUrl);
if ($dataciteResponse && isset($dataciteResponse['data'])) {
$citationResponse = formatCitationResponse($dataciteResponse['data'], 'datacite');
}
}
}

return $citationResponse;

// if ($citationResponse) {
// if ($citationResponse['doi'] == $doi) {
// $citation = Citation::where('doi', $citationResponse['doi'])->first();
// if ($citation === null) {
// $citation = Citation::create($citationResponse);
// $citation->save();
// } else {
// unset($citationResponse['doi']);
// $citation->update($citationResponse);
// $citation->save();
// }
// }
// }
}

function makeRequest($url, $params = [])
{
try {
$response = Http::timeout(600)->get($url, $params);
if ($response->successful()) {
return $response->json();
} else {
return null; // Handle error here
}
} catch (Exception $e) {
return null; // Handle exception here
}
}

function formatCitationResponse($obj, $apiType)
{
$journalTitle = '';
$yearofPublication = '';
$volume = '';
$issue = '';
$pageInfo = '';
$formattedCitationRes = [];

if ($obj) {
switch ($apiType) {
case 'europemc':
$journalTitle = isset($obj['journalInfo']['journal']['title']) ? $obj['journalInfo']['journal']['title'] : '';
$yearofPublication = isset($obj['journalInfo']['yearOfPublication']) ? $obj['journalInfo']['yearOfPublication'] : '';
$volume = isset($obj['journalInfo']['volume']) ? $obj['journalInfo']['volume'] : '';
$issue = isset($obj['journalInfo']['issue']) ? $obj['journalInfo']['issue'] : '';
$pageInfo = isset($obj['pageInfo']) ? $obj['pageInfo'] : '';
$formattedCitationRes['title'] = isset($obj['title']) ? $obj['title'] : '';
$formattedCitationRes['authors'] = isset($obj['authorString']) ? $obj['authorString'] : '';
$formattedCitationRes['citation_text'] = $journalTitle.' '.$yearofPublication.' '.$volume.' ( '.$issue.' ) '.$pageInfo;
$formattedCitationRes['doi'] = isset($obj['doi']) ? $obj['doi'] : '';
break;
case 'datacite':
$journalTitle = isset($obj['attributes']['titles'][0]['title']) ? $obj['attributes']['titles'][0]['title'] : '';
$yearofPublication = isset($obj['attributes']['publicationYear']) ? $obj['attributes']['publicationYear'] : null;
$volume = isset($obj['attributes']['volume']) ? $obj['attributes']['volume'] : '';
$issue = isset($obj['attributes']['issue']) ? $obj['attributes']['issue'] : '';
$pageInfo = isset($obj['attributes']['page']) ? $obj['attributes']['page'] : '';
$formattedCitationRes['title'] = $journalTitle;
if (isset($obj['attributes']['creators'])) {
$formattedCitationRes['authors'] = implode(', ', array_map(function ($author) {
return $author['name'];
}, $obj['attributes']['creators']));
}
$formattedCitationRes['citation_text'] = $journalTitle.' '.$yearofPublication;
$formattedCitationRes['doi'] = isset($obj['attributes']['doi']) ? $obj['attributes']['doi'] : '';
break;
case 'crossref':
$journalTitle = isset($obj['title'][0]) ? $obj['title'][0] : '';
$yearofPublication = isset($obj['published-online']['date-parts'][0][0]) ? $obj['published-online']['date-parts'][0][0] : '';
$volume = isset($obj['volume']) ? $obj['volume'] : '';
$issue = isset($obj['issue']) ? $obj['issue'] : '';
$pageInfo = isset($obj['page']) ? $obj['page'] : '';
$formattedCitationRes['title'] = $journalTitle;
if (isset($obj['author'])) {
$formattedCitationRes['authors'] = implode(', ', array_map(function ($author) {
$fullName = '';
if (isset($author['given'])) {
$fullName .= $author['given'].' ';
}
if (isset($author['family'])) {
$fullName .= $author['family'];
}

return $fullName;
}, $obj['author']));
}
$formattedCitationRes['citation_text'] = $journalTitle.' '.$yearofPublication.' '.$volume.' ( '.$issue.' ) '.$pageInfo;
$formattedCitationRes['doi'] = isset($obj['DOI']) ? $obj['DOI'] : '';
break;
}
}

return $formattedCitationRes;
}

21 changes: 21 additions & 0 deletions app/Livewire/ShowStatus.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

namespace App\Livewire;

use Livewire\Component;
use Filament\Forms\Concerns\InteractsWithForms;

class ShowStatus extends Component
{
public $status = null;

public function mount($status)
{
$this->status = $status;
}

public function render()
{
return view('livewire.show-status');
}
}
3 changes: 3 additions & 0 deletions resources/views/livewire/show-job-status.blade.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
<div>
<div wire:poll>
@if($failMessage)
{{ $failMessage }}
@endif
@if($status == 'PROCESSING')
<div class="rounded-md border bg-yellow p-4">
<div class="flex">
Expand Down
4 changes: 4 additions & 0 deletions resources/views/livewire/show-status.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<div>
<!-- <x-filament::loading-indicator class="h-5 w-5" /> -->
{{ $status }}
</div>

0 comments on commit 8f43650

Please sign in to comment.