robert.elder commited on
Commit
87f0a60
·
1 Parent(s): 0d05dfd

updated error handling

Browse files
Files changed (1) hide show
  1. ChemID.py +29 -32
ChemID.py CHANGED
@@ -70,7 +70,7 @@ def ResolveChemical(chemName, IDtype, debug=False):
70
  name = cas2name(cas)
71
  rho, rho_origin = string2density(cas)
72
  mp, mp_origin = mol2mp(cas, name, smiles)
73
- if rho is None and name:
74
  rho, rho_origin = string2density(name)
75
  try:
76
  mol = Chem.MolFromSmiles(smiles)
@@ -92,7 +92,7 @@ def ResolveChemical(chemName, IDtype, debug=False):
92
  if name:
93
  cas = name2cas(name)
94
  rho, rho_origin = string2density(name)
95
- if rho is None and cas:
96
  rho, rho_origin = string2density(cas)
97
 
98
  mp, mp_origin = mol2mp(cas, name, smiles)
@@ -111,19 +111,19 @@ def ResolveChemical(chemName, IDtype, debug=False):
111
  elif IDtype == 'common':
112
  name = chemName
113
 
114
- # try this first because sometimes iupac names don't work
115
- rho, rho_origin = string2density(name)
116
-
117
  name, name_origin = name2iupac(name)
118
  smiles = name2smiles(name)
119
  cas = name2cas(name)
120
  if not smiles:
121
  smiles = cas2smiles(cas)
122
 
123
- if rho is None and cas:
124
  rho, rho_origin = string2density(cas)
125
- if rho is None:
126
  rho, rho_origin = string2density(name)
 
 
 
127
 
128
  if smiles:
129
  mp, mp_origin = mol2mp(cas, name, smiles)
@@ -502,11 +502,11 @@ def string2density(name):
502
  mask = df_pred['CASRN'] == name
503
  if sum(mask):
504
  rho = float(df_pred[mask]['DENSITY_G/CM^3_TEST_PRED'])
505
- rho_origin = 'pred'
506
  # try to scrape from PubChem
507
  if pd.isna(rho):
 
508
  try:
509
- content = None
510
  compounds = pcp.get_compounds(name, namespace='name')
511
  c = compounds[0]
512
  cid = c.cid
@@ -603,7 +603,7 @@ def string2density(name):
603
  value = re.sub(' \([0-9]*\)', '', text.strip())
604
  try:
605
  rho = float(value)
606
- rho_origin = 'expt/dsstox'
607
  except:
608
  rho, rho_origin = None, None
609
  if pd.isna(rho):
@@ -612,16 +612,14 @@ def string2density(name):
612
  value = re.sub(' \([0-9]*\)', '', text.strip())
613
  try:
614
  rho = float(value)
615
- rho_origin = 'pred/dsstox'
616
  except:
617
  rho, rho_origin = None, None
618
  else:
619
  rho, rho_origin = None, None
620
  else:
621
  rho, rho_origin = None, None
622
- if pd.isna(rho):
623
- rho = None
624
- rho_origin = None
625
  return rho, rho_origin
626
 
627
  def return_non_duplicate_index(tuples): ##Given a list of sets return index of non_duplicate items
@@ -860,13 +858,13 @@ def string2mp(name):
860
  # try the chemicals package
861
  if is_cas(name):
862
  cas = name
863
- if mp is None:
864
  mp = chemicals.Tm(cas)
865
  if mp: mp = mp-273.15
866
  methods = chemicals.Tm_methods(cas)
867
  if methods: mp_origin = 'chem/'+methods[0]
868
  # experimental values
869
- if mp is None:
870
  # some rows have multiple comma-separated cas numbers
871
  mask = dfmp_expt['CAS'].str.contains(cas)
872
  if sum(mask):
@@ -876,21 +874,21 @@ def string2mp(name):
876
  mp = float(dfmp_expt.iloc[index]['MP'])
877
  mp_origin = 'expt'
878
  # predicted values from Mansouri
879
- #if mp is None:
880
  # mask = dfmp_pred['Substance_CASRN'] == cas
881
  # if sum(mask):
882
  # mp = float(dfmp_pred[mask]['NCCT_MP'])
883
  # mp_origin = 'pred'
884
  # predicted values from OPERA (CompTox dashboard)
885
- if mp is None:
886
  mask = df_pred['CASRN'] == cas
887
  if sum(mask):
888
  mp = float(df_pred[mask]['MELTING_POINT_DEGC_OPERA_PRED'])
889
- mp_origin = 'pred'
890
  # try to scrape from PubChem
891
- if mp is None:
 
892
  try:
893
- content = None
894
  compounds = pcp.get_compounds(name, namespace='name')
895
  c = compounds[0]
896
  cid = c.cid
@@ -954,7 +952,7 @@ def string2mp(name):
954
  mp, mp_origin = None, None
955
  # try to scrape from DSSTOX website...
956
  if try_dsstox:
957
- if mp is None:
958
  dtxsid = None
959
  # try to find it via the dsstox dashboard
960
  try:
@@ -1004,24 +1002,23 @@ def string2mp(name):
1004
  value = re.sub(' \([0-9]*\)', '', text.strip())
1005
  try:
1006
  mp = float(value)
1007
- mp_origin = 'expt/dsstox'
1008
  except:
1009
  mp, mp_origin = None, None
1010
- if mp is None:
1011
  rows = mysoup.find_all('div', attrs={'col-id':'predavg'})
1012
  text = rows[ifound].text
1013
  value = re.sub(' \([0-9]*\)', '', text.strip())
1014
  try:
1015
  mp = float(value)
1016
- mp_origin = 'pred/dsstox'
1017
  except:
1018
  mp, mp_origin = None, None
1019
  else:
1020
  mp, mp_origin = None, None
1021
  else:
1022
  mp, mp_origin = None, None
1023
- if mp is not None and np.isnan(mp): mp = None
1024
- if mp is None: mp_origin = None
1025
  return mp, mp_origin
1026
 
1027
  def smiles2mp(smiles):
@@ -1038,15 +1035,15 @@ def mol2mp(cas, name, smiles):
1038
  mp, mp_origin = string2mp(cas)
1039
  except:
1040
  mp, mp_origin = None, None
1041
- if mp is None and name:
1042
  try:
1043
  mp, mp_origin = string2mp(name)
1044
  except:
1045
  mp, mp_origin = None, None
1046
- if mp is None and smiles:
1047
  try:
1048
  mp = smiles2mp(smiles)
1049
- mp_origin = 'calc'
1050
  except:
1051
  mp, mp_origin = None, None
1052
  return mp, mp_origin
@@ -1057,9 +1054,9 @@ def getLogP(cas,mol):
1057
  mask = df_pred['CASRN'] == cas
1058
  if sum(mask):
1059
  LogP = float(df_pred[mask]['OCTANOL_WATER_PARTITION_LOGP_OPERA_PRED'])
1060
- LogP_origin = 'pred'
1061
  if pd.isna(LogP) and mol:
1062
  LogP = Crippen.MolLogP(mol)
1063
- LogP_origin = 'calc'
1064
  return LogP, LogP_origin
1065
 
 
70
  name = cas2name(cas)
71
  rho, rho_origin = string2density(cas)
72
  mp, mp_origin = mol2mp(cas, name, smiles)
73
+ if pd.isna(rho) and name:
74
  rho, rho_origin = string2density(name)
75
  try:
76
  mol = Chem.MolFromSmiles(smiles)
 
92
  if name:
93
  cas = name2cas(name)
94
  rho, rho_origin = string2density(name)
95
+ if pd.isna(rho) and cas:
96
  rho, rho_origin = string2density(cas)
97
 
98
  mp, mp_origin = mol2mp(cas, name, smiles)
 
111
  elif IDtype == 'common':
112
  name = chemName
113
 
 
 
 
114
  name, name_origin = name2iupac(name)
115
  smiles = name2smiles(name)
116
  cas = name2cas(name)
117
  if not smiles:
118
  smiles = cas2smiles(cas)
119
 
120
+ if pd.isna(rho) and cas:
121
  rho, rho_origin = string2density(cas)
122
+ if pd.isna(rho):
123
  rho, rho_origin = string2density(name)
124
+ if pd.isna(rho):
125
+ # try this because sometimes iupac names don't work
126
+ rho, rho_origin = string2density(chemName)
127
 
128
  if smiles:
129
  mp, mp_origin = mol2mp(cas, name, smiles)
 
502
  mask = df_pred['CASRN'] == name
503
  if sum(mask):
504
  rho = float(df_pred[mask]['DENSITY_G/CM^3_TEST_PRED'])
505
+ rho_origin = 'comptox/pred'
506
  # try to scrape from PubChem
507
  if pd.isna(rho):
508
+ content = None
509
  try:
 
510
  compounds = pcp.get_compounds(name, namespace='name')
511
  c = compounds[0]
512
  cid = c.cid
 
603
  value = re.sub(' \([0-9]*\)', '', text.strip())
604
  try:
605
  rho = float(value)
606
+ rho_origin = 'dsstox/expt'
607
  except:
608
  rho, rho_origin = None, None
609
  if pd.isna(rho):
 
612
  value = re.sub(' \([0-9]*\)', '', text.strip())
613
  try:
614
  rho = float(value)
615
+ rho_origin = 'dsstox/pred'
616
  except:
617
  rho, rho_origin = None, None
618
  else:
619
  rho, rho_origin = None, None
620
  else:
621
  rho, rho_origin = None, None
622
+ if pd.isna(rho): rho,rho_origin = None, None
 
 
623
  return rho, rho_origin
624
 
625
  def return_non_duplicate_index(tuples): ##Given a list of sets return index of non_duplicate items
 
858
  # try the chemicals package
859
  if is_cas(name):
860
  cas = name
861
+ if pd.isna(mp):
862
  mp = chemicals.Tm(cas)
863
  if mp: mp = mp-273.15
864
  methods = chemicals.Tm_methods(cas)
865
  if methods: mp_origin = 'chem/'+methods[0]
866
  # experimental values
867
+ if pd.isna(mp):
868
  # some rows have multiple comma-separated cas numbers
869
  mask = dfmp_expt['CAS'].str.contains(cas)
870
  if sum(mask):
 
874
  mp = float(dfmp_expt.iloc[index]['MP'])
875
  mp_origin = 'expt'
876
  # predicted values from Mansouri
877
+ #if pd.isna(mp):
878
  # mask = dfmp_pred['Substance_CASRN'] == cas
879
  # if sum(mask):
880
  # mp = float(dfmp_pred[mask]['NCCT_MP'])
881
  # mp_origin = 'pred'
882
  # predicted values from OPERA (CompTox dashboard)
883
+ if pd.isna(mp):
884
  mask = df_pred['CASRN'] == cas
885
  if sum(mask):
886
  mp = float(df_pred[mask]['MELTING_POINT_DEGC_OPERA_PRED'])
887
+ mp_origin = 'comptox/pred'
888
  # try to scrape from PubChem
889
+ if pd.isna(mp):
890
+ content = None
891
  try:
 
892
  compounds = pcp.get_compounds(name, namespace='name')
893
  c = compounds[0]
894
  cid = c.cid
 
952
  mp, mp_origin = None, None
953
  # try to scrape from DSSTOX website...
954
  if try_dsstox:
955
+ if pd.isna(mp):
956
  dtxsid = None
957
  # try to find it via the dsstox dashboard
958
  try:
 
1002
  value = re.sub(' \([0-9]*\)', '', text.strip())
1003
  try:
1004
  mp = float(value)
1005
+ mp_origin = 'dsstox/expt'
1006
  except:
1007
  mp, mp_origin = None, None
1008
+ if pd.isna(mp):
1009
  rows = mysoup.find_all('div', attrs={'col-id':'predavg'})
1010
  text = rows[ifound].text
1011
  value = re.sub(' \([0-9]*\)', '', text.strip())
1012
  try:
1013
  mp = float(value)
1014
+ mp_origin = 'dsstox/pred'
1015
  except:
1016
  mp, mp_origin = None, None
1017
  else:
1018
  mp, mp_origin = None, None
1019
  else:
1020
  mp, mp_origin = None, None
1021
+ if pd.isna(mp): mp, mp_origin = None, None
 
1022
  return mp, mp_origin
1023
 
1024
  def smiles2mp(smiles):
 
1035
  mp, mp_origin = string2mp(cas)
1036
  except:
1037
  mp, mp_origin = None, None
1038
+ if pd.isna(mp) and name:
1039
  try:
1040
  mp, mp_origin = string2mp(name)
1041
  except:
1042
  mp, mp_origin = None, None
1043
+ if pd.isna(mp) and smiles:
1044
  try:
1045
  mp = smiles2mp(smiles)
1046
+ mp_origin = 'joback-reid/calc'
1047
  except:
1048
  mp, mp_origin = None, None
1049
  return mp, mp_origin
 
1054
  mask = df_pred['CASRN'] == cas
1055
  if sum(mask):
1056
  LogP = float(df_pred[mask]['OCTANOL_WATER_PARTITION_LOGP_OPERA_PRED'])
1057
+ LogP_origin = 'comptox/pred'
1058
  if pd.isna(LogP) and mol:
1059
  LogP = Crippen.MolLogP(mol)
1060
+ LogP_origin = 'rdkit/calc'
1061
  return LogP, LogP_origin
1062