-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: new command to import np classifier columns
- Loading branch information
1 parent
e2ecb4f
commit 3c3cb16
Showing
6 changed files
with
145 additions
and
3 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,95 @@ | ||
<?php | ||
|
||
namespace App\Console\Commands; | ||
|
||
use App\Models\Molecule; | ||
use DB; | ||
use Illuminate\Console\Command; | ||
use Log; | ||
|
||
class ImportNPClassifierOutput extends Command | ||
{ | ||
/** | ||
* The name and signature of the console command. | ||
* | ||
* @var string | ||
*/ | ||
protected $signature = 'coconut:import-np-classifier-output {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; | ||
} | ||
|
||
Log::info('Reading file: '.$file); | ||
|
||
$batchSize = 10000; | ||
$header = null; | ||
$data = []; | ||
$rowCount = 0; | ||
|
||
if (($handle = fopen($file, 'r')) !== false) { | ||
while (($row = fgetcsv($handle, 0, "\t")) !== false) { | ||
if (! $header) { | ||
$header = $row; | ||
} else { | ||
try { | ||
$data[] = array_combine($header, $row); | ||
$rowCount++; | ||
if ($rowCount % $batchSize == 0) { | ||
$this->insertBatch($data); | ||
$this->info('Rows inserted: '.$rowCount); | ||
$data = []; | ||
} | ||
} catch (\ValueError $e) { | ||
Log::info('An error occurred: '.$e->getMessage()); | ||
Log::info($rowCount++); | ||
} | ||
} | ||
} | ||
fclose($handle); | ||
|
||
if (! empty($data)) { | ||
$this->insertBatch($data); | ||
} | ||
} | ||
|
||
$this->info('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) { | ||
$properties = Molecule::where('identifier', $row['identifier'])->first()->properties()->get()[0]; | ||
$properties['pathway_results'] = $row['pathway_results'] == '' ? null : $row['pathway_results']; | ||
$properties['superclass_results'] = $row['superclass_results'] == '' ? null : $row['superclass_results']; | ||
$properties['class_results'] = $row['class_results'] == '' ? null : $row['class_results']; | ||
$properties['isglycoside'] = $row['isglycoside'] == '' ? null : $row['isglycoside']; | ||
$properties->save(); | ||
} | ||
}); | ||
} | ||
} |
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
32 changes: 32 additions & 0 deletions
32
database/migrations/2024_09_18_150749_add_columns_on_properties_table.php
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,32 @@ | ||
<?php | ||
|
||
use Illuminate\Database\Migrations\Migration; | ||
use Illuminate\Database\Schema\Blueprint; | ||
use Illuminate\Support\Facades\Schema; | ||
|
||
return new class extends Migration | ||
{ | ||
/** | ||
* Run the migrations. | ||
*/ | ||
public function up(): void | ||
{ | ||
Schema::table('properties', function (Blueprint $table) { | ||
$table->text('pathway_results')->nullable(); | ||
$table->text('superclass_results')->nullable(); | ||
$table->text('class_results')->nullable(); | ||
$table->boolean('isglycoside')->nullable(); | ||
|
||
}); | ||
} | ||
|
||
/** | ||
* Reverse the migrations. | ||
*/ | ||
public function down(): void | ||
{ | ||
Schema::table('properties', function (Blueprint $table) { | ||
$table->dropColumn(['pathway_results', 'superclass_results', 'class_results', 'isglycoside']); | ||
}); | ||
} | ||
}; |