aboutsummaryrefslogtreecommitdiff
path: root/pyhegp.py
diff options
context:
space:
mode:
authorArun Isaac2025-07-08 16:14:00 +0100
committerArun Isaac2025-07-17 01:47:44 +0100
commita6bf9abb91ad6576ce23e7f8731e74c2a73d2ea6 (patch)
treec2e97040871cb4f779a1a59687565995bd1b3a80 /pyhegp.py
parent2829c4311d0c21d983621ce8b2c5565ddbc5dbf4 (diff)
downloadpyhegp-a6bf9abb91ad6576ce23e7f8731e74c2a73d2ea6.tar.gz
pyhegp-a6bf9abb91ad6576ce23e7f8731e74c2a73d2ea6.tar.lz
pyhegp-a6bf9abb91ad6576ce23e7f8731e74c2a73d2ea6.zip
Organize source into directory structure.
* pyhegp/__init__.py: New file. * pyhegp.py: Move to pyhegp/pyhegp.py. * test_pyhegp.py: Move to tests/test_pyhegp.py. Import from pyhegp.pyhegp instead of from pyhegp. * pyproject.toml (project.scripts)[pyhegp]: Switch to pyhegp.pyhegp:main.
Diffstat (limited to 'pyhegp.py')
-rw-r--r--pyhegp.py71
1 files changed, 0 insertions, 71 deletions
diff --git a/pyhegp.py b/pyhegp.py
deleted file mode 100644
index 053ddaf..0000000
--- a/pyhegp.py
+++ /dev/null
@@ -1,71 +0,0 @@
-### 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=",")
-
-@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()