Bash for loop to iterate over files matching a pattern and produce output files with the same pattern

时间:2017-12-18 06:32:21

标签: bash file loops bioinformatics

I was trying to produce smaller VCF files based on a group of SNPs in each chromosome. I produced the first 3 manually using this syntax

tabix ALL.chr3.phase3_shapeit2_mvncall_integrated_v5a.20130502.genotypes.vcf.gz -R chr3.tabix.txt > chr3SNPS.vcf

I then tried to run the same function in a bash loop for chromosomes 4 to 17.

I tried a few different ways but only one of them worked and not exactly the way I wanted it to.

first I tried

for i in {4..17}; do tabix ALL.chr$i.phase3_shapeit2_mvncall_integrated_v5a.20130502.genotypes.vcf.gz -R chr$i.tabix.txt > chr$iSNPS.vcf;done

but when run that just output one file named chr.vcf

I then tried

for i in {4..17}; do tabix ALL.chr$i.phase3_shapeit2_mvncall_integrated_v5a.20130502.genotypes.vcf.gz -R chr$i.tabix.txt > chr$i_SNPS.vcf;done

but got the exact same result.

Then I tried this and realised I was going even farther afield

for i in {4..17}; do X="ALL.chr$i.phase3_shapeit2_mvncall_integrated_v5a.20130502.genotypes.vcf.gz";Y="chr$i.tabix.txt";Z= "chr$iSNPS.vcf";tabix $X -R $Y > $Z;done
-bash: ALL.chr4.phase3_shapeit2_mvncall_integrated_v5a.20130502.genotypes.vcf.gz: command not found
-bash: chr.vcf: command not found
-bash: ${Z}: ambiguous redirect
-bash: ALL.chr5.phase3_shapeit2_mvncall_integrated_v5a.20130502.genotypes.vcf.gz: command not found
-bash: chr.vcf: command not found
-bash: ${Z}: ambiguous redirect
…
…
…
-bash: ALL.chr17.phase3_shapeit2_mvncall_integrated_v5a.20130502.genotypes.vcf.gz: command not found
-bash: chr.vcf: command not found
-bash: ${Z}: ambiguous redirect

I finally tried this:

for i in {4..17}; do tabix "ALL.chr$i.phase3_shapeit2_mvncall_integrated_v5a.20130502.genotypes.vcf.gz" -R "chr$i.tabix.txt" > "chr$i.snps.vcf";done

and it worked but it procuded files that where in the form chr4.snps.vcf instead of the chr4SNPS.vcf that I wanted

In case it wasn't clear the files being input and output

0 个答案:

没有答案