diff --git a/src/Chromosome.php b/src/Chromosome.php index 432add0..33dc549 100644 --- a/src/Chromosome.php +++ b/src/Chromosome.php @@ -14,12 +14,15 @@ class Chromosome public function __construct(string $value) { /** Matches human chromosomes with or without "chr" prefix: chr1-chr22, chrX, chrY, chrM, chrMT, or 1-22, X, Y, M, MT. */ - if (preg_match('/^(chr)?(1[0-9]|[1-9]|2[0-2]|X|Y|M|MT)$/i', $value, $matches) === 0) { + if (preg_match('/^(?:chr)?(1[0-9]|[1-9]|2[0-2]|X|Y|M|MT)$/i', $value, $matches) === 0) { throw new \InvalidArgumentException("Invalid chromosome: {$value}. Expected format: chr1-chr22, chrX, chrY, chrM, or without chr prefix."); } - $value = strtoupper($matches[2]); - $this->value = $value === self::MITOCHONDRIAL_ENSEMBL ? self::MITOCHONDRIAL : $value; + assert(isset($matches[1])); + $value = strtoupper($matches[1]); + $this->value = $value === self::MITOCHONDRIAL_ENSEMBL + ? self::MITOCHONDRIAL + : $value; } public function value(): string diff --git a/src/GenomicPosition.php b/src/GenomicPosition.php index bdf4eb3..f66e061 100644 --- a/src/GenomicPosition.php +++ b/src/GenomicPosition.php @@ -23,11 +23,16 @@ public function __construct(Chromosome $chromosome, int $position) /** @example GenomicPosition::parse('chr1:123456') */ public static function parse(string $value): self { - if (preg_match('/^([^:]+):(g\.|)(\d+)$/', $value, $matches) === 0) { + if (preg_match('/^([^:]+):(?:g\.)?(\d+)$/', $value, $matches) === 0) { throw new \InvalidArgumentException("Invalid genomic position format: {$value}. Expected format: chr1:123456."); } - return new self(new Chromosome($matches[1]), (int) $matches[3]); + assert(isset($matches[1], $matches[2])); + + return new self( + new Chromosome($matches[1]), + SafeCast::toInt($matches[2]) + ); } public function equals(self $other): bool diff --git a/src/GenomicRegion.php b/src/GenomicRegion.php index 3acf3a1..793897e 100644 --- a/src/GenomicRegion.php +++ b/src/GenomicRegion.php @@ -36,14 +36,16 @@ public function __construct( public static function parse(string $value): self { - if (preg_match('/^([^:]+):(g\.|)(\d+)(-(\d+)|)$/', $value, $matches) === 0) { + if (preg_match('/^([^:]+):(?:g\.)?(\d+)(?:-(\d+))?$/', $value, $matches) === 0) { throw new \InvalidArgumentException("Invalid genomic region format: {$value}. Expected format: chr1:123-456."); } + assert(isset($matches[1], $matches[2])); + return new self( new Chromosome($matches[1]), - (int) $matches[3], - (int) ($matches[5] ?? $matches[3]) + SafeCast::toInt($matches[2]), + SafeCast::toInt($matches[3] ?? $matches[2]) ); } diff --git a/src/IlluminaRunFolder.php b/src/IlluminaRunFolder.php index b371c9f..9428e34 100644 --- a/src/IlluminaRunFolder.php +++ b/src/IlluminaRunFolder.php @@ -94,10 +94,14 @@ public static function parse(string $runFolder): self private static function extractFlowcellID(string $flowcellSegment): string { if (preg_match(self::ZERO_PREFIXED_PATTERN, $flowcellSegment, $matches) === 1) { + assert(isset($matches[1])); + return $matches[1]; } if (preg_match(self::SIDE_PREFIXED_PATTERN, $flowcellSegment, $matches) === 1) { + assert(isset($matches[1])); + return $matches[1]; }