from app.drivers.base import BaseDriver
from app.writers import prottable as writers
from app.readers import tsv as reader
from app.preparation import prottable as preparation
from app.lookups.sqlite.proteingroups import ProteinGroupProteinTableDB


class AddProteinInfoDriver(BaseDriver):
    outsuffix = '_proteindata.txt'

    def __init__(self, **kwargs):
        super().__init__(**kwargs)
        self.fasta = kwargs.get('fasta')
        self.pgdb = kwargs.get('protgroupdb')

    def run(self):
        self.oldheader = reader.get_tsv_header(self.fn)
        self.set_protein_generator()
        self.write()
        self.finish()

    def write(self):
        outfn = self.create_outfilepath(self.fn, self.outsuffix)
        writers.write_prottable(self.header, self.proteins, outfn)

    def set_protein_generator(self):
        protgroupdb = ProteinGroupProteinTableDB(self.pgdb)
        # FIXME copy db file to workdir first
        preparation.collect_descriptions(protgroupdb, self.fasta)
        self.header = preparation.get_header_with_proteindata(self.oldheader)
        proteins = reader.generate_tsv_proteins(self.fn, self.oldheader)
        self.proteins = preparation.add_protein_data(proteins,
                                                     self.oldheader,
                                                     protgroupdb)

@@ -1,16 +1,40 @@
+import os%0Aimport shutil%0A
 from app.drivers
@@ -575,16 +575,50 @@
 elf.fn)%0A
+        self.copy_db_to_workdir()%0A
         
@@ -690,16 +690,235 @@
 nish()%0A%0A
+    def copy_db_to_workdir(self):%0A        pgdbfn = os.path.basename(self.pgdb)%0A        copied_lookup = os.path.join(self.workdir, pgdbfn)%0A        shutil.copy(self.pgdb, copied_lookup)%0A        self.pgdb = copied_lookup%0A%0A
     def 

Copy sql lookup to workdir before running. This slows things down due to the copy, but there is a major win on nfs-mounted sqlite lookups