From f903610a2f647391b42534fd4b0c54987034e820 Mon Sep 17 00:00:00 2001 From: Helena Rasche Date: Fri, 24 Nov 2023 15:34:29 +0100 Subject: [PATCH 1/5] wip addorg --- apolpi.py | 46 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) diff --git a/apolpi.py b/apolpi.py index 164774b..292599e 100644 --- a/apolpi.py +++ b/apolpi.py @@ -95,6 +95,14 @@ ; """ +INSERT = """ +INSERT INTO organism + (id, version, common_name, directory, genome_fasta, genome_fasta_index, genus, species, obsolete, publicMode) +VALUES + (:id, 2, :commonName, :directory, 'seq/genome.fasta', 'seq/genome.fasta.fai', :genus, :species, false, :publicMode); +""" + + columns = [ "commonName", "blatdb", "metadata", "obsolete", "directory", "publicMode", "valid", "genomeFastaIndex", "genus", "species", "id", @@ -111,6 +119,10 @@ def _fetch(): return out +def _insert(var): + return db.session.execute(text(INSERT), var) + + @app.route("/organism/findAllOrganisms", methods=["GET", "POST"]) def doit(): global CACHED_TIME @@ -143,3 +155,37 @@ def doit(): final_list = [x for x in final_list if str(x['publicMode']).lower() == str(showPublicOnly).lower()] return jsonify(final_list) + + +@app.route("/organism/addOrganism", methods=["POST"]) +def insert(): + req_json = dict(request.json) + # '{"commonName": "sacCer1 (gx654)", + # "directory": "/data/dnb01/apollo/149296708", + # "publicMode": false, "genus": "S", "species": "cerevisiae", + # "username": "XXXXXXXXX", "password": "XXXXXXXXX"}' + # -[ RECORD 1 ]-----------------+----------------------------- + # id | 5483083 + # version | 2 + # abbreviation | + # blatdb | + # comment | + # common_name | sacCer1 (gx654) + # data_added_via_web_services | + # directory | /data/dnb01/apollo/149296708 + # genome_fasta | seq/genome.fasta + # genome_fasta_index | seq/genome.fasta.fai + # genus | S + # metadata | {"creator":"31"} + # non_default_translation_table | + # obsolete | f + # public_mode | f + # species | cerevisiae + # valid | t + # official_gene_set_track | + print(req_json) + # This is terrible. + req_json['id'] = -int(time.time()) + + print(_insert(req_json)) + return jsonify({}) From 29603a2e26f2f6fd6bad8f62908789aa128c2131 Mon Sep 17 00:00:00 2001 From: Helena Rasche Date: Fri, 24 Nov 2023 15:39:00 +0100 Subject: [PATCH 2/5] fix column name --- apolpi.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/apolpi.py b/apolpi.py index 292599e..816ac9e 100644 --- a/apolpi.py +++ b/apolpi.py @@ -97,7 +97,7 @@ INSERT = """ INSERT INTO organism - (id, version, common_name, directory, genome_fasta, genome_fasta_index, genus, species, obsolete, publicMode) + (id, version, common_name, directory, genome_fasta, genome_fasta_index, genus, species, obsolete, public_mode) VALUES (:id, 2, :commonName, :directory, 'seq/genome.fasta', 'seq/genome.fasta.fai', :genus, :species, false, :publicMode); """ From 160313c63d7614629e86c7aa13f14d1272a3376e Mon Sep 17 00:00:00 2001 From: Helena Rasche Date: Fri, 24 Nov 2023 15:43:28 +0100 Subject: [PATCH 3/5] better --- apolpi.py | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/apolpi.py b/apolpi.py index 816ac9e..b959275 100644 --- a/apolpi.py +++ b/apolpi.py @@ -120,7 +120,11 @@ def _fetch(): def _insert(var): - return db.session.execute(text(INSERT), var) + print(text(INSERT)) + res = db.session.execute(text(INSERT), var) + print(res) + print(db.session.commit()) + return res @app.route("/organism/findAllOrganisms", methods=["GET", "POST"]) @@ -187,5 +191,6 @@ def insert(): # This is terrible. req_json['id'] = -int(time.time()) - print(_insert(req_json)) + for row in _insert(req_json): + print(row) return jsonify({}) From be0936a088ffa74ccff96da0afbfe9b795ef0d38 Mon Sep 17 00:00:00 2001 From: Helena Rasche Date: Fri, 24 Nov 2023 16:22:25 +0100 Subject: [PATCH 4/5] works? --- apolpi.py | 55 ++++++++++++++++++++++++++++++++++++++++++------------- 1 file changed, 42 insertions(+), 13 deletions(-) diff --git a/apolpi.py b/apolpi.py index b959275..6f0cfa4 100644 --- a/apolpi.py +++ b/apolpi.py @@ -1,4 +1,5 @@ import os +import json import time from flask import Flask @@ -97,11 +98,27 @@ INSERT = """ INSERT INTO organism - (id, version, common_name, directory, genome_fasta, genome_fasta_index, genus, species, obsolete, public_mode) + (id, version, common_name, directory, genome_fasta, genome_fasta_index, genus, species, obsolete, public_mode, valid) VALUES - (:id, 2, :commonName, :directory, 'seq/genome.fasta', 'seq/genome.fasta.fai', :genus, :species, false, :publicMode); + (:id, 2, :commonName, :directory, 'seq/genome.fasta', 'seq/genome.fasta.fai', :genus, :species, false, :publicMode, true); """ +INSERT_PERMISSIONS = """ +INSERT INTO permission + (id, organism_id, class, user_id, permissions, version) +VALUES + (:permid, :id, 'org.bbop.apollo.UserOrganismPermission', 31, '["ADMINISTRATE"]', 1); +""" + +INSERT_REFSEQ = """ +INSERT INTO sequence + (id, version, sequence_end, length, name, organism_id, sequence_start) +VALUES + (:refseqid, 0, :length, :length, :name, :id, 0); +""" +# id | version | sequence_end | length | name | organism_id | seq_chunk_size | sequence_start +# ---------+---------+--------------+---------+---------+-------------+----------------+---------------- +# 5482965 | 0 | 230218 | 230218 | chrI | 5482963 | | 0 columns = [ "commonName", "blatdb", "metadata", "obsolete", "directory", @@ -120,11 +137,26 @@ def _fetch(): def _insert(var): - print(text(INSERT)) - res = db.session.execute(text(INSERT), var) - print(res) - print(db.session.commit()) - return res + # Need to get /data/dnb01/apollo/149296708/seq/refSeqs.json + refseqjson = os.path.join(var['directory'], 'seq', 'refSeqs.json') + with open(refseqjson, 'r') as handle: + refSeqs = json.load(handle) + + # Wrap it all in a connection + with db.session.begin(): + org_id = list(db.session.execute(text("select max(id) + 2 from organism")))[0][0] + var['id'] = org_id + db.session.execute(text(INSERT), var) + + perm_id = list(db.session.execute(text("select max(id) + 2 from permission")))[0][0] + db.session.execute(text(INSERT_PERMISSIONS), {'permid': perm_id, 'id': org_id}) + + max_rowid = list(db.session.execute(text("select max(id) from sequence")))[0][0] + for i, rec in enumerate(refSeqs): + refVars = {'refseqid': max_rowid + 1 + i, 'length': rec['length'], 'name': rec['name'], 'id': org_id} + db.session.execute(text(INSERT_REFSEQ), refVars) + + return True @app.route("/organism/findAllOrganisms", methods=["GET", "POST"]) @@ -188,9 +220,6 @@ def insert(): # valid | t # official_gene_set_track | print(req_json) - # This is terrible. - req_json['id'] = -int(time.time()) - - for row in _insert(req_json): - print(row) - return jsonify({}) + # This is intensely terrible. + print(_insert(req_json)) + return doit() From d09c2d5448dff30081282caca232bc3b41553a53 Mon Sep 17 00:00:00 2001 From: Helena Rasche Date: Fri, 24 Nov 2023 16:24:13 +0100 Subject: [PATCH 5/5] lint --- apolpi.py | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/apolpi.py b/apolpi.py index 6f0cfa4..d8dd672 100644 --- a/apolpi.py +++ b/apolpi.py @@ -1,5 +1,5 @@ -import os import json +import os import time from flask import Flask @@ -104,19 +104,19 @@ """ INSERT_PERMISSIONS = """ -INSERT INTO permission - (id, organism_id, class, user_id, permissions, version) +INSERT INTO permission + (id, organism_id, class, user_id, permissions, version) VALUES - (:permid, :id, 'org.bbop.apollo.UserOrganismPermission', 31, '["ADMINISTRATE"]', 1); + (:permid, :id, 'org.bbop.apollo.UserOrganismPermission', 31, '["ADMINISTRATE"]', 1); """ INSERT_REFSEQ = """ -INSERT INTO sequence +INSERT INTO sequence (id, version, sequence_end, length, name, organism_id, sequence_start) VALUES (:refseqid, 0, :length, :length, :name, :id, 0); """ -# id | version | sequence_end | length | name | organism_id | seq_chunk_size | sequence_start +# id | version | sequence_end | length | name | organism_id | seq_chunk_size | sequence_start # ---------+---------+--------------+---------+---------+-------------+----------------+---------------- # 5482965 | 0 | 230218 | 230218 | chrI | 5482963 | | 0 @@ -203,22 +203,22 @@ def insert(): # -[ RECORD 1 ]-----------------+----------------------------- # id | 5483083 # version | 2 - # abbreviation | - # blatdb | - # comment | + # abbreviation | + # blatdb | + # comment | # common_name | sacCer1 (gx654) - # data_added_via_web_services | + # data_added_via_web_services | # directory | /data/dnb01/apollo/149296708 # genome_fasta | seq/genome.fasta # genome_fasta_index | seq/genome.fasta.fai # genus | S # metadata | {"creator":"31"} - # non_default_translation_table | + # non_default_translation_table | # obsolete | f # public_mode | f # species | cerevisiae # valid | t - # official_gene_set_track | + # official_gene_set_track | print(req_json) # This is intensely terrible. print(_insert(req_json))