I gave a call in this way:

print("-L", str(cfg.project["phenotype"][0]), str(cfg.project["phenotype"][1]))

This will read the below one
phenotype:
- embryo: [SAMN00990702-1]
- larva: [SAMN00990702-2]

And it should give:

-L embryo,larva

But it is giving like:

-L {'embryo': ['SAMN00990702-1']} {'larva': ['SAMN00990702-2']}

Can anyone help me in solving this problem. Thank you !!

Dani AI

Generated

The printed output shows dictionary objects because the YAML loader produced a sequence of single-key mappings (e.g. a list whose items are {label: [samples]}). Instead of printing the dicts' repr, extract the mapping keys and join them into the comma string that the tool expects. This follows the same idea proposed by (extract the key), but covers variable-length phenotype lists and shows a safer way to build the command.

A compact, robust way to get the labels (works if an item is already a simple string or a single-key dict) is:

pheno_entries = cfg.project["phenotype"]
labels = ",".join(
    str(list(entry)[0]) if isinstance(entry, dict) and entry else str(entry)
    for entry in pheno_entries
)
print("-L " + labels)

For running cuffdiff, avoid concatenating a big shell string; build an argument list and run it without shell interpolation to preserve quoting and prevent subtle splitting/injection issues:

import subprocess, shlex

cmd = [
    cfg.tool_cmd("cuffdiff"),
    "-p", str(cfg.project["analysis"]["threads"]),
    "-b", cfg.project["genome"]["fasta"],
    "-u", cfg.project["experiment"]["merged"],
    "-L", labels,
    "-o", output_folder,
] + [s["files"]["bam"] for s in cfg.project["samples"][:2]]

print(" ".join(shlex.quote(a) for a in cmd))
subprocess.run(cmd, check=True)

Troubleshooting notes: verify the structure with a quick debug print of the full object (type and value) if labels still look wrong. Also avoid the syntax in Post #3 that tried to call cfg.project as a function; use indexing like cfg.project['phenotype'][0]. If phenotype labels may contain commas or spaces, quote or escape them according to the cuffdiff CLI expectations.

Recommended Answers

All 5 Replies

It means that cfg.project["phenotype"][0] is a dictionary with a single key and you only want to print the key. You can get a key from a non empty dictionary with next(iter(dictionary)). So I suggest

d = cfg.project["phenotype"]
print("-L", next(iter(d[0])), next(iter(d[1])))

Of course, if there are more than 1 key in d[i], some keys won't be shown.

I should not replace cfg.project["phenotype"] with "d"

So, Can I give it in this way:

"-L", str(cfg.project(["phenotype"][0])), str(cfg.project(["phenotype"][1]))

Why shouldn't you replace cfg.project["phenotype"] with d ? d is shorter.

actually this is my whole command:

print(' '.join([cfg.tool_cmd("cuffdiff"), "-p", str(cfg.project["analysis"]["threads"]), "-b", str(cfg.project["genome"]["fasta"]), "-u", cfg.project["experiment"]["merged"], "-L", str(cfg.project(["phenotype"][0])), str(cfg.project(["phenotype"][1])), "-o", output_folder] + [cfg.project["samples"][0]["files"]["bam"] + ' ' + cfg.project["samples"][1]["files"]["bam"]], cfg.project["analysis"]["log_file"])

You can try something such as

p = cfg.project

command = (
    "{cuffdiff} -p {threads} -b {fasta} -u {merged} "
    "-L {pheno0},{pheno1} -o {ofolder} {bam0} {bam1} {log}"
    ).format(
    cuffdiff=cfg.tool_cmd("cuffdiff"),
    threads=p["analysis"]["threads"],
    fasta=p["genome"]["fasta"],
    merged=p["experiment"]["merged"],
    pheno0=p["phenotype"][0],
    pheno1=p["phenotype"][1],
    ofolder=output_folder,
    bam0=p["samples"][0]["files"]["bam"],
    bam1=p["samples"][1]["files"]["bam"],
    log=p["analysis"]["log_file"]
)

print(command)

with your corrections. (for example, you may try to replace p["phenotype"][0] with next(iter(p["phenotype"][0]))).

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.