Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 19 additions & 12 deletions src/IlluminaSampleSheet/V2/BclConvert/BclSample.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,20 +46,27 @@ public function __construct(
$this->barcodeMismatchesIndex2 = $barcodeMismatchesIndex2;
}

public function toString(OverrideCycleCounter $overrideCycleCounter): string
public function toString(OverrideCycleCounter $overrideCycleCounter, bool $includeBarcodeMismatchesIndex2): string
{
$lines = array_map(
fn (int $lane): string => implode(',', [
$lane,
$this->sampleID,
$this->indexRead1,
$this->indexRead2 ?? '',
$this->overrideCycles->toString($overrideCycleCounter),
$this->adapterRead1,
$this->adapterRead2,
$this->barcodeMismatchesIndex1,
$this->barcodeMismatchesIndex2 ?? '',
]),
function (int $lane) use ($overrideCycleCounter, $includeBarcodeMismatchesIndex2): string {
$fields = [
$lane,
$this->sampleID,
$this->indexRead1,
$this->indexRead2 ?? '',
$this->overrideCycles->toString($overrideCycleCounter),
$this->adapterRead1,
$this->adapterRead2,
$this->barcodeMismatchesIndex1,
];

if ($includeBarcodeMismatchesIndex2) {
$fields[] = $this->barcodeMismatchesIndex2 ?? '';
}

return implode(',', $fields);
},
$this->flowcellType->lanes
);

Expand Down
45 changes: 40 additions & 5 deletions src/IlluminaSampleSheet/V2/Sections/BclConvertDataSection.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,6 @@

class BclConvertDataSection implements Section
{
/** @var string */
public const HEADER_ROW = 'Lane,Sample_ID,Index,Index2,OverrideCycles,AdapterRead1,AdapterRead2,BarcodeMismatchesIndex1,BarcodeMismatchesIndex2';

/** @var Collection<int, BclSample> */
public Collection $bclSampleList;

Expand All @@ -31,14 +28,52 @@ public function __construct(Collection $bclSampleList)
public function convertSectionToString(): string
{
$this->assertNotEmpty();
$this->assertConsistentBarcodeMismatchesIndex2();

$includeBarcodeMismatchesIndex2 = $this->hasBarcodeMismatchesIndex2();

return
self::HEADER_ROW . PHP_EOL
self::headerRow($includeBarcodeMismatchesIndex2) . PHP_EOL
. $this->bclSampleList
->map(fn (BclSample $bclSample): string => $bclSample->toString($this->overrideCycleCounter))
->map(fn (BclSample $bclSample): string => $bclSample->toString($this->overrideCycleCounter, $includeBarcodeMismatchesIndex2))
->join(PHP_EOL) . PHP_EOL;
}

private function hasBarcodeMismatchesIndex2(): bool
{
return $this->bclSampleList->contains(fn (BclSample $bclSample): bool => $bclSample->barcodeMismatchesIndex2 !== null);
}

private function assertConsistentBarcodeMismatchesIndex2(): void
{
$withIndex2 = $this->bclSampleList->contains(fn (BclSample $bclSample): bool => $bclSample->barcodeMismatchesIndex2 !== null);
$withoutIndex2 = $this->bclSampleList->contains(fn (BclSample $bclSample): bool => $bclSample->barcodeMismatchesIndex2 === null);

if ($withIndex2 && $withoutIndex2) {
throw new IlluminaSampleSheetException('Either all or no samples must have a barcodeMismatchesIndex2.');
}
}

private static function headerRow(bool $includeBarcodeMismatchesIndex2): string
{
$columns = [
'Lane',
'Sample_ID',
'Index',
'Index2',
'OverrideCycles',
'AdapterRead1',
'AdapterRead2',
'BarcodeMismatchesIndex1',
];

if ($includeBarcodeMismatchesIndex2) {
$columns[] = 'BarcodeMismatchesIndex2';
}

return implode(',', $columns);
}

public function assertNotEmpty(): void
{
if ($this->bclSampleList->isEmpty()) {
Expand Down
130 changes: 130 additions & 0 deletions tests/IlluminaSampleSheet/V2/BclConvertDataSectionTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
<?php declare(strict_types=1);

namespace MLL\Utils\Tests\IlluminaSampleSheet\V2;

use Illuminate\Support\Collection;
use MLL\Utils\Flowcells\NovaSeqX1_5B;
use MLL\Utils\IlluminaSampleSheet\IlluminaSampleSheetException;
use MLL\Utils\IlluminaSampleSheet\V2\BclConvert\BclSample;
use MLL\Utils\IlluminaSampleSheet\V2\BclConvert\OverrideCycles;
use MLL\Utils\IlluminaSampleSheet\V2\IndexOrientation;
use MLL\Utils\IlluminaSampleSheet\V2\Sections\BclConvertDataSection;
use PHPUnit\Framework\TestCase;

final class BclConvertDataSectionTest extends TestCase
{
public function testOmitsBarcodeMismatchesIndex2ColumnWhenAllNull(): void
{
$indexOrientation = IndexOrientation::FORWARD();

$bclSample0 = new BclSample(
new NovaSeqX1_5B([1]),
'Sample1',
'Index1',
'Index2',
OverrideCycles::fromString('Y151;I8;I8;Y151', $indexOrientation),
'Adapter1',
'Adapter2',
'0',
null
);

$bclSample1 = new BclSample(
new NovaSeqX1_5B([2]),
'Sample2',
'Index3',
'Index4',
OverrideCycles::fromString('Y151;I8;I8;Y151', $indexOrientation),
'Adapter3',
'Adapter4',
'1',
null
);

$section = new BclConvertDataSection(new Collection([$bclSample0, $bclSample1]));
$result = $section->convertSectionToString();

$expected = <<<'CSV'
Lane,Sample_ID,Index,Index2,OverrideCycles,AdapterRead1,AdapterRead2,BarcodeMismatchesIndex1
1,Sample1,Index1,Index2,Y151;I8;I8;Y151,Adapter1,Adapter2,0
2,Sample2,Index3,Index4,Y151;I8;I8;Y151,Adapter3,Adapter4,1

CSV;
self::assertSame($expected, $result);
}

public function testIncludesBarcodeMismatchesIndex2ColumnWhenAllSet(): void
{
$indexOrientation = IndexOrientation::FORWARD();

$bclSample0 = new BclSample(
new NovaSeqX1_5B([1]),
'Sample1',
'Index1',
'Index2',
OverrideCycles::fromString('Y151;I8;I8;Y151', $indexOrientation),
'Adapter1',
'Adapter2',
'0',
'0'
);

$bclSample1 = new BclSample(
new NovaSeqX1_5B([2]),
'Sample2',
'Index3',
'Index4',
OverrideCycles::fromString('Y151;I8;I8;Y151', $indexOrientation),
'Adapter3',
'Adapter4',
'1',
'1'
);

$section = new BclConvertDataSection(new Collection([$bclSample0, $bclSample1]));
$result = $section->convertSectionToString();

$expected = <<<'CSV'
Lane,Sample_ID,Index,Index2,OverrideCycles,AdapterRead1,AdapterRead2,BarcodeMismatchesIndex1,BarcodeMismatchesIndex2
1,Sample1,Index1,Index2,Y151;I8;I8;Y151,Adapter1,Adapter2,0,0
2,Sample2,Index3,Index4,Y151;I8;I8;Y151,Adapter3,Adapter4,1,1

CSV;
self::assertSame($expected, $result);
}

public function testThrowsWhenBarcodeMismatchesIndex2IsInconsistent(): void
{
$indexOrientation = IndexOrientation::FORWARD();

$bclSampleWithIndex2 = new BclSample(
new NovaSeqX1_5B([1]),
'Sample1',
'Index1',
'Index2',
OverrideCycles::fromString('Y151;I8;I8;Y151', $indexOrientation),
'Adapter1',
'Adapter2',
'0',
'0'
);

$bclSampleWithoutIndex2 = new BclSample(
new NovaSeqX1_5B([2]),
'Sample2',
'Index3',
'Index4',
OverrideCycles::fromString('Y151;I8;I8;Y151', $indexOrientation),
'Adapter3',
'Adapter4',
'1',
null
);

$section = new BclConvertDataSection(new Collection([$bclSampleWithIndex2, $bclSampleWithoutIndex2]));

$this->expectException(IlluminaSampleSheetException::class);
$this->expectExceptionMessage('Either all or no samples must have a barcodeMismatchesIndex2.');
$section->convertSectionToString();
}
}
Loading