generating Percent ID using CIGAR strings

Hello everyone,
I'm currently working with a bam file and I need to generate the percent ID of matches that are found in the CIGAR string. I'm having some trouble getting there, here are the libraries that I have.
library(Biostrings)
library(Rsamtools)
library(readr)
library(GenomicAlignments)
library(GenomicRanges)
library(IRanges)
library(GenomeInfoDb)
library(XVector)
Here are some of the codes that I'm working with.
###Gal is a gene alignments object where each element describes a genomic alignment
BamFile(file= "Example.bowtie2.bam")
gal <- readGAlignments("Example.bowtie2.bam")
gal
###I have a data table here that gives me the number of matches, insertions and deletions
colSums(cigarOpTable(cigar(gal)))

The percent identity = match-(NM)/length.
I will appreciate the help.