Metaphlan4 calculate_diversity.R error

Hello! I am trying to calculate alpha and beta diversity using the script calculate_diversity.R which is present in the packages of Metaphlan4. I run the command in the conda environment where I installed Metaphlan4, but it does not work.

The command I used to calculate beta diversity:
Rscript calculate_diversity.R -f merged_metaphlan_samples.tsv -d beta -m bray-curtis

Fatal Error:
Cannot open file ‘calculate_diversity.R’: No such file or directory

Any input will be appreciated.

Hi @angel
Have you try to call the script using the absolute path to it?
Rscript /absolute_path/calculate_diversity.R -f merged_metaphlan_samples.tsv -d beta -m bray-curtis

1 Like

Hi @aitor.blancomiguez

Thanks for the suggestion. Using the absolute path fixed the issue.

Hi! I am encountering the same issue but am having trouble finding the absolute path. I used the path to the directory where I created the metaphlan environment, but I get the same error.

For example, these were the commands I used to create and activate the environment:

cd /workdir/slg/diet_manip/metaphlan4_v2
conda install -c conda-forge -c bioconda metaphlan
conda create --name mpa -c conda-forge -c bioconda python=3.7 metaphlan
conda update -n base -c defaults conda
conda activate mpa

And this is how I am trying to calculate diversity after getting my merged table:

Rscript /workdir/slg/diet_manip/metaphlan4_v2/calculate_diversity.R -f merged_abundance_table.txt -d alpha -m jaccard

I don’t see a utils folder anywhere

Any help is appreciated. Thank you!

Hi @samanthagoldman
Running metaphlan --help will provide you the path where the MetaPhlAn scripts are installed. If you look at the default value of the --bowtie2db , e.g:

Mapping arguments:                                                                                                                                                                                                 
  --force               Force profiling of the input file by removing the bowtie2out file                                                                                                                          
  --bowtie2db METAPHLAN_BOWTIE2_DB                                                                                                                                                                                 
                        Folder containing the MetaPhlAn database. You can specify the location by exporting the DEFAULT_DB_FOLDER variable in the shell.[default /etc/anaconda3/e
nvs/metaphlan-4/lib/python3.9/site-packages/MetaPhlAn/metaphlan/metaphlan_databases]

Your metaphlan and utils script will be in the parent folder of the databases. E.g in my case will be

/etc/anaconda3/e
nvs/metaphlan-4/lib/python3.9/site-packages/MetaPhlAn/metaphlan

I am also encountering a similar problem.
Rscript /absolute_path /calculate_diversity.R -f merged_metaphlan_samples.tsv -d beta -m bray-curtis
I tried this command. However, I am getting an error as below.

Installing package into ‘/usr/local/lib/R/site-library’
(as ‘lib’ is unspecified)
Warning in install.packages(x, repos = “http://cran.us.r-project.org”) :
‘lib = “/usr/local/lib/R/site-library”’ is not writable
Error in install.packages(x, repos = “http://cran.us.r-project.org”) :
unable to install packages
Calls: sapply → lapply → FUN → install.packages
Execution halted

How to find the Rscript or activate it in this case or make the path writable?

Thank you

I have this issue too!

Tried using chmod 777 to overwrite but it didn’t work.

Rscript $UTILS/calculate_diversity.R -f $OUTPUT/abundance_tables/merged_metaphlan_abundance_table.txt -d alpha -m shannon
Warning in install.packages(x, repos = “http://cran.us.r-project.org”) :
‘lib = “/apps/R/4.4.0-mkl/lib64/R/library”’ is not writable
Error in install.packages(x, repos = “http://cran.us.r-project.org”) :
unable to install packages
Calls: sapply → lapply → FUN → install.packages
Execution halted

Hi chikusquish,

I get it work by getting the absolute path to Rscript as well.