Skip to content

Commit 5a960cc

Browse files
authored
Fix max read1 used for read2 (#26)
1 parent 3e45ddf commit 5a960cc

File tree

9 files changed

+97
-24
lines changed

9 files changed

+97
-24
lines changed

CHANGELOG.md

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,17 @@ See [GitHub releases](https://github.com/mll-lab/php-utils/releases).
99

1010
## Unreleased
1111

12-
## v2.2.1
12+
## v3.0.0
1313

1414
### Changed
1515

16-
- All OverrideCycle total counts on DataSection must grow to the Reads-Sections maximum value by adding the N-tag
16+
- All `OverrideCycle` total counts on `DataSection` must grow to the `ReadsSection` maximum value by adding the N-tag
17+
- Throw when trying to use an empty `DataSection`
18+
- Breaking Change: class `OverrideCycles` requires class `DataSection` to calculate the max cycles
19+
20+
### Fixed
21+
22+
- Use method `maxRead2Cycles` for calculating `$fillUpToMax` for `read2`, not `maxRead1Cycles`
1723

1824
## v2.2.0
1925

src/IlluminaSampleSheet/V2/BclConvert/DataSection.php

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace MLL\Utils\IlluminaSampleSheet\V2\BclConvert;
44

55
use Illuminate\Support\Collection;
6+
use MLL\Utils\IlluminaSampleSheet\IlluminaSampleSheetException;
67
use MLL\Utils\IlluminaSampleSheet\Section;
78
use MLL\Utils\IlluminaSampleSheet\V2\ReadsSection;
89

@@ -23,12 +24,14 @@ public function addSample(BclSample $bclSample): void
2324

2425
public function convertSectionToString(): string
2526
{
27+
$this->assertNotEmpty();
28+
2629
/** @var array<string> $samplePropertiesOfFirstSample */
2730
$samplePropertiesOfFirstSample = array_keys(get_object_vars($this->dataRows[0]));
2831
foreach ($this->dataRows as $sample) {
2932
$actualProperties = array_keys(get_object_vars($sample));
3033
if ($samplePropertiesOfFirstSample !== $actualProperties) {
31-
throw new \Exception('All samples must have the same properties. Expected: ' . \Safe\json_encode($samplePropertiesOfFirstSample) . ', Actual: ' . \Safe\json_encode($actualProperties));
34+
throw new IlluminaSampleSheetException('All samples must have the same properties. Expected: ' . \Safe\json_encode($samplePropertiesOfFirstSample) . ', Actual: ' . \Safe\json_encode($actualProperties));
3235
}
3336
}
3437

@@ -114,4 +117,11 @@ public function maxIndex2Cycles(): ?int
114117

115118
return $index2Cycles;
116119
}
120+
121+
public function assertNotEmpty(): void
122+
{
123+
if ($this->dataRows->isEmpty()) {
124+
throw new IlluminaSampleSheetException('At least one sample must be added to the DataSection.');
125+
}
126+
}
117127
}

src/IlluminaSampleSheet/V2/BclConvert/OverrideCycle.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
namespace MLL\Utils\IlluminaSampleSheet\V2\BclConvert;
44

5+
use MLL\Utils\IlluminaSampleSheet\IlluminaSampleSheetException;
6+
57
class OverrideCycle
68
{
79
/** @var array<CycleTypeWithCount> */
@@ -17,8 +19,9 @@ public function __construct(array $cycles)
1719
public function toString(int $fillUpToMax, ?bool $isSecondIndexAndForwardDirection): string
1820
{
1921
$countOfAllCycleTypes = $this->sumCountOfAllCycles();
20-
assert($countOfAllCycleTypes <= $fillUpToMax, 'The sum of all cycle types must be less than or equal to the fill up to max value.');
21-
22+
if ($countOfAllCycleTypes > $fillUpToMax) {
23+
throw new IlluminaSampleSheetException("The sum of all cycle types must be less than or equal to the fill up to max value. \$countOfAllCycleTypes: {$countOfAllCycleTypes} > \$fillUpToMax: {$fillUpToMax}");
24+
}
2225
$rawOverrideCycle = implode('', array_map(
2326
fn (CycleTypeWithCount $cycle): string => $cycle->toString(),
2427
$this->cycles

src/IlluminaSampleSheet/V2/BclConvert/OverrideCycles.php

Lines changed: 29 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -29,25 +29,13 @@ public function __construct(DataSection $dataSection, string $read1, string $ind
2929
public function toString(): string
3030
{
3131
$dataSection = $this->dataSection;
32-
33-
if ($this->index2 instanceof OverrideCycle) {
34-
$maxIndex2Cycles = $dataSection->maxIndex2Cycles();
35-
if ($maxIndex2Cycles === null) {
36-
throw new IlluminaSampleSheetException('MaxIndex2Cycles is required when Index2 is set.');
37-
}
38-
39-
$index2 = $this->index2->toString($maxIndex2Cycles, HeaderSection::isForwardIndexOrientation());
40-
} else {
41-
$index2 = null;
42-
}
32+
$dataSection->assertNotEmpty();
4333

4434
return implode(';', array_filter([
4535
$this->read1->toString($dataSection->maxRead1Cycles(), null),
4636
$this->index1->toString($dataSection->maxIndex1Cycles(), null),
47-
$index2,
48-
$this->read2 instanceof OverrideCycle
49-
? $this->read2->toString($dataSection->maxRead1Cycles(), null)
50-
: null,
37+
$this->index2(),
38+
$this->read2(),
5139
]));
5240
}
5341

@@ -70,4 +58,30 @@ public function makeOverrideCycle(string $cycleString): OverrideCycle
7058
)
7159
);
7260
}
61+
62+
private function index2(): ?string
63+
{
64+
if (! $this->index2 instanceof OverrideCycle) {
65+
return null;
66+
}
67+
$maxIndex2Cycles = $this->dataSection->maxIndex2Cycles();
68+
if ($maxIndex2Cycles === null) {
69+
throw new IlluminaSampleSheetException('MaxIndex2Cycles is required when Index2 is set.');
70+
}
71+
72+
return $this->index2->toString($maxIndex2Cycles, HeaderSection::isForwardIndexOrientation());
73+
}
74+
75+
private function read2(): ?string
76+
{
77+
if (! $this->read2 instanceof OverrideCycle) {
78+
return null;
79+
}
80+
$maxIndex2Cycles = $this->dataSection->maxRead2Cycles();
81+
if ($maxIndex2Cycles === null) {
82+
throw new IlluminaSampleSheetException('MaxRead2Cycles is required when Read2 is set.');
83+
}
84+
85+
return $this->read2->toString($maxIndex2Cycles, null);
86+
}
7387
}

tests/DnaSequenceTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
use MLL\Utils\DnaSequence;
66
use PHPUnit\Framework\TestCase;
77

8-
class DnaSequenceTest extends TestCase
8+
final class DnaSequenceTest extends TestCase
99
{
1010
public function testReverse(): void
1111
{

tests/IlluminaSampleSheet/V1/DataSectionTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
use MLL\Utils\IlluminaSampleSheet\V1\RowForDualIndexWithoutLane;
99
use PHPUnit\Framework\TestCase;
1010

11-
class DataSectionTest extends TestCase
11+
final class DataSectionTest extends TestCase
1212
{
1313
public function testDataSectionWithDualIndexWithLaneWorksNotWhenRowWithWrongTypeIsAdded(): void
1414
{

tests/IlluminaSampleSheet/V1/DualIndexTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
use PHPUnit\Framework\Attributes\DataProvider;
88
use PHPUnit\Framework\TestCase;
99

10-
class DualIndexTest extends TestCase
10+
final class DualIndexTest extends TestCase
1111
{
1212
/** @dataProvider provideValidDualIndexes */
1313
#[DataProvider('provideValidDualIndexes')]

tests/IlluminaSampleSheet/V1/SampleSheetTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
use MLL\Utils\IlluminaSampleSheet\V1\SettingsSection;
1414
use PHPUnit\Framework\TestCase;
1515

16-
class SampleSheetTest extends TestCase
16+
final class SampleSheetTest extends TestCase
1717
{
1818
public function testDataSectionWithDualIndexWithLaneShouldReturnExpectedResult(): void
1919
{
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace MLL\Utils\Tests\IlluminaSampleSheet\V2;
4+
5+
use MLL\Utils\IlluminaSampleSheet\IlluminaSampleSheetException;
6+
use MLL\Utils\IlluminaSampleSheet\V2\BclConvert\BclSample;
7+
use MLL\Utils\IlluminaSampleSheet\V2\BclConvert\DataSection;
8+
use MLL\Utils\IlluminaSampleSheet\V2\BclConvert\OverrideCycles;
9+
use PHPUnit\Framework\TestCase;
10+
11+
final class DataSectionTest extends TestCase
12+
{
13+
public function testToString(): void
14+
{
15+
$dataSection = new DataSection();
16+
$overrideCycles = new OverrideCycles($dataSection, 'Y130', 'I8', 'I10', 'Y100');
17+
$dataSection->addSample(new BclSample(100, 'Sample1', 'Index1', $overrideCycles));
18+
19+
$overrideCycles = new OverrideCycles($dataSection, 'Y100', 'I11', 'I7', 'Y151');
20+
$dataSection->addSample(new BclSample(101, 'Sample2', 'Index3', $overrideCycles));
21+
22+
$expected = <<<'CSV'
23+
[BCLConvert_Data]
24+
Lane,Sample_ID,Index,OverrideCycles
25+
100,Sample1,Index1,Y130;I8N3;I10;Y100N51
26+
101,Sample2,Index3,Y100N30;I11;N3I7;Y151
27+
28+
CSV;
29+
self::assertSame($expected, $dataSection->convertSectionToString());
30+
}
31+
32+
public function testThrowsExceptionIfDataSectionIsEmpty(): void
33+
{
34+
$dataSection = new DataSection();
35+
36+
$this->expectException(IlluminaSampleSheetException::class);
37+
$this->expectExceptionMessage('At least one sample must be added to the DataSection.');
38+
$dataSection->convertSectionToString();
39+
}
40+
}

0 commit comments

Comments
 (0)