Commit e5d40559 authored by mirandaa's avatar mirandaa
Browse files

init: gui

parent 8bfbd92d
import os
from chem_parser import txt2dict
from gooey import Gooey, GooeyParser
@Gooey
def getArgs():
parser = GooeyParser(description='MFQL generator v0.0.1')
parser.add_argument('compound', type=str, help='Compound name')
parser.add_argument('formula_ms1', type=str, help='Specify formula (eg h2o or C[1..20]H[4..80]O[0..1])')
parser.add_argument('adduct_ms1', type=str, help='Specify adduct type (optional for ranges)')
parser.add_argument('ms_mode', type=str, help='MS mode + (is positive?)')
parser.add_argument('mfql_dir', type=str, help='output dir for MFQL files', widget='DirChooser')
args = parser.parse_args()
return args
def addDicts(cc, af, ms):
#adjust for the adduct
# only H, NH4 and HCOO are defined
allelems = dict(cc)
allelems.update(af)
allelems.keys() # just to get all the keys in both dicts
is_H = len(af) == 1 and 'H' in af
if is_H and ms == '+':
res = {}
for key in allelems.keys():
low = cc.get(key, (0,0))[0]
high = cc.get(key, (0,0))[1]
low += af.get(key, (0,0))[0]
high += af.get(key, (0,0))[1]
res[key] = (low,high)
elif is_H and ms == '-':
res = {}
for key in allelems.keys():
low = cc.get(key, (0,0))[0]
high = cc.get(key, (0,0))[1]
low -= af.get(key, (0,0))[0]
high -= af.get(key, (0,0))[1]
res[key] = (low,high)
else : # al other adducts
res = {}
for key in allelems.keys():
low = cc.get(key, (0,0))[0]
high = cc.get(key, (0,0))[1]
low += af.get(key, (0,0))[0]
high += af.get(key, (0,0))[1]
res[key] = (low,high)
return res
def calc_dbe(cc_dict):
val_minus_2 = {
'C' : 2,
'H' : -1,
'N' : 1,
'P' : 1,
'O' : 0,
'S' : 4,
'2H' : -1
}
# from Man-XCALI-97617-Xcalibur-31-Qual-ManXCALI97617-A-EN.pdf page 189: Double Bond/Ring equevelentse
dbe = 1 + sum([cc_dict.get(e, 0)*val_minus_2[e] for e in val_minus_2.keys()])/2
return dbe
def makeSuch(res):
#test for odd/even number of hydrogens
if res['H'][0] % 2 == 0:
such = 'isEven'
else:
such = 'isOdd'
return such
def res2String(res):
ec=''
for k,v in res.items():
if k == '2H':
k = 'D'
if v[0]==v[1]:
ec += f'{k}[{v[0]}] '
else:
ec += f'{k}[{v[0]}..{v[1]}] '
return ec
def res2dbe(res):
low_res = {k:v[0] for k,v in res.items()}
high_res = {k:v[1] for k,v in res.items()}
dbe_low = calc_dbe(low_res)
dbe_high = calc_dbe(high_res)
return (dbe_low, dbe_high)
def main():
args = getArgs()
ms = args.ms_mode
cc = txt2dict(args.formula_ms1)
af = txt2dict(args.adduct_ms1) #get chemical composition of adduct
res = addDicts(cc,af, ms)
such = makeSuch(res)
ec = res2String(res)
dbe_low, dbe_high = res2dbe(res)
line1 = f'#Identify {args.compound}#'
line2 = f'QUERYNAME = {args.compound};'
line3 = f"DEFINE pr = '{ec}' WITH DBR = ({dbe_low},{dbe_high}), CHG = {ms}1;"
line4 = f'IDENTIFY\n\tpr in MS1{ms}'
line5 = f'SUCHTHAT\n\t{such}(pr.chemsc[H]) AND \n\tisOdd(pr.chemsc[db]*2)'
line6 = f'REPORT\n\tPRM = pr.mass;\n\tEC = pr.chemsc;\n\tCLASS = "{args.compound}" % "()";\n\tQSERR = "%2.2f" % "(pr.errppm)";\n\tQS = pr.intensity;;'
filename = os.path.join(args.mfql_dir, args.compound + '.mfql')
with open(filename, 'w') as f:
f.write(line1 + '\n')
f.write(line2 + '\n')
f.write(line3 + '\n')
f.write(line4 + '\n')
f.write(line5 + '\n')
f.write(line6 + '\n')
if __name__ == "__main__":
main()
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