Skip to content

Commit

Permalink
Use Constants instead of initiating constants object
Browse files Browse the repository at this point in the history
  • Loading branch information
memgonzales committed Sep 2, 2023
1 parent a2016d8 commit 1221799
Show file tree
Hide file tree
Showing 17 changed files with 100 additions and 116 deletions.
8 changes: 4 additions & 4 deletions app.py
Original file line number Diff line number Diff line change
Expand Up @@ -261,14 +261,14 @@
callbacks.text_mining.callbacks.init_callback(app)

# Create database table
const = Constants()
make_dir(const.TEMP)

make_dir(Constants.TEMP)

try:
connection = sqlite3.connect(const.FILE_STATUS_DB)
connection = sqlite3.connect(Constants.FILE_STATUS_DB)
cursor = connection.cursor()

query = f'CREATE TABLE IF NOT EXISTS {const.FILE_STATUS_TABLE} (name TEXT, UNIQUE(name));'
query = f'CREATE TABLE IF NOT EXISTS {Constants.FILE_STATUS_TABLE} (name TEXT, UNIQUE(name));'

cursor.execute(query)
connection.commit()
Expand Down
7 changes: 3 additions & 4 deletions callbacks/browse_loci/callbacks.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
from ..file_util import *

from ..constants import Constants
const = Constants()


def init_callback(app):
Expand Down Expand Up @@ -73,15 +72,15 @@ def handle_exception(e):
@app.server.route('/genomes_nipponbare/<path:filename>')
def send_genomes_nipponbare_url(filename):
try:
return send_from_directory(const.GENOMES_NIPPONBARE, filename)
return send_from_directory(Constants.GENOMES_NIPPONBARE, filename)
except FileNotFoundError:
abort(404)

@app.server.route('/annotations_nb/<nb_intervals_str>/<path:foldername>/<selected_interval_str>/<file_format>')
def send_annotations_nb_url(nb_intervals_str, foldername, selected_interval_str, file_format):
try:
temp_output_folder_dir = get_path_to_temp(
nb_intervals_str, const.TEMP_IGV, foldername)
nb_intervals_str, Constants.TEMP_IGV, foldername)

selected_interval_str_filename = convert_text_to_path(
selected_interval_str)
Expand All @@ -96,7 +95,7 @@ def send_annotations_nb_url(nb_intervals_str, foldername, selected_interval_str,
@app.server.route('/open_chromatin_panicle/<path:filename>')
def send_open_chromatin_panicle_url(filename):
try:
return send_from_directory(const.OPEN_CHROMATIN_PANICLE, filename)
return send_from_directory(Constants.OPEN_CHROMATIN_PANICLE, filename)

except FileNotFoundError:
abort(404)
Expand Down
10 changes: 3 additions & 7 deletions callbacks/browse_loci/util.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,13 @@
from ..lift_over import util
import gffutils
import pandas as pd
import os
from ..file_util import *
from ..constants import Constants

const = Constants()


def write_igv_tracks_to_file(nb_intervals_str):
# tracks found in igv
track_db = [[const.ANNOTATIONS_NB, 'IRGSPMSU.gff.db', 'gff'],
[const.OPEN_CHROMATIN_PANICLE, 'SRR7126116_ATAC-Seq_Panicles.bed', 'bed']]
track_db = [[Constants.ANNOTATIONS_NB, 'IRGSPMSU.gff.db', 'gff'],
[Constants.OPEN_CHROMATIN_PANICLE, 'SRR7126116_ATAC-Seq_Panicles.bed', 'bed']]

# write to file the data for igv
for db in track_db:
Expand All @@ -32,7 +28,7 @@ def write_gff_igv_track_to_file(source_dir, source_file, nb_intervals_str):
nb_intervals_str)

temp_folder = get_path_to_temp(
nb_intervals_str, const.TEMP_IGV, source_file)
nb_intervals_str, Constants.TEMP_IGV, source_file)
make_dir(temp_folder)

for i in range(len(loci_list)):
Expand Down
39 changes: 20 additions & 19 deletions callbacks/coexpression/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@

from collections import namedtuple

const = Constants()

# Settings for the module detection algorithms:
# - multiplier: Value multiplied to the parameter to get the name of the directory
Expand Down Expand Up @@ -60,7 +59,7 @@

def get_user_facing_parameter(algo, parameter, network='OS-CX'):
parameters = sorted(
map(int, os.listdir(f'{const.NETWORK_MODULES}/{network}/MSU/{algo}')))
map(int, os.listdir(f'{Constants.NETWORK_MODULES}/{network}/MSU/{algo}')))

return parameters.index(parameter) + 1

Expand Down Expand Up @@ -90,7 +89,7 @@ def get_parameters_for_algo(algo, network='OS-CX'):
"""
param_dict = {}
parameters = sorted(
map(int, os.listdir(f'{const.NETWORK_MODULES}/{network}/MSU/{algo}')))
map(int, os.listdir(f'{Constants.NETWORK_MODULES}/{network}/MSU/{algo}')))

# Display the user-facing parameters for the module detection algorithms
for idx, parameter in enumerate(parameters):
Expand Down Expand Up @@ -124,10 +123,10 @@ def create_module_enrichment_results_dir(genomic_intervals, addl_genes, network,
"""
if addl_genes:
temp_output_folder_dir = get_path_to_temp(
genomic_intervals, const.TEMP_COEXPRESSION, f'{shorten_name(addl_genes)}/{network}/{algo}/{parameters}')
genomic_intervals, Constants.TEMP_COEXPRESSION, f'{shorten_name(addl_genes)}/{network}/{algo}/{parameters}')
else:
temp_output_folder_dir = get_path_to_temp(
genomic_intervals, const.TEMP_COEXPRESSION, f'{network}/{algo}/{parameters}')
genomic_intervals, Constants.TEMP_COEXPRESSION, f'{network}/{algo}/{parameters}')

if not path_exists(temp_output_folder_dir):
make_dir(temp_output_folder_dir)
Expand Down Expand Up @@ -180,7 +179,7 @@ def do_module_enrichment_analysis(implicated_gene_ids, genomic_intervals, addl_g
if not path_exists(ENRICHED_MODULES_PATH):
ENRICHED_MODULES_PATH_WITH_TIMESTAMP = append_timestamp_to_filename(
ENRICHED_MODULES_PATH)
MODULES_PATH = f'{const.NETWORK_MODULES}/{network}/MSU/{algo}/{parameters}/{algo}-module-list.tsv'
MODULES_PATH = f'{Constants.NETWORK_MODULES}/{network}/MSU/{algo}/{parameters}/{algo}-module-list.tsv'

# ====================================================================================
# This replicates the logic of running the universal enrichment function `enricher()`
Expand Down Expand Up @@ -214,7 +213,7 @@ def do_module_enrichment_analysis(implicated_gene_ids, genomic_intervals, addl_g

adj_p_values = false_discovery_control(p_values, method='bh')
significant_adj_p_values = [(p_values_indices[idx], adj_p_value) for idx, adj_p_value in enumerate(
adj_p_values) if adj_p_value < const.P_VALUE_CUTOFF]
adj_p_values) if adj_p_value < Constants.P_VALUE_CUTOFF]
significant_adj_p_values.sort(key=lambda x: x[1])
significant_adj_p_values = [
f'{ID}\t{adj_p_value}' for ID, adj_p_value in significant_adj_p_values]
Expand Down Expand Up @@ -265,7 +264,7 @@ def convert_transcript_to_msu_id(transcript_ids_str, network):
Returns:
- Equivalent MSU accessions of the KEGG transcript IDs
"""
with open(f'{const.GENE_ID_MAPPING}/{network}/transcript-to-msu-id.pickle', 'rb') as f:
with open(f'{Constants.GENE_ID_MAPPING}/{network}/transcript-to-msu-id.pickle', 'rb') as f:
mapping_dict = pickle.load(f)

output_str = ''
Expand All @@ -279,14 +278,14 @@ def convert_transcript_to_msu_id(transcript_ids_str, network):


def get_genes_in_module(module_idx, network, algo, parameters):
with open(f'{const.NETWORK_MODULES}/{network}/transcript/{algo}/{parameters}/{algo}-module-list.tsv') as f:
with open(f'{Constants.NETWORK_MODULES}/{network}/transcript/{algo}/{parameters}/{algo}-module-list.tsv') as f:
for idx, module in enumerate(f):
if idx + 1 == int(module_idx):
return set(module.split('\t'))


def get_genes_in_pathway(pathway_id, network):
with open(f'{const.ENRICHMENT_ANALYSIS}/{network}/{const.KEGG_DOSA_GENESET}', 'rb') as f:
with open(f'{Constants.ENRICHMENT_ANALYSIS}/{network}/{Constants.KEGG_DOSA_GENESET}', 'rb') as f:
genes_in_pathway = pickle.load(f)

return genes_in_pathway[pathway_id]
Expand All @@ -298,7 +297,7 @@ def get_genes_in_module_and_pathway(pathway_id, module_idx, network, algo, param


def get_kegg_pathway_name(pathway_id, network):
with open(f'{const.ENRICHMENT_ANALYSIS}/{network}/{const.KEGG_DOSA_PATHWAY_NAMES}') as pathways:
with open(f'{Constants.ENRICHMENT_ANALYSIS}/{network}/{Constants.KEGG_DOSA_PATHWAY_NAMES}') as pathways:
for line in pathways:
line = line.split('\t')
if line[0].rstrip() == pathway_id:
Expand Down Expand Up @@ -408,7 +407,8 @@ def convert_to_df_pe(result, module_idx, network, algo, parameters):
if result.empty:
return create_empty_df_with_cols(cols)

result = result.loc[result['Adj. Combined p-value'] < const.P_VALUE_CUTOFF]
result = result.loc[result['Adj. Combined p-value']
< Constants.P_VALUE_CUTOFF]

# IMPORTANT: Do not change ordering of instructions

Expand Down Expand Up @@ -444,7 +444,8 @@ def convert_to_df_spia(result, network):
if result.empty:
return create_empty_df_with_cols(cols)

result = result.loc[result['Adj. Combined p-value'] < const.P_VALUE_CUTOFF]
result = result.loc[result['Adj. Combined p-value']
< Constants.P_VALUE_CUTOFF]

# Prettify display of ID
result['ID'] = 'dosa' + result['ID']
Expand Down Expand Up @@ -486,7 +487,7 @@ def convert_to_df(active_tab, module_idx, network, algo, parameters):
dir = enrichment_tabs[get_tab_index(active_tab)].path
enrichment_type = dir.split('/')[-1]

file = f'{const.ENRICHMENT_ANALYSIS}/{network}/output/{algo}/{parameters}/{dir}/results/{enrichment_type}-df-{module_idx}.tsv'
file = f'{Constants.ENRICHMENT_ANALYSIS}/{network}/output/{algo}/{parameters}/{dir}/results/{enrichment_type}-df-{module_idx}.tsv'

columns = {'go': ['ID', 'Gene Ontology Term', 'Gene Ratio',
'BG Ratio', 'p-value', 'Adj. p-value', 'q-value', 'Genes', 'Count'],
Expand Down Expand Up @@ -591,12 +592,12 @@ def load_module_graph(implicated_gene_ids, module, network, algo, parameters, la
try:
# Ignore the word "Module" at the start
module_idx = int(module.split(' ')[1])
OUTPUT_DIR = f'{const.TEMP}/{network}/{algo}/modules/{parameters}'
OUTPUT_DIR = f'{Constants.TEMP}/{network}/{algo}/modules/{parameters}'
coexpress_nw = f'{OUTPUT_DIR}/module-{module_idx}.tsv'

if not path_exists(coexpress_nw):
NETWORK_FILE = f'{const.NETWORKS}/{network}.txt'
MODULE_FILE = f'{const.NETWORK_MODULES}/{network}/MSU/{algo}/{parameters}/{algo}-module-list.tsv'
NETWORK_FILE = f'{Constants.NETWORKS}/{network}.txt'
MODULE_FILE = f'{Constants.NETWORK_MODULES}/{network}/MSU/{algo}/{parameters}/{algo}-module-list.tsv'

convert_modules_to_edgelist(
NETWORK_FILE, MODULE_FILE, module_idx, OUTPUT_DIR)
Expand All @@ -621,7 +622,7 @@ def load_module_graph(implicated_gene_ids, module, network, algo, parameters, la


def count_modules(network, algo, parameters):
with open(f'{const.NETWORK_MODULES}/{network}/MSU/{algo}/{parameters}/{algo}-module-list.tsv') as f:
with open(f'{Constants.NETWORK_MODULES}/{network}/MSU/{algo}/{parameters}/{algo}-module-list.tsv') as f:
return len(f.readlines())


Expand All @@ -637,7 +638,7 @@ def get_noun_for_active_tab(active_tab):


def count_genes_in_module(implicated_genes, module_idx, network, algo, parameters):
with open(f'{const.NETWORK_MODULES}/{network}/MSU/{algo}/{parameters}/{algo}-module-list.tsv') as modules:
with open(f'{Constants.NETWORK_MODULES}/{network}/MSU/{algo}/{parameters}/{algo}-module-list.tsv') as modules:
for idx, module in enumerate(modules):
if idx == module_idx - 1:
module_genes = module.strip().split('\t')
Expand Down
19 changes: 9 additions & 10 deletions callbacks/file_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@
import time
import sqlite3

const = Constants()


def path_exists(path):
"""
Expand Down Expand Up @@ -64,7 +62,7 @@ def get_path_to_temp(genomic_interval, analysis_type, *args):

analysis_type = convert_text_to_path(analysis_type)

temp_dir = f'{const.TEMP}/{genomic_interval_foldername}/{analysis_type}'
temp_dir = f'{Constants.TEMP}/{genomic_interval_foldername}/{analysis_type}'
for folder in args:
temp_dir += f'/{convert_text_to_path(folder)}'

Expand All @@ -76,20 +74,21 @@ def get_path_to_temp(genomic_interval, analysis_type, *args):
def get_path_to_text_mining_temp(analysis_type, *args):
analysis_type = convert_text_to_path(analysis_type)

temp_dir = f'{const.TEMP}/{analysis_type}'
temp_dir = f'{Constants.TEMP}/{analysis_type}'
for folder in args:
temp_dir += f'/{convert_text_to_path(folder)}'

temp_dir = re.sub(r'/+', '/', temp_dir)

return temp_dir



def shorten_name(name):
try:
connection = sqlite3.connect(const.FILE_STATUS_DB)
connection = sqlite3.connect(Constants.FILE_STATUS_DB)
cursor = connection.cursor()

query = f'INSERT OR IGNORE INTO {const.FILE_STATUS_TABLE}(name) VALUES("{name}")'
query = f'INSERT OR IGNORE INTO {Constants.FILE_STATUS_TABLE}(name) VALUES("{name}")'

cursor.execute(query)
connection.commit()
Expand All @@ -100,10 +99,10 @@ def shorten_name(name):
pass

try:
connection = sqlite3.connect(const.FILE_STATUS_DB)
connection = sqlite3.connect(Constants.FILE_STATUS_DB)
cursor = connection.cursor()

query = f'SELECT rowid FROM {const.FILE_STATUS_TABLE} WHERE name = "{name}"'
query = f'SELECT rowid FROM {Constants.FILE_STATUS_TABLE} WHERE name = "{name}"'
cursor.execute(query)
row_id = cursor.fetchall()[0][0]

Expand Down
9 changes: 3 additions & 6 deletions callbacks/homepage/callbacks.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@

from ..style_util import *

const = Constants()


def init_callback(app):

Expand Down Expand Up @@ -72,7 +70,8 @@ def parse_input(nb_intervals_str, n_clicks, n_submit, dccStore_children, *_):

if 'homepage-reset' == ctx.triggered_id:
# clear data for items in dcc.Store found in session-container
dccStore_children = get_cleared_dccStore_data_excluding_some_data(dccStore_children)
dccStore_children = get_cleared_dccStore_data_excluding_some_data(
dccStore_children)

return dccStore_children, None, {'display': 'none'}, False, ''

Expand Down Expand Up @@ -130,8 +129,7 @@ def set_input_fields_with_preset_input(example_genomic_interval_n_clicks):
return get_example_genomic_interval(ctx.triggered_id['description'])

raise PreventUpdate



@app.callback(
Output('homepage-genomic-intervals-saved-input',
'data', allow_duplicate=True),
Expand All @@ -141,7 +139,6 @@ def set_input_fields_with_preset_input(example_genomic_interval_n_clicks):
def set_input_fields(genomic_intervals):
return genomic_intervals


@app.callback(
Output('homepage-results-container', 'style'),
Input('homepage-is-submitted', 'data'),
Expand Down
19 changes: 9 additions & 10 deletions callbacks/homepage/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,22 @@

import sqlite3

const = Constants()

example_genomic_intervals = {
'pre-harvest': 'Chr01:1523625-1770814;Chr04:4662701-4670717',
'anaerobic-germination': 'Chr07:6000000-6900000'}


def clear_cache_folder():
if os.path.exists(const.TEMP):
shutil.rmtree(const.TEMP, ignore_errors=True)
if os.path.exists(Constants.TEMP):
shutil.rmtree(Constants.TEMP, ignore_errors=True)

# Drop the table
try:
connection = sqlite3.connect(const.FILE_STATUS_DB)
connection = sqlite3.connect(Constants.FILE_STATUS_DB)
cursor = connection.cursor()

query = f'DROP TABLE {const.FILE_STATUS_TABLE}'
query = f'DROP TABLE {Constants.FILE_STATUS_TABLE}'

cursor.execute(query)
connection.commit()
Expand All @@ -33,13 +32,13 @@ def clear_cache_folder():
pass

# Recreate the database
make_dir(const.TEMP)
make_dir(Constants.TEMP)

try:
connection = sqlite3.connect(const.FILE_STATUS_DB)
connection = sqlite3.connect(Constants.FILE_STATUS_DB)
cursor = connection.cursor()

query = f'CREATE TABLE IF NOT EXISTS {const.FILE_STATUS_TABLE} (name TEXT, UNIQUE(name));'
query = f'CREATE TABLE IF NOT EXISTS {Constants.FILE_STATUS_TABLE} (name TEXT, UNIQUE(name));'

cursor.execute(query)
connection.commit()
Expand All @@ -59,10 +58,10 @@ def get_cleared_dccStore_data_excluding_some_data(dccStore_children, *args):
for arg in args:
if arg in dccStore_ID:
flag = True

if not flag:
dccStore_children[i]['props']['data'] = ''

else:
dccStore_children[i]['props']['data'] = ''

Expand Down
1 change: 0 additions & 1 deletion callbacks/lift_over/callbacks.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
from .util import *
from ..constants import Constants
from ..general_util import *
const = Constants()


def init_callback(app):
Expand Down
Loading

0 comments on commit 1221799

Please sign in to comment.