Skip to content
Open
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
9 changes: 6 additions & 3 deletions src/Chromosome.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
9 changes: 7 additions & 2 deletions src/GenomicPosition.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 5 additions & 3 deletions src/GenomicRegion.php
Original file line number Diff line number Diff line change
Expand Up @@ -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])
);
}

Expand Down
4 changes: 4 additions & 0 deletions src/IlluminaRunFolder.php
Original file line number Diff line number Diff line change
Expand Up @@ -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];
}

Expand Down
Loading