I have the following vectors that getting different entries based on another array.
PIP_ID
SAMPLE_TYPE_IDS
MPX_KIT_IDS
Depending on the content of those I am creating different files using if
statements/rules. For example:
if [ $PIP_ID == 8 ]; then
EXTENSION="ATLAS_"
fi
if [[ $SAMPLE_TYPE_IDS == 81 ]] || [[ $SAMPLE_TYPE_IDS == 86 ]]; then
EXTENSION="SmartSeq3_"
fi
if [[ $SAMPLE_TYPE_IDS == 61 ]] || [[ $SAMPLE_TYPE_IDS == 72 ]] || [[ $SAMPLE_TYPE_IDS == 102]] || [[ $SAMPLE_TYPE_IDS == 84 ]] ; then
EXTENSION="scRNAv3_"
fi
if ([[ $MPX_KIT_IDS == 89 ]] || [[ $MPX_KIT_IDS == 91 ]] || [[ $MPX_KIT_IDS == 92 ]] || [[ $MPX_KIT_IDS == 93 ]]) && [[ SAMPLE_TYPE_IDS != 105 ]] ; then
EXTENSION="AGENTtrim_"
fi
if [[ $MPX_KIT_IDS == 17 ]] || [[ $MPX_KIT_IDS == 27 ]] || [[ $MPX_KIT_IDS == 53 ]]; then
EXTENSION="miRNA_"
fi
Note that the one that creates the AGENTtrim_
has multiple OR combined with && statements grouped with parenthesis.
How can I turn those into a more simpler, more elegant solution using case
? I do not know how to combine all the 3 variables into one, since the one that creates the AGENTtrim_
gets info from 2 independent variables.
CodePudding user response:
Converting it to a case statement would be ugly. You can simplify it by using extended pattern matching.
if [[ $MPX_KIT_IDS == @(89|91|92|93) && $SAMPLE_TYPE_IDS != 105 ]]; then
EXTENSION="AGENTtrim_"
elif [[ $MPX_KIT_IDS == @(17|27|53) ]]; then
EXTENSION="miRNA_"
elif [[ $SAMPLE_TYPE_IDS == @(81|86) ]]; then
EXTENSION="SmartSeq3_"
elif [[ $SAMPLE_TYPE_IDS == @(61|72|102|84) ]]; then
EXTENSION="scRNAv3_"
elif [[ $PIP_ID == 8 ]]; then
EXTENSION="ATLAS_"
fi
CodePudding user response:
To be honest I do not know how to combine all the 3 variables into one
You can just put them together with a separator. For example, assuming there will be no commas in values, just pick a comma as separator and do a string:
shopt -s extglob
case "$PIP_ID,$SAMPLE_TYPE_IDS,$MPX_KIT_IDS" in
8,*) EXTENSION="ATLAS_" ;;
*,81,*|*,86,*) EXTENSION="SmartSeq3_" ;;
*,61,*|*,72,*|*,102,*|*,85,*) EXTENSION="scRNAv3_" ;;
# with extended glob shopt -s extglob
*,!(105),@(89|91)) EXTENSION="AGENTtrim_" ;;
*,17|*,27|*,53) EXTENSION="miRNA_" ;;
# etc...
esac
CodePudding user response:
This probably helps to make the code clearer (with bash
):
Replace
[[ $SAMPLE_TYPE_IDS == 61 ]] || [[ $SAMPLE_TYPE_IDS == 72 ]] || [[ $SAMPLE_TYPE_IDS == 102]] || [[ $SAMPLE_TYPE_IDS == 84 ]]
with
[[ $SAMPLE_TYPE_IDS =~ ^(61|72|102|84)$ ]]
CodePudding user response:
Make use of functions which name can clarify the rule.
atlas_pip() {
[[ $1 == 8 ]]
}
smart_id() {
[[ $1 =~ ^(61|72)$ ]]
}
rna_id() {
[[ $1 =~ ^(61|72|102|84)$ ]]
}
agent_id() {
[[ $1 =~ ^(89|91|92|93)$ ]]
}
mirna_id() {
[[ $1 =~ ^(17|27|53)$ ]]
}
atlas_pip $PIP_ID && EXTENSION="ATLAS_"
smart_id $SAMPLE_TYPE_IDS && EXTENSION="SmartSeq3_"
rna_id $SAMPLE_TYPE_IDS && EXTENSION="scRNAv3_"
agent_id $MPX_KIT_IDS && $SAMPLE_TYPE_IDS != 105 && EXTENSION="AGENTtrim_"
mirna_id $MPX_KIT_IDS && EXTENSION="miRNA_"
CodePudding user response:
IF your nonexclusive if
statements cannot overwrite because a later condition is also met but has a higher priority, then this nested case
structure should should create the same effective logic.
case $PIP_ID in
8) EXTENSION="ATLAS_" ;;
*) case $SAMPLE_TYPE_IDS in # PIP_ID is NOT 8
8[16]) EXTENSION="SmartSeq3_" ;;
61|72|102|84) EXTENSION="scRNAv3_" ;;
*) case $MPX_KIT_IDS in
[12]7|53) EXTENSION="miRNA_" ;;
89|9[123]) case $SAMPLE_TYPE_IDS in
105) : nothing to do ;; # sets NO extension if it gets to this
*) EXTENSION="AGENTtrim_" ;;
esac ;;
esac ;;
esac ;;
esac