Skip to content

Commit

Permalink
Merge pull request #767 from sfu-ireceptor/master
Browse files Browse the repository at this point in the history
Merge master into production-v4
  • Loading branch information
bcorrie authored Jan 24, 2024
2 parents 6db5a89 + 16f927c commit aeef9a4
Show file tree
Hide file tree
Showing 139 changed files with 5,299 additions and 1,921 deletions.
799 changes: 0 additions & 799 deletions app/Agave.php

This file was deleted.

2 changes: 1 addition & 1 deletion app/Bookmark.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public static function findGroupedByMonthForUser($user_id)

public static function get($id, $user_id)
{
$b = static::where(['user_id' => $user_id, 'id'=> $id])->first();
$b = static::where(['user_id' => $user_id, 'id' => $id])->first();

return $b;
}
Expand Down
84 changes: 77 additions & 7 deletions app/CachedSample.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace App;

use Illuminate\Support\Facades\Log;
use Illuminate\Support\Str;
use Jenssegers\Mongodb\Eloquent\Model;

Expand All @@ -14,25 +15,52 @@ class CachedSample extends Model
// cache samples from REST services
public static function cache()
{
// get data
// delete any previously cached data
self::truncate();

// Keep track of number of samples cached.
$sample_count = 0;
// get sequnce data
$sample_data = Sample::find([], 'titi'); // use Jérôme's username (titi) for now
$sample_list = $sample_data['items'];

// delete any previously cached data
self::truncate();
// cache data
foreach ($sample_list as $s) {
$s->ir_repertoire_type = 'sequence';
self::create(json_decode(json_encode($s), true));
}
$sample_count += count($sample_list);

// get clone data
$sample_data = Sample::find([], 'titi', true, 'clone');
$sample_list = $sample_data['items'];

// cache data
foreach ($sample_list as $s) {
$s->ir_repertoire_type = 'clone';
self::create(json_decode(json_encode($s), true));
}
$sample_count += count($sample_list);

// get cell data
$sample_data = Sample::find([], 'titi', true, 'cell'); // use Jérôme's username (titi) for now
$sample_list = $sample_data['items'];

// cache data
foreach ($sample_list as $s) {
$s->ir_repertoire_type = 'cell';
self::create(json_decode(json_encode($s), true));
}
$sample_count += count($sample_list);

return count($sample_list);
return $sample_count;
}

// return cached samples
public static function cached()
public static function cached($sample_type = 'sequence')
{
return self::all();
return self::where('ir_repertoire_type', '=', $sample_type)->get();
//return self::all();
}

// return metadata by querying cached samples
Expand Down Expand Up @@ -60,12 +88,31 @@ public static function metadata()
// list of REST services
$t['rest_service_list'] = RestService::findEnabled(['id', 'name', 'rest_service_group_code'])->toArray();

// stats
// Get the total number of repositories.
$t['total_repositories'] = count(self::distinctValuesGrouped(['rest_service_id']));
$t['total_repositories_sequences'] = count(self::distinctValuesFiltered('rest_service_id', 'ir_repertoire_type', 'sequence'));
$t['total_repositories_clones'] = count(self::distinctValuesFiltered('rest_service_id', 'ir_repertoire_type', 'clone'));
$t['total_repositories_cells'] = count(self::distinctValuesFiltered('rest_service_id', 'ir_repertoire_type', 'cell'));

// Get the total number of labs.
$t['total_labs'] = count(self::distinctValuesGrouped(['rest_service_id', 'lab_name']));

// Get the study counts for total, sequence, clone, and cell studies.
$t['total_projects'] = count(self::distinctValuesGrouped(['rest_service_id', 'study_title']));
$t['total_projects_sequences'] = count(self::distinctValuesFiltered('study_title', 'ir_repertoire_type', 'sequence'));
$t['total_projects_cells'] = count(self::distinctValuesFiltered('study_title', 'ir_repertoire_type', 'cell'));
$t['total_projects_clones'] = count(self::distinctValuesFiltered('study_title', 'ir_repertoire_type', 'clone'));

// Get the repertoire counts for all, sequence, clone, and cell repertoires.
$t['total_samples'] = self::count();
$t['total_samples_sequences'] = self::where('ir_repertoire_type', '=', 'sequence')->count();
$t['total_samples_clones'] = self::where('ir_repertoire_type', '=', 'clone')->count();
$t['total_samples_cells'] = self::where('ir_repertoire_type', '=', 'cell')->count();

// Get the total counts for sequences, clones, and cells.
$t['total_sequences'] = self::sum('ir_sequence_count');
$t['total_clones'] = self::sum('ir_clone_count');
$t['total_cells'] = self::sum('ir_cell_count');

return $t;
}
Expand All @@ -90,18 +137,41 @@ public static function distinctValues($fieldName)
return $t;
}

public static function distinctValuesFiltered($fieldName, $filterField, $filterValue)
{
$l = self::where($filterField, '=', $filterValue)->whereNotNull($fieldName)->distinct($fieldName)->get();
$l = $l->toArray();

// replace each array item (a one-item array) by the value directly
// Ex: [0]=>[[0]=> "Unknown"] is replaced by [0]=> "Unknown"
$t = [];
foreach ($l as $lt) {
if (! empty(trim($lt[0]))) {
$val = trim($lt[0]);
if (! in_array($val, $t)) {
$t[] = $val;
}
}
}

return $t;
}

public static function distinctValuesGrouped($fields)
{
$l = self::groupBy($fields);
//Log::debug('distinctValuesGrouped: l = ' . json_encode($l));

// exclude null values
foreach ($fields as $fieldName) {
$l = $l->whereNotNull($fieldName);
}
//Log::debug('distinctValuesGrouped: l = ' . json_encode($l));

// do query
$l = $l->get();
$l = $l->toArray();
//Log::debug('distinctValuesGrouped: l = ' . json_encode($l));

// remove useless '_id' key
foreach ($l as $k => $v) {
Expand Down
Loading

0 comments on commit aeef9a4

Please sign in to comment.