Create isopotomacLib.py
Browse files- isopotomacLib.py +119 -0
isopotomacLib.py
ADDED
@@ -0,0 +1,119 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from itertools import product
|
2 |
+
|
3 |
+
class Isotope:
|
4 |
+
def __init__(self, symbol, mass_number):
|
5 |
+
self.symbol = symbol
|
6 |
+
self.mass_number = mass_number
|
7 |
+
self.superscript = self.generate_superscript(str(mass_number))
|
8 |
+
|
9 |
+
def generate_superscript(self, text):
|
10 |
+
"""
|
11 |
+
Generate superscript text based on mass number.
|
12 |
+
|
13 |
+
Parameters:
|
14 |
+
text (str): Text to convert to superscript.
|
15 |
+
|
16 |
+
Returns:
|
17 |
+
superscript_text (str): Superscript version of the input text.
|
18 |
+
"""
|
19 |
+
superscript_mapping = {
|
20 |
+
'0': '⁰',
|
21 |
+
'1': '¹',
|
22 |
+
'2': '²',
|
23 |
+
'3': '³',
|
24 |
+
'4': '⁴',
|
25 |
+
'5': '⁵',
|
26 |
+
'6': '⁶',
|
27 |
+
'7': '⁷',
|
28 |
+
'8': '⁸',
|
29 |
+
'9': '⁹',
|
30 |
+
}
|
31 |
+
|
32 |
+
superscript_text = ''.join([superscript_mapping.get(char, char) for char in text])
|
33 |
+
|
34 |
+
return superscript_text
|
35 |
+
|
36 |
+
def mass_number_dict(self):
|
37 |
+
"""
|
38 |
+
Generate a dictionary of mass numbers for the isotopes.
|
39 |
+
|
40 |
+
Returns:
|
41 |
+
mass_number_dict (dict): Dictionary containing isotopes as keys
|
42 |
+
and their corresponding mass numbers as values.
|
43 |
+
"""
|
44 |
+
if self.superscript:
|
45 |
+
symbol_with_superscript = self.symbol + self.superscript
|
46 |
+
else:
|
47 |
+
symbol_with_superscript = self.symbol
|
48 |
+
return {symbol_with_superscript: self.mass_number}
|
49 |
+
|
50 |
+
def calculate_isotopomers(elements):
|
51 |
+
"""
|
52 |
+
Calculate isotopomers using brute-force method.
|
53 |
+
|
54 |
+
Parameters:
|
55 |
+
elements (list): List of tuples containing Isotope objects for each element.
|
56 |
+
|
57 |
+
Returns:
|
58 |
+
isotopomers (list): List of isotopomers generated using brute-force method.
|
59 |
+
"""
|
60 |
+
isotopomers = []
|
61 |
+
|
62 |
+
# Generate all possible combinations of isotopes for each element
|
63 |
+
isotope_combinations = product(*elements)
|
64 |
+
|
65 |
+
# Iterate over each combination to calculate isotopomers
|
66 |
+
for combination in isotope_combinations:
|
67 |
+
isotopomer_mass_numbers = {}
|
68 |
+
for isotope in combination:
|
69 |
+
isotopomer_mass_numbers.update(isotope.mass_number_dict())
|
70 |
+
isotopomers.append(isotopomer_mass_numbers)
|
71 |
+
|
72 |
+
return isotopomers
|
73 |
+
|
74 |
+
def sum_mass_numbers(isotopomers):
|
75 |
+
"""
|
76 |
+
Calculate the sum of mass numbers for each chemical formula in the isotopomers list.
|
77 |
+
|
78 |
+
Parameters:
|
79 |
+
isotopomers (list): List of dictionaries where keys are chemical formulas
|
80 |
+
and values are mass numbers.
|
81 |
+
|
82 |
+
Returns:
|
83 |
+
formula_mass_sum (list): List of dictionaries where keys are chemical formulas
|
84 |
+
and values are the sum of mass numbers for each formula.
|
85 |
+
"""
|
86 |
+
formula_mass_sum = []
|
87 |
+
|
88 |
+
for isotopomer in isotopomers:
|
89 |
+
formula = ''.join(isotopomer.keys())
|
90 |
+
mass_sum = sum(isotopomer.values())
|
91 |
+
formula_mass_sum.append({formula: mass_sum})
|
92 |
+
|
93 |
+
return formula_mass_sum
|
94 |
+
|
95 |
+
def remove_duplicates_and_count(isotopomers):
|
96 |
+
"""
|
97 |
+
Remove duplicates from isotopomers dictionaries and count the remaining lists.
|
98 |
+
|
99 |
+
Parameters:
|
100 |
+
isotopomers (list): List of dictionaries where keys are chemical formulas
|
101 |
+
and values are lists of mass numbers.
|
102 |
+
|
103 |
+
Returns:
|
104 |
+
remaining_lists (int): Number of unique lists remaining after removing duplicates.
|
105 |
+
"""
|
106 |
+
# Convert isotopomers to sets of tuples for easier comparison
|
107 |
+
sets_of_tuples = [frozenset(isotopomer.items()) for isotopomer in isotopomers]
|
108 |
+
|
109 |
+
# Remove duplicates
|
110 |
+
unique_sets_of_tuples = []
|
111 |
+
for set_of_tuples in sets_of_tuples:
|
112 |
+
if set_of_tuples not in unique_sets_of_tuples:
|
113 |
+
unique_sets_of_tuples.append(set_of_tuples)
|
114 |
+
|
115 |
+
# Convert back to dictionaries
|
116 |
+
unique_isotopomers = [dict(set_of_tuples) for set_of_tuples in unique_sets_of_tuples]
|
117 |
+
|
118 |
+
return len(unique_isotopomers)
|
119 |
+
|