Hide keyboard shortcuts

Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

135

136

137

138

139

140

141

142

143

144

145

146

147

148

149

150

151

152

153

154

155

156

157

158

159

160

161

162

163

164

165

166

167

168

169

170

171

172

173

174

175

176

177

178

179

180

181

182

183

184

185

186

187

188

189

190

191

192

193

194

195

196

197

198

199

200

201

202

203

204

205

206

207

208

209

210

211

212

213

214

215

216

217

218

219

220

221

222

223

224

225

226

227

228

229

230

231

232

233

234

235

236

237

238

239

240

241

242

243

244

245

246

247

248

249

250

251

252

253

254

255

256

257

258

259

260

261

262

263

264

265

266

267

268

269

270

271

272

273

274

275

276

277

278

279

280

281

282

283

284

285

286

287

288

289

290

291

292

293

294

295

296

297

298

299

300

301

302

303

304

305

306

307

308

309

310

311

312

313

314

315

316

317

318

319

320

321

322

323

324

325

326

327

328

329

330

331

332

333

334

335

336

337

338

339

340

341

342

343

344

345

346

347

348

349

350

351

352

353

354

355

356

357

358

359

360

361

362

363

364

365

366

367

368

369

370

371

372

373

374

375

376

377

378

379

380

381

382

383

384

385

386

387

388

389

390

391

392

393

394

395

396

397

398

399

400

401

402

403

404

405

406

407

408

409

410

411

412

413

414

415

416

417

418

419

420

421

422

423

424

425

426

427

428

429

430

431

432

433

434

435

436

437

438

439

# -*- coding: utf-8 -*- 

'''Chemical Engineering Design Library (ChEDL). Utilities for process modeling. 

Copyright (C) 2016, Caleb Bell <Caleb.Andrew.Bell@gmail.com> 

 

Permission is hereby granted, free of charge, to any person obtaining a copy 

of this software and associated documentation files (the "Software"), to deal 

in the Software without restriction, including without limitation the rights 

to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 

copies of the Software, and to permit persons to whom the Software is 

furnished to do so, subject to the following conditions: 

 

The above copyright notice and this permission notice shall be included in all 

copies or substantial portions of the Software. 

 

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 

IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 

FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 

AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 

LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 

OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 

SOFTWARE.''' 

 

from __future__ import division 

 

__all__ = ['checkCAS', 'CASfromAny', 'PubChem', 'MW', 'formula', 'smiles', 

'InChI', 'InChI_Key', 'IUPAC_name', 'name', 'synonyms', 

'_MixtureDict', 'mixture_from_any', 'cryogenics', 'dippr_compounds', 

'pubchem_dict'] 

import os 

from thermo.utils import to_num 

 

folder = os.path.join(os.path.dirname(__file__), 'Identifiers') 

 

def checkCAS(CASRN): 

'''Checks if a CAS number is valid. Crashes if not. 

 

Parameters 

---------- 

CASRN : string 

A three-piece, dash-separated set of numbers 

 

Returns 

------- 

result : bool 

Boolean value if CASRN was valid. If parsing fails, return False also. 

 

Notes 

----- 

Check method is according to Chemical Abstract Society. However, no lookup 

to their service is performed; therefore, this function cannot detect 

false positives. 

 

Function also does not support additional separators, apart from '-'. 

 

Examples 

-------- 

>>> checkCAS('7732-18-5') 

True 

>>> checkCAS('77332-18-5') 

False 

 

References 

---------- 

TODO 

''' 

try: 

check = CASRN[-1] 

CASRN = CASRN[::-1][1:] 

productsum = 0 

i = 1 

for num in CASRN: 

if num == '-': 

pass 

else: 

productsum+= i*int(num) 

i +=1 

result = (productsum % 10 == int(check)) 

return result 

except: 

return False 

 

 

 

 

_cas_from_pubchem_dict = {} 

_cas_from_smiles_dict = {} 

_cas_from_inchi_dict = {} 

_cas_from_inchikey_dict = {} 

_cas_from_name_dict = {} 

_cas_from_iupacname_dict = {} 

 

 

pubchem_dict = {} 

 

 

with open(os.path.join(folder, 'chemical identifiers.csv')) as f: 

for line in f: 

values = line.rstrip('\n').split('\t') 

(pubchemid, CAS, formula, mw, smiles, inchi, inchikey, iupac_name, common_name) = values[0:9] 

allnames = values[7:] 

pubchemid = int(pubchemid) 

mw = float(mw) 

# Create lookup dictionaries 

_cas_from_pubchem_dict[pubchemid] = CAS 

_cas_from_smiles_dict[smiles] = CAS 

_cas_from_inchi_dict[inchi] = CAS 

_cas_from_inchikey_dict[inchikey] = CAS 

if iupac_name in _cas_from_iupacname_dict: 

# TODO: make unnecessary by removing previously unique identifiers, 

# which are no longer unique after making them lower case. 

pass 

else: 

_cas_from_iupacname_dict[iupac_name] = CAS 

for name in allnames: 

# TODO: make unnecessary by removing previously unique identifiers, 

# which are no longer unique after making them lower case. 

if name in _cas_from_name_dict: 

pass 

else: 

_cas_from_name_dict[name] = CAS 

 

pubchem_dict[CAS] = {'Pubchem ID': pubchemid, 'formula': formula, 

'MW': mw, 'SMILES': smiles, 'InChI': inchi, 'InChI Key': inchikey, 

'IUPAC name': iupac_name, 'common name': common_name, 'Names': allnames} 

# _pubchem_info(int(pubchemid), formula, float(mw), smiles, inchi, inchikey, iupac_name, common_name, allnames) 

 

del pubchemid, formula, mw, smiles, inchi, inchikey, iupac_name, \ 

common_name, allnames, name, line, f, CAS, values 

#print len(_cas_from_name_dict)/float(len(_cas_from_pubchem_dict)) 

#print _cas_from_name_dict['Water'.lower()] 

#print _pubchem_dict['7732-18-5'] 

 

 

def CASfromAny(ID): 

'''Input must be string 

First check if input is InChI, best defined format 

TODO: if int, format as rest-2digits-1digit and see if in dict. All CASs are in there. 

''' 

CASRN = None 

ID = ID.strip() 

if checkCAS(ID): 

# print 'CAS' 

try: 

a = pubchem_dict[ID] 

CASRN = ID 

except: 

try: 

CASRN = _cas_from_name_dict[ID] 

except: 

pass 

 

if type(ID) == type('') and len(ID) > 9 and not CASRN: 

# InChI strings and keys are not stored with format for space reasons 

if ID[0:9] == 'InChI=1S/': 

try: 

CASRN = _cas_from_inchi_dict[ID[9:]] 

except: 

pass 

 

if ID[0:8] == 'InChI=1/' and not CASRN: 

try: 

CASRN = _cas_from_inchi_dict[ID[8:]] 

except: 

pass 

 

if ID[0:9] == 'InChIKey=' and not CASRN: 

try: 

CASRN = _cas_from_inchikey_dict[ID[9:]] 

except: 

pass 

if type(ID) == type('') and not CASRN: 

# Parsing SMILES is an option, but this is faster 

# Pybel API also prints messages to console on failure 

try: 

CASRN = _cas_from_smiles_dict[ID] 

except: 

pass 

 

if type(ID) == type('') or type(ID) == type(1324) and not CASRN: 

try: 

intID = int(ID) 

CASRN = _cas_from_pubchem_dict[intID] 

except: 

pass 

if type(ID) == type('') and not CASRN: 

# print 'Early error' 

try: 

CASRN = _cas_from_iupacname_dict[ID.lower()] 

 

# print 'CASRN' 

except: 

pass 

if type(ID) == type('') and not CASRN: 

try: 

CASRN = _cas_from_name_dict[ID.lower()] 

except: 

try: 

ID = ID.replace(' ', '') 

CASRN = _cas_from_name_dict[ID.lower()] 

except: 

try: 

ID = ID.replace('-', '') 

CASRN = _cas_from_name_dict[ID.lower()] 

except: 

CASRN = None 

# raise Exception('Not Found') 

return CASRN 

 

 

 

 

 

def PubChem(CASRN): 

''' 

Given a CASRN in the database, obtain the PubChem database 

number of the compound. 

 

Parameters 

---------- 

CASRN : string 

Valid CAS number in PubChem database 

 

Returns 

------- 

pubchem : int 

PubChem database id, as an integer 

 

Notes 

----- 

CASRN must be an indexing key in the pubchem database. 

 

Examples 

-------- 

>>> PubChem('7732-18-5') 

962 

 

References 

---------- 

.. [1] Pubchem. 

 

''' 

pubchem = pubchem_dict[CASRN]['Pubchem ID'] 

return pubchem 

 

 

def MW(CASRN): 

'''Given a CASRN in the database, obtain the Molecular weight of the 

compound, if it is in the database. 

 

Parameters 

---------- 

CASRN : string 

Valid CAS number in PubChem database 

 

Returns 

------- 

MolecularWeight : float 

 

Notes 

----- 

CASRN must be an indexing key in the pubchem database. No MW Calculation is 

performed; nor are any historical isotopic corrections applied. 

 

Examples 

-------- 

>>> MW('7732-18-5') 

18.01528 

 

References 

---------- 

.. [1] Pubchem. 

''' 

 

MolecularWeight = pubchem_dict[CASRN]['MW'] 

return MolecularWeight 

 

 

def formula(CASRN): 

''' 

>>> formula('7732-18-5') 

'H2O' 

''' 

Formula = pubchem_dict[CASRN]['formula'] 

return Formula 

 

 

def smiles(CASRN): 

''' 

>>> smiles('7732-18-5') 

'O' 

''' 

Smiles = pubchem_dict[CASRN]['SMILES'] 

return Smiles 

 

 

def InChI(CASRN): 

''' 

>>> InChI('7732-18-5') 

'H2O/h1H2' 

''' 

inchi = pubchem_dict[CASRN]['InChI'] 

return inchi 

 

 

def InChI_Key(CASRN): 

''' 

>>> InChI_Key('7732-18-5') 

'XLYOFNOQVPJJNP-UHFFFAOYSA-N' 

''' 

inchikey = pubchem_dict[CASRN]['InChI Key'] 

return inchikey 

 

 

def IUPAC_name(CASRN): 

''' 

>>> IUPAC_name('7732-18-5') 

'oxidane' 

''' 

iupac_name = pubchem_dict[CASRN]['IUPAC name'] 

return iupac_name 

 

def name(CASRN): 

''' 

>>> name('7732-18-5') 

'water' 

''' 

common_name = pubchem_dict[CASRN]['common name'] 

return common_name 

 

 

def synonyms(CASRN): 

''' 

>>> synonyms('98-00-0') 

['furan-2-ylmethanol', 'furfuryl alcohol', '2-furanmethanol', '2-furancarbinol', '2-furylmethanol', '2-furylcarbinol', '98-00-0', '2-furanylmethanol', 'furfuranol', 'furan-2-ylmethanol', '2-furfuryl alcohol', '5-hydroxymethylfuran', 'furfural alcohol', 'alpha-furylcarbinol', '2-hydroxymethylfuran', 'furfuralcohol', 'furylcarbinol', 'furyl alcohol', '2-(hydroxymethyl)furan', 'furan-2-yl-methanol', 'furfurylalcohol', 'furfurylcarb', 'methanol, (2-furyl)-', '2-furfurylalkohol', 'furan-2-methanol', '2-furane-methanol', '2-furanmethanol, homopolymer', '(2-furyl)methanol', '2-hydroxymethylfurane', 'furylcarbinol (van)', '2-furylmethan-1-ol', '25212-86-6', '93793-62-5', 'furanmethanol', 'polyfurfuryl alcohol', 'pffa', 'poly(furfurylalcohol)', 'poly-furfuryl alcohol', '(fur-2-yl)methanol', '.alpha.-furylcarbinol', '2-hydroxymethyl-furan', 'poly(furfuryl alcohol)', '.alpha.-furfuryl alcohol', 'agn-pc-04y237', 'h159', 'omega-hydroxypoly(furan-2,5-diylmethylene)', '(2-furyl)-methanol (furfurylalcohol)', '40795-25-3', '88161-36-8'] 

''' 

_synonyms = pubchem_dict[CASRN]['Names'] 

return _synonyms 

 

 

 

 

 

_MixtureDict = {} 

with open(os.path.join(folder, 'Mixtures Compositions.csv')) as f: 

'''Read in a dict of 90 or so mixutres, their components, and synonyms. 

Small errors in mole fractions not adding to 1 are known. 

Errors in adding mass fraction are less common, present at the 5th decimal. 

TODO: Normalization 

Mass basis is assumed for all mixtures. 

''' 

next(f) 

for line in f: 

values = to_num(line.strip('\n').strip('\t').split('\t')) 

_name, _source, N = values[0:3] 

N = int(N) 

_CASs, _names, _ws, _zs = values[3:3+N], values[3+N:3+2*N], values[3+2*N:3+3*N], values[3+3*N:3+4*N] 

_syns = values[3+4*N:] 

if _syns: 

_syns = [i.lower() for i in _syns] 

_syns.append(_name.lower()) 

_MixtureDict[_name] = {"CASs": _CASs, "N": N, "Source": _source, 

"Names": _names, "ws": _ws, "zs": _zs, 

"Synonyms": _syns} 

 

def mixture_from_any(ID): 

if type(ID) == type([]): 

if len(ID) == 1: 

ID = ID[0] 

else: 

raise Exception('Input cannot be multiple components') 

ID = ID.lower() 

ID = ID.strip() 

ID2 = ID.replace(' ', '') 

ID3 = ID.replace('-', '') 

 

for i in _MixtureDict: 

d = _MixtureDict[i]["Synonyms"] 

if ID in d or ID2 in d or ID3 in d: 

return i 

 

# TODO LIST OF REFRIGERANTS FOR USE IN HEAT TRANSFER CORRELATIONS 

 

cryogenics = {'132259-10-0': 'Air', '7440-37-1': 'Argon', '630-08-0': 

'carbon monoxide', '7782-39-0': 'deuterium', '7782-41-4': 'fluorine', 

'7440-59-7': 'helium', '1333-74-0': 'hydrogen', '7439-90-9': 'krypton', 

'74-82-8': 'methane', '7440-01-9': 'neon', '7727-37-9': 'nitrogen', 

'7782-44-7': 'oxygen', '7440-63-3': 'xenon'} 

 

 

 

### DIPPR Database, chemical list only 

# Obtained via the command: 

# list(pd.read_excel('http://www.aiche.org/sites/default/files/docs/pages/sponsor_compound_list-2014.xlsx')['Unnamed: 2'])[2:] 

dippr_compounds = set() 

with open(os.path.join(folder, 'dippr_2014.csv')) as f: 

dippr_compounds.update(f.read().split('\n')) 

 

 

 

 

 

 

 

 

 

 

 

### Temporary functions which served a purpose, once 

 

 

#def mid(ID): 

# print(CASfromAny(ID) + '\t' + ID + '\n')*20 

# 

# 

##def paste(str): 

## from subprocess import Popen, PIPE 

## p = Popen(['xsel', '-bi'], stdin=PIPE) 

## p.communicate(input=str) 

# 

# 

##def cid(ID): 

## paste((CASfromAny(ID) + '\t' + ID + '\n')*60) 

# 

#def fid(ID): 

# a = CASfromAny(ID) 

# paste((a + '\t' + IUPAC_name(a) + '\n')*60) 

# 

#def mancas(ID): 

# paste((ID + '\n')*60) 

# 

#def cas(ID): 

# a = CASfromAny(ID) 

# paste(a) 

# 

#def autocas(): 

# while True: 

# data = raw_input() 

# cas(data)