-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: ux/ui updates, text changes and commands added
- Loading branch information
Showing
18 changed files
with
529 additions
and
59 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'], | ||
] | ||
); | ||
} | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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']), | ||
] | ||
); | ||
} | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.