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

3482 update output format #5

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
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
38 changes: 25 additions & 13 deletions src/ontoform/transformers/homologue.py
Original file line number Diff line number Diff line change
@@ -1,21 +1,33 @@
import json
import subprocess
from pathlib import Path
from typing import BinaryIO
from uuid import uuid4

import polars as pl

def transform(src: Path, dst: Path) -> None:
# load the homologues
initial = pl.read_json(src)

# prepare node data
inputGenes = pl.DataFrame(
initial['genes']
)
def transform(src: BinaryIO, dst: BinaryIO) -> None:
jq_command = '.genes | {"genes": map({id: .id, name: .name})}'

# Generate a temporary file path tp store the source file
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
# Generate a temporary file path tp store the source file
# Generate a temporary file path to store the source file

temp_path = Path(f'/tmp/{uuid4()}.json')

# extract genes list
genes_list = inputGenes.explode('genes').unnest('genes')
temp_path.write_bytes(src.read())

# read id and name
output = genes_list.select(["id","name"])
# Ejecuta jq con subprocess
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should keep comments in english

result = subprocess.run(
['jq', jq_command, temp_path],
capture_output=True,
text=True
)

# write the result
output.write_ndjson(dst)
# Verifica si el comando se ejecutó correctamente
Copy link
Contributor

@javfg javfg Nov 4, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here!

if result.returncode == 0:
output = json.loads(result.stdout)
input_genes = pl.DataFrame(output, strict=False, infer_schema_length=3)
# extract genes list
genes_list = input_genes.unnest('genes')
# # read id and name
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
# # read id and name
# read id and name

output = genes_list.select(['id', 'name'])
output.write_csv(dst, separator='\t', include_header=True)