findMotif method

Map<String, dynamic> findMotif({
  1. required String motif,
  2. dynamic overlap = true,
})

Returns the indices of all motif matches.

Prevent overlapping matches by setting overlap to false.

Implementation

Map<String, dynamic> findMotif({required String motif, overlap = true}) {
  List<Map<String, dynamic>> matchData = [];
  Map<String, dynamic> matchMotifMap = {};

  // 'ATGC' -> 'ATGC', 'N{P}[ST]{P}' -> 'N[^P][S|T|][^P]'
  String tempRegexMotif = Utils.motifToRe(motif: motif);

  // ?= is the regex lookahead operator and allows for matches to overlap.
  RegExp regexMotif = overlap == true ? RegExp('(?=$tempRegexMotif)') : RegExp(tempRegexMotif);

  Iterable<RegExpMatch> allMatches = regexMotif.allMatches(seq);
  for (RegExpMatch match in allMatches) {
    matchData.add({
      kMatch: motif,
      kStartIndex: match.start,
      kEndIndex: match.start + motif.length - 1,
    });
  }
  matchMotifMap[kMatchCount] = allMatches.length;
  matchMotifMap[kMatchIndices] = matchData;
  return matchMotifMap;
}