Commit d4672b1d authored by mirandaa's avatar mirandaa
Browse files

Notebook not used anymore

parent 813e64b3
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Automatically create mfql files"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from chem_parser import txt2dict\n",
"import warnings"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Generate mfql"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"#specify some parameters, for now only works for chemical compositions with CHDNOP\n",
"#probably it's better to use dictionaries!\n",
"\n",
"#how to deal with other chemical compositions!\n",
"#make it more flexible\n",
"cn = input('Define compound name:')\n",
"qn = input('Define query name (no spaces/underscores):')#Ultimate Species!\n",
"\n",
"#define formula and use molmass to generate the tuple\n",
"f = input ('Specify formula (eg h2o or C[1..20]H[4..80]O[0..1]):')\n",
"f = f.upper()\n",
"warnings.warn('for H is in a range, the first number will indicate if it should be odd or even')\n",
"\n",
"#define adduct and use molmass to generate the tuple\n",
"adduct = input('Specify adduct type (optinal for ranges):')\n",
"adduct = adduct.upper()\n",
"\n",
"#define MS mode\n",
"ms = input('Define MS mode (either + or -):')\n",
"\n",
"#define filename\n",
"filename = input('Define file name:')\n",
"mfql = '.mfql'\n",
"filename = \"\".join((filename, mfql))"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"scrolled": true
},
"outputs": [],
"source": [
"cc = txt2dict(f)\n",
"af = txt2dict(adduct) #get chemical composition of adduct"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Based on the adduct it will be added or substracted"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"#adjust for the adduct\n",
"# only H, NH4 and HCOO are defined\n",
"allelems = dict(cc)\n",
"allelems.update(af)\n",
"allelems.keys() # just to get all the keys in both dicts\n",
"if adduct == 'H' and ms == '+':\n",
" res = {}\n",
" for key in allelems.keys():\n",
" low = cc.get(key, (0,0))[0]\n",
" high = cc.get(key, (0,0))[1]\n",
" low += af.get(key, (0,0))[0]\n",
" high += af.get(key, (0,0))[1]\n",
" res[key] = (low,high)\n",
" \n",
"elif adduct == 'H' and ms == '-':\n",
" res = {}\n",
" for key in allelems.keys():\n",
" low = cc.get(key, (0,0))[0]\n",
" high = cc.get(key, (0,0))[1]\n",
" low -= af.get(key, (0,0))[0]\n",
" high -= af.get(key, (0,0))[1]\n",
" res[key] = (low,high)\n",
" \n",
"else : # al other adducts\n",
" res = {}\n",
" for key in allelems.keys():\n",
" low = cc.get(key, (0,0))[0]\n",
" high = cc.get(key, (0,0))[1]\n",
" low += af.get(key, (0,0))[0]\n",
" high += af.get(key, (0,0))[1]\n",
" res[key] = (low,high)\n",
" \n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"def calc_dbe(cc_dict):\n",
" val_minus_2 = {\n",
" 'C' : 2,\n",
" 'H' : -1,\n",
" 'N' : 1,\n",
" 'P' : 1,\n",
" 'O' : 0,\n",
" 'S' : 4,\n",
" '2H' : -1\n",
" }\n",
" # from Man-XCALI-97617-Xcalibur-31-Qual-ManXCALI97617-A-EN.pdf page 189: Double Bond/Ring equevelentse\n",
" dbe = 1 + sum([cc_dict.get(e, 0)*val_minus_2[e] for e in val_minus_2.keys()])/2\n",
" return dbe"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"#test for odd/even number of hydrogens\n",
"if res['H'][0] % 2 == 0:\n",
" such = 'isEven'\n",
"else:\n",
" such = 'isOdd'\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"ec=''\n",
"for k,v in res.items():\n",
" if k == '2H':\n",
" k = 'D' \n",
" if v[0]==v[1]:\n",
" ec += f'{k}[{v[0]}] '\n",
" else:\n",
" ec += f'{k}[{v[0]}..{v[1]}] '\n",
"ec"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"low_res = {k:v[0] for k,v in res.items()}\n",
"high_res = {k:v[1] for k,v in res.items()}"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"dbe_low = calc_dbe(low_res)\n",
"dbe_high = calc_dbe(high_res)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"line1 = f'#Identify {cn}#'\n",
"line2 = f'QUERYNAME = {qn};'\n",
"line3 = f\"DEFINE pr = '{ec}' WITH DBR = ({dbe_low},{dbe_high}), CHG = {ms}1;\"\n",
"line4 = f'IDENTIFY\\n\\tpr in MS1{ms}'\n",
"line5 = f'SUCHTHAT\\n\\t{such}(pr.chemsc[H]) AND \\n\\tisOdd(pr.chemsc[db]*2)'\n",
"line6 = f'REPORT\\n\\tPRM = pr.mass;\\n\\tEC = pr.chemsc;\\n\\tCLASS = \"{qn}\" % \"()\";\\n\\tQSERR = \"%2.2f\" % \"(pr.errppm)\";\\n\\tQS = pr.intensity;;'\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"with open(filename, 'w') as f:\n",
" f.write(line1 + '\\n')\n",
" f.write(line2 + '\\n')\n",
" f.write(line3 + '\\n')\n",
" f.write(line4 + '\\n')\n",
" f.write(line5 + '\\n')\n",
" f.write(line6 + '\\n')\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3.7.5 64-bit ('mfql_des': conda)",
"language": "python",
"name": "python37564bitmfqldesconda008f121642b041f890de0d051e241abf"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.7.5-final"
}
},
"nbformat": 4,
"nbformat_minor": 4
}
\ No newline at end of file
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment