freq method

Map<String, double> freq({
  1. bool norm = false,
  2. bool ignoreStopAA = false,
})

Returns the frequency of each monomer.

Return the percentage total of each monomer by setting norm to true. Exclude the X 'amino acid' in the count by setting ignoreStopAA to true.

Implementation

Map<String, double> freq({bool norm = false, bool ignoreStopAA = false}) {
  Map<String, double> freqMap = {};
  this._seq.split('').forEach((mon) {
    // If this is a Peptide and [ignoreStopAA] is `true`, we want
    // to perform the calulation without taking the stop codon product
    // X into consideration.
    if (this is Peptide && ignoreStopAA) {
      if (mon != 'X') {
        if (freqMap.containsKey(mon)) {
          freqMap[mon] = freqMap[mon]! + 1;
        } else {
          freqMap[mon] = 1;
        }
      }
      // If either this is not a Peptide or [ignoreStopAA] is `false`,
      // then proceed without checking if the character is X.
    } else {
      if (freqMap.containsKey(mon)) {
        freqMap[mon] = freqMap[mon]! + 1;
      } else {
        freqMap[mon] = 1;
      }
    }
  });

  if (this is Peptide && ignoreStopAA) {
    if (norm) {
      // The sequence is a Peptide and [ignoreStopAA] is `true` and [norm] is `true`.
      // Return normalised results based on length without the X character.
      return freqMap.map(
        (key, value) => MapEntry(
          key,
          // Use the length of the peptide in absence of the X character.
          double.parse(((value / lenMinus(monomer: 'X')) * 100).toStringAsFixed(1)),
        ),
      );
    }
    // The sequence is not a Peptide or [ignoreStopAA] is `false`.
  } else {
    if (norm) {
      // Return normalised results based on sequence length.
      return freqMap.map(
        (key, value) => MapEntry(
          key,
          double.parse(((value / this._len) * 100).toStringAsFixed(1)),
        ),
      );
    }
  }
  // The sequence is not a Peptide or [ignoreStopAA] is `false`, or [norm] is `false`.
  // Return non-normalised results based on sequence length.
  return freqMap;
}