Passed
Branch feature/2.0 (9789a8)
by Jonathan
14:17
created

pedigreeCollapseByGenerationData()   B

Complexity

Conditions 2
Paths 2

Size

Total Lines 130
Code Lines 96

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 96
c 0
b 0
f 0
nc 2
nop 0
dl 0
loc 130
rs 8.0872

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
/**
4
 * webtrees-lib: MyArtJaub library for webtrees
5
 *
6
 * @package MyArtJaub\Webtrees
7
 * @subpackage Sosa
8
 * @author Jonathan Jaubart <[email protected]>
9
 * @copyright Copyright (c) 2009-2020, Jonathan Jaubart
10
 * @license http://www.gnu.org/licenses/gpl.html GNU General Public License, version 3
11
 */
12
13
declare(strict_types=1);
14
15
namespace MyArtJaub\Webtrees\Module\Sosa\Services;
16
17
use Fisharebest\Webtrees\Individual;
18
use Fisharebest\Webtrees\Registry;
19
use Fisharebest\Webtrees\Tree;
20
use Fisharebest\Webtrees\User;
21
use Illuminate\Database\Capsule\Manager as DB;
22
use Illuminate\Database\Query\Builder;
23
use Illuminate\Database\Query\JoinClause;
24
use Illuminate\Support\Collection;
25
26
/**
27
 * Service for retrieving Sosa statistics
28
 */
29
class SosaStatisticsService
30
{
31
32
    /**
33
     * Reference user
34
     * @var User $user
35
     */
36
    private $user;
37
38
    /**
39
     * Reference tree
40
     * @var Tree $tree
41
     */
42
    private $tree;
43
44
    /**
45
     * Constructor for Sosa Statistics Service
46
     *
47
     * @param Tree $tree
48
     * @param User $user
49
     */
50
    public function __construct(Tree $tree, User $user)
51
    {
52
        $this->tree = $tree;
53
        $this->user = $user;
54
    }
55
56
    /**
57
     * Return the root individual for the reference tree and user.
58
     *
59
     * @return Individual|NULL
60
     */
61
    public function rootIndividual(): ?Individual
62
    {
63
        $root_indi_id = $this->tree->getUserPreference($this->user, 'MAJ_SOSA_ROOT_ID');
64
        return Registry::individualFactory()->make($root_indi_id, $this->tree);
65
    }
66
67
    /**
68
     * Get the highest generation for the reference tree and user.
69
     *
70
     * @return int
71
     */
72
    public function maxGeneration(): int
73
    {
74
        return (int) DB::table('maj_sosa')
75
            ->where('majs_gedcom_id', '=', $this->tree->id())
76
            ->where('majs_user_id', '=', $this->user->id())
77
            ->max('majs_gen');
78
    }
79
80
    /**
81
     * Get the total count of individuals in the tree.
82
     *
83
     * @return int
84
     */
85
    public function totalIndividuals(): int
86
    {
87
        return DB::table('individuals')
88
            ->where('i_file', '=', $this->tree->id())
89
            ->count();
90
    }
91
92
    /**
93
     * Get the total count of Sosa ancestors for all generations
94
     *
95
     * @return int
96
     */
97
    public function totalAncestors(): int
98
    {
99
        return DB::table('maj_sosa')
100
            ->where('majs_gedcom_id', '=', $this->tree->id())
101
            ->where('majs_user_id', '=', $this->user->id())
102
            ->count();
103
    }
104
105
    /**
106
     * Get the total count of Sosa ancestors for a generation
107
     *
108
     * @return int
109
     */
110
    public function totalAncestorsAtGeneration(int $gen): int
111
    {
112
        return DB::table('maj_sosa')
113
            ->where('majs_gedcom_id', '=', $this->tree->id())
114
            ->where('majs_user_id', '=', $this->user->id())
115
            ->where('majs_gen', '=', $gen)
116
            ->count();
117
    }
118
119
    /**
120
     * Get the total count of distinct Sosa ancestors for all generations
121
     *
122
     * @return int
123
     */
124
    public function totalDistinctAncestors(): int
125
    {
126
        return DB::table('maj_sosa')
127
            ->where('majs_gedcom_id', '=', $this->tree->id())
128
            ->where('majs_user_id', '=', $this->user->id())
129
            ->distinct()
130
            ->count('majs_i_id');
131
    }
132
133
    /**
134
     * Get the mean generation time, as the slope of the linear regression of birth years vs generations
135
     *
136
     * @return float
137
     */
138
    public function meanGenerationTime(): float
139
    {
140
        $row = DB::table('maj_sosa')
141
            ->where('majs_gedcom_id', '=', $this->tree->id())
142
            ->where('majs_user_id', '=', $this->user->id())
143
            ->whereNotNull('majs_birth_year')
144
            ->selectRaw('COUNT(majs_sosa) AS n')
145
            ->selectRaw('SUM(majs_gen * majs_birth_year) AS sum_xy')
146
            ->selectRaw('SUM(majs_gen) AS sum_x')
147
            ->selectRaw('SUM(majs_birth_year) AS sum_y')
148
            ->selectRaw('SUM(majs_gen * majs_gen) AS sum_x2')
149
            ->get()->first();
150
151
        return $row->n == 0 ? 0 :
152
            -($row->n * $row->sum_xy - $row->sum_x * $row->sum_y) / ($row->n * $row->sum_x2 - pow($row->sum_x, 2));
153
    }
154
155
    /**
156
     * Get the statistic array detailed by generation.
157
     * Statistics for each generation are:
158
     *  - The number of Sosa in generation
159
     *  - The number of Sosa up to generation
160
     *  - The number of distinct Sosa up to generation
161
     *  - The year of the first birth in generation
162
     *  - The year of the first estimated birth in generation
163
     *  - The year of the last birth in generation
164
     *  - The year of the last estimated birth in generation
165
     *  - The average year of birth in generation
166
     *
167
     * @return array<int, array<string, int|null>> Statistics array
168
     */
169
    public function statisticsByGenerations(): array
170
    {
171
        $stats_by_gen = $this->statisticsByGenerationBasicData();
172
        $cumul_stats_by_gen = $this->statisticsByGenerationCumulativeData();
173
174
        $statistics_by_gen = [];
175
        foreach ($stats_by_gen as $gen => $stats_gen) {
176
            $statistics_by_gen[(int) $stats_gen->gen] = array(
177
                'sosaCount'             =>  (int) $stats_gen->total_sosa,
178
                'sosaTotalCount'        =>  (int) $cumul_stats_by_gen[$gen]->total_cumul,
179
                'diffSosaTotalCount'    =>  (int) $cumul_stats_by_gen[$gen]->total_distinct_cumul,
180
                'firstBirth'            =>  $stats_gen->first_year,
181
                'firstEstimatedBirth'   =>  $stats_gen->first_est_year,
182
                'lastBirth'             =>  $stats_gen->last_year,
183
                'lastEstimatedBirth'    =>  $stats_gen->last_est_year
184
            );
185
        }
186
187
        return $statistics_by_gen;
188
    }
189
190
    /**
191
     * Returns the basic statistics data by generation.
192
     *
193
     * @return Collection
194
     */
195
    private function statisticsByGenerationBasicData(): Collection
196
    {
197
        return DB::table('maj_sosa')
198
            ->where('majs_gedcom_id', '=', $this->tree->id())
199
            ->where('majs_user_id', '=', $this->user->id())
200
            ->groupBy('majs_gen')
201
            ->orderBy('majs_gen', 'asc')
202
            ->select('majs_gen AS gen')
203
            ->selectRaw('COUNT(majs_sosa) AS total_sosa')
204
            ->selectRaw('MIN(majs_birth_year) AS first_year')
205
            ->selectRaw('MIN(majs_birth_year_est) AS first_est_year')
206
            ->selectRaw('MAX(majs_birth_year) AS last_year')
207
            ->selectRaw('MAX(majs_birth_year_est) AS last_est_year')
208
            ->get()->keyBy('gen');
209
    }
210
211
    /**
212
     * Returns the cumulative statistics data by generation
213
     *
214
     * @return Collection
215
     */
216
    private function statisticsByGenerationCumulativeData(): Collection
217
    {
218
        $list_gen = DB::table('maj_sosa')->select('majs_gen')->distinct()
219
            ->where('majs_gedcom_id', '=', $this->tree->id())
220
            ->where('majs_user_id', '=', $this->user->id());
221
222
        return DB::table('maj_sosa')
223
            ->joinSub($list_gen, 'list_gen', function (JoinClause $join): void {
224
                $join->on('maj_sosa.majs_gen', '<=', 'list_gen.majs_gen')
225
                ->where('majs_gedcom_id', '=', $this->tree->id())
226
                ->where('majs_user_id', '=', $this->user->id());
227
            })
228
            ->groupBy('list_gen.majs_gen')
229
            ->select('list_gen.majs_gen AS gen')
230
            ->selectRaw('COUNT(majs_i_id) AS total_cumul')
231
            ->selectRaw('COUNT(DISTINCT majs_i_id) AS total_distinct_cumul')
232
            ->selectRaw('1 - COUNT(DISTINCT majs_i_id) / COUNT(majs_i_id) AS pedi_collapse_simple')
233
            ->get()->keyBy('gen');
234
    }
235
236
    /**
237
     * Returns the pedigree collapse improved calculation by generation.
238
     *
239
     * Format:
240
     *  - key : generation
241
     *  - values:
242
     *      - pedi_collapse_roots : pedigree collapse of ancestor roots for the generation
243
     *      - pedi_collapse_xgen : pedigree cross-generation shrinkage for the generation
244
     *
245
     * @return array<int, array<string, float>>
246
     */
247
    public function pedigreeCollapseByGenerationData(): array
248
    {
249
        $table_prefix = DB::connection()->getTablePrefix();
250
251
        $list_gen = DB::table('maj_sosa')->select('majs_gen')->distinct()
252
            ->where('majs_gedcom_id', '=', $this->tree->id())
253
            ->where('majs_user_id', '=', $this->user->id());
254
255
        /* Compute the contributions of nodes of previous generations to the current generation */
256
        $root_ancestors_contributions = DB::table('maj_sosa AS sosa')
257
            ->select(['list_gen.majs_gen AS gen', 'sosa.majs_gedcom_id', 'sosa.majs_user_id'])
258
            ->addSelect(['sosa.majs_i_id', 'sosa.majs_gen'])
259
            ->selectRaw(
260
                '(CASE ' .
261
                    ' WHEN ' . $table_prefix . 'sosa_fat.majs_i_id IS NULL' .
262
                    ' THEN POWER(2, ' . $table_prefix . 'list_gen.majs_gen - ' . $table_prefix . 'sosa.majs_gen - 1)' .
263
                    ' ELSE 0 ' .
264
                ' END)' .
265
                ' + (CASE ' .
266
                    ' WHEN ' . $table_prefix . 'sosa_mot.majs_i_id IS NULL' .
267
                    ' THEN POWER(2, ' . $table_prefix . 'list_gen.majs_gen - ' . $table_prefix . 'sosa.majs_gen - 1)' .
268
                    ' ELSE 0 ' .
269
                ' END) contrib'
270
            )
271
            ->joinSub($list_gen, 'list_gen', function (JoinClause $join): void {
272
                $join->on('sosa.majs_gen', '<', 'list_gen.majs_gen')
273
                    ->where('majs_gedcom_id', '=', $this->tree->id())
274
                    ->where('majs_user_id', '=', $this->user->id());
275
            })
276
            ->leftJoin('maj_sosa AS sosa_fat', function (JoinClause $join) use ($table_prefix): void {
277
                // Link to sosa's father
278
                $join->whereRaw($table_prefix . 'sosa_fat.majs_sosa = 2 * ' . $table_prefix . 'sosa.majs_sosa')
279
                    ->where('sosa_fat.majs_gedcom_id', '=', $this->tree->id())
280
                    ->where('sosa_fat.majs_user_id', '=', $this->user->id());
281
            })
282
            ->leftJoin('maj_sosa AS sosa_mot', function (JoinClause $join) use ($table_prefix): void {
283
                // Link to sosa's mother
284
                $join->whereRaw($table_prefix . 'sosa_mot.majs_sosa = 2 * ' . $table_prefix . 'sosa.majs_sosa + 1')
285
                    ->where('sosa_mot.majs_gedcom_id', '=', $this->tree->id())
286
                    ->where('sosa_mot.majs_user_id', '=', $this->user->id());
287
            })
288
            ->where('sosa.majs_gedcom_id', '=', $this->tree->id())
289
            ->where('sosa.majs_user_id', '=', $this->user->id())
290
            ->where(function (Builder $query): void {
291
                $query->whereNull('sosa_fat.majs_i_id')
292
                ->orWhereNull('sosa_mot.majs_i_id');
293
            });
294
295
        /* Identify nodes in the generations with ancestors who are also in the same generation.
296
         * This is the vertical/generational collapse that will reduce the number or roots.
297
         */
298
        $non_roots_ancestors = DB::table('maj_sosa AS sosa')
299
            ->select(['sosa.majs_gen', 'sosa.majs_gedcom_id', 'sosa.majs_user_id', 'sosa.majs_sosa'])
300
            ->selectRaw('MAX(' . $table_prefix . 'sosa_anc.majs_sosa) - MIN(' . $table_prefix . 'sosa_anc.majs_sosa)' .
301
                ' AS full_ancestors')
302
            ->join('maj_sosa AS sosa_anc', function (JoinClause $join) use ($table_prefix): void {
303
                $join->on('sosa.majs_gen', '<', 'sosa_anc.majs_gen')
304
                    ->whereRaw('FLOOR(' . $table_prefix . 'sosa_anc.majs_sosa / POWER(2, ' .
305
                        $table_prefix . 'sosa_anc.majs_gen - ' . $table_prefix . 'sosa.majs_gen)) = ' .
306
                        $table_prefix . 'sosa.majs_sosa')
307
                    ->where('sosa_anc.majs_gedcom_id', '=', $this->tree->id())
308
                    ->where('sosa_anc.majs_user_id', '=', $this->user->id());
309
            })
310
            ->where('sosa.majs_gedcom_id', '=', $this->tree->id())
311
            ->where('sosa.majs_user_id', '=', $this->user->id())
312
            ->whereIn('sosa_anc.majs_i_id', function (Builder $query) use ($table_prefix): void {
313
                $query->from('maj_sosa AS sosa_gen')
314
                ->select('sosa_gen.majs_i_id')->distinct()
315
                ->where('sosa_gen.majs_gedcom_id', '=', $this->tree->id())
316
                ->where('sosa_gen.majs_user_id', '=', $this->user->id())
317
                ->whereRaw($table_prefix . 'sosa_gen.majs_gen = ' . $table_prefix . 'sosa.majs_gen');
318
            })
319
            ->groupBy(['sosa.majs_gen', 'sosa.majs_gedcom_id', 'sosa.majs_user_id',
320
                'sosa.majs_sosa', 'sosa.majs_i_id']);
321
322
        /* Compute the contribution of the nodes in the generation,
323
         * excluding the nodes with ancestors in the same generation.
324
         * Nodes with a parent missing are not excluded to cater for the missing one.
325
         */
326
        $known_ancestors_contributions = DB::table('maj_sosa AS sosa')
327
            ->select(['sosa.majs_gen AS gen', 'sosa.majs_gedcom_id', 'sosa.majs_user_id'])
328
            ->addSelect(['sosa.majs_i_id', 'sosa.majs_gen'])
329
            ->selectRaw('1 AS contrib')
330
            ->leftJoinSub($non_roots_ancestors, 'nonroot', function (JoinClause $join): void {
331
                $join->on('sosa.majs_gen', '=', 'nonroot.majs_gen')
332
                    ->on('sosa.majs_sosa', '=', 'nonroot.majs_sosa')
333
                    ->where('nonroot.full_ancestors', '>', 0)
334
                    ->where('nonroot.majs_gedcom_id', '=', $this->tree->id())
335
                    ->where('nonroot.majs_user_id', '=', $this->user->id());
336
            })
337
            ->where('sosa.majs_gedcom_id', '=', $this->tree->id())
338
            ->where('sosa.majs_user_id', '=', $this->user->id())
339
            ->whereNull('nonroot.majs_sosa');
340
341
        /* Aggregate both queries, and calculate the sum of contributions by generation roots.
342
         * Exclude as well nodes that already appear in lower generations, as their branche has already been reduced.
343
         */
344
        $ancestors_contributions_sum = DB::connection()->query()
345
            ->fromSub($root_ancestors_contributions->unionAll($known_ancestors_contributions), 'sosa_contribs')
346
            ->select(['sosa_contribs.gen', 'sosa_contribs.majs_gedcom_id', 'sosa_contribs.majs_user_id'])
347
            ->addSelect(['sosa_contribs.majs_i_id', 'sosa_contribs.contrib'])
348
            ->selectRaw('COUNT(' . $table_prefix . 'sosa_contribs.majs_i_id) * ' .
349
                $table_prefix . 'sosa_contribs.contrib AS totalContrib')
350
            ->leftJoin('maj_sosa AS sosa_low', function (JoinClause $join): void {
351
                $join->on('sosa_low.majs_gen', '<', 'sosa_contribs.majs_gen')
352
                    ->on('sosa_low.majs_i_id', '=', 'sosa_contribs.majs_i_id')
353
                    ->where('sosa_low.majs_gedcom_id', '=', $this->tree->id())
354
                    ->where('sosa_low.majs_user_id', '=', $this->user->id());
355
            })
356
            ->whereNull('sosa_low.majs_sosa')
357
            ->groupBy(['sosa_contribs.gen', 'sosa_contribs.majs_gedcom_id', 'sosa_contribs.majs_user_id',
358
                'sosa_contribs.majs_i_id', 'sosa_contribs.contrib']);
359
360
        // Aggregate all generation roots to compute root and generation pedigree collapse
361
        $pedi_collapse_coll = DB::connection()->query()->fromSub($ancestors_contributions_sum, 'sosa_contribs_sum')
362
            ->select(['gen'])->selectRaw('SUM(contrib), SUM(totalContrib)')
363
            ->selectRaw('1 - SUM(contrib) / SUM(totalContrib) AS pedi_collapse_roots')  // Roots/horizontal collapse
364
            ->selectRaw('1 - SUM(totalContrib) / POWER ( 2, gen - 1) AS pedi_collapse_xgen') // Crossgeneration collapse
365
            ->groupBy(['gen', 'majs_gedcom_id', 'majs_user_id'])
366
            ->orderBy('gen')
367
            ->get();
368
369
        $pedi_collapse_by_gen = [];
370
        foreach ($pedi_collapse_coll as $collapse_gen) {
371
            $pedi_collapse_by_gen[(int) $collapse_gen->gen] = array(
372
                'pedi_collapse_roots'   =>  (float) $collapse_gen->pedi_collapse_roots,
373
                'pedi_collapse_xgen'   =>  (float) $collapse_gen->pedi_collapse_xgen
374
            );
375
        }
376
        return $pedi_collapse_by_gen;
377
    }
378
379
    /**
380
     * Return a Collection of the mean generation depth and deviation for all Sosa ancestors at a given generation.
381
     * Sosa 1 is of generation 1.
382
     *
383
     * Mean generation depth and deviation are calculated based on the works of Marie-Héléne Cazes and Pierre Cazes,
384
     * published in Population (French Edition), Vol. 51, No. 1 (Jan. - Feb., 1996), pp. 117-140
385
     * http://kintip.net/index.php?option=com_jdownloads&task=download.send&id=9&catid=4&m=0
386
     *
387
     * Format:
388
     *  - key : sosa number of the ancestor
389
     *  - values:
390
     *      - root_ancestor_id : ID of the ancestor
391
     *      - mean_gen_depth : Mean generation depth
392
     *      - stddev_gen_depth : Standard deviation of generation depth
393
     *
394
     * @param int $gen Sosa generation
395
     * @return Collection
396
     */
397
    public function generationDepthStatsAtGeneration(int $gen): Collection
398
    {
399
        $table_prefix = DB::connection()->getTablePrefix();
400
        $missing_ancestors_by_gen = DB::table('maj_sosa AS sosa')
401
            ->selectRaw($table_prefix . 'sosa.majs_gen - ? AS majs_gen_norm', [$gen])
402
            ->selectRaw('FLOOR(((' . $table_prefix . 'sosa.majs_sosa / POW(2, ' . $table_prefix . 'sosa.majs_gen -1 )) - 1) * POWER(2, ? - 1)) + POWER(2, ? - 1) AS root_ancestor', [$gen, $gen])   //@phpcs:ignore Generic.Files.LineLength.TooLong
403
            ->selectRaw('SUM(CASE WHEN ' . $table_prefix . 'sosa_fat.majs_i_id IS NULL AND ' . $table_prefix . 'sosa_mot.majs_i_id IS NULL THEN 1 ELSE 0 END) AS full_root_count')  //@phpcs:ignore Generic.Files.LineLength.TooLong
404
            ->selectRaw('SUM(CASE WHEN ' . $table_prefix . 'sosa_fat.majs_i_id IS NULL AND ' . $table_prefix . 'sosa_mot.majs_i_id IS NULL THEN 0 ELSE 1 END) As semi_root_count')  //@phpcs:ignore Generic.Files.LineLength.TooLong
405
            ->leftJoin('maj_sosa AS sosa_fat', function (JoinClause $join) use ($table_prefix): void {
406
                // Link to sosa's father
407
                $join->whereRaw($table_prefix . 'sosa_fat.majs_sosa = 2 * ' . $table_prefix . 'sosa.majs_sosa')
408
                ->where('sosa_fat.majs_gedcom_id', '=', $this->tree->id())
409
                ->where('sosa_fat.majs_user_id', '=', $this->user->id());
410
            })
411
            ->leftJoin('maj_sosa AS sosa_mot', function (JoinClause $join) use ($table_prefix): void {
412
                // Link to sosa's mother
413
                $join->whereRaw($table_prefix . 'sosa_mot.majs_sosa = 2 * ' . $table_prefix . 'sosa.majs_sosa + 1')
414
                ->where('sosa_mot.majs_gedcom_id', '=', $this->tree->id())
415
                ->where('sosa_mot.majs_user_id', '=', $this->user->id());
416
            })
417
            ->where('sosa.majs_gedcom_id', '=', $this->tree->id())
418
            ->where('sosa.majs_user_id', '=', $this->user->id())
419
            ->where('sosa.majs_gen', '>=', $gen)
420
            ->where(function (Builder $query): void {
421
                $query->whereNull('sosa_fat.majs_i_id')
422
                    ->orWhereNull('sosa_mot.majs_i_id');
423
            })
424
            ->groupBy(['sosa.majs_gen', 'root_ancestor']);
425
426
        return DB::table('maj_sosa AS sosa_list')
427
            ->select(['stats_by_gen.root_ancestor AS root_ancestor_sosa', 'sosa_list.majs_i_id as root_ancestor_id'])
428
            ->selectRaw('1 + SUM( (majs_gen_norm) * ( 2 * full_root_count + semi_root_count) /  (2 * POWER(2, majs_gen_norm))) AS mean_gen_depth')  //@phpcs:ignore Generic.Files.LineLength.TooLong
429
            ->selectRaw(' SQRT(' .
430
                '   SUM(POWER(majs_gen_norm, 2) * ( 2 * full_root_count + semi_root_count) /  (2 * POWER(2, majs_gen_norm)))' .     //@phpcs:ignore Generic.Files.LineLength.TooLong
431
                '   - POWER( SUM( (majs_gen_norm) * ( 2 * full_root_count + semi_root_count) /  (2 * POWER(2, majs_gen_norm))), 2)' .       //@phpcs:ignore Generic.Files.LineLength.TooLong
432
                ' ) AS stddev_gen_depth')
433
            ->joinSub($missing_ancestors_by_gen, 'stats_by_gen', function (JoinClause $join): void {
434
                $join->on('sosa_list.majs_sosa', '=', 'stats_by_gen.root_ancestor')
435
                    ->where('sosa_list.majs_gedcom_id', '=', $this->tree->id())
436
                    ->where('sosa_list.majs_user_id', '=', $this->user->id());
437
            })
438
            ->groupBy(['stats_by_gen.root_ancestor', 'sosa_list.majs_i_id'])
439
            ->orderBy('stats_by_gen.root_ancestor')
440
            ->get()->keyBy('root_ancestor_sosa');
441
    }
442
443
    /**
444
     * Return a collection of the most duplicated root Sosa ancestors.
445
     * The number of ancestors to return is limited by the parameter $limit.
446
     * If several individuals are tied when reaching the limit, none of them are returned,
447
     * which means that there can be less individuals returned than requested.
448
     *
449
     * Format:
450
     *  - value:
451
     *      - sosa_i_id : sosa individual
452
     *      - sosa_count: number of duplications of the ancestor (e.g. 3 if it appears 3 times)
453
     *
454
     * @param int $limit
455
     * @return Collection
456
     */
457
    public function topMultipleAncestorsWithNoTies(int $limit): Collection
458
    {
459
        $table_prefix = DB::connection()->getTablePrefix();
460
        $multiple_ancestors = DB::table('maj_sosa AS sosa')
461
            ->select('sosa.majs_i_id AS sosa_i_id')
462
            ->selectRaw('COUNT(' . $table_prefix . 'sosa.majs_sosa) AS sosa_count')
463
            ->leftJoin('maj_sosa AS sosa_fat', function (JoinClause $join) use ($table_prefix): void {
464
                // Link to sosa's father
465
                $join->whereRaw($table_prefix . 'sosa_fat.majs_sosa = 2 * ' . $table_prefix . 'sosa.majs_sosa')
466
                    ->where('sosa_fat.majs_gedcom_id', '=', $this->tree->id())
467
                    ->where('sosa_fat.majs_user_id', '=', $this->user->id());
468
            })
469
            ->leftJoin('maj_sosa AS sosa_mot', function (JoinClause $join) use ($table_prefix): void {
470
                // Link to sosa's mother
471
                $join->whereRaw($table_prefix . 'sosa_mot.majs_sosa = 2 * ' . $table_prefix . 'sosa.majs_sosa + 1')
472
                ->where('sosa_mot.majs_gedcom_id', '=', $this->tree->id())
473
                ->where('sosa_mot.majs_user_id', '=', $this->user->id());
474
            })
475
            ->where('sosa.majs_gedcom_id', '=', $this->tree->id())
476
            ->where('sosa.majs_user_id', '=', $this->user->id())
477
            ->whereNull('sosa_fat.majs_sosa')   // We keep only root individuals, i.e. those with no father or mother
478
            ->whereNull('sosa_mot.majs_sosa')
479
            ->groupBy('sosa.majs_i_id')
480
            ->havingRaw('COUNT(' . $table_prefix . 'sosa.majs_sosa) > 1')    // Limit to the duplicate sosas.
481
            ->orderByRaw('COUNT(' . $table_prefix . 'sosa.majs_sosa) DESC, MIN(' . $table_prefix . 'sosa.majs_sosa) ASC')   //@phpcs:ignore Generic.Files.LineLength.TooLong
482
            ->limit($limit + 1)     // We want to select one more than required, for ties
483
            ->get();
484
485
        if ($multiple_ancestors->count() > $limit) {
486
            $last_count = $multiple_ancestors->last()->sosa_count;
487
            $multiple_ancestors = $multiple_ancestors->reject(function ($element) use ($last_count): bool {
488
                return $element->sosa_count ==  $last_count;
489
            });
490
        }
491
        return $multiple_ancestors;
492
    }
493
494
    /**
495
     * Return a computed array of statistics about the dispersion of ancestors across the ancestors
496
     * at a specified generation.
497
     *
498
     * Format:
499
     *  - key : rank of the ancestor in generation G for which exclusive ancestors have been found
500
     *          For instance 3 represent the maternal grand father
501
     *          0 is used for shared ancestors
502
     *  - values: number of ancestors exclusively in the ancestors of the ancestor in key
503
     *
504
     *  For instance a result at generation 3 could be :
505
     *      array (   0     =>  12      -> 12 ancestors are shared by the grand-parents
506
     *                1     =>  32      -> 32 ancestors are exclusive to the paternal grand-father
507
     *                2     =>  25      -> 25 ancestors are exclusive to the paternal grand-mother
508
     *                3     =>  12      -> 12 ancestors are exclusive to the maternal grand-father
509
     *                4     =>  30      -> 30 ancestors are exclusive to the maternal grand-mother
510
     *            )
511
     *
512
     * @param int $gen
513
     * @return Collection
514
     */
515
    public function ancestorsDispersionForGeneration(int $gen): Collection
516
    {
517
        $ancestors_branches = DB::table('maj_sosa')
518
            ->select('majs_i_id AS i_id')
519
            ->selectRaw('FLOOR(majs_sosa / POW(2, (majs_gen - ?))) - POW(2, ? -1) + 1 AS branch', [$gen, $gen])
520
            ->where('majs_gedcom_id', '=', $this->tree->id())
521
            ->where('majs_user_id', '=', $this->user->id())
522
            ->where('majs_gen', '>=', $gen)
523
            ->groupBy('majs_i_id', 'branch');
524
525
526
        $consolidated_ancestors_branches = DB::table('maj_sosa')
527
            ->fromSub($ancestors_branches, 'indi_branch')
528
            ->select('i_id')
529
            ->selectRaw('CASE WHEN COUNT(branch) > 1 THEN 0 ELSE MIN(branch) END AS branches')
530
            ->groupBy('i_id');
531
532
        return DB::table('maj_sosa')
533
            ->fromSub($consolidated_ancestors_branches, 'indi_branch_consolidated')
534
            ->select('branches')
535
            ->selectRaw('COUNT(i_id) AS count_indi')
536
            ->groupBy('branches')
537
            ->get()->pluck('count_indi', 'branches');
538
    }
539
}
540