我如何指定通过bash脚本运行的python脚本中要使用的CPU内核数?

问题描述 投票:0回答:1

我想将一些fasta文件与基因组对齐,并且要快速完成此操作,我有一个python脚本,该脚本在目录中循环以仅选择某些文件。我使用了子流程,如果我没有在循环中运行它,那么我会把它放在基本的bash脚本中。问题是我想并行运行它,但是在子进程中使用$ NSLOTS不起作用。

我的bash脚本是这样的:

#!/bin/bash --login
#$ -cwd
#$ -pe smp.pe 8 #specifies to use 8 cores

module load apps/binapps/anaconda3/2019.03  #Python 3.7.3
module load apps/bioinf
module load apps/star/2.5.3a/gcc-4.8.5

python STAR_aligner.py

我的python脚本是这样的:

import subprocess
import os

directory = '/mnt/fls01-home01/mfbx3sgh/scratch/Trimming'
genome_idx = '/mnt/fls01-home01/mfbx3sgh/scratch/Genome_Index'
genome_dir = '/mnt/fls01-home01/mfbx3sgh/scratch/Genome/'

file_list = os.listdir(directory)

for read_1 in file_list:

    if 'paired' in read_1:

        if '_1_' in read_1:

            read_2 = read_1.replace('_1_', '_2_')

            subprocess.run(['STAR', '--runThreadN', '$NSLOTS', '--genomeDir', genome_idx, '--readFilesIn', read_1,
                            read_2, '--readFilesCommand', 'gunzip', '-c', '--outFileNamePrefix', genome_dir])

正如您所看到的,我在bash脚本中指定使用8个内核,然后在子进程中,我调用$ NSLOTS,当它不在python脚本中时,它将与指定的内核一起使用。

我得到的错误是

EXITING: fatal input ERROR: runThreadN must be >0, user-defined runThreadN=0

Sep 30 11:52:24 ...... FATAL ERROR, exiting
python python-3.x bash parallel-processing subprocess
1个回答
0
投票

Python脚本似乎根本没有添加任何值,您应该将其全部编写为Bash脚本。

#!/bin/bash
root="mnt/fls01-home01/mfbx3sgh/scratch"
for read_1 in "$root/Trimming/"*; do
    case $read_1 in
     *paired*_1_*|*_1_*paired*)
        STAR --runThreadN "$NSLOTS" --genomeDir "$root/Genome_Index/" \
          --readFilesIn "$read_1" "${read_1//_1_/_2_}" \
          --readFilesCommand gunzip -c --outFileNamePrefix "$root/Genome/" ;;
    esac
done

如果您的问题是如何正确使用Python中的$NSLOTS,那将是

os.environ['NSLOTS']

(字符串'$NSLOTS'就是静态字符串$NS等。]

© www.soinside.com 2019 - 2024. All rights reserved.