czbiohub-sf / MIDAS

Metagenomic Intra-Species Diversity Analysis (MIDAS)
MIT License
35 stars 10 forks source link

merge function writing chunks in database folder which may not be writeable #107

Closed FranckLejzerowicz closed 1 year ago

FranckLejzerowicz commented 1 year ago

Hello,

We are running MIDAS2 on our computer cluster, where I was given temporary rights to install the database in a shared location, for many users to enjoy your tool. Then, the admins removed my rights to write in the install location (which also relieves my storage quota ;) ).

The problem is that when running the merge command, MIDAS2 is trying to write chunks at this database location, where a user may not have write access rights.

See below the stderr for the job affected by this (and the command):

midas2 merge_snps \
    --num_cores 12 \
    --midasdb_name gtdb \
    --midasdb_dir /cluster/shared/databases/MIDSA2/latest/gtdb \
    --genome_depth 5.0 \
    --sample_counts 2 \
    --site_depth 2 \
    --site_ratio 3.0 \
    --site_prev 0.9 \
    --snv_type common \
    --snp_pooled_method prevalence \
    --snp_maf 0.1 \
    --snp_type {bi,tri,quad} \
    --locus_type any \
    --force \
    --samples_list \
    ${SCRATCH_FOLDER}/cluster/projects/nn8075k/federica/outputs/midas2_merge/after_midas2_78f86426289cf46f1cc5/illumina/gtdb/all_species/sample_list.txt \
    ${SCRATCH_FOLDER}/cluster/projects/nn8075k/federica/outputs/midas2_merge/after_midas2_78f86426289cf46f1cc5/illumina/gtdb/all_species
$ cat outputs/midas2_merge/after_midas2_78f86426289cf46f1cc5/jobs/output/slurm-midas2_merge.fdprf.mds2_78f86426289cf46f1cc5._illumina-midas2_snps_7425188.e
The following modules were not unloaded:
  (Use "module --force purge" to unload all):

  1) StdEnv
1673959603.1:  Across samples population SNV calling in subcommand merge_snps with args
1673959603.1:  {
1673959603.1:      "subcommand": "merge_snps",
1673959603.1:      "force": true,
1673959603.1:      "debug": false,
1673959603.1:      "zzz_worker_mode": false,
1673959603.1:      "batch_branch": "master",
1673959603.1:      "batch_memory": 378880,
1673959603.1:      "batch_vcpus": 48,
1673959603.1:      "batch_queue": "pairani",
1673959603.1:      "batch_ecr_image": "pairani:latest",
1673959603.1:      "midas_outdir": "/cluster/work/jobs/7425188/cluster/projects/nn8075k/federica/outputs/midas2_merge/after_midas2_78f86426289cf46f1cc5/illumina/gtdb/all_species",
1673959603.1:      "samples_list": "/cluster/work/jobs/7425188/cluster/projects/nn8075k/federica/outputs/midas2_merge/after_midas2_78f86426289cf46f1cc5/illumina/gtdb/all_species/sample_list.txt",
1673959603.1:      "midasdb_name": "gtdb",
1673959603.1:      "midasdb_dir": "/cluster/shared/databases/MIDSA2/latest/gtdb",
1673959603.1:      "species_list": null,
1673959603.1:      "genome_depth": 5.0,
1673959603.1:      "genome_coverage": 0.4,
1673959603.1:      "sample_counts": 2,
1673959603.1:      "site_depth": 2,
1673959603.1:      "site_ratio": 3.0,
1673959603.1:      "site_prev": 0.9,
1673959603.1:      "snv_type": "common",
1673959603.1:      "snp_pooled_method": "prevalence",
1673959603.1:      "snp_maf": 0.1,
1673959603.1:      "snp_type": [
1673959603.1:          "bi",
1673959603.1:          "tri",
1673959603.1:          "quad"
1673959603.1:      ],
1673959603.1:      "locus_type": [
1673959603.1:          "any"
1673959603.1:      ],
1673959603.1:      "num_cores": 12,
1673959603.1:      "chunk_size": 1000000,
1673959603.1:      "advanced": false,
1673959603.1:      "robust_chunk": false
1673959603.1:  }
1673959603.5:  98 species pass the filter
1673959603.5:  Create OUTPUT directory.
1673959603.5:  'rm -rf /cluster/work/jobs/7425188/cluster/projects/nn8075k/federica/outputs/midas2_merge/after_midas2_78f86426289cf46f1cc5/illumina/gtdb/all_species/snps'
1673959603.5:  'mkdir -p /cluster/work/jobs/7425188/cluster/projects/nn8075k/federica/outputs/midas2_merge/after_midas2_78f86426289cf46f1cc5/illumina/gtdb/all_species/snps'
1673959603.5:  Create TEMP directory.
1673959603.5:  'rm -rf /cluster/work/jobs/7425188/cluster/projects/nn8075k/federica/outputs/midas2_merge/after_midas2_78f86426289cf46f1cc5/illumina/gtdb/all_species/temp/snps'
1673959603.5:  'mkdir -p /cluster/work/jobs/7425188/cluster/projects/nn8075k/federica/outputs/midas2_merge/after_midas2_78f86426289cf46f1cc5/illumina/gtdb/all_species/temp/snps'
1673959606.4:  MIDAS2::write_species_summary::start
1673959606.4:  MIDAS2::write_species_summary::finish
1673959607.9:  MIDAS2::design_chunks::start
mkdir: cannot create directory ‘/cluster/shared/databases/MIDSA2/latest/gtdb/temp/chunksize.1000000/120476’: Permission denied
mkdir: cannot create directory ‘/cluster/shared/databases/MIDSA2/latest/gtdb/temp/chunksize.1000000/110537’: Permission denied
mkdir: cannot create directory ‘/cluster/shared/databases/MIDSA2/latest/gtdb/temp/chunksize.1000000/144385’: Permission denied
mkdir: cannot create directory ‘/cluster/shared/databases/MIDSA2/latest/gtdb/temp/chunksize.1000000/106379’: Permission denied
mkdir: cannot create directory ‘/cluster/shared/databases/MIDSA2/latest/gtdb/temp/chunksize.1000000/126839’: Permission denied
mkdir: cannot create directory ‘/cluster/shared/databases/MIDSA2/latest/gtdb/temp/chunksize.1000000/113335’: Permission denied
mkdir: cannot create directory ‘/cluster/shared/databases/MIDSA2/latest/gtdb/temp/chunksize.1000000/131364’: Permission denied
mkdir: cannot create directory ‘/cluster/shared/databases/MIDSA2/latest/gtdb/temp/chunksize.1000000/123321’: Permission denied
mkdir: cannot create directory ‘/cluster/shared/databases/MIDSA2/latest/gtdb/temp/chunksize.1000000/141587’: Permission denied
mkdir: cannot create directory ‘/cluster/shared/databases/MIDSA2/latest/gtdb/temp/chunksize.1000000/102787’: Permission denied
mkdir: cannot create directory ‘/cluster/shared/databases/MIDSA2/latest/gtdb/temp/chunksize.1000000/117262’: Permission denied
mkdir: cannot create directory ‘/cluster/shared/databases/MIDSA2/latest/gtdb/temp/chunksize.1000000/108799’: Permission denied
mkdir: cannot create directory ‘/cluster/shared/databases/MIDSA2/latest/gtdb/temp/chunksize.1000000/141985’: Permission denied
mkdir: cannot create directory ‘/cluster/shared/databases/MIDSA2/latest/gtdb/temp/chunksize.1000000/101791’: Permission denied
mkdir: cannot create directory ‘/cluster/shared/databases/MIDSA2/latest/gtdb/temp/chunksize.1000000/106238’: Permission denied
mkdir: cannot create directory ‘/cluster/shared/databases/MIDSA2/latest/gtdb/temp/chunksize.1000000/116023’: Permission denied
mkdir: cannot create directory ‘/cluster/shared/databases/MIDSA2/latest/gtdb/temp/chunksize.1000000/101899’: Permission denied
mkdir: cannot create directory ‘/cluster/shared/databases/MIDSA2/latest/gtdb/temp/chunksize.1000000/123465’: Permission denied
mkdir: cannot create directory ‘/cluster/shared/databases/MIDSA2/latest/gtdb/temp/chunksize.1000000/113950’: Permission denied
mkdir: cannot create directory ‘/cluster/shared/databases/MIDSA2/latest/gtdb/temp/chunksize.1000000/143698’: Permission denied
mkdir: cannot create directory ‘/cluster/shared/databases/MIDSA2/latest/gtdb/temp/chunksize.1000000/125635’: Permission denied
mkdir: cannot create directory ‘/cluster/shared/databases/MIDSA2/latest/gtdb/temp/chunksize.1000000/130996’: Permission denied
mkdir: cannot create directory ‘/cluster/shared/databases/MIDSA2/latest/gtdb/temp/chunksize.1000000/118769’: Permission denied
mkdir: cannot create directory ‘/cluster/shared/databases/MIDSA2/latest/gtdb/temp/chunksize.1000000/110920’: Permission denied
mkdir: cannot create directory ‘/cluster/shared/databases/MIDSA2/latest/gtdb/temp/chunksize.1000000/139883’: Permission denied
mkdir: cannot create directory ‘/cluster/shared/databases/MIDSA2/latest/gtdb/temp/chunksize.1000000/121846’: Permission denied
mkdir: cannot create directory ‘/cluster/shared/databases/MIDSA2/latest/gtdb/temp/chunksize.1000000/127445’: Permission denied
mkdir: cannot create directory ‘/cluster/shared/databases/MIDSA2/latest/gtdb/temp/chunksize.1000000/122185’: Permission denied
mkdir: cannot create directory ‘/cluster/shared/databases/MIDSA2/latest/gtdb/temp/chunksize.1000000/141780’: Permission denied
mkdir: cannot create directory ‘/cluster/shared/databases/MIDSA2/latest/gtdb/temp/chunksize.1000000/147354’: Permission denied
mkdir: cannot create directory ‘/cluster/shared/databases/MIDSA2/latest/gtdb/temp/chunksize.1000000/102854’: Permission denied
mkdir: cannot create directory ‘/cluster/shared/databases/MIDSA2/latest/gtdb/temp/chunksize.1000000/114718’: Permission denied
mkdir: cannot create directory ‘/cluster/shared/databases/MIDSA2/latest/gtdb/temp/chunksize.1000000/110932’: Permission denied
mkdir: cannot create directory ‘/cluster/shared/databases/MIDSA2/latest/gtdb/temp/chunksize.1000000/108804’: Permission denied
mkdir: cannot create directory ‘/cluster/shared/databases/MIDSA2/latest/gtdb/temp/chunksize.1000000/136285’: Permission denied
mkdir: cannot create directory ‘/cluster/shared/databases/MIDSA2/latest/gtdb/temp/chunksize.1000000/137039’: Permission denied
mkdir: cannot create directory ‘/cluster/shared/databases/MIDSA2/latest/gtdb/temp/chunksize.1000000/128710’: Permission denied
mkdir: cannot create directory ‘/cluster/shared/databases/MIDSA2/latest/gtdb/temp/chunksize.1000000/126638’: Permission denied
mkdir: cannot create directory ‘/cluster/shared/databases/MIDSA2/latest/gtdb/temp/chunksize.1000000/142444’: Permission denied
mkdir: cannot create directory ‘/cluster/shared/databases/MIDSA2/latest/gtdb/temp/chunksize.1000000/114432’: Permission denied
mkdir: cannot create directory ‘/cluster/shared/databases/MIDSA2/latest/gtdb/temp/chunksize.1000000/147309’: Permission denied
mkdir: cannot create directory ‘/cluster/shared/databases/MIDSA2/latest/gtdb/temp/chunksize.1000000/116721’: Permission denied
mkdir: cannot create directory ‘/cluster/shared/databases/MIDSA2/latest/gtdb/temp/chunksize.1000000/134149’: Permission denied
mkdir: cannot create directory ‘/cluster/shared/databases/MIDSA2/latest/gtdb/temp/chunksize.1000000/138056’: Permission denied
mkdir: cannot create directory ‘/cluster/shared/databases/MIDSA2/latest/gtdb/temp/chunksize.1000000/111522’: Permission denied
mkdir: cannot create directory ‘/cluster/shared/databases/MIDSA2/latest/gtdb/temp/chunksize.1000000/128099’: Permission denied
mkdir: cannot create directory ‘/cluster/shared/databases/MIDSA2/latest/gtdb/temp/chunksize.1000000/117326’: Permission denied
mkdir: cannot create directory ‘/cluster/shared/databases/MIDSA2/latest/gtdb/temp/chunksize.1000000/106410’: Permission denied
mkdir: cannot create directory ‘/cluster/shared/databases/MIDSA2/latest/gtdb/temp/chunksize.1000000/109726’: Permission denied
mkdir: cannot create directory ‘/cluster/shared/databases/MIDSA2/latest/gtdb/temp/chunksize.1000000/130446’: Permission denied
mkdir: cannot create directory ‘/cluster/shared/databases/MIDSA2/latest/gtdb/temp/chunksize.1000000/135207’: Permission denied
mkdir: cannot create directory ‘/cluster/shared/databases/MIDSA2/latest/gtdb/temp/chunksize.1000000/125475’: Permission denied
mkdir: cannot create directory ‘/cluster/shared/databases/MIDSA2/latest/gtdb/temp/chunksize.1000000/140833’: Permission denied
mkdir: cannot create directory ‘/cluster/shared/databases/MIDSA2/latest/gtdb/temp/chunksize.1000000/104567’: Permission denied
mkdir: cannot create directory ‘/cluster/shared/databases/MIDSA2/latest/gtdb/temp/chunksize.1000000/136688’: Permission denied
mkdir: cannot create directory ‘/cluster/shared/databases/MIDSA2/latest/gtdb/temp/chunksize.1000000/116478’: Permission denied
mkdir: cannot create directory ‘/cluster/shared/databases/MIDSA2/latest/gtdb/temp/chunksize.1000000/140766’: Permission denied
mkdir: cannot create directory ‘/cluster/shared/databases/MIDSA2/latest/gtdb/temp/chunksize.1000000/113916’: Permission denied
mkdir: cannot create directory ‘/cluster/shared/databases/MIDSA2/latest/gtdb/temp/chunksize.1000000/107040’: Permission denied
mkdir: cannot create directory ‘/cluster/shared/databases/MIDSA2/latest/gtdb/temp/chunksize.1000000/116797’: Permission denied
mkdir: cannot create directory ‘/cluster/shared/databases/MIDSA2/latest/gtdb/temp/chunksize.1000000/142790’: Permission denied
mkdir: cannot create directory ‘/cluster/shared/databases/MIDSA2/latest/gtdb/temp/chunksize.1000000/117786’: Permission denied
mkdir: cannot create directory ‘/cluster/shared/databases/MIDSA2/latest/gtdb/temp/chunksize.1000000/136029’: Permission denied
mkdir: cannot create directory ‘/cluster/shared/databases/MIDSA2/latest/gtdb/temp/chunksize.1000000/120329’: Permission denied
mkdir: cannot create directory ‘/cluster/shared/databases/MIDSA2/latest/gtdb/temp/chunksize.1000000/103143’: Permission denied
mkdir: cannot create directory ‘/cluster/shared/databases/MIDSA2/latest/gtdb/temp/chunksize.1000000/140084’: Permission denied
mkdir: cannot create directory ‘/cluster/shared/databases/MIDSA2/latest/gtdb/temp/chunksize.1000000/106335’: Permission denied
mkdir: cannot create directory ‘/cluster/shared/databases/MIDSA2/latest/gtdb/temp/chunksize.1000000/144859’: Permission denied
mkdir: cannot create directory ‘/cluster/shared/databases/MIDSA2/latest/gtdb/temp/chunksize.1000000/129716’: Permission denied
mkdir: cannot create directory ‘/cluster/shared/databases/MIDSA2/latest/gtdb/temp/chunksize.1000000/110859’: Permission denied
mkdir: cannot create directory ‘/cluster/shared/databases/MIDSA2/latest/gtdb/temp/chunksize.1000000/143588’: Permission denied
mkdir: cannot create directory ‘/cluster/shared/databases/MIDSA2/latest/gtdb/temp/chunksize.1000000/131067’: Permission denied
mkdir: cannot create directory ‘/cluster/shared/databases/MIDSA2/latest/gtdb/temp/chunksize.1000000/138633’: Permission denied
mkdir: cannot create directory ‘/cluster/shared/databases/MIDSA2/latest/gtdb/temp/chunksize.1000000/121261’: Permission denied
mkdir: cannot create directory ‘/cluster/shared/databases/MIDSA2/latest/gtdb/temp/chunksize.1000000/127200’: Permission denied
mkdir: cannot create directory ‘/cluster/shared/databases/MIDSA2/latest/gtdb/temp/chunksize.1000000/139834’: Permission denied
mkdir: cannot create directory ‘/cluster/shared/databases/MIDSA2/latest/gtdb/temp/chunksize.1000000/130720’: Permission denied
mkdir: cannot create directory ‘/cluster/shared/databases/MIDSA2/latest/gtdb/temp/chunksize.1000000/140619’: Permission denied
1673959648.2:  Deleting untrustworthy outputs due to error. Specify --debug flag to keep.
Traceback (most recent call last):
  File "/cluster/projects/nn8075k/conda_envs/midas2/bin/midas2", line 10, in <module>
    sys.exit(main())
  File "/cluster/projects/nn8075k/conda_envs/midas2/lib/python3.7/site-packages/midas2/__main__.py", line 24, in main
    return subcommand_main(subcommand_args)
  File "/cluster/projects/nn8075k/conda_envs/midas2/lib/python3.7/site-packages/midas2/subcommands/merge_snps.py", line 664, in main
    merge_snps(args)
  File "/cluster/projects/nn8075k/conda_envs/midas2/lib/python3.7/site-packages/midas2/subcommands/merge_snps.py", line 658, in merge_snps
    raise error
  File "/cluster/projects/nn8075k/conda_envs/midas2/lib/python3.7/site-packages/midas2/subcommands/merge_snps.py", line 639, in merge_snps
    arguments_list = design_chunks(species_ids_of_interest, midas_db)
  File "/cluster/projects/nn8075k/conda_envs/midas2/lib/python3.7/site-packages/midas2/subcommands/merge_snps.py", line 220, in design_chunks
    all_site_chunks = multithreading_map(design_chunks_per_species, [(sp, midas_db) for sp in dict_of_species.values()], num_cores) #<---
  File "/cluster/projects/nn8075k/conda_envs/midas2/lib/python3.7/site-packages/midas2/common/utils.py", line 540, in multithreading_map
    return _multi_map(func, items, num_threads, ThreadPool)
  File "/cluster/projects/nn8075k/conda_envs/midas2/lib/python3.7/site-packages/midas2/common/utils.py", line 520, in _multi_map
    return p.map(func, items, chunksize=1)
  File "/cluster/projects/nn8075k/conda_envs/midas2/lib/python3.7/multiprocessing/pool.py", line 268, in map
    return self._map_async(func, iterable, mapstar, chunksize).get()
  File "/cluster/projects/nn8075k/conda_envs/midas2/lib/python3.7/multiprocessing/pool.py", line 657, in get
    raise self._value
  File "/cluster/projects/nn8075k/conda_envs/midas2/lib/python3.7/multiprocessing/pool.py", line 121, in worker
    result = (True, func(*args, **kwds))
  File "/cluster/projects/nn8075k/conda_envs/midas2/lib/python3.7/multiprocessing/pool.py", line 44, in mapstar
    return list(map(*args))
  File "/cluster/projects/nn8075k/conda_envs/midas2/lib/python3.7/site-packages/midas2/subcommands/merge_snps.py", line 205, in design_chunks_per_species
    return sp.compute_snps_chunks(midas_db, chunk_size, "merge")
  File "/cluster/projects/nn8075k/conda_envs/midas2/lib/python3.7/site-packages/midas2/models/species.py", line 103, in compute_snps_chunks
    command(f"mkdir -p {os.path.dirname(loc_fp)}")
  File "/cluster/projects/nn8075k/conda_envs/midas2/lib/python3.7/site-packages/midas2/common/utils.py", line 246, in command
    return subprocess.run(cmd, shell=shell, **subproc_args)
  File "/cluster/projects/nn8075k/conda_envs/midas2/lib/python3.7/subprocess.py", line 512, in run
    output=stdout, stderr=stderr)
subprocess.CalledProcessError: Command 'mkdir -p /cluster/shared/databases/MIDSA2/latest/gtdb/temp/chunksize.1000000/120476' returned non-zero exit status 1.

Is there a way to tell MIDAS2 to write elsewhere, maybe in a $TMPDIR location?

Thanks! F

zhaoc1 commented 1 year ago

Hi,

MIDAS2 actually writes the temporary merging chunks into the midas_outdir (line 37). From the log, what MIDAS2 was doing is to generate the chunks cache of the contig ids for the repgenome. Because this is related to the MIDASDB and only needed to be run once for a given chunksize, I put it under the midas_db (line 56). Thanks for the issue. I overlooked the potential writing access problem to local MIDASDB. I will update the fix in the next version.

For a quick and dirty fix, we can modify the _get_local_path() function as following:

def _get_local_path(file_name, db_dir):
    """ Append local MIDAS_DB path to file(s) """
    if isinstance(file_name, list):
        return [os.path.join(db_dir, f"{fn}") for fn in file_name]
    if file_name == "chunks_contig_lists":
       return os.path.join(`NEW_TEMP_DB`, f"{file_name}"
   else:
       return os.path.join(db_dir, f"{file_name}")

And replace the new_temp_db with the absolute path that you would like to place the cache of the repgenome, build and install MIDAS2 locally.

Hope this can solve the problem.

Thanks Chunyu

FranckLejzerowicz commented 1 year ago

Yep - that should do and I will let you know ASAP. Thanks for pointing me to the code to edit so quickly - Kudos on a very versatile tool! High hope to cite you soon:)

FranckLejzerowicz commented 1 year ago

Hi @zhaoc1,

I have slightly fixed your fix, since file_name is never "chunks_contig_lists". Indeed, "chunks_contig_lists" is the key to the dict looked up in get_midasdb_layout() (called from construct_dest_path()), and not the value of this dict, which is returned and fortunately, is the only value starting with temp.

Hence, I could adapt the fix like so:

def _get_local_path(file_name, db_dir):
    """ Append local MIDAS_DB path to file(s) """
    if isinstance(file_name, list):
        return [os.path.join(db_dir, f"{fn}") for fn in file_name]
    if file_name.startswith("temp"):
        return os.path.join(os.environ['TMPDIR'], f"{file_name}")
    else:
        return os.path.join(db_dir, f"{file_name}")

(In my jobs there is a $TMPDIR variable that does to trick)

And it works!

Thanks again for the neat code it was fun to look at ;) Best Franck

zhaoc1 commented 1 year ago

You are very welcome. My memory of the code is a little rusty and appreciate the nice fix!