Example #1
0
    with open('data/stepic_2e.txt') as input_data:
        n, spec = [
            int(line.strip()) if i == 0 else map(int,
                                                 line.strip().split())
            for i, line in enumerate(input_data.readlines())
        ]

    # Create the protein weight dictionary.
    weight = ProteinWeightDict()
    # Initialize the scores dictionary.
    scores = dict()
    # Build the intial peptides.
    seq = filter(lambda L: L[0] != -1,
                 [[spectrum_score(peptide, spec), peptide]
                  for peptide in append_protein(weight.keys())])

    # Build the sequence until the masses all grow too large.
    while seq != []:
        # Store the scores of the current sequence in a dictionary.
        scores = dict()
        for item in seq:
            if item[0] in scores:
                scores[item[0]].append(item[1])
            else:
                scores[item[0]] = [item[1]]

        # Get the n leading scores with ties, remove lower scores from dictionary.
        leaders, leader_scores = [], []
        if sum(len(peptides) for peptides in scores.values()) < n:
            leaders = scores[max(scores.keys())]
	# Return -1 if the peptide has more mass than exp_spec.
	if pep_spec[-1] > exp_spec[-1]:
		return -1
	return sum([min(pep_spec.count(protein),exp_spec.count(protein)) for protein in set(pep_spec)])

if __name__ == '__main__':

	with open('data/stepic_2e.txt') as input_data:
		n, spec = [int(line.strip()) if i==0 else map(int,line.strip().split()) for i, line in enumerate(input_data.readlines())]
	
	# Create the protein weight dictionary.
	weight = ProteinWeightDict()
	# Initialize the scores dictionary.
	scores = dict()
	# Build the intial peptides.
	seq = filter(lambda L: L[0] != -1, [[spectrum_score(peptide,spec), peptide] for peptide in append_protein(weight.keys())]) 

	# Build the sequence until the masses all grow too large.
	while seq != []:
		# Store the scores of the current sequence in a dictionary.
		scores = dict()
		for item in seq:
			if item[0] in scores:
				scores[item[0]].append(item[1])
			else:
				scores[item[0]] = [item[1]]

		# Get the n leading scores with ties, remove lower scores from dictionary.
		leaders, leader_scores = [], []
		if sum(len(peptides) for peptides in scores.values()) < n:
			leaders = scores[max(scores.keys())]
Example #3
0
	# Return -1 if the peptide has more mass than exp_spec.
	if pep_spec[-1] > exp_spec[-1]:
		return -1
	return sum([min(pep_spec.count(protein),exp_spec.count(protein)) for protein in set(pep_spec)])

if __name__ == '__main__':

	with open('data/textbook/rosalind_2e.txt') as input_data:
		n, spec = [int(line.strip()) if i==0 else map(int,line.strip().split()) for i, line in enumerate(input_data.readlines())]
	
	# Create the protein weight dictionary.
	weight = ProteinWeightDict()
	# Initialize the scores dictionary.
	scores = dict()
	# Build the intial peptides.
	seq = filter(lambda L: L[0] != -1, [[spectrum_score(peptide,spec), peptide] for peptide in append_protein(weight.keys())]) 

	# Build the sequence until the masses all grow too large.
	while seq != []:
		# Store the scores of the current sequence in a dictionary.
		scores = dict()
		for item in seq:
			if item[0] in scores:
				scores[item[0]].append(item[1])
			else:
				scores[item[0]] = [item[1]]

		# Get the n leading scores with ties, remove lower scores from dictionary.
		leaders, leader_scores = [], []
		if sum(len(peptides) for peptides in scores.values()) < n:
			leaders = scores[max(scores.keys())]