Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add AssemblyMappingManager to handle assembl
- Loading branch information
1 parent
822b8b7
commit 129c643
Showing
1 changed file
with
62 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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,62 @@ | ||
<?php | ||
|
||
namespace Dna\Snps; | ||
|
||
use PharData; | ||
use GuzzleHttp\Client; | ||
use Exception; | ||
|
||
class AssemblyMappingManager | ||
{ | ||
private Client $httpClient; | ||
|
||
public function __construct() | ||
{ | ||
$this->httpClient = new Client(); | ||
} | ||
|
||
public function getAssemblyMappingData(string $sourceAssembly, string $targetAssembly): string | ||
{ | ||
$filename = "assembly_mapping_{$sourceAssembly}_to_{$targetAssembly}.tar.gz"; | ||
$filepath = __DIR__ . "/resources/{$filename}"; | ||
|
||
if (!file_exists($filepath)) { | ||
$url = "http://example.com/assembly_mapping/{$sourceAssembly}/{$targetAssembly}"; | ||
try { | ||
$response = $this->httpClient->get($url); | ||
if ($response->getStatusCode() === 200) { | ||
file_put_contents($filepath, $response->getBody()->getContents()); | ||
} else { | ||
throw new Exception("Failed to download assembly mapping data."); | ||
} | ||
} catch (Exception $e) { | ||
throw new Exception("Error downloading assembly mapping data: " . $e->getMessage()); | ||
} | ||
} | ||
|
||
return $filepath; | ||
} | ||
|
||
public static function loadAssemblyMappingData(string $filename): array | ||
{ | ||
$assemblyMappingData = []; | ||
try { | ||
$tar = new PharData($filename); | ||
foreach ($tar as $file) { | ||
if (strpos($file->getFilename(), '.json') !== false) { | ||
$content = file_get_contents($file->getPathname()); | ||
$data = json_decode($content, true); | ||
if (json_last_error() === JSON_ERROR_NONE) { | ||
$assemblyMappingData[] = $data; | ||
} else { | ||
throw new Exception("Error parsing JSON data."); | ||
} | ||
} | ||
} | ||
} catch (Exception $e) { | ||
throw new Exception("Error loading assembly mapping data: " . $e->getMessage()); | ||
} | ||
|
||
return $assemblyMappingData; | ||
} | ||
} |