Assign environment variable to a variable's name

Hi there,

I’m working on a pipeline (see below) where I defined a few environment variables. Also, I’m creating some on the way.

In particular, I have a CONTIG variable which I wish to make univocal for each sample (ID), haplotype (HAP), and chromosome (CHR_P/M) combination; however, I’m not sure how to do it… simply defining CONTIG as CONTIG_$ID_$h_$c sounds wrong to me but I have never done this before, so I have no prior knowledge and could not find very helpful, succinct documentation on this.

This is the part of the pipeline of interest:

#!/bin/bash
#SBATCH --nodes=1 --ntasks=1 --cpus-per-task=12
#SBATCH --time=2-00:00:00
#SBATCH --mem=50gb
#
#SBATCH --job-name=name_test
#SBATCH --output=name_test.out
#SBATCH --array=[1-232]%1
#
#SBATCH --partition=DSS,NBFC

NAMES=$1
d=$(sed -n "$SLURM_ARRAY_TASK_ID"p $NAMES)

ID="$(echo ${d}/*_hap1_hprc_r2.fa.gz | sed 's#/path/to/hprc_y2_assemblies/[A-Z,0-9]\+_[a-z]/##' | sed 's#_hap1_hprc_r2.fa.gz##')"

HAP=( "hap1" "hap2")
SEX="$(echo ${d} | sed 's#/path/to/hprc_y2_assemblies/[A-Z,0-9]\+_##')"
CHR_P=("chr1" "chr2" "chr3" "chr4" "chr5" "chr6" "chr7" "chr8" "chr9" "chr10" "chr11" "chr12" "chr13" "chr14" "chr15" "chr16" "chr17" "chr18" "chr19" "chr20" "chr21" "chr22" "chrY" )
CHR_M=("chr1" "chr2" "chr3" "chr4" "chr5" "chr6" "chr7" "chr8" "chr9" "chr10" "chr11" "chr12" "chr13" "chr14" "chr15" "chr16" "chr17" "chr18" "chr19" "chr20" "chr21" "chr22" "chrX" )

BEDTOOLS="./tools/bedtools"
MINIMAP="./tools/minimap2"
SEQKIT="./tools/seqkit"
BGZIP="./tools/bgzip"

DNABRNN="./tools/dna-nn/dna-brnn"
MODELS="./tools/dna-nn/models/attcc-alpha.knm"


cd ~


#PREPARES THE INPUT
if [ -e $d/select.done ]
        then

                echo "regions selecion done!"

        else
                for h in "${HAP[@]}"; do
                        ${MINIMAP} -cx asm5 -t 12 CHM13v2.0.fna.gz $d/${ID}_${h}_hprc_r2.fa.gz > $d/${ID}_${h}.paf && \
                        if [ "$SEX" == "m" ]
                                then
                                if [ "$HAP" == "hap1" ]
                                        then
                                        for c in "${CHR_P[@]}"; do
                                                CONTIG=$(grep -w "${c}" $d/${ID}_${h}.paf | awk '{print $1}' | uniq) && \
                                                for s in "${CONTIG[@]}"; do
                                                ###generate chromosome FASTA
                                                        printf "%s\n" ${s} > $d/list_${h}_${c}.txt && ${SEQKIT} grep -f $d/list_${h}_${c}.txt $d/${ID}_${h}_hprc_r2.fa.gz -j 12 | ${BGZIP} -@12 > $d/${ID}_${h}_${c}-chm13.fna.gz
                                                ###generate centromere FASTA
                                                        ${DNABRNN} -Ai ${MODELS} -t32 $d/${ID}_${h}_${c}-chm13.fna.gz > $d/$h/cent/${ID}_${h}_${c}.bed && awk -F '\t' '$4==2' $d/$h/cent/${ID}_${h}_${c}.bed > $d/$h/cent/${ID}_${h}_${c}_cent.bed && ${BEDTOOLS} getfasta -fi $d/${ID}_${h}_${c}-chm13.fna.gz -bed $d/$h/cent/${ID}_${h}_${c}_cent.bed | ${BGZIP} -@12 > $d/$h/cent/${ID}_${h}_${c}_cent.fna.gz;
                                                done
                                        done
                                #some other operations here

Until now, I have never had issues but I’m afraid the CONTIG variable would get overwritten if not created with the specific ID_HAP_CHR combination. Sill, if this is not a problem I would like to know how to do it for the sake of improving my scripting. Thanks!

Hi Overcrtaft,

Welcome to the wonderful world of data structures :wink: where you get to design how your data is stored so as to enrich it with meaning.

When you need to have a variable name have a different value depending on the value of another variable, you can use an associative array to relate them.

eg.

#! /bin/bash

declare -A colour

fruits=("apple" "orange" "banana" "cherry")

colour["apple"]="green"
colour["orange"]="orange"
colour["banana"]="yellow"
colour["cherry"]="Red"

#for  fruit in ${fruits[*]}; do
# Treat array members, protect them with quotes
for  fruit in "${fruits[@]}"; do
        echo "$fruit is ${colour[$fruit]}"
done

@Skrynesaver I see, that makes sense since your example is pretty straightforward! So, in my case should I do something like the following:

#! /bin/bash

declare -A contig

HAP=( "hap1" "hap2")
CHR_P=("chr1" "chr2" "chr3" "chr4" "chr5" "chr6" "chr7" "chr8" "chr9" "chr10" "chr11" "chr12" "chr13" "chr14" "chr15" "chr16" "chr17" "chr18" "chr19" "chr20" "chr21" "chr22" "chrY" )
CHR_M=("chr1" "chr2" "chr3" "chr4" "chr5" "chr6" "chr7" "chr8" "chr9" "chr10" "chr11" "chr12" "chr13" "chr14" "chr15" "chr16" "chr17" "chr18" "chr19" "chr20" "chr21" "chr22" "chrX" )

contig["hap1" "chr1"]="contig_hap1_chr1"

with all possible combinations of HAP and CHR_P/M? Also, these are arrays I created within my script by assignment; on the other hand, ID is obtained from a list of paths containing the same name folder as per the sample in question (see below), how can I deal with it in the declare statement? Thanks again!

names.list first three entries

/path/to/hprc_y2_assemblies/HG00097_f
/path/to/hprc_y2_assemblies/HG00099_f
/path/to/hprc_y2_assemblies/HG00126_m

In bash an associate array (or hash) takes one string as index. Multi-dimensional indexes are not supported. So for clarity write

contig["hap1 chr1"]="contig_hap1_chr1"

where the space is a field separator that may not occur within the fields.
In your script, within the nested loops you can construct it like

  contig["${h} ${c} ${ID}"]=$CONTIG

Hi @MadeInGermany got it,

I guess it will take me a bit of experimentation to have this done the right way, but I’ll try to integrate it in the script I have done. Thanks!