def sequence_book(spectrum, n, m):
    if spectrum[0] != 0:
        spectrum = [0] + spectrum
    bitches = convolutions(spectrum, m)

    matches = defaultdict(list)
    leader, leader_score = (0, ), 0
    leaderboard = [leader]
    parent = spectrum[-1]
    while leaderboard:
        leaderboard = expand(leaderboard, bitches)
        next_leaderboard = []
        for peptide in leaderboard:
            if sum(peptide) == parent:
                score = get_score(peptide, spectrum)
                if score > leader_score:
                    leader, leader_score = peptide, score
                    print ts(peptide), '', score, '', ts(
                        generate_spec(peptide))
                matches[score].append(peptide)
            if sum(peptide) < parent:
                next_leaderboard.append(peptide)
        leaderboard = next_leaderboard
        leaderboard = cut_book(leaderboard, spectrum, n)

    return leader, leader_score, matches
def brute_forse(spec):
    possible_spec_acids = [acid for acid in spec if acid in all_acids]
    desired_sum = spec[-1]
    max_len = len(possible_spec_acids)

    matches = []

    def combinate(peptides):
        next_peps = set()
        for peptide in peptides:
            if sum(peptide) == desired_sum:
                matches.append(peptide)
            if sum(peptide) < desired_sum and len(peptide) <= max_len:
                l = len(peptide)
                for acid in possible_spec_acids:
                    next_peps.add(peptide + (acid, ))
        if next_peps:
            print l, len(next_peps)
            combinate(next_peps)

    combinate([(a, ) for a in possible_spec_acids])

    print '\nMatches:', len(matches)
    for peptide in matches:
        test_spec = generate_spec(peptide)
        if test_spec == spec:
            print '-'.join(map(str, peptide)), ' ', \
                ''.join(map(acid_by_mass.get, peptide)), ' ', \
                ' '.join(map(str, test_spec))
def sequence_book(spectrum, n, m):
    if spectrum[0] != 0:
        spectrum = [0] + spectrum
    bitches = convolutions(spectrum, m)

    matches = defaultdict(list)
    leader, leader_score = (0,), 0
    leaderboard = [leader]
    parent = spectrum[-1]
    while leaderboard:
        leaderboard = expand(leaderboard, bitches)
        next_leaderboard = []
        for peptide in leaderboard:
            if sum(peptide) == parent:
                score = get_score(peptide, spectrum)
                if score > leader_score:
                    leader, leader_score = peptide, score
                    print ts(peptide), '', score, '', ts(generate_spec(peptide))
                matches[score].append(peptide)
            if sum(peptide) < parent:
                next_leaderboard.append(peptide)
        leaderboard = next_leaderboard
        leaderboard = cut_book(leaderboard, spectrum, n)

    return leader, leader_score, matches
def get_score(peptide, ref_spec):
    spectrum = [0] + sorted(generate_spec(peptide))
    ref_spec = ref_spec[:]
    #print ' '.join(map(str, spectrum))
    #print ' '.join(map(str, ref_spec))
    score = 0
    i, j = 0, 0
    while i < len(spectrum) and j < len(ref_spec):
        if spectrum[i] == ref_spec[j]:
            #print spectrum[i],
            score += 1
            #print spectrum[i], ref_spec[j], '', score
            i += 1
            j += 1
        elif spectrum[i] < ref_spec[j]:
            #print spectrum[i], '   '
            i += 1
        elif spectrum[i] > ref_spec[j]:
            #print '   ', ref_spec[j]
            j += 1
    #print
    return score
def get_score(peptide, ref_spec):
    spectrum = [0] + sorted(generate_spec(peptide))
    ref_spec = ref_spec[:]
    #print ' '.join(map(str, spectrum))
    #print ' '.join(map(str, ref_spec))
    score = 0
    i, j = 0, 0
    while i < len(spectrum) and j < len(ref_spec):
        if spectrum[i] == ref_spec[j]:
            #print spectrum[i],
            score += 1
            #print spectrum[i], ref_spec[j], '', score
            i += 1
            j += 1
        elif spectrum[i] < ref_spec[j]:
            #print spectrum[i], '   '
            i += 1
        elif spectrum[i] > ref_spec[j]:
            #print '   ', ref_spec[j]
            j += 1
    #print
    return score