This is a test version of Biostars. For the public version, visit https://www.biostars.org.
for loop in a bash script

Hi!

I have this bash script which is just for one file and I want to use for a multiple files in a folder. I have been trying with the for loop, but my script does not work. THanks in advance!

#!/bin/bash

# USAGE
# sh getSNPs_and_clean.sh vcf output_path
## Note this only works when we use chromosomes with sites to be exchanged
## Note this only works on one chromosome VCF

FILE=$1; # A VCF file multisample or mono sample

OUTPUT_PATH=$2;

file_name=${FILE%.vcf*};
file_name=${file_name##*/};

# SNPs
bcftools view --threads 40 -m2 -M2 -v snps -Oz -o ${OUTPUT_PATH}/${file_name}.snps.vcf.gz ${FILE}
bcftools index --threads 40 ${OUTPUT_PATH}/${file_name}.snps.vcf.gz
bcftools view --threads 40 -e 'POS=80294998' -Oz -o ${OUTPUT_PATH}/${file_name}.snpsTMP.vcf.gz ${OUTPUT_PATH}/${file_name}.snps.vcf.gz # without duplicates
mv -f ${OUTPUT_PATH}/${file_name}.snpsTMP.vcf.gz ${OUTPUT_PATH}/${file_name}.snps.vcf.gz
bcftools index --threads 40 ${OUTPUT_PATH}/${file_name}.snps.vcf.gz

echo "${FILE} completed!"
bash

what is the aim of that script ? finding the di allelic snps NOT at POS=80294998 ?

Its is just a correction

I want to put a for loop inside this script

Its is just a correction

what does that mean ?

as far as I can understand you don't need such complicated script, you don't need an index, you don't need two steps....

bcftools view   -m2 -M2 -v snps  -e 'POS=80294998' -Oz -o /apth/output.vcf.gz input.vcf

what I am trying to do is something like this (for multiple files):

#!/bin/bash

# USAGE
# sh getSNPs_and_clean.sh vcf output_path
## Note this only works when we use chromosomes with sites to be exchanged
## Note this only works on one chromosome VCF

FILE=$1; # A VCF file multisample or mono sample

OUTPUT_PATH=$2;

file_name=${FILE%.vcf*};
file_name=${file_name##*/};
for i in FILE
do
# SNPs
bcftools view --threads 40 -m2 -M2 -v snps -Oz -o ${OUTPUT_PATH}/${file_name}.snps.vcf.gz ${FILE}
bcftools index --threads 40 ${OUTPUT_PATH}/${file_name}.snps.vcf.gz
bcftools view --threads 40 -e 'POS=80294998' -Oz -o ${OUTPUT_PATH}/${file_name}.snpsTMP.vcf.gz ${OUTPUT_PATH}/${file_name}.snps.vcf.gz # without duplicates
mv -f ${OUTPUT_PATH}/${file_name}.snpsTMP.vcf.gz ${OUTPUT_PATH}/${file_name}.snps.vcf.gz
bcftools index --threads 40 ${OUTPUT_PATH}/${file_name}.snps.vcf.gz
done
echo "${FILE} completed!"

I'm not really sure what you're trying to do either, but in terms of simply running a loop, the example above has only one element for looping (FILE has only a single value), and you never use "i" in the rest of the code (so the loop appears to have no point). Don't you want something like:

FILES="*.vcf"

for i in FILES
do
    echo $i
    # shave off vcf extension                                                                                                                                                                    
    file_name=${i%.vcf*}
    echo "Now I can use $file_name as a base value."
    echo "${file_name}.snps.vcf.gz"
    # I don't know what this operation does
    file_name=${file_name##*/}
    echo $file_name
done

Create the simplest toy example and echo your file names to work out your loop, before inserting the complex command stuff.

, but my script does not work.

information is missing. What "does not work" ? how can we know ?

after #!/bin/bash add

set -e
set -x

to see what's happening and stop at errors.

you can also prefix your bcftools commands with echo to see what's happening.

0 answers

No answers yet.

Log in to answer this question.