-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
15 changed files
with
208 additions
and
10 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,14 @@ | ||
# Create a Docker image for the Local Neighborhood algorithm here | ||
|
||
# Local Neighborhood wrapper | ||
|
||
FROM python:3.12.3-alpine3.20 | ||
|
||
WORKDIR /LocalNeighborhood | ||
|
||
# Copy the py file to the working directory | ||
COPY local_neighborhood.py . | ||
|
||
# Copy the data subfolder to the working directory | ||
COPY ln-network.txt . | ||
COPY ln-nodes.txt . |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
A|B|E | ||
C|B | ||
C|D | ||
D|E | ||
A|E |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
A|B | ||
C|B | ||
C|D | ||
D|E | ||
A|E |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
A | ||
B |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
A|B | ||
C|B | ||
A|E |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
A|B | ||
C|B | ||
A|E |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,136 @@ | ||
from spras.prm import PRM | ||
from pathlib import Path | ||
from spras.containers import prepare_volume, run_container | ||
from spras.util import add_rank_column | ||
import pandas as pd | ||
from spras.interactome import reinsert_direction_col_undirected | ||
|
||
__all__ = ['LocalNeighborhood'] | ||
|
||
|
||
class LocalNeighborhood(PRM): | ||
required_inputs = ['network', 'nodetypes'] | ||
|
||
@staticmethod | ||
def generate_inputs(data, filename_map): | ||
# both edge list and prizes | ||
""" | ||
Access fields from the dataset and write the required input files | ||
@param data: dataset | ||
@param filename_map: a dict mapping file types in the required_inputs to the filename for that type | ||
@return: | ||
""" | ||
# print(filename_map) | ||
# print(data) | ||
|
||
for input_type in LocalNeighborhood.required_inputs: | ||
if input_type not in filename_map: | ||
raise ValueError(f"{input_type} filename is missing") | ||
|
||
node_df = None | ||
|
||
if data.contains_node_columns('prize'): | ||
node_df = data.request_node_columns(['prize']) | ||
elif data.contains_node_columns(['active', 'sources', 'targets']): | ||
node_df = data.request_node_columns(['active', 'sources', 'targets']) | ||
node_df['prize'] = 0.0 # Initialize 'prize' column | ||
node_df.loc[node_df['active'] == True, 'prize'] = 1.0 | ||
node_df.loc[node_df['sources'] == True, 'prize'] = 1.0 | ||
node_df.loc[node_df['targets'] == True, 'prize'] = 1.0 | ||
else: | ||
raise ValueError("Local Neighborhood requires node prizes or sources and targets") | ||
|
||
print(node_df) | ||
|
||
node_df.to_csv(filename_map['nodetypes'],sep='\t',index=False,columns=['NODEID'],header=False) | ||
|
||
edges_df = data.get_interactome() | ||
|
||
print(edges_df) | ||
|
||
edges_df.to_csv(filename_map['network'],sep='|',index=False, | ||
columns=['Interactor1','Interactor2'], | ||
header=False) | ||
|
||
|
||
#TODO: ????? | ||
@staticmethod | ||
def run(nodetypes=None, network=None, output_file=None, container_framework="docker"): | ||
""" | ||
Run PathLinker with Docker | ||
@param nodetypes: input node types with sources and targets (required) | ||
@param network: input network file (required) | ||
@param output_file: path to the output pathway file (required) | ||
@param k: path length (optional) | ||
@param container_framework: choose the container runtime framework, currently supports "docker" or "singularity" (optional) | ||
""" | ||
# Add additional parameter validation | ||
# Do not require k | ||
# Use the PathLinker default | ||
# Could consider setting the default here instead | ||
if not nodetypes or not network or not output_file: | ||
raise ValueError('Required Local Neighborhood arguments are missing') | ||
|
||
work_dir = '/spras' | ||
|
||
# Each volume is a tuple (src, dest) | ||
volumes = list() | ||
|
||
bind_path, node_file = prepare_volume(nodetypes, work_dir) | ||
volumes.append(bind_path) | ||
|
||
bind_path, network_file = prepare_volume(network, work_dir) | ||
volumes.append(bind_path) | ||
|
||
# PathLinker does not provide an argument to set the output directory | ||
# Use its --output argument to set the output file prefix to specify an absolute path and prefix | ||
# out_dir = Path(output_file).parent | ||
# PathLinker requires that the output directory exist | ||
# out_dir.mkdir(parents=True, exist_ok=True) | ||
bind_path, mapped_out_file = prepare_volume(output_file, work_dir) | ||
volumes.append(bind_path) | ||
# mapped_out_prefix = mapped_out_dir + '/out' # Use posix path inside the container | ||
|
||
# print(mapped_out_prefix) | ||
#TODO: change for local neighborhood | ||
command = ['python', | ||
'/LocalNeighborhood/local_neighborhood.py', | ||
'--network', network_file, | ||
'--nodes', node_file, | ||
'--output', mapped_out_file] | ||
|
||
print('Running Local Neighborhood with arguments: {}'.format(' '.join(command)), flush=True) | ||
|
||
container_suffix = "local-neighborhood" #TODO change | ||
out = run_container(container_framework, | ||
container_suffix, | ||
command, | ||
volumes, | ||
work_dir) | ||
print(out) | ||
|
||
# Rename the primary output file to match the desired output filename | ||
# Currently PathLinker only writes one output file so we do not need to delete others | ||
# We may not know the value of k that was used | ||
# output_edges = Path(next(out_dir.glob('out*-ranked-edges.txt'))) | ||
# output_edges.rename(output_file) | ||
|
||
|
||
@staticmethod | ||
def parse_output(raw_pathway_file, standardized_pathway_file): | ||
""" | ||
Convert a predicted pathway into the universal format | ||
@param raw_pathway_file: pathway file produced by an algorithm's run function | ||
@param standardized_pathway_file: the same pathway written in the universal format | ||
""" | ||
try: | ||
df = pd.read_csv(raw_pathway_file, sep='|', header=None) | ||
except pd.errors.EmptyDataError: | ||
with open(standardized_pathway_file, 'w'): | ||
pass | ||
return | ||
# df.columns = ["vertex1", "vertex2", "1"] | ||
df = add_rank_column(df) | ||
df = reinsert_direction_col_undirected(df) | ||
df.to_csv(standardized_pathway_file, index=False,header=False, sep='\t') | ||
|
This file contains 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
3 changes: 3 additions & 0 deletions
3
test/generate-inputs/expected/local_neighborhood-network-expected.txt
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
A|B | ||
C|B | ||
A|E |
This file contains 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
6 changes: 6 additions & 0 deletions
6
test/parse-outputs/expected/local_neighborhood-pathway-expected.txt
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
A B 1 U | ||
B C 1 U | ||
A D 1 U | ||
C D 1 U | ||
C E 1 U | ||
C F 1 U |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
A|B | ||
B|C | ||
A|D | ||
C|D | ||
C|E | ||
C|F |
This file contains 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