combinations method

List<String> combinations({
  1. dynamic sorted = false,
})

Returns every possible combination of this sequence.

Sort the result from longest to shortest by setting sorted to true.

Peptide pep = Peptide(seq: 'ATG');
pep.combinations()
// > [A, AT, ATG, T, TG];

Implementation

List<String> combinations({sorted = false}) {
  List<String> listSeq = this._seq.split("");
  List<String> combinations = [];

  // Generates all possible combinations of this sequence.
  for (int i = 0; i < listSeq.length; i++) {
    if (i != listSeq.length - 1) {
      combinations.add(listSeq[i]);
    }
    List<String> temp = [listSeq[i]];
    for (int j = i + 1; j < listSeq.length; j++) {
      temp.add(listSeq[j]);
      combinations.add(temp.join());
    }
  }
  if (sorted) {
    // Sorts with longest combination first.
    combinations.sort((b, a) => a.length.compareTo(b.length));
    return combinations;
  }
  return combinations;
}