Merge pull request #64 from solgenomics/topic/update_view_permission
[SMMID.git] / script / calculate_molecular_weights.pl
blob0fb61b43255097c647cf60a2c4548749d52f6d17
2 use strict;
4 use SMIDDB;
6 my $schema = SMIDDB->connect('dbi:Pg:dbname=smid_db;host=localhost;user=postgres;password=DebianBox**');
8 my $compounds = $schema->resultset("SMIDDB::Result::Compound")->search( {} );
10 while (my $compound = $compounds->next()) {
11 my $molecular_weight = molecular_weight($compound->formula());
13 $compound->update( { molecular_weight => $molecular_weight });
20 sub molecular_weight {
21 #...
22 #The default variable will be used as the chemical Formula
23 $_ = shift(@_);
25 my %elements = (
26 "H" => 1.00794,
27 "D" => 2.014101,
28 "T" => 3.016049,
29 "He" => 4.002602,
30 "Li" => 6.941,
31 "Be" => 9.012182,
32 "B" => 10.811,
33 "C" => 12.0107,
34 "N" => 14.00674,
35 "O" => 15.9994,
36 "F" => 18.9984032,
37 "Ne" => 20.1797,
38 "Na" => 22.989770,
39 "Mg" => 24.3050,
40 "Al" => 26.981538,
41 "Si" => 28.0855,
42 "P" => 30.973761,
43 "S" => 32.066,
44 "Cl" => 35.4527,
45 "Ar" => 39.948,
46 "K" => 39.0983,
47 "Ca" => 40.078,
48 "Sc" => 44.955910,
49 "Ti" => 47.867,
50 "V" => 50.9415,
51 "Cr" => 51.9961,
52 "Mn" => 54.938049,
53 "Fe" => 55.845,
54 "Co" => 58.933200,
55 "Ni" => 58.6934,
56 "Cu" => 63.546,
57 "Zn" => 65.39,
58 "Ga" => 69.723,
59 "Ge" => 72.61,
60 "As" => 74.92160,
61 "Se" => 78.96,
62 "Br" => 79.904,
63 "Kr" => 83.80,
64 "Rb" => 85.4678,
65 "Sr" => 87.62,
66 "Y" => 88.90585,
67 "Zr" => 91.224,
68 "Nb" => 92.90638,
69 "Mo" => 95.94,
70 "Tc" => 98,
71 "Ru" => 101.07,
72 "Rh" => 102.90550,
73 "Pd" => 106.42,
74 "Ag" => 107.8682,
75 "Cd" => 112.411,
76 "In" => 114.818,
77 "Sn" => 118.710,
78 "Sb" => 121.760,
79 "Te" => 127.60,
80 "I" => 126.90447,
81 "Xe" => 131.29,
82 "Cs" => 132.90545,
83 "Ba" => 137.327,
84 "La" => 138.9055,
85 "Ce" => 140.116,
86 "Pr" => 140.90765,
87 "Nd" => 144.24,
88 "Pm" => 145,
89 "Sm" => 150.36,
90 "Eu" => 151.964,
91 "Gd" => 157.25,
92 "Tb" => 158.92534,
93 "Dy" => 162.50,
94 "Ho" => 164.93032,
95 "Er" => 167.26,
96 "Tm" => 168.93421,
97 "Yb" => 173.04,
98 "Lu" => 174.967,
99 "Hf" => 178.49,
100 "Ta" => 180.9479,
101 "W" => 183.84,
102 "Re" => 186.207,
103 "Os" => 190.23,
104 "Ir" => 192.217,
105 "Pt" => 195.078,
106 "Au" => 196.96655,
107 "Hg" => 200.59,
108 "Tl" => 204.3833,
109 "Pb" => 207.2,
110 "Bi" => 208.98038,
111 "Po" => 209,
112 "At" => 210,
113 "Rn" => 222,
114 "Fr" => 223,
115 "Ra" => 226,
116 "Ac" => 227,
117 "Th" => 232.038,
118 "Pa" => 231.03588,
119 "U" => 238.0289,
120 "Np" => 237,
121 "Pu" => 244,
122 "Am" => 243,
123 "Cm" => 247,
124 "Bk" => 247,
125 "Cf" => 251,
126 "Es" => 252,
127 "Fm" => 257,
128 "Md" => 258,
129 "No" => 259,
130 "Lr" => 262,
131 "Rf" => 261,
132 "Db" => 262,
133 "Sg" => 266,
134 "Bh" => 264,
135 "Hs" => 269,
136 "Mt" => 268,
137 "Uun" => 271,
138 "Uuu" => 272
140 my $weight = 0;
142 my @pairs = /([CHONPS][0-9]*)/g;
143 foreach my $pair (@pairs){
144 if (length($pair)==1){
145 $weight += $elements{substr($pair, 0, 1)};
146 }else{
147 $weight += $elements{substr($pair, 0, 1)}*substr($pair, 1);
150 return $weight;