#!/bin/bash

# Check if the filename is provided as an argument
if [ "$#" -ne 1 ]; then
    echo "Usage: $0 <cigar_file.txt>"
    exit 1
fi

# Initialize counters
total_M=0
total_I=0
total_D=0

# Read the file line by line
while IFS= read -r line; do
    # Skip lines that are just '*'
    if [[ "$line" == "*" ]]; then
        continue
    fi

    # Use grep and awk to extract counts of M, I, and D
    echo "$line" | grep -oP '\d+M|\d+I|\d+D' | while IFS= read -r op; do
        count=${op::-1}  # Extract the numeric part
        case "${op: -1}" in
            M) total_M=$((total_M + count)) ;;
            I) total_I=$((total_I + count)) ;;
            D) total_D=$((total_D + count)) ;;
        esac
    done
done < "$1"

# Output the results
echo "Total counts for CIGAR operations:"
echo "M: $total_M"
echo "I: $total_I"
echo "D: $total_D"

