def import_redundant_nucleotides(self, pdbs, recalculate=False):
        """
        """
        try:
            logging.info("Importing redundant nucleotides")
            if not recalculate:
                recalculate = self.config["recalculate"]["redundant_nts"]
            if recalculate:
                pdb_list = pdbs
                self.__delete_old_data(pdbs)
            else:
                pdb_list = self.filter_out_analyzed_pdbs(pdbs, "redundant_nts")

            if pdb_list:
                MotifAtlasBaseClass._setup_matlab(self)

            for pdb_file in pdb_list:
                logging.info("Running matlab on %s", pdb_file)
                ifn, err_msg = self.mlab.loadRedundantNucleotides(pdb_file, nout=2)
                if err_msg == "":
                    self.__import_temporary_file(ifn, pdb_file)
                else:
                    MotifAtlasBaseClass._crash(self, err_msg)

                self.mark_pdb_as_analyzed(pdb_file, "redundant_nts")

            logging.info("%s", "=" * 40)
        except:
            e = sys.exc_info()[1]
            MotifAtlasBaseClass._crash(self, e)
예제 #2
0
 def import_loops(self, Loops, l, pdb_id, loop_type):
     """
     """
     try:
         if Loops == 0:
             self.mark_pdb_as_analyzed(pdb_id, loop_type)
             return
         for i in xrange(l):
             loop_id = self._get_loop_id(Loops[i].AllLoops_table.full_id,
                                         pdb_id, loop_type)
             Loops[i].Filename = loop_id
             session.merge(
                 AllLoops(id            = loop_id,
                          type          = loop_type,
                          pdb           = pdb_id,
                          sequential_id = loop_id[-3:],
                          length        = int(Loops[i].NumNT[0][0]),
                          seq           = Loops[i].AllLoops_table.seq,
                          r_seq         = Loops[i].AllLoops_table.r_seq,
                          nwc_seq       = Loops[i].AllLoops_table.nwc,
                          r_nwc_seq     = Loops[i].AllLoops_table.r_nwc,
                          pdb_file      = Loops[i].PDBFilename,
                          nt_ids        = Loops[i].AllLoops_table.full_id,
                          loop_name     = Loops[i].AllLoops_table.loop_name))
         self.save_mat_files(Loops)
         self.mark_pdb_as_analyzed(pdb_id, loop_type)
         logging.info('%s from %s successfully imported', loop_type, pdb_id)
     except:
         e = sys.exc_info()[1]
         MotifAtlasBaseClass._crash(self,e)
    def import_best_chains_and_models(self, pdbs, recalculate=False):
        """
        """
        try:
            logging.info("Importing best chains and models")
            if not recalculate:
                recalculate = self.config["recalculate"]["best_chains_and_models"]
            if recalculate:
                pdb_list = pdbs
                self.__delete_old_data(pdbs)
            else:
                pdb_list = self.filter_out_analyzed_pdbs(pdbs, "best_chains_and_models")

            if pdb_list:
                MotifAtlasBaseClass._setup_matlab(self)

            for pdb_file in pdb_list:
                logging.info("Running matlab on %s", pdb_file)
                # 'ABC', '1,2', ''
                best_chains, best_models, err_msg = self.mlab.loadBestChainsAndModels(pdb_file, nout=3)
                best_chains = ",".join(list(best_chains))

                if err_msg == "":
                    self.__import_into_db(pdb_file, best_chains, best_models)
                else:
                    MotifAtlasBaseClass._crash(self, err_msg)

                self.mark_pdb_as_analyzed(pdb_file, "best_chains_and_models")

            logging.info("%s", "=" * 40)
        except:
            e = sys.exc_info()[1]
            MotifAtlasBaseClass._crash(self, e)
예제 #4
0
 def save_mat_files(self,Loops):
     """Pass the Loops structure array back to matlab so that it can
     save the .mat files in the specified location."""
     MotifAtlasBaseClass._setup_matlab(self)
     [status, err_msg] = self.mlab.aSaveLoops(Loops,
                                              self.config['locations']['loops_mat_files'],
                                              nout=2)
     if status == 0:
         logging.info('mat files saved')
     else:
         MotifAtlasBaseClass._crash(self,err_msg)
예제 #5
0
 def check_loop_quality(self, pdbs):
     """
     """
     try:
         logging.info('Loop Quality Assurance')
         release = LoopRelease(mode=self.config['release_mode']['loops'])
         for pdb_id in pdbs:
             self.loop_qa(pdb_id, release.id)
         if pdbs:
             session.add(release)
             session.commit()
         logging.info('Loop QA complete')
         logging.info('%s', '='*40)
     except:
         e = sys.exc_info()[1]
         MotifAtlasBaseClass._crash(self,e)
예제 #6
0
 def extract_and_import_loops(self, pdbs, recalculate=None):
     """Loops over `pdbs`, extracts and imports all loops"""
     try:
         for loop_type in self.loop_types:
             logging.info('Extracting %s' % loop_type)
             if recalculate is None:
                 recalculate = self.config['recalculate'][loop_type]
             if recalculate:
                 pdb_list = pdbs[:]
             else:
                 pdb_list = self.filter_out_analyzed_pdbs(pdbs, loop_type)
             for pdb_id in pdb_list:
                 logging.info('Extracting %s from %s', loop_type, pdb_id)
                 (Loops,l) = self.extract_loops(pdb_id, loop_type)
                 self.import_loops(Loops, l, pdb_id, loop_type)
                 logging.info('%s', '='*40)
     except:
         e = sys.exc_info()[1]
         MotifAtlasBaseClass._crash(self,e)
예제 #7
0
    def load_loop_positions(self):
        """update loop_positions table by loading data from the mat files
        stored in the PrecomputedData folder"""
        if not self.mlab:
            self._setup_matlab()

        # loop over directories
        for folder in os.listdir(self.precomputedData):
            if re.search(self.pdb_regex, folder):
                logging.info('Importing loop annotations from %s', folder)
            else:
                continue
            [outputFile, err_msg] = self.mlab.loadLoopPositions(os.path.join(self.precomputedData, folder), nout=2)
            if err_msg != '':
                MotifAtlasBaseClass._crash(self, err_msg)
            else:
                reader = csv.reader(open(outputFile), delimiter=',', quotechar='"')
                for row in reader:
                    (loop_id, position, nt_id, bulge, flanking, border) = row
                    existing = session.query(LoopPositions). \
                                       filter(LoopPositions.loop_id==loop_id). \
                                       filter(LoopPositions.position==position). \
                                       filter(LoopPositions.border==border). \
                                       first()
                    if existing:
                        if self.update:
                            existing.flanking = int(flanking)
                            existing.bulge = int(bulge)
                            existing.nt_id = nt_id
                            existing.border = int(border)
                            session.merge(existing)
                        else:
                            logging.info('Keeping existing annotations')
                    else:
                        session.add(LoopPositions(loop_id=loop_id,
                                                  position=position,
                                                  nt_id=nt_id,
                                                  flanking=int(flanking),
                                                  bulge=int(bulge),
                                                  border=int(border)))
                session.commit()
                os.remove(outputFile) # delete temporary csv file
예제 #8
0
    def extract_loops(self, pdb_id, loop_type):
        """
        """
        try:
            MotifAtlasBaseClass._setup_matlab(self)
            """Loops - array of FR3D File structures. l - its length"""
            [Loops, l, err_msg] = self.mlab.extractLoops(pdb_id, loop_type, nout=3)

            if err_msg != '':
                MotifAtlasBaseClass._crash(self,err_msg)

            if Loops == 0:
                logging.info('No %s in %s', loop_type, pdb_id)
                return (0, 0)
            else:
                logging.info('Found %i loops', l)
                return (Loops, l)
        except:
            e = sys.exc_info()[1]
            MotifAtlasBaseClass._crash(self,e)
예제 #9
0
    def load_loop_searches(self):
        """
            directory structure: loopSearchDir filesep IL_1S72_001 filesep IL_1S72_001_IL_1J5E_001.mat
        """
        # loop over directories
        for loop_id in os.listdir(self.loopSearchDir):
            if re.search(self.loop_regex, loop_id):
                logging.info('Importing %s searches', loop_id)
            else:
                continue

            # read in No_candidates.txt if it exists
            self._read_no_candidates_file(loop_id)

            # get stored loop searches and list all matfiles
            imported = self._get_imported_loop_searches(loop_id)
            matfiles = self._get_saved_mat_files(os.path.join(self.loopSearchDir, loop_id, '*.mat'))
            toImport = matfiles - imported;

            if len(toImport) == 0:
                continue

            toImport = [os.path.join(self.loopSearchDir, loop_id, x + '.mat') for x in toImport]

            if not self.mlab:
                self._setup_matlab()

            # run matlab to create a temporary csv file with results
            [outputFile, err_msg] = self.mlab.loadLoopSearchFile(','.join(toImport), os.path.join(self.loopSearchDir, loop_id), nout=2)

            if err_msg != '':
                MotifAtlasBaseClass._crash(self, err_msg)
            else:
                reader = csv.reader(open(outputFile), delimiter=',', quotechar='"')
                for row in reader:
                    (loop_id1, loop_id2, disc, nt_list1, nt_list2) = row
                    self._store_in_database(loop_id1, loop_id2, disc, nt_list1, nt_list2)
                os.remove(outputFile) # delete temporary csv file
예제 #10
0
    def import_interactions(self, pdbs, recalculate=False):
        """Determines what files need to be analyzed, deletes stored data if
           necessary, loops over the pdbs, runs matlab on each of them
           independently, matlab generates a temporary csv file, it's imported
           and immediately deleted."""
        try:
            logging.info('Inside import_interactions')
            if not recalculate:
                recalculate = self.config['recalculate']['interactions']
            if recalculate:
                pdb_list = pdbs
                self.__delete_interactions(pdbs)
            else:
                pdb_list = self.filter_out_analyzed_pdbs(pdbs,'interactions')

            if pdb_list:
                MotifAtlasBaseClass._setup_matlab(self)

            for pdb_file in pdb_list:
                logging.info('Running matlab on %s', pdb_file)
                ifn, status, err_msg = self.mlab.loadInteractions(pdb_file,nout=3)
                status = status[0][0]
                if status == 0:
                    self.__import_interactions_from_csv(ifn, pdb_file)
                elif status == 2: # no nucleotides in the pdb file
                    logging.info('Pdb file %s has no nucleotides', pdb_file)
                else:
                    logging.warning('Matlab error code %i when analyzing %s',
                                     status, pdb_file)
                    MotifAtlasBaseClass._crash(self,err_msg)

                self.mark_pdb_as_analyzed(pdb_file,'interactions')
            self.success = True
            logging.info('%s', '='*40)
        except:
            e = sys.exc_info()[1]
            MotifAtlasBaseClass._crash(self,e)