Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix getTranscriptNetwork request #68

Merged
merged 2 commits into from
Jan 24, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
92 changes: 49 additions & 43 deletions app/controllers/transcriptInteraction.py
Original file line number Diff line number Diff line change
Expand Up @@ -883,30 +883,30 @@ def getTranscriptCounts(dataset_ID: int = None, disease_name=None, enst_number=N

def get_transcript_network(dataset_ID: int = None, disease_name=None,
minBetweenness:float = None, minNodeDegree:float = None, minEigenvector:float = None,
pValue=0.05, pValueDirection="<",
mscor=None, mscorDirection="<",
correlation=None, correlationDirection="<",
sorting=None, descending=True, limit=100, offset=0, sponge_db_version: int = LATEST):
maxPValue=0.05, minMscor=None, minCorrelation=None,
edgeSorting: str = None, nodeSorting: str = None,
maxNodes: int = 100, maxEdges: int = 100,
offsetNodes: int = None, offsetEdges: int = None,
sponge_db_version: int = LATEST):
"""
Optimized function for fetching ceRNA network nodes and edges with applied filters and sorting. Handles route /ceRNAInteraction/getTranscriptNetwork
:param dataset_ID: dataset_ID of interest
:param disease_name: disease_name of interest
:param minBetweenness: betweenness cutoff (>)
:param minNodeDegree: degree cutoff (>)
:param minEigenvector: eigenvector cutoff (>)
:param pValue: pValue cutoff
:param pValueDirection: < or >
:param mscor: multiple miRNA sensitivity correlation
:param mscorDirection: < or >
:param correlation: correlation cutoff
:param correlationDirection: < or >
:param sorting: how the results of the db query should be sorted, one of 'pValue', 'mscor', 'correlation', 'betweenness', 'degree', 'eigenvector'
:param descending: should the results be sorted in descending or ascending order
:param limit: number of results that should be shown
:param offset: startpoint from where results should be shown
:param maxPValue: p-value cutoff (<)
:param minMscor: mscor cutoff (>)
:param minCorrelation: correlation cutoff (>)
:param edgeSorting: sorting key for edges
:param nodeSorting: sorting key for nodes
:param maxNodes: maximum number of nodes
:param maxEdges: maximum number of edges
:param offsetNodes: offset for node pagination
:param offsetEdges: offset for edge pagination
:param sponge_db_version: version of the sponge database
:return: all ceRNAInteractions in the dataset of interest that satisfy the given filters

"""

# Step 1: Filter for SpongeRun IDs
Expand All @@ -926,16 +926,16 @@ def get_transcript_network(dataset_ID: int = None, disease_name=None,
edge_query = db.select(models.TranscriptInteraction).filter(
models.TranscriptInteraction.sponge_run_ID.in_(run_query),
)
if pValue:
if maxPValue:
edge_query = edge_query.filter(
models.TranscriptInteraction.p_value < pValue if pValueDirection == "<" else models.TranscriptInteraction.p_value > pValue
models.TranscriptInteraction.p_value <= maxPValue
)

# Get prefiltered edges
edges = db.session.execute(edge_query).scalars().all()
tr_ids_in_edges = set([edge.transcript_ID_1 for edge in edges] + [edge.transcript_ID_2 for edge in edges])

# Step 3: Filter nodes by edges that pass the p-value filter
# Filter nodes by edges that pass the p-value filter
node_query = db.select(models.networkAnalysisTranscript).filter(
models.networkAnalysisTranscript.sponge_run_ID.in_(run_query),
models.networkAnalysisTranscript.transcript_ID.in_(tr_ids_in_edges)
Expand All @@ -949,7 +949,20 @@ def get_transcript_network(dataset_ID: int = None, disease_name=None,
if minEigenvector:
node_query = node_query.filter(models.networkAnalysisTranscript.eigenvector >= minEigenvector)

# Step 4: Finalize edges by filtering based on filtered nodes
# Sorting nodes
if nodeSorting == "betweenness":
node_query = node_query.order_by(models.networkAnalysisTranscript.betweenness.desc())
elif nodeSorting == "degree":
node_query = node_query.order_by(models.networkAnalysisTranscript.node_degree.desc())
elif nodeSorting == "eigenvector":
node_query = node_query.order_by(models.networkAnalysisTranscript.eigenvector.desc())
else:
raise ValueError("Invalid node sorting key. Choose one of 'betweenness', 'degree', 'eigenvector'")

# node pagination
node_query = node_query.offset(offsetNodes).limit(maxNodes)

# filter edges based on filtered nodes
nodes = db.session.execute(node_query).scalars().all()
node_tr_ids = set([node.transcript_ID for node in nodes])
edge_query = db.select(models.TranscriptInteraction).filter(
Expand All @@ -960,30 +973,23 @@ def get_transcript_network(dataset_ID: int = None, disease_name=None,
)

# Apply edge-specific filters
if mscor:
edge_query = edge_query.filter(models.TranscriptInteraction.mscor <= mscor if mscorDirection == "<" else models.TranscriptInteraction.mscor >= mscor)
if correlation:
edge_query = edge_query.filter(models.TranscriptInteraction.correlation <= correlation if correlationDirection == "<" else models.TranscriptInteraction.correlation >= correlation)

# Apply sorting
# Sorting
if sorting:
sort_column = {
"pValue": models.GeneInteraction.p_value,
"mscor": models.GeneInteraction.mscor,
"correlation": models.GeneInteraction.correlation,
"betweenness": models.networkAnalysisTranscript.betweenness,
"degree": models.networkAnalysisTranscript.node_degree,
"eigenvector": models.networkAnalysisTranscript.eigenvector
}.get(sorting)
if sorting in ["pValue", "mscor", "correlation"]:
edge_query = edge_query.order_by(sort_column.desc() if descending else sort_column.asc())
elif sorting in ['betweenness', 'degree', 'eigenvector']:
node_query = node_query.order_by(sort_column.desc() if descending else sort_column.asc())

# Pagination
edge_query = edge_query.offset(offset).limit(limit)
node_query = node_query.offset(offset).limit(limit)
if minMscor:
edge_query = edge_query.filter(models.TranscriptInteraction.mscor >= minMscor)
if minCorrelation:
edge_query = edge_query.filter(models.TranscriptInteraction.correlation >= minCorrelation)

# Sort edges
if edgeSorting == "pValue":
edge_query = edge_query.order_by(models.TranscriptInteraction.p_value.asc())
elif edgeSorting == "mscor":
edge_query = edge_query.order_by(models.TranscriptInteraction.mscor.desc())
elif edgeSorting == "correlation":
edge_query = edge_query.order_by(models.TranscriptInteraction.correlation.desc())
else:
raise ValueError("Invalid edge sorting key. Choose one of 'pValue', 'mscor', 'correlation'")

# edge pagination
edge_query = edge_query.offset(offsetEdges).limit(maxEdges)

# Execute queries
node_results = db.session.execute(node_query).scalars().all()
Expand Down
Loading