generated from spawnia/php-package-template
-
Notifications
You must be signed in to change notification settings - Fork 0
feat: Implement logic for Chromosome.php, GenomicPosition.php and Gen… #76
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
KingKong1213
wants to merge
9
commits into
master
Choose a base branch
from
chromosome-genomicPosition-genomicRegion
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+341
−0
Open
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
32264d8
feat: Implement logic for Chromosome.php, GenomicPosition.php and Gen…
dhaupt88 e0c815e
Apply php-cs-fixer changes
KingKong1213 f493c7b
Use NamingConvention.php instead of ReferenceGenome
dhaupt88 7ed2ca6
Apply php-cs-fixer changes
KingKong1213 bfb37cd
Fix Mitochondrial Chromosome
dhaupt88 731d8aa
Fix point in HGVSg
dhaupt88 900a024
Missspelling
dhaupt88 466c5e5
Bug Fix. Clarify differnces between containsGenomicRegion, intersects…
dhaupt88 d1a85a1
Add validation step for NamingConvention.php
dhaupt88 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,37 @@ | ||
| <?php declare(strict_types=1); | ||
|
|
||
| namespace MLL\Utils; | ||
|
|
||
| class Chromosome | ||
| { | ||
| private string $value; | ||
|
|
||
| private NamingConvention $namingConvention; | ||
|
|
||
| public function __construct(string $chromosomeAsString) | ||
| { | ||
| /** Matches human chromosomes with or without "chr" prefix: chr1-chr22, chrX, chrY, chrM, chrMT, or 1-22, X, Y, M, MT. */ | ||
| if (\Safe\preg_match('/^(chr)?(1[0-9]|[1-9]|2[0-2]|X|Y|M|MT)$/i', $chromosomeAsString, $matches) === 0) { | ||
| throw new \InvalidArgumentException("Invalid chromosome: {$chromosomeAsString}. Expected format: chr1-chr22, chrX, chrY, chrM, or without chr prefix."); | ||
| } | ||
| $this->namingConvention = $matches[1] === 'chr' | ||
| ? new NamingConvention(NamingConvention::UCSC) | ||
| : new NamingConvention(NamingConvention::ENSEMBL); | ||
|
|
||
| $this->value = strtoupper($matches[2]); | ||
| } | ||
|
|
||
| public function toString(?NamingConvention $namingConvention = null): string | ||
| { | ||
| $namingConvention ??= $this->namingConvention; | ||
|
|
||
| switch ($namingConvention->value) { | ||
| case NamingConvention::ENSEMBL: | ||
| return $this->value === 'M' ? 'MT' : $this->value; | ||
| case NamingConvention::UCSC: | ||
| return "chr{$this->value}"; | ||
| default: | ||
| throw new \InvalidArgumentException("No toString logic implemented for valid naming convention: {$namingConvention->value}"); | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,35 @@ | ||
| <?php declare(strict_types=1); | ||
|
|
||
| namespace MLL\Utils; | ||
|
|
||
| class GenomicPosition | ||
| { | ||
| public Chromosome $chromosome; | ||
|
|
||
| public int $position; | ||
|
|
||
| public function __construct(Chromosome $chromosome, int $position) | ||
| { | ||
| if ($position < 1) { | ||
| throw new \InvalidArgumentException("Position must be positive, got: {$position}."); | ||
| } | ||
|
|
||
| $this->chromosome = $chromosome; | ||
| $this->position = $position; | ||
| } | ||
|
|
||
| /** @example GenomicPosition::parse('chr1:123456') */ | ||
| public static function parse(string $genomicPosition): self | ||
| { | ||
| if (\Safe\preg_match('/^(.+):(g\.|)(\d+)$/', $genomicPosition, $matches) === 0) { | ||
| throw new \InvalidArgumentException("Invalid genomic position format: {$genomicPosition}. Expected format: chr1:123456."); | ||
| } | ||
|
|
||
| return new self(new Chromosome($matches[1]), (int) $matches[3]); | ||
| } | ||
|
|
||
| public function toString(?NamingConvention $namingConvention = null): string | ||
| { | ||
| return "{$this->chromosome->toString($namingConvention)}:{$this->position}"; | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,89 @@ | ||
| <?php declare(strict_types=1); | ||
|
|
||
| namespace MLL\Utils; | ||
|
|
||
| use function Safe\preg_match; | ||
|
|
||
| final class GenomicRegion | ||
| { | ||
| public Chromosome $chromosome; | ||
|
|
||
| public int $start; | ||
|
|
||
| public int $end; | ||
|
|
||
| public function __construct( | ||
| Chromosome $chromosome, | ||
| int $start, | ||
| int $end | ||
| ) { | ||
| if ($start < 1) { | ||
| throw new \InvalidArgumentException("Start must be positive, got: {$start}."); | ||
| } | ||
|
|
||
| if ($end < 1) { | ||
| throw new \InvalidArgumentException("End must be positive, got: {$end}."); | ||
| } | ||
|
|
||
| if ($start > $end) { | ||
| throw new \InvalidArgumentException("End ({$end}) must be greater than start ({$start})"); | ||
| } | ||
|
|
||
| $this->chromosome = $chromosome; | ||
| $this->start = $start; | ||
| $this->end = $end; | ||
| } | ||
|
|
||
| public static function parse(string $genomicRegion): self | ||
| { | ||
| if (preg_match('/^(.+):(g\.|)(\d+)(-(\d+)|)$/', $genomicRegion, $matches) === 0) { | ||
| throw new \InvalidArgumentException("Invalid genomic region format: {$genomicRegion}. Expected format: chr1:123-456."); | ||
| } | ||
|
|
||
| return new self( | ||
| new Chromosome($matches[1]), | ||
| (int) $matches[3], | ||
| (int) ($matches[5] ?? $matches[3]) | ||
| ); | ||
| } | ||
|
|
||
| public function containsGenomicPosition(GenomicPosition $genomicPosition): bool | ||
| { | ||
| return $this->chromosome->toString() === $genomicPosition->chromosome->toString() | ||
| && $this->positionIsBetweenStartAndEnd($genomicPosition->position); | ||
| } | ||
|
|
||
| public function containsGenomicRegion(GenomicRegion $genomicRegion): bool | ||
| { | ||
| return $this->chromosome->toString() === $genomicRegion->chromosome->toString() | ||
| && $this->positionIsBetweenStartAndEnd($genomicRegion->start) | ||
| && $this->positionIsBetweenStartAndEnd($genomicRegion->end); | ||
| } | ||
|
|
||
| public function isCoveredByGenomicRegion(GenomicRegion $genomicRegion): bool | ||
| { | ||
| return $this->chromosome->toString() === $genomicRegion->chromosome->toString() | ||
| && $genomicRegion->start <= $this->start | ||
| && $genomicRegion->end >= $this->end; | ||
| } | ||
|
|
||
| public function intersectsWithGenomicRegion(GenomicRegion $genomicRegion): bool | ||
| { | ||
| return $this->chromosome->toString() === $genomicRegion->chromosome->toString() | ||
| && ( | ||
| $this->isCoveredByGenomicRegion($genomicRegion) | ||
| || $this->positionIsBetweenStartAndEnd($genomicRegion->start) | ||
| || $this->positionIsBetweenStartAndEnd($genomicRegion->end) | ||
| ); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Bug: Falsch bei vollständiger Überlappung Wenn Region B die Region A komplett umschließt (z.B. A= Fix — die kanonische Intervall-Formel: return $this->chromosome->equals($other->chromosome)
&& $this->start <= $other->end
&& $other->start <= $this->end; |
||
| } | ||
|
|
||
| private function positionIsBetweenStartAndEnd(int $position): bool | ||
| { | ||
| return $position >= $this->start && $position <= $this->end; | ||
| } | ||
|
|
||
| public function toString(?NamingConvention $namingConvention = null): string | ||
| { | ||
| return "{$this->chromosome->toString($namingConvention)}:{$this->start}-{$this->end}"; | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| <?php declare(strict_types=1); | ||
|
|
||
| namespace MLL\Utils; | ||
|
|
||
| class NamingConvention | ||
| { | ||
| public const ENSEMBL = 'ENSEMBL'; | ||
| public const UCSC = 'UCSC'; | ||
|
|
||
| public string $value; | ||
|
|
||
| public function __construct(string $value) | ||
| { | ||
KingKong1213 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| switch ($value) { | ||
| case NamingConvention::ENSEMBL: | ||
| case NamingConvention::UCSC: | ||
| $this->value = $value; | ||
| break; | ||
| default: | ||
| throw new \InvalidArgumentException("Invalid naming convention: {$value}"); | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,40 @@ | ||
| <?php declare(strict_types=1); | ||
|
|
||
| use MLL\Utils\Chromosome; | ||
| use MLL\Utils\NamingConvention; | ||
| use PHPUnit\Framework\TestCase; | ||
|
|
||
| final class ChromosomeTest extends TestCase | ||
| { | ||
| public function testToStringWithDefault(): void | ||
| { | ||
| $chromosome = new Chromosome('chr11'); | ||
| self::assertSame('chr11', $chromosome->toString()); | ||
| } | ||
|
|
||
| public function testToStringForEnsembl(): void | ||
| { | ||
| $chromosome = new Chromosome('chr11'); | ||
| self::assertSame('11', $chromosome->toString(new NamingConvention(NamingConvention::ENSEMBL))); | ||
| } | ||
|
|
||
| public function testInitWithUCSC(): void | ||
| { | ||
| $chromosome = new Chromosome('11'); | ||
| self::assertSame('11', $chromosome->toString()); | ||
| } | ||
|
|
||
| public function testToStringWithUCSCAndMitochondrialChromosome(): void | ||
| { | ||
| $chromosome = new Chromosome('chrM'); | ||
| self::assertSame('MT', $chromosome->toString(new NamingConvention(NamingConvention::ENSEMBL))); | ||
| } | ||
|
|
||
| public function testFailedInit(): void | ||
| { | ||
| $chromosomeAsString = 'FOO11'; | ||
| self::expectException(\InvalidArgumentException::class); | ||
| self::expectExceptionMessage("Invalid chromosome: {$chromosomeAsString}. Expected format: chr1-chr22, chrX, chrY, chrM, or without chr prefix."); | ||
| new Chromosome($chromosomeAsString); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| <?php declare(strict_types=1); | ||
|
|
||
| use MLL\Utils\GenomicPosition; | ||
| use PHPUnit\Framework\TestCase; | ||
|
|
||
| final class GenomicPositionTest extends TestCase | ||
| { | ||
| public function testParseOnSuccessHG19(): void | ||
| { | ||
| $genomicPosition = GenomicPosition::parse('chr11:1'); | ||
| self::assertSame('chr11:1', $genomicPosition->toString()); | ||
| } | ||
|
|
||
| public function testParseOnSuccessGRC37(): void | ||
| { | ||
| $genomicPosition = GenomicPosition::parse('11:1'); | ||
| self::assertSame('11:1', $genomicPosition->toString()); | ||
| } | ||
|
|
||
| public function testParseOnSuccessHGVSg(): void | ||
| { | ||
| $genomicPosition = GenomicPosition::parse('chr11:g.1'); | ||
| self::assertSame('chr11:1', $genomicPosition->toString()); | ||
| } | ||
|
|
||
| public function testParseOnError(): void | ||
| { | ||
| $genomicPositionAsString = '11:1test'; | ||
| self::expectException(\InvalidArgumentException::class); | ||
| self::expectExceptionMessage("Invalid genomic position format: {$genomicPositionAsString}. Expected format: chr1:123456."); | ||
| GenomicPosition::parse($genomicPositionAsString); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,84 @@ | ||
| <?php declare(strict_types=1); | ||
|
|
||
| use MLL\Utils\GenomicPosition; | ||
| use MLL\Utils\GenomicRegion; | ||
| use PHPUnit\Framework\TestCase; | ||
|
|
||
| final class GenomicRegionTest extends TestCase | ||
| { | ||
| public function testParseOnSuccessUCSC(): void | ||
| { | ||
| $genomicRegion = GenomicRegion::parse('chr11:1-2'); | ||
| self::assertSame('chr11:1-2', $genomicRegion->toString()); | ||
| } | ||
|
|
||
| public function testParseOnSuccessEnsembl(): void | ||
| { | ||
| $genomicRegion = GenomicRegion::parse('11:1-2'); | ||
| self::assertSame('11:1-2', $genomicRegion->toString()); | ||
| } | ||
|
|
||
| public function testParseOnSuccessHGVSg(): void | ||
| { | ||
| $genomicRegion = GenomicRegion::parse('chr11:g.1-2'); | ||
| self::assertSame('chr11:1-2', $genomicRegion->toString()); | ||
| } | ||
|
|
||
| public function testParseOnError(): void | ||
| { | ||
| $genomicRegionAsString = '11:1_2'; | ||
| self::expectException(\InvalidArgumentException::class); | ||
| self::expectExceptionMessage("Invalid genomic region format: {$genomicRegionAsString}. Expected format: chr1:123-456."); | ||
| GenomicRegion::parse($genomicRegionAsString); | ||
| } | ||
|
|
||
| public function testStartIsGerateThenEnd(): void | ||
| { | ||
| $genomicRegionAsString = '11:2-1'; | ||
| self::expectException(\InvalidArgumentException::class); | ||
| self::expectExceptionMessage('End (1) must be greater than start (2)'); | ||
| GenomicRegion::parse($genomicRegionAsString); | ||
| } | ||
|
|
||
| public function testContainsGenomicPositionIsTrue(): void | ||
| { | ||
| $genomicRegion = GenomicRegion::parse('chr11:g.1-20'); | ||
| self::assertTrue($genomicRegion->containsGenomicPosition(GenomicPosition::parse('chr11:20'))); | ||
| } | ||
|
|
||
| public function testContainsGenomicPositionIsFalse(): void | ||
| { | ||
| $genomicRegion = GenomicRegion::parse('chr11:g.1-20'); | ||
| self::assertFalse($genomicRegion->containsGenomicPosition(GenomicPosition::parse('chr11:21'))); | ||
| } | ||
|
|
||
| public function testContainsGenomicRegionIsTrue(): void | ||
| { | ||
| $genomicRegion = GenomicRegion::parse('chr11:g.1-20'); | ||
| self::assertTrue($genomicRegion->containsGenomicRegion(GenomicRegion::parse('chr11:19-20'))); | ||
| } | ||
|
|
||
| public function testContainsGenomicRegionIsFalse(): void | ||
| { | ||
| $genomicRegion = GenomicRegion::parse('chr11:g.1-20'); | ||
| self::assertFalse($genomicRegion->containsGenomicRegion(GenomicRegion::parse('chr11:21-22'))); | ||
| } | ||
|
|
||
| public function testCoversGenomicRegionIsTrue(): void | ||
| { | ||
| $genomicRegion = GenomicRegion::parse('chr11:g.20-30'); | ||
| self::assertTrue($genomicRegion->isCoveredByGenomicRegion(GenomicRegion::parse('chr11:g.15-35'))); | ||
| } | ||
|
|
||
| public function testIntersectsFullyWithGenomicRegionIsTrue(): void | ||
| { | ||
| $genomicRegion = GenomicRegion::parse('chr11:g.20-30'); | ||
| self::assertTrue($genomicRegion->intersectsWithGenomicRegion(GenomicRegion::parse('chr11:g.15-35'))); | ||
| } | ||
|
|
||
| public function testIntersectsWithGenomicRegionIsFalse(): void | ||
| { | ||
| $genomicRegion = GenomicRegion::parse('chr11:g.20-30'); | ||
| self::assertFalse($genomicRegion->intersectsWithGenomicRegion(GenomicRegion::parse('chr11:15-19'))); | ||
| } | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.