File: fragment_search.py

package info (click to toggle)
bkchem 0.14.0~pre4%2Bgit20211228-5
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 29,340 kB
  • sloc: python: 46,432; xml: 909; javascript: 49; sh: 37; makefile: 16
file content (91 lines) | stat: -rw-r--r-- 2,849 bytes parent folder | download | duplicates (2)
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
from __future__ import division

import os
import time
try:
    import tkinter.filedialog as tkFileDialog
except ImportError:
    import tkFileDialog

import logger
import dialogs

from singleton_store import Store



def process_directory(directory):
    files = 0
    matching = 0

    dialog = dialogs.progress_dialog(App, title=_("Search progress"))

    files_to_go = [f for f in [os.path.join(directory, filename) for filename in os.listdir(directory)] if os.path.isfile(f) and os.path.splitext(f)[1] in (".svg",".cdml")]

    for f in files_to_go:
        #print f
        dialog.update(files/len(files_to_go), top_text=os.path.split(f)[1], bottom_text=_("Found: %d matching") % matching)
        files += 1
        App.in_batch_mode = True
        if App.add_new_paper(name=f):
            if App._load_CDML_file(f, draw=False):
                found = False
                for mol in App.paper.molecules:
                    gen = mol.select_matching_substructures(fragment, implicit_freesites=True)
                    try:
                        gen.next()
                    except StopIteration:
                        pass
                    else:
                        found = True
                        matching += 1
                        mol.clean_after_search(fragment)
                        break
                if not found:
                    App.close_current_paper()
                else:
                    App.in_batch_mode = False
                    [o.draw() for o in App.paper.stack]
                    App.paper.set_bindings()
                    App.paper.add_bindings()
            else:
                App.close_current_paper()

    App.in_batch_mode = False
    dialog.close()
    return files



t = time.time()
selected_mols = [o for o in App.paper.selected_to_unique_top_levels()[0] if o.object_type == 'molecule']
if not selected_mols and len(App.paper.molecules) == 1:
    selected_mols = App.paper.molecules


if len(selected_mols) > 1:
    Store.log(_("Select only one molecule"), message_type="error")

elif len(selected_mols) == 0:
    Store.log(_("Draw a molecule that you want to use as the fragment for search"), message_type="error")

else:
    # we may proceed
    fragment = selected_mols[0]

    directory = tkFileDialog.askdirectory(parent=App,
                                          initialdir=App.save_dir or "./")

    if directory:
        Store.logger.handling = logger.ignorant
        files = process_directory(directory)

        t = time.time() - t
        #print "%d files, %.2fs, %.2fms per file" % (files, t, 1000*(t/files))

        Store.logger.handling = logger.normal
        if files:
            Store.log(_("Searched %d files, %.2fs, %.2fms per file") % (files, t, 1000*(t/files)))
        else:
            Store.log(_("No files to search in were found"))