aboutsummaryrefslogtreecommitdiff
path: root/pyhegp.py
blob: 68a33917a510a1e04f243d9fd82e7d3d5c083baa (plain)
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
### pyhegp --- Homomorphic encryption of genotypes and phenotypes
### Copyright © 2025 Arun Isaac <arunisaac@systemreboot.net>
###
### This file is part of pyhegp.
###
### pyhegp is free software: you can redistribute it and/or modify it
### under the terms of the GNU General Public License as published by
### the Free Software Foundation, either version 3 of the License, or
### (at your option) any later version.
###
### pyhegp is distributed in the hope that it will be useful, but
### WITHOUT ANY WARRANTY; without even the implied warranty of
### MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
### General Public License for more details.
###
### You should have received a copy of the GNU General Public License
### along with pyhegp. If not, see <https://www.gnu.org/licenses/>.

import click
import numpy as np
from scipy.stats import special_ortho_group

def random_key(rng, n):
    return special_ortho_group.rvs(n, random_state=rng)

def standardize(genotype_matrix, maf):
    m, _ = genotype_matrix.shape
    return ((genotype_matrix - np.tile(maf, (m, 1)))
            @ np.diag(1 / np.sqrt(2 * maf * (1 - maf))))

def hegp_encrypt(plaintext, maf, key):
    return key @ plaintext
    # FIXME: Add standardization.
    # return key @ standardize(plaintext, maf)

def hegp_decrypt(ciphertext, key):
    return np.transpose(key) @ ciphertext

def read_genotype(genotype_file):
    return np.loadtxt(genotype_file, delimiter=",")
    # snps = genotype_file.readline().split(",")
    # return np.loadtxt(genotype_file, delimiter=",", skiprows=1, usecols=range(1, 1+len(snps)))

@click.group()
def main():
    pass

@main.command()
@click.argument("genotype-file", type=click.File("r"))
@click.argument("maf-file", type=click.File("r"))
@click.argument("key-path", type=click.Path())
@click.argument("ciphertext-path", type=click.Path())
def encrypt(genotype_file, maf_file, key_path, ciphertext_path):
    genotype = read_genotype(genotype_file)
    maf = np.loadtxt(maf_file)
    rng = np.random.default_rng()
    key = random_key(rng, len(genotype))
    encrypted_genotype = hegp_encrypt(genotype, maf, key)
    np.savetxt(key_path, key, delimiter=",", fmt="%f")
    np.savetxt(ciphertext_path, encrypted_genotype, delimiter=",", fmt="%f")

@main.command()
@click.argument("key-file", type=click.File("r"))
@click.argument("ciphertext-file", type=click.File("r"))
@click.argument("plaintext-path", type=click.Path())
def decrypt(key_file, ciphertext_file, plaintext_path):
    key = np.loadtxt(key_file, delimiter=",")
    ciphertext = np.loadtxt(ciphertext_file, delimiter=",")
    genotype = hegp_decrypt(ciphertext, key)
    np.savetxt(plaintext_path, genotype, delimiter=",", fmt="%f")

if __name__ == "__main__":
    main()