Skip to content

Commit

Permalink
feat: new command to import np classifier columns
Browse files Browse the repository at this point in the history
  • Loading branch information
sriramkanakam87 committed Sep 19, 2024
1 parent e2ecb4f commit 3c3cb16
Show file tree
Hide file tree
Showing 6 changed files with 145 additions and 3 deletions.
95 changes: 95 additions & 0 deletions app/Console/Commands/ImportNPClassifierOutput.php
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();
}
});
}
}
7 changes: 7 additions & 0 deletions app/Helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,13 @@ function customAuditLog($event_type, $model_objects, $column_name, $currentValue

function changeAudit(array $data): array
{
// set the user_id and user_type if they are null (commands)
if (! $data['user_id']) {
$data['user_id'] = 11;
}
if (! $data['user_type']) {
$data['user_type'] = 'App\Models\User';
}

if (($data['event'] === 're-assign' || $data['event'] === 'detach' || $data['event'] === 'attach' || $data['event'] === 'sync') && $data['old_values'] && $data['new_values']) {
$whitelist = [
Expand Down
4 changes: 2 additions & 2 deletions app/Livewire/MoleculeHistoryTimeline.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ public function getHistory()
// $audit_data[$index]['affected_columns'][$key]['new_value'] = $value['new']??null;
// }

$old_key = explode('.', array_keys($audit->old_values)[0]??null)[0]??'none';
$new_key = explode('.', array_keys($audit->new_values)[0]??null)[0]??'none';
$old_key = $audit->old_values ? explode('.', array_keys($audit->old_values)[0])[0] : null;
$new_key = $audit->new_values ? explode('.', array_keys($audit->new_values)[0])[0] : null;

$old_key = $old_key ?: $new_key;
$new_key = $new_key ?: $old_key;
Expand Down
8 changes: 8 additions & 0 deletions app/Models/Properties.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,9 @@ class Properties extends Model implements Auditable
'chemical_sub_class' => 'array',
'chemical_super_class' => 'array',
'direct_parent_classification' => 'array',
'pathway_results' => 'array',
'superclass_results' => 'array',
'class_results' => 'array',
];

/**
Expand All @@ -71,4 +74,9 @@ public function molecule()
{
return $this->belongsTo(Molecule::class, 'molecule_id');
}

public function transformAudit(array $data): array
{
return changeAudit($data);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public function up(): void
public function down(): void
{
Schema::table('molecules', function (Blueprint $table) {
$table->json('comment')->nullable()->change();
// $table->json('comment')->nullable()->change();
});
}
};
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']);
});
}
};

0 comments on commit 3c3cb16

Please sign in to comment.