1

I am running a command using sbatch for a tool on the server, that takes a lot of space. The command uses some directories (some files) from a main directory(tq_first) in a for loop. I was wondering if there was a way to delete the directories once it's iterated in the loop and completely executed? will the remove command work there without interfering with the loop?

For e.g. for this loop

set -eu 


export PATH=/home/bin:${PATH}
reference_dir=/mnt/scratchb/REF


for fastq_dir in fastq_first/*; do
    barcode=`basename ${fastq_dir}`

    cmd="cellranger count \
        --id=${barcode} \
        --fastqs= ${fastq_dir}   \
        --sample=${barcode} \
        --transcriptome=${reference_dir} \
        --localcores=32 \
        --localmem=92"

    sbatch --nodes=1 \
        --cpus-per-task=32 \
        --mem=96G \
        --time=2880 \
        -o cellranger_count.%j.out \
        -e cellranger_count.%j.err \
        -J cellranger_count <<EOF

   cmd="rm -r $fastq_dir"
#!/bin/bash
echo "Start Cell Ranger count "`date`
echo ${cmd}
eval ${cmd}
echo "Done "`date`
cellranger count --version
EOF

done
13
  • 2
    Where are you running the command? There's no command being executed in your script. Also, the shebang (#!/bin/bash) needs to be the first line of your file, you can't have it in the middle like that. Yes, you can absolutely delete the directory after running the command, but we need to see the actual script you use to be able to help you with it, one that shows us when you execute the command. Commented May 19, 2022 at 11:11
  • The script above rm works! I am just wondering if I can delete a folder after executing through cmd. and is it the right way to do this? Commented May 19, 2022 at 11:51
  • Please edit your question instead of giving more info in the comments. Commented May 19, 2022 at 12:30
  • No, that script won't work. You haven't closed the for loop and the #!/bin/bash is ignored as a comment. Please show us your actual script, including the part where you execute $sbatch so we can understand what you are doing. Commented May 19, 2022 at 12:33
  • Are all people rude over here? @ArtemS.Tashkinov No, my question isn't about whether my script is right or not. I am simply asking if there is a way to delete folders once executing the cmd sbatch. Commented May 19, 2022 at 14:03

1 Answer 1

0

If I understand correctly, what you want to do is:

  1. Submit one cellranger job to Slurm for each of your fastq directories.
  2. Delete the directory.

However, that isn't what your script is doing. You have

    cmd="cellranger count \
    [. . . ]

And then you have:

    sbatch --nodes=1 \
         -J cellranger_count <<EOF
         [. . . ]
   cmd="rm -r $fastq_dir"

So in the job you are actually passing to sbatch, $cmd is just rm -rf $fastq_dir and not cellrangercount....

In any case, I suspect that the rm -rf "$fastq_dir" won't work anyway, not unless the sbatch is running in the same directory, so you might want to use the full path in the for loop. For example, if your fastq_first/ is actually /home/echo94/data/fastq_first, then you should change your for to be like this:

for fastq_dir in /home/echo94/data/fastq_first/*; do

Also, since you only want directories, you can add a slash to ensure that no files will be selected by the for loop:

for fastq_dir in /home/echo94/data/fastq_first/*/; do

You now have the full path to each directory so you can rm $fastq_dir from anywhere on the system. Putting all this together, this script is probably what you need:

for fastq_dir in /home/echo94/data/fastq_first/*/; do
    barcode=`basename "$fastq_dir"`

    cmd="cellranger count \
        --id=${barcode} \
        --fastqs= ${fastq_dir}   \
        --sample=${barcode} \
        --transcriptome=${reference_dir} \
        --localcores=32 \
        --localmem=92"

    sbatch --nodes=1 \
        --cpus-per-task=32 \
        --mem=96G \
        --time=2880 \
        -o cellranger_count.%j.out \
        -e cellranger_count.%j.err \
        -J cellranger_count <<EOF
#!/bin/bash
set -ue
echo "Start Cell Ranger count `date`"
echo "$cmd" 
eval "$cmd"
echo "Done "`date` 
cellranger count --version
rm -r "$fastq_dir"
EOF

done
4
  • Thanks so much for editing this. I am going to try this and let you know if it works correctly! Commented May 19, 2022 at 16:44
  • You're welcome, @Echo94. I hope you understand why we were so pushy and insisted on more information now. As you can see, the devil's in the details! :) Commented May 19, 2022 at 16:57
  • thanks so much for but that didn't work with the rm command at the end of the file, may be because its out of the for loop. Commented May 20, 2022 at 13:56
  • @Echo94 please post a new question, give the script you actually used, make sure to chech what the value of the $fastq_dir variable is (did you use the full path as I suggested?) and explain exactly how it fails. Also try to save the script you are creating into a file instead of passing it with EOF so you can see what is actually being passed. Commented May 20, 2022 at 19:27

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.