calcObservedMutations - Count the number of observed mutations in a sequence.


calcObservedMutations determines all the mutations in a given input seqeunce compared to its germline sequence.


calcObservedMutations(inputSeq, germlineSeq, regionDefinition = NULL,
mutationDefinition = NULL, ambiguousMode = c("eitherOr", "and"),
returnRaw = FALSE, frequency = FALSE)


input sequence. IUPAC ambiguous characters for DNA are supported.
germline sequence. IUPAC ambiguous characters for DNA are supported.
RegionDefinition object defining the regions and boundaries of the Ig sequences. Note, only the part of sequences defined in regionDefinition are analyzed. If NULL, mutations are counted for entire sequence.
MutationDefinition object defining replacement and silent mutation criteria. If NULL then replacement and silent are determined by exact amino acid identity.
whether to consider ambiguous characters as "either or" or "and" when determining and counting the type(s) of mutations. Applicable only if inputSeq and/or germlineSeq contain(s) ambiguous characters. One of c("eitherOr", "and"). Default is "eitherOr".
return the positions of point mutations and their corresponding mutation types, as opposed to counts of mutations across positions. Also returns the number of bases used as the denominator when calculating frequency. Default is FALSE.
logical indicating whether or not to calculate mutation frequencies. The denominator used is the number of bases that are not one of “N”, “-“, or “.” in either the input or the germline sequences. If set, this overwrites returnRaw. Default is FALSE.


For returnRaw=FALSE, an array with the numbers of replacement (R) and silent (S) mutations.

For returnRaw=TRUE, a list containing

  • $pos: A data frame whose columns (position, R, S, and region) indicate, respecitively, the nucleotide position, the number of R mutations at that position, the number of S mutations at that position, and the region in which that nucleotide is in.
  • $nonN: A vector indicating the number of bases in regions defined by regionDefinition (excluding non-triplet overhang, if any) that are not one of “N”, “-“, or “.” in either the inputSeq or germlineSeq.

For frequency=TRUE, regardless of returnRaw, an array with the frequencies of replacement (R) and silent (S) mutations.


Each mutation is considered independently in the germline context. If specified, only the part of inputSeq defined in regionDefinition is analyzed. For example, when using the default IMGT_V definition, then mutations in positions beyond 312 will be ignored. Additionally, non-triplet overhang at the sequence end is ignored.

Only replacement (R) and silent (S) mutations are included in the results. Excluded are:

  • Stop mutations
  • Mutations occurring in codons where one or both of the observed and the germline involve(s) one or more of “N”, “-“, or “.”.

E.g.: the case in which NNN in the germline sequence is observed as NNC in the input sequence.

In other words, a result that is NA or zero indicates absence of R and S mutations, not necessarily all types of mutations, such as the excluded ones mentioned above.

NA is also returned if inputSeq or germlineSeq is shorter than 3 nucleotides.

Ambiguous characters

When there are ambiguous characters present, the user could choose how mutations involving ambiguous characters are counted through ambiguousMode. The two available modes are "eitherOr" and "and".

  • With "eitherOr", ambiguous characters are each expanded but only 1 mutation is recorded. When determining the type of mutation, the priority for different types of mutations, in decreasing order, is as follows: no mutation, replacement mutation, silent mutation, and stop mutation.

When counting the number of non-N, non-dash, and non-dot positions, each position is counted only once, regardless of the presence of ambiguous characters.

As an example, consider the case where germlineSeq is "TST" and inputSeq is "THT". Expanding "H" at position 2 in inputSeq into "A", "C", and "T", as well as expanding "S" at position 2 in germlineSeq into "C" and "G", one gets:

  • "TCT" (germline) to "TAT" (observed): replacement
  • "TCT" (germline) to "TCT" (observed): no mutation
  • "TCT" (germline) to "TTT" (observed): replacement
  • "TGT" (germline) to "TAT" (observed): replacement
  • "TGT" (germline) to "TCT" (observed): replacement
  • "TGT" (germline) to "TTT" (observed): replacement

Because “no mutation” takes priority over replacement mutation, the final mutation count returned for this example is NA (recall that only R and S mutations are returned). The number of non-N, non-dash, and non-dot positions is 3.

  • With "and", ambiguous characters are each expanded and mutation(s) from all expansions are recorded.

When counting the number of non-N, non-dash, and non-dot positions, if a position contains ambiguous character(s) in inputSeq and/or germlineSeq, the count at that position is taken to be the total number of combinations of germline and observed codons after expansion.

Using the same example from above, the final result returned for this example is that there are 5 R mutations at position 2. The number of non-N, non-dash, and non-dot positions is 8, since there are 6 combinations stemming from position 2 after expanding the germline codon ("TST") and the observed codon ("THT").


# Use an entry in the example data for input and germline sequence
data(ExampleDb, package="alakazam")
in_seq <- ExampleDb[["SEQUENCE_IMGT"]][100]
germ_seq <-  ExampleDb[["GERMLINE_IMGT_D_MASK"]][100]

# Identify all mutations in the sequence
ex1_raw <- calcObservedMutations(in_seq, germ_seq, returnRaw=TRUE)
# Count all mutations in the sequence
ex1_count <- calcObservedMutations(in_seq, germ_seq, returnRaw=FALSE)
ex1_freq <- calcObservedMutations(in_seq, germ_seq, returnRaw=FALSE, frequency=TRUE)
# Compare this with ex1_count
table(ex1_raw$pos$region, ex1_raw$pos$R)[, "1"]

[1] 11

table(ex1_raw$pos$region, ex1_raw$pos$S)[, "1"]

[1] 7

# Compare this with ex1_freq
table(ex1_raw$pos$region, ex1_raw$pos$R)[, "1"]/ex1_raw$nonN


table(ex1_raw$pos$region, ex1_raw$pos$S)[, "1"]/ex1_raw$nonN


# Identify only mutations the V segment minus CDR3
ex2_raw <- calcObservedMutations(in_seq, germ_seq, 
regionDefinition=IMGT_V, returnRaw=TRUE)
# Count only mutations the V segment minus CDR3
ex2_count <- calcObservedMutations(in_seq, germ_seq, 
regionDefinition=IMGT_V, returnRaw=FALSE)
ex2_freq <- calcObservedMutations(in_seq, germ_seq, 
regionDefinition=IMGT_V, returnRaw=FALSE,
# Compare this with ex2_count
table(ex2_raw$pos$region, ex2_raw$pos$R)[, "1"]

  4   7 

table(ex2_raw$pos$region, ex2_raw$pos$S)[, "1"]                              

  1   4 

# Compare this with ex2_freq
table(ex2_raw$pos$region, ex2_raw$pos$R)[, "1"]/ex2_raw$nonN     

       CDR        FWR 
0.08333333 0.02916667 

table(ex2_raw$pos$region, ex2_raw$pos$S)[, "1"]/ex2_raw$nonN                                       

       CDR        FWR 
0.02083333 0.01666667 

# Identify mutations by change in hydropathy class
ex3_raw <- calcObservedMutations(in_seq, germ_seq, regionDefinition=IMGT_V,
# Count mutations by change in hydropathy class
ex3_count <- calcObservedMutations(in_seq, germ_seq, regionDefinition=IMGT_V,
ex3_freq <- calcObservedMutations(in_seq, germ_seq, regionDefinition=IMGT_V,
returnRaw=FALSE, frequency=TRUE)
# Compre this with ex3_count
table(ex3_raw$pos$region, ex3_raw$pos$R)[, "1"]

  3   4 

table(ex3_raw$pos$region, ex3_raw$pos$S)[, "1"]

  2   7 

# Compare this with ex3_freq
table(ex3_raw$pos$region, ex3_raw$pos$R)[, "1"]/ex3_raw$nonN                                        

       CDR        FWR 
0.06250000 0.01666667 

table(ex3_raw$pos$region, ex3_raw$pos$S)[, "1"]/ex3_raw$nonN
       CDR        FWR 
0.04166667 0.02916667 

See also

See observedMutations for counting the number of observed mutations in a data.frame.