|
from itertools import product |
|
|
|
class Isotope: |
|
def __init__(self, symbol, mass_number): |
|
self.symbol = symbol |
|
self.mass_number = mass_number |
|
self.superscript = self.generate_superscript(str(mass_number)) |
|
|
|
def generate_superscript(self, text): |
|
""" |
|
Generate superscript text based on mass number. |
|
|
|
Parameters: |
|
text (str): Text to convert to superscript. |
|
|
|
Returns: |
|
superscript_text (str): Superscript version of the input text. |
|
""" |
|
superscript_mapping = { |
|
'0': '⁰', |
|
'1': '¹', |
|
'2': '²', |
|
'3': '³', |
|
'4': '⁴', |
|
'5': '⁵', |
|
'6': '⁶', |
|
'7': '⁷', |
|
'8': '⁸', |
|
'9': '⁹', |
|
} |
|
|
|
superscript_text = ''.join([superscript_mapping.get(char, char) for char in text]) |
|
|
|
return superscript_text |
|
|
|
def mass_number_dict(self): |
|
""" |
|
Generate a dictionary of mass numbers for the isotopes. |
|
|
|
Returns: |
|
mass_number_dict (dict): Dictionary containing isotopes as keys |
|
and their corresponding mass numbers as values. |
|
""" |
|
if self.superscript: |
|
symbol_with_superscript = self.symbol + self.superscript |
|
else: |
|
symbol_with_superscript = self.symbol |
|
return {symbol_with_superscript: self.mass_number} |
|
|
|
def calculate_isotopomers(elements): |
|
""" |
|
Calculate isotopomers using brute-force method. |
|
|
|
Parameters: |
|
elements (list): List of tuples containing Isotope objects for each element. |
|
|
|
Returns: |
|
isotopomers (list): List of isotopomers generated using brute-force method. |
|
""" |
|
isotopomers = [] |
|
|
|
|
|
isotope_combinations = product(*elements) |
|
|
|
|
|
for combination in isotope_combinations: |
|
isotopomer_mass_numbers = {} |
|
for isotope in combination: |
|
isotopomer_mass_numbers.update(isotope.mass_number_dict()) |
|
isotopomers.append(isotopomer_mass_numbers) |
|
|
|
return isotopomers |
|
|
|
def sum_mass_numbers(isotopomers): |
|
""" |
|
Calculate the sum of mass numbers for each chemical formula in the isotopomers list. |
|
|
|
Parameters: |
|
isotopomers (list): List of dictionaries where keys are chemical formulas |
|
and values are mass numbers. |
|
|
|
Returns: |
|
formula_mass_sum (list): List of dictionaries where keys are chemical formulas |
|
and values are the sum of mass numbers for each formula. |
|
""" |
|
formula_mass_sum = [] |
|
|
|
for isotopomer in isotopomers: |
|
formula = ''.join(isotopomer.keys()) |
|
mass_sum = sum(isotopomer.values()) |
|
formula_mass_sum.append({formula: mass_sum}) |
|
|
|
return formula_mass_sum |
|
|
|
def remove_duplicates_and_count(isotopomers): |
|
""" |
|
Remove duplicates from isotopomers dictionaries and count the remaining lists. |
|
|
|
Parameters: |
|
isotopomers (list): List of dictionaries where keys are chemical formulas |
|
and values are lists of mass numbers. |
|
|
|
Returns: |
|
remaining_lists (int): Number of unique lists remaining after removing duplicates. |
|
""" |
|
|
|
sets_of_tuples = [frozenset(isotopomer.items()) for isotopomer in isotopomers] |
|
|
|
|
|
unique_sets_of_tuples = [] |
|
for set_of_tuples in sets_of_tuples: |
|
if set_of_tuples not in unique_sets_of_tuples: |
|
unique_sets_of_tuples.append(set_of_tuples) |
|
|
|
|
|
unique_isotopomers = [dict(set_of_tuples) for set_of_tuples in unique_sets_of_tuples] |
|
|
|
return len(unique_isotopomers) |
|
|
|
|