Skip to content

Commit

Permalink
Merge pull request #26 from NII-DG/patch-for-sapporo
Browse files Browse the repository at this point in the history
Patch for demo using Sapporo
  • Loading branch information
suecharo committed Apr 19, 2023
2 parents 49f0be2 + 0063be9 commit 8c684da
Show file tree
Hide file tree
Showing 18 changed files with 405 additions and 194,840 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -165,3 +165,7 @@ cython_debug/
# ---

**/ro-crate-metadata.json
sapporo_example/run
sapporo_example/results
sapporo_example/ro-crate-metadata.json
sapporo_example/ro-crate-metadata_failed.json
130 changes: 0 additions & 130 deletions sapporo_example/complete/ro-crate-metadata.json

This file was deleted.

32 changes: 32 additions & 0 deletions sapporo_example/compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
version: "3.5"
services:
nii-dg:
build:
context: ..
dockerfile: Dockerfile-api
image: nii-dg
container_name: nii-dg
ports:
- 0.0.0.0:5000:5000
restart: on-failure
networks:
- nii-dg-network
sapporo-service:
image: ghcr.io/sapporo-wes/sapporo-service:1.4.9
container_name: sapporo-service
volumes:
- ${PWD}/run:${PWD}/run
- /var/run/docker.sock:/var/run/docker.sock
- /tmp:/tmp
environment:
- SAPPORO_DEBUG=False
- SAPPORO_RUN_DIR=${PWD}/run
ports:
- 0.0.0.0:1122:1122
restart: on-failure
networks:
- nii-dg-network

networks:
nii-dg-network:
name: nii-dg-network
48 changes: 48 additions & 0 deletions sapporo_example/download_results.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
#!/usr/bin/env python3
# coding: utf-8

import argparse
import json
from pathlib import Path
from typing import List
from urllib.request import urlopen


def fetch_output_file_list(run_id: str, endpoint: str) -> List[str]:
with urlopen(f"{endpoint.rstrip('/')}/runs/{run_id}") as response:
result = json.load(response)
return [output["file_name"] for output in result["outputs"]]


def download_results(endpoint: str, run_id: str, output_dir: Path) -> None:
output_dir.mkdir(parents=True, exist_ok=True)
output_file_list = fetch_output_file_list(run_id, endpoint)

for output_file in output_file_list:
with urlopen(f"{endpoint.rstrip('/')}/runs/{run_id}/data/outputs/{output_file}") as response:
file_path = output_dir.joinpath("outputs").joinpath(output_file).resolve()
file_path.parent.mkdir(parents=True, exist_ok=True)
with open(file_path, "wb") as f:
f.write(response.read())

# download run_request.json
with urlopen(f"{endpoint.rstrip('/')}/runs/{run_id}/data/run_request.json") as response:
file_path = output_dir.joinpath("run_request.json").resolve()
with open(file_path, "wb") as f:
f.write(response.read())


def main() -> None:
parser = argparse.ArgumentParser()
parser.add_argument("endpoint", help="The endpoint to send the request (default: http://localhost:1122)",
default="http://localhost:1122", nargs="?")
parser.add_argument("run_id", help="The RUN_ID of the executed workflow")
parser.add_argument("output_dir", help="The output directory to save the results (default: ./results)",
default="./results", nargs="?")
args = parser.parse_args()

download_results(args.endpoint, args.run_id, Path(args.output_dir).resolve())


if __name__ == "__main__":
main()
46 changes: 46 additions & 0 deletions sapporo_example/execute_workflow.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
#!/usr/bin/env python3
# coding: utf-8

import argparse
import json
from urllib.parse import urlencode
from urllib.request import Request, urlopen


def execute_workflow(endpoint: str) -> None:
data = {
"workflow_params": json.dumps({
"fastq_1": {"location": "ERR034597_1.small.fq.gz", "class": "File"},
"fastq_2": {"location": "ERR034597_2.small.fq.gz", "class": "File"},
"nthreads": 2
}),
"workflow_type": "CWL",
"workflow_type_version": "v1.0",
"workflow_url": "https://raw.githubusercontent.com/sapporo-wes/sapporo-service/main/tests/resources/cwltool/trimming_and_qc.cwl",
"workflow_engine_name": "cwltool",
"workflow_attachment": json.dumps([
{"file_url": "https://raw.githubusercontent.com/sapporo-wes/sapporo-service/main/tests/resources/cwltool/ERR034597_2.small.fq.gz", "file_name": "ERR034597_2.small.fq.gz"},
{"file_url": "https://raw.githubusercontent.com/sapporo-wes/sapporo-service/main/tests/resources/cwltool/ERR034597_1.small.fq.gz", "file_name": "ERR034597_1.small.fq.gz"}
])
}

data = urlencode(data).encode("utf-8") # type: ignore
headers = {"Content-Type": "application/x-www-form-urlencoded"}
# remove trailing slash from endpoint
request = Request(f"{endpoint.rstrip('/')}/runs", data=data, headers=headers) # type: ignore

with urlopen(request) as response:
result = json.load(response)
print(result)


def main() -> None:
parser = argparse.ArgumentParser()
parser.add_argument("endpoint", help="The endpoint to send the request (default: http://localhost:1122)",
default="http://localhost:1122", nargs="?")
args = parser.parse_args()
execute_workflow(args.endpoint)


if __name__ == "__main__":
main()

0 comments on commit 8c684da

Please sign in to comment.