File size: 3,413 Bytes
7f50cd1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import corpora.DataLoader;
import relations.EventExtraction;
import utils.DBUtils;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardCopyOption;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Stream;

/** Input/output driver only. All linguistic processing is in the upstream jar. */
public final class BioSemBatch {
    public static void main(String[] args) throws Exception {
        Path input = Path.of(args[0]);
        Path output = Path.of(args[2]);
        Files.createDirectories(output);
        DBUtils source = new DBUtils();
        source.openDB(args[1]);
        // Invoke the original serializer, including its duplicate handling.
        Method write = EventExtraction.class.getDeclaredMethod("writeResult", String.class, String.class);
        write.setAccessible(true);
        EventExtraction extractor = null;
        List<Path> paths = new ArrayList<>();
        try (Stream<Path> files = Files.list(input)) {
            files.filter(p -> p.toString().endsWith(".txt")).forEach(paths::add);
        }
        // Stable adapter filenames; opaque biomedical record IDs do not affect order.
        paths.sort(Comparator.comparing(p -> p.getFileName().toString().replace(".txt", "")));
        try {
            for (Path path : paths) {
                String id = path.getFileName().toString().replace(".txt", "");
                DBUtils document = null;
                Path result = output.resolve(id + ".a2");
                try {
                    String text = Files.readString(path, StandardCharsets.UTF_8);
                    List<String> proteins = Files.readAllLines(input.resolve(id + ".a1"), StandardCharsets.UTF_8);
                    // Upstream programmatic loader preserves text and offsets, including empty text.
                    document = new DataLoader().Txt2Db("document", text, proteins);
                    if (extractor == null) {
                        extractor = new EventExtraction(source, document);
                        extractor.init();
                    } else {
                        extractor.setDb(document);
                    }
                    extractor.extractEvents("document");
                    write.invoke(extractor, id, output.toString());
                    // Only a closed, complete upstream result is exposed to Python.
                    Files.move(result, output.resolve(id + ".ready"), StandardCopyOption.REPLACE_EXISTING);
                } catch (Exception | StackOverflowError failure) {
                    Throwable cause = failure instanceof InvocationTargetException
                            ? ((InvocationTargetException) failure).getCause() : failure;
                    System.err.println("BioSem record " + id + " failed; no annotations emitted: " + cause);
                    cause.printStackTrace(System.err);
                    Files.deleteIfExists(result);
                    // Never reuse partially mutated extractor state after an exception.
                    extractor = null;
                } finally {
                    if (document != null) document.closeDB();
                }
            }
        } finally {
            source.closeDB();
        }
    }
}