Created
January 4, 2021 14:43
-
-
Save jodyphelan/3f449290c57b0088aca4485f309c317f to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| ################### | |
| #### Create DB #### | |
| ################### | |
| # This is just a simple in-memory sqlite database for this example. | |
| # It will obviously not be necessary in your case as you already created the database. | |
| from sqlalchemy import Table, Column, Integer, String, MetaData, ForeignKey | |
| from sqlalchemy import create_engine | |
| engine = create_engine('sqlite:///:memory:', echo=True) | |
| metadata = MetaData() | |
| patient_mutations = Table('patient_genes_mutations', metadata, | |
| Column('id', Integer, primary_key=True), | |
| Column('mut_id', Integer), | |
| Column('patient_id', Integer), | |
| ) | |
| unique_mutations = Table('unqiue_mutations', metadata, | |
| Column('id', Integer, primary_key=True), | |
| Column('mutation', String), | |
| Column('gene_id', String), | |
| ) | |
| patients = Table('patients', metadata, | |
| Column('id', Integer, primary_key=True), | |
| ) | |
| metadata.create_all(engine) | |
| ########################### | |
| #### Using tb-profiler #### | |
| ########################### | |
| # Here we use download an example fastq file and upload it to TB-Profiler webserver using the driver | |
| import tbprofiler_webdriver | |
| import urllib | |
| from time import sleep | |
| # Get an example file | |
| urllib.request.urlretrieve("http://bioinformatics.lshtm.ac.uk/static/por5A_min.fastq.gz", "por5A_min.fastq.gz") | |
| # Create server object (default: host="https://localhost:5000") | |
| server = tbprofiler_webdriver.tbprofiler_server(host="http://bioinformatics.lshtm.ac.uk") | |
| # Send fastq files to be processed (default: platform="illumina") | |
| run_id = server.send_fastq("por5A_min.fastq.gz") | |
| # Retrieve results | |
| result = server.get_result(run_id) | |
| while result["status"]!="OK": | |
| result = server.get_result(run_id) | |
| print("Result not ready yet... Waiting 5 seconds.") | |
| sleep(5) | |
| ########################### | |
| ##### Insert into DB ###### | |
| ########################### | |
| # This is an example of how you can commit the results to your database | |
| from sqlalchemy.sql import select | |
| # Patient ID is defined by process | |
| patient_id = "12345" | |
| conn = engine.connect() | |
| for var in result["result"]["dr_variants"]: | |
| # Test if mutation exists already in unique_mutations table | |
| if conn.execute("SELECT * FROM unqiue_mutations WHERE mutation=? and gene_id=?", var["change"], var["locus_tag"]).first()==None: | |
| conn.execute(unique_mutations.insert(), mutation=var["change"], gene_id=var["locus_tag"]) | |
| # Get mutaion ID | |
| mutation_id = conn.execute("SELECT * FROM unqiue_mutations WHERE mutation=? and gene_id=?", var["change"], var["locus_tag"]).first()[0] | |
| # Add Mutation -> Patient rows | |
| conn.execute(patient_mutations.insert(), mut_id=mutation_id, patient_id=patient_id) | |
| # Check table contents | |
| for row in conn.execute(select([unique_mutations])): | |
| print(dict(row)) | |
| for row in conn.execute(select([patient_mutations])): | |
| print(dict(row)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment