PHP code example of saurabhgayali / uniprot-php

1. Go to this page and download the library: Download saurabhgayali/uniprot-php library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

saurabhgayali / uniprot-php example snippets



UniProtPHP\Http\HttpClientFactory;
use UniProtPHP\UniProt\UniProtEntry;

$httpClient = HttpClientFactory::create();
$entry = new UniProtEntry($httpClient);

// Get a protein
$protein = $entry->get('P12345');
echo "Accession: " . $protein['primaryAccession'] . "\n";
echo "Organism: " . $protein['organism']['commonName'] . "\n";
echo "Sequence length: " . $protein['sequence']['length'] . " amino acids\n";

$entry = new UniProtEntry($httpClient);
$protein = $entry->get('P12345');

// Batch retrieval
$results = $entry->getBatch(['P12345', 'P00750', 'P05067']);

// Check existence
if ($entry->exists('P12345')) {
    // ...
}

$search = new UniProtSearch($httpClient);

// Automatically fetches ALL results (e.g., 22,400 human proteins)
// Requests are made in chunks of 500 as needed
$results = $search->search('organism_id:9606 AND reviewed:true', ['size' => 500]);

$count = 0;
foreach ($results as $entry) {
    echo $entry['primaryAccession'] . "\n";
    $count++;
}
echo "Total: $count entries\n";  // Prints total, fetching 500 at a time

$search = new UniProtSearch($httpClient);

// Get page 2 (showing results 21-30)
$offset = 20;      // Results to skip
$pageSize = 10;    // Results per page
$query = 'organism_id:9606 AND reviewed:true';

$page = $search->getPaginatedResults($query, $offset, $pageSize);

echo "Page {$page['currentPage']} of {$page['totalPages']}\n";
echo "Showing {$page['pageSize']} results (total: {$page['totalResults']})\n";

foreach ($page['results'] as $index => $entry) {
    $globalNumber = $offset + $index + 1;  // Actual result number (S.No.)
    echo "$globalNumber. " . $entry['primaryAccession'] . "\n";
}

// Navigation
if ($page['hasNextPage']) {
    echo "Next page offset: " . $page['nextOffset'] . "\n";
}
if ($page['hasPreviousPage']) {
    echo "Previous page offset: " . $page['previousOffset'] . "\n";
}

// All pages links
foreach ($page['pageLinks'] as $pageNum => $pageOffset) {
    echo "Page $pageNum: offset=$pageOffset\n";
}

$mapping = new UniProtIdMapping($httpClient);

// Submit and wait
$jobId = $mapping->submitAndWait(
    'UniProtKB_AC-ID',
    'Ensembl',
    ['P05067', 'P12345']
);

// Get results
$results = $mapping->getResults($jobId);
foreach ($results['results'] as $r) {
    echo "{$r['from']} → {$r['to']['id']}\n";
}

try {
    $entry->get('P12345');
} catch (UniProtException $e) {
    echo $e->getDetailedMessage();
    if ($e->isClientError()) { /* ... */ }
}
bash
git clone https://github.com/your-repo/uniprot-php.git
cd uniprot-php
bash
# Example 1: Single Entry Retrieval
php examples/get_entry.php

# Example 2: Search with Pagination
php examples/search_entries.php

# Example 3: ID Mapping
php examples/map_ids.php
bash
cd tests
php -S localhost:8880
bash
php tests/run_tests.php

src/
├── Exception/
│   └── UniProtException.php          # Structured error handling
├── Http/
│   ├── HttpClientInterface.php       # HTTP transport interface
│   ├── CurlClient.php                # cURL implementation
│   ├── StreamClient.php              # Stream-based implementation
│   └── HttpClientFactory.php         # Transport selection
└── UniProt/
    ├── UniProtEntry.php              # Single entry retrieval
    ├── UniProtSearch.php             # Search with pagination
    └── UniProtIdMapping.php          # ID mapping jobs