Mercurial > repos > recetox > biotransformer
annotate wrapper_biotransformer.py @ 4:6cd2a9e6ac49 draft default tip
planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/biotransformer commit 6de696dca6e9b7633469189e3b85194264478277
author | recetox |
---|---|
date | Thu, 15 Aug 2024 14:45:06 +0000 |
parents | 0b86600b715e |
children |
rev | line source |
---|---|
0
0b86600b715e
planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/biotransformer commit 9b8e9941cdf0689518021bc0aa4b7196b28d25d7
recetox
parents:
diff
changeset
|
1 import re |
0b86600b715e
planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/biotransformer commit 9b8e9941cdf0689518021bc0aa4b7196b28d25d7
recetox
parents:
diff
changeset
|
2 import subprocess |
0b86600b715e
planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/biotransformer commit 9b8e9941cdf0689518021bc0aa4b7196b28d25d7
recetox
parents:
diff
changeset
|
3 import sys |
0b86600b715e
planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/biotransformer commit 9b8e9941cdf0689518021bc0aa4b7196b28d25d7
recetox
parents:
diff
changeset
|
4 import tempfile |
0b86600b715e
planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/biotransformer commit 9b8e9941cdf0689518021bc0aa4b7196b28d25d7
recetox
parents:
diff
changeset
|
5 |
0b86600b715e
planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/biotransformer commit 9b8e9941cdf0689518021bc0aa4b7196b28d25d7
recetox
parents:
diff
changeset
|
6 import pandas |
0b86600b715e
planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/biotransformer commit 9b8e9941cdf0689518021bc0aa4b7196b28d25d7
recetox
parents:
diff
changeset
|
7 from openbabel import openbabel, pybel |
0b86600b715e
planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/biotransformer commit 9b8e9941cdf0689518021bc0aa4b7196b28d25d7
recetox
parents:
diff
changeset
|
8 openbabel.obErrorLog.StopLogging() |
0b86600b715e
planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/biotransformer commit 9b8e9941cdf0689518021bc0aa4b7196b28d25d7
recetox
parents:
diff
changeset
|
9 |
0b86600b715e
planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/biotransformer commit 9b8e9941cdf0689518021bc0aa4b7196b28d25d7
recetox
parents:
diff
changeset
|
10 |
0b86600b715e
planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/biotransformer commit 9b8e9941cdf0689518021bc0aa4b7196b28d25d7
recetox
parents:
diff
changeset
|
11 def InchiToSmiles(df): |
0b86600b715e
planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/biotransformer commit 9b8e9941cdf0689518021bc0aa4b7196b28d25d7
recetox
parents:
diff
changeset
|
12 '''Translate inchi to smiles''' |
0b86600b715e
planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/biotransformer commit 9b8e9941cdf0689518021bc0aa4b7196b28d25d7
recetox
parents:
diff
changeset
|
13 sm = [] |
0b86600b715e
planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/biotransformer commit 9b8e9941cdf0689518021bc0aa4b7196b28d25d7
recetox
parents:
diff
changeset
|
14 for item in df['InChI']: |
0b86600b715e
planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/biotransformer commit 9b8e9941cdf0689518021bc0aa4b7196b28d25d7
recetox
parents:
diff
changeset
|
15 tmp = pybel.readstring("inchi", item) |
0b86600b715e
planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/biotransformer commit 9b8e9941cdf0689518021bc0aa4b7196b28d25d7
recetox
parents:
diff
changeset
|
16 sm.append(tmp.write("smi")) |
0b86600b715e
planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/biotransformer commit 9b8e9941cdf0689518021bc0aa4b7196b28d25d7
recetox
parents:
diff
changeset
|
17 return sm |
0b86600b715e
planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/biotransformer commit 9b8e9941cdf0689518021bc0aa4b7196b28d25d7
recetox
parents:
diff
changeset
|
18 |
0b86600b715e
planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/biotransformer commit 9b8e9941cdf0689518021bc0aa4b7196b28d25d7
recetox
parents:
diff
changeset
|
19 |
0b86600b715e
planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/biotransformer commit 9b8e9941cdf0689518021bc0aa4b7196b28d25d7
recetox
parents:
diff
changeset
|
20 executable = ["biotransformer"] |
0b86600b715e
planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/biotransformer commit 9b8e9941cdf0689518021bc0aa4b7196b28d25d7
recetox
parents:
diff
changeset
|
21 |
0b86600b715e
planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/biotransformer commit 9b8e9941cdf0689518021bc0aa4b7196b28d25d7
recetox
parents:
diff
changeset
|
22 argv = sys.argv[1:] |
0b86600b715e
planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/biotransformer commit 9b8e9941cdf0689518021bc0aa4b7196b28d25d7
recetox
parents:
diff
changeset
|
23 icsv = argv.pop(argv.index("-icsv") + 1) |
0b86600b715e
planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/biotransformer commit 9b8e9941cdf0689518021bc0aa4b7196b28d25d7
recetox
parents:
diff
changeset
|
24 argv.remove("-icsv") |
0b86600b715e
planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/biotransformer commit 9b8e9941cdf0689518021bc0aa4b7196b28d25d7
recetox
parents:
diff
changeset
|
25 ocsv = argv.pop(argv.index("-ocsv") + 1) |
0b86600b715e
planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/biotransformer commit 9b8e9941cdf0689518021bc0aa4b7196b28d25d7
recetox
parents:
diff
changeset
|
26 argv.remove("-ocsv") |
0b86600b715e
planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/biotransformer commit 9b8e9941cdf0689518021bc0aa4b7196b28d25d7
recetox
parents:
diff
changeset
|
27 ocsv_dup = argv.pop(argv.index("-ocsvDup") + 1) |
0b86600b715e
planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/biotransformer commit 9b8e9941cdf0689518021bc0aa4b7196b28d25d7
recetox
parents:
diff
changeset
|
28 argv.remove("-ocsvDup") |
0b86600b715e
planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/biotransformer commit 9b8e9941cdf0689518021bc0aa4b7196b28d25d7
recetox
parents:
diff
changeset
|
29 ocsv_dup2 = argv.pop(argv.index("-ocsvDup2") + 1) |
0b86600b715e
planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/biotransformer commit 9b8e9941cdf0689518021bc0aa4b7196b28d25d7
recetox
parents:
diff
changeset
|
30 argv.remove("-ocsvDup2") |
0b86600b715e
planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/biotransformer commit 9b8e9941cdf0689518021bc0aa4b7196b28d25d7
recetox
parents:
diff
changeset
|
31 |
0b86600b715e
planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/biotransformer commit 9b8e9941cdf0689518021bc0aa4b7196b28d25d7
recetox
parents:
diff
changeset
|
32 in_df = pandas.read_csv(icsv, header=None) |
0b86600b715e
planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/biotransformer commit 9b8e9941cdf0689518021bc0aa4b7196b28d25d7
recetox
parents:
diff
changeset
|
33 out_df1 = pandas.DataFrame() # all results |
0b86600b715e
planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/biotransformer commit 9b8e9941cdf0689518021bc0aa4b7196b28d25d7
recetox
parents:
diff
changeset
|
34 out_df2 = pandas.DataFrame() # filtered results based on 6 columns |
0b86600b715e
planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/biotransformer commit 9b8e9941cdf0689518021bc0aa4b7196b28d25d7
recetox
parents:
diff
changeset
|
35 out_df3 = pandas.DataFrame() # filtered results based on 3 columns |
0b86600b715e
planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/biotransformer commit 9b8e9941cdf0689518021bc0aa4b7196b28d25d7
recetox
parents:
diff
changeset
|
36 |
0b86600b715e
planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/biotransformer commit 9b8e9941cdf0689518021bc0aa4b7196b28d25d7
recetox
parents:
diff
changeset
|
37 smList1 = [] # list with smiles string |
0b86600b715e
planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/biotransformer commit 9b8e9941cdf0689518021bc0aa4b7196b28d25d7
recetox
parents:
diff
changeset
|
38 smList2 = [] |
0b86600b715e
planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/biotransformer commit 9b8e9941cdf0689518021bc0aa4b7196b28d25d7
recetox
parents:
diff
changeset
|
39 smList3 = [] |
0b86600b715e
planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/biotransformer commit 9b8e9941cdf0689518021bc0aa4b7196b28d25d7
recetox
parents:
diff
changeset
|
40 for _, (smiles,) in in_df.iterrows(): |
0b86600b715e
planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/biotransformer commit 9b8e9941cdf0689518021bc0aa4b7196b28d25d7
recetox
parents:
diff
changeset
|
41 with tempfile.NamedTemporaryFile() as out: |
0b86600b715e
planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/biotransformer commit 9b8e9941cdf0689518021bc0aa4b7196b28d25d7
recetox
parents:
diff
changeset
|
42 print("Working on compound: " + smiles) |
0b86600b715e
planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/biotransformer commit 9b8e9941cdf0689518021bc0aa4b7196b28d25d7
recetox
parents:
diff
changeset
|
43 if not re.search(r'\.', smiles): |
0b86600b715e
planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/biotransformer commit 9b8e9941cdf0689518021bc0aa4b7196b28d25d7
recetox
parents:
diff
changeset
|
44 subprocess.run(executable + argv + ["-ismi", smiles] + ["-ocsv", out.name]) |
0b86600b715e
planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/biotransformer commit 9b8e9941cdf0689518021bc0aa4b7196b28d25d7
recetox
parents:
diff
changeset
|
45 try: |
0b86600b715e
planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/biotransformer commit 9b8e9941cdf0689518021bc0aa4b7196b28d25d7
recetox
parents:
diff
changeset
|
46 bio_out = pandas.read_csv(out.name) |
0b86600b715e
planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/biotransformer commit 9b8e9941cdf0689518021bc0aa4b7196b28d25d7
recetox
parents:
diff
changeset
|
47 tmp2 = bio_out.drop_duplicates(subset=["InChI", "InChIKey", "Synonyms", "Molecular formula", "Major Isotope Mass", "ALogP"]) |
0b86600b715e
planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/biotransformer commit 9b8e9941cdf0689518021bc0aa4b7196b28d25d7
recetox
parents:
diff
changeset
|
48 tmp3 = bio_out.drop_duplicates(subset=["Molecular formula", "Major Isotope Mass", "ALogP"]) |
0b86600b715e
planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/biotransformer commit 9b8e9941cdf0689518021bc0aa4b7196b28d25d7
recetox
parents:
diff
changeset
|
49 |
0b86600b715e
planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/biotransformer commit 9b8e9941cdf0689518021bc0aa4b7196b28d25d7
recetox
parents:
diff
changeset
|
50 smList1.append([smiles] * bio_out.shape[0]) |
0b86600b715e
planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/biotransformer commit 9b8e9941cdf0689518021bc0aa4b7196b28d25d7
recetox
parents:
diff
changeset
|
51 smList2.append([smiles] * tmp2.shape[0]) |
0b86600b715e
planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/biotransformer commit 9b8e9941cdf0689518021bc0aa4b7196b28d25d7
recetox
parents:
diff
changeset
|
52 smList3.append([smiles] * tmp3.shape[0]) |
0b86600b715e
planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/biotransformer commit 9b8e9941cdf0689518021bc0aa4b7196b28d25d7
recetox
parents:
diff
changeset
|
53 |
0b86600b715e
planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/biotransformer commit 9b8e9941cdf0689518021bc0aa4b7196b28d25d7
recetox
parents:
diff
changeset
|
54 out_df1 = pandas.concat([out_df1, bio_out]) |
0b86600b715e
planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/biotransformer commit 9b8e9941cdf0689518021bc0aa4b7196b28d25d7
recetox
parents:
diff
changeset
|
55 out_df2 = pandas.concat([out_df2, tmp2]) |
0b86600b715e
planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/biotransformer commit 9b8e9941cdf0689518021bc0aa4b7196b28d25d7
recetox
parents:
diff
changeset
|
56 out_df3 = pandas.concat([out_df3, tmp3]) |
0b86600b715e
planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/biotransformer commit 9b8e9941cdf0689518021bc0aa4b7196b28d25d7
recetox
parents:
diff
changeset
|
57 except pandas.errors.EmptyDataError: |
0b86600b715e
planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/biotransformer commit 9b8e9941cdf0689518021bc0aa4b7196b28d25d7
recetox
parents:
diff
changeset
|
58 continue |
0b86600b715e
planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/biotransformer commit 9b8e9941cdf0689518021bc0aa4b7196b28d25d7
recetox
parents:
diff
changeset
|
59 else: |
0b86600b715e
planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/biotransformer commit 9b8e9941cdf0689518021bc0aa4b7196b28d25d7
recetox
parents:
diff
changeset
|
60 print("ERROR: Input compound cannot be a mixture.") |
0b86600b715e
planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/biotransformer commit 9b8e9941cdf0689518021bc0aa4b7196b28d25d7
recetox
parents:
diff
changeset
|
61 smList1 = sum(smList1, []) # merge sublists into one list |
0b86600b715e
planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/biotransformer commit 9b8e9941cdf0689518021bc0aa4b7196b28d25d7
recetox
parents:
diff
changeset
|
62 smList2 = sum(smList2, []) |
0b86600b715e
planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/biotransformer commit 9b8e9941cdf0689518021bc0aa4b7196b28d25d7
recetox
parents:
diff
changeset
|
63 smList3 = sum(smList3, []) |
0b86600b715e
planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/biotransformer commit 9b8e9941cdf0689518021bc0aa4b7196b28d25d7
recetox
parents:
diff
changeset
|
64 |
0b86600b715e
planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/biotransformer commit 9b8e9941cdf0689518021bc0aa4b7196b28d25d7
recetox
parents:
diff
changeset
|
65 out_df1.insert(0, "SMILES query", smList1) |
0b86600b715e
planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/biotransformer commit 9b8e9941cdf0689518021bc0aa4b7196b28d25d7
recetox
parents:
diff
changeset
|
66 out_df1.insert(1, "SMILES target", InchiToSmiles(out_df1)) |
0b86600b715e
planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/biotransformer commit 9b8e9941cdf0689518021bc0aa4b7196b28d25d7
recetox
parents:
diff
changeset
|
67 out_df1.to_csv(ocsv, sep='\t') |
0b86600b715e
planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/biotransformer commit 9b8e9941cdf0689518021bc0aa4b7196b28d25d7
recetox
parents:
diff
changeset
|
68 |
0b86600b715e
planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/biotransformer commit 9b8e9941cdf0689518021bc0aa4b7196b28d25d7
recetox
parents:
diff
changeset
|
69 out_df2.insert(0, "SMILES query", smList2) |
0b86600b715e
planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/biotransformer commit 9b8e9941cdf0689518021bc0aa4b7196b28d25d7
recetox
parents:
diff
changeset
|
70 out_df2.insert(1, "SMILES target", InchiToSmiles(out_df2)) |
0b86600b715e
planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/biotransformer commit 9b8e9941cdf0689518021bc0aa4b7196b28d25d7
recetox
parents:
diff
changeset
|
71 out_df2.to_csv(ocsv_dup, sep='\t') |
0b86600b715e
planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/biotransformer commit 9b8e9941cdf0689518021bc0aa4b7196b28d25d7
recetox
parents:
diff
changeset
|
72 |
0b86600b715e
planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/biotransformer commit 9b8e9941cdf0689518021bc0aa4b7196b28d25d7
recetox
parents:
diff
changeset
|
73 out_df3.insert(0, "SMILES query", smList3) |
0b86600b715e
planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/biotransformer commit 9b8e9941cdf0689518021bc0aa4b7196b28d25d7
recetox
parents:
diff
changeset
|
74 out_df3.insert(1, "SMILES target", InchiToSmiles(out_df3)) |
0b86600b715e
planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/biotransformer commit 9b8e9941cdf0689518021bc0aa4b7196b28d25d7
recetox
parents:
diff
changeset
|
75 out_df3.to_csv(ocsv_dup2, sep='\t') |