重写for循环以使用“并行”

时间:2017-02-09 21:51:40

标签: bash gnu-parallel

我正在尝试重写for循环(效果很好)并将其作为并行运行但我遇到各种各样的问题。这是我的for循环

function no_sam {
    filename=$(basename "$file")
    extension="${file##*.}"

    if [ $extension = "sam" ];
      then
        filename="${filename%.*}"
        feat_out=$filename.out
        htseq-count -f $types -r "$pos" -m "$mode" -i "$attribute" -s "$strand" -t "$feature" -a "$qual" "$file" "$input_gff" > "$feat_out"
        grep -v "_" "$feat_out" > temp && mv temp "$feat_out"
        mv "$feat_out" "$counts_folder"
    elif [ $extension = "bam" ];
      then
        filename="${filename%.*}"
        feat_out=$filename.out
        htseq-count -f $types -r "$pos" -m "$mode" -i "$attribute" -s "$strand" -t "$feature" -a "$qual" "$file" "$input_gff" > "$feat_out"
        grep -v "_" "$feat_out" > temp && mv temp "$feat_out"
        mv "$feat_out" "$counts_folder"
    fi    
}

for file in "${multi[@]}"; do 
  no_sam
done

当我用GNU parallel替换for循环时,我收到错误

"${multi[@]}" no_sam | parallel 

testfile.sam: command not found

1 个答案:

答案 0 :(得分:0)

试试这个(基于https://www.gnu.org/software/parallel/man.html#EXAMPLE:-Using-shell-variableshttps://www.gnu.org/software/parallel/man.html#EXAMPLE:-Calling-Bash-functions):

function no_sam {
    file="$1"
    filename=$(basename "$file")
    extension="${file##*.}"

    if [ $extension = "sam" ];
      then
        filename="${filename%.*}"
        feat_out=$filename.out
        htseq-count -f $types -r "$pos" -m "$mode" -i "$attribute" -s "$strand" -t "$feature" -a "$qual" "$file" "$input_gff" > "$feat_out"
        grep -v "_" "$feat_out" > temp && mv temp "$feat_out"
        mv "$feat_out" "$counts_folder"
    elif [ $extension = "bam" ];
      then
        filename="${filename%.*}"
        feat_out=$filename.out
        htseq-count -f $types -r "$pos" -m "$mode" -i "$attribute" -s "$strand" -t "$feature" -a "$qual" "$file" "$input_gff" > "$feat_out"
        grep -v "_" "$feat_out" > temp && mv temp "$feat_out"
        mv "$feat_out" "$counts_folder"
    fi    
}
export -f no_sam

parallel no_sam ::: "${multi[@]}"

从您尝试使用GNU Parallel的方式来看,我认为您将从花费一小时走过man parallel_tutorial中受益。 你的命令行会爱你。