Variants in a database

The variants of the VCF 4.3 example are stored in the vcf_variant table with Doctrine. The domain objects stay independent of Doctrine: a mapper turns them into entities and back.

1

Starting code

This is the PHP code used by this example.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
public function database(
    VcfReaderInterface $vcfReader,
    VcfVariantRecordMapperInterface $vcfRecordMapper,
    EntityManagerInterface $entityManager
) {
    $aVariants = $vcfReader->read(file($this->getParameter('kernel.project_dir') . '/public/data/sample.vcf'))
        ->getVariants();
 
    // Start again from an empty table at each visit of the demo
    $entityManager->createQuery('DELETE FROM ' . VcfVariantRecord::class . ' r WHERE r.chrom = :chrom')
        ->setParameter('chrom', '20')
        ->execute();
 
    // The domain objects stay independent of Doctrine: the mapper turns them into entities
    foreach ($aVariants as $oVariant) {
        $entityManager->persist($vcfRecordMapper->toRecord($oVariant));
    }
    $entityManager->flush();
    $entityManager->clear();
 
    // Ask the database: which variants passed every filter with a quality of 30 or more?
    $aRecords = $entityManager->createQueryBuilder()
        ->select('r')
        ->from(VcfVariantRecord::class, 'r')
        ->where('r.filter = :pass')
        ->andWhere('r.quality >= :quality')
        ->setParameter('pass', 'PASS')
        ->setParameter('quality', 30)
        ->orderBy('r.position', 'ASC')
        ->getQuery()
        ->getResult();
 
    // Back to domain objects: rebuilding a variant runs its validation again
    $aStored = array_map(fn(VcfVariantRecord $oRecord) => $vcfRecordMapper->toVariant($oRecord), $aRecords);
 
    return $this->render('default/variantsDatabase.html.twig', [
        'stored'   => count($aVariants),
        'records'  => $aRecords,
        'variants' => $this->describe($aStored),
    ]);
}
2

Result

Result returned by BioPHP for the demonstration data.

5 variants stored; 3 passed every filter with a quality of 30 or more
Row CHROM POS ID QUAL
23 20 1110696 rs6040355 67
24 20 1230237 . 47
25 20 1234567 microsat1 50
Back to VcfVariant objects
CHROM POS ID REF ALT QUAL FILTER INFO
20 1110696 rs6040355 A G (SNV)
T (SNV)
67 ✓ PASS AA=T
AF=0.333,0.667
DB
DP=10
NS=2
20 1230237 . T none: reference call 47 ✓ PASS AA=T
DP=13
NS=3
20 1234567 microsat1 GTC G (deletion)
GTCT (insertion)
50 ✓ PASS AA=G
DP=9
NS=3

The code above is the one actually executed by this page. Find the other examples in the left-hand column, or the tools in BioTools.