예제 #1
0
    def __init__(self, database, overwrite=None, clean=True):
        '''
        @param:    database
        @pdef:     database to blast upon.
        @ptype:    {String}

        @param:    overwrite
        @pdef:     For writing actions. Decides whether it can overwrite an
                   existing file.
        @pdefault: _SBIglobals.overwrite_
        @ptype:    {Boolean}

        @param:    clean
        @pdef:     remove the temporary files after the data is read.
        @pdefault: _True_
        @pclash:   if _SBIglobals.debug_ is _True_, clean is _False_
        @ptype:    {Boolean}

        @raises: {HmmError}
        '''
        self._error = HmmError()

        #hmmer executable configuration
        if HmmExe._EXE is None:
            self._set_default_executable('hmmer')
            HmmExe._DBFORMATER = HmmExe._CONFIG.get('hmmer', 'dbformatexe')

        # Local overwrite takes precedence over Global overwrite
        self._overwrite = SBIg.decide_overwrite(overwrite)

        self._database = self._check_database(os.path.abspath(database))
        self._clean_files = clean

        #Optional execution parameters
        self._parameters = {'attr': [], 'flag': []}
예제 #2
0
    def write(self, output_file=None, format='PDB', force=None, clean=False):
        """
        Writes the object in a specific format

        @type  output_file: String
        @param output_file: File to write

        @type  format: String
        @param format: Format of the file to print
        """
        outfile = File(
            file_name=output_file, action='w', overwrite=SBIg.decide_overwrite(force))
        if format == 'PDB':
            self._write_PDB_file(pdb_file=outfile, clean=clean)
예제 #3
0
    def __init__(self, file_name=None, action='r', overwrite=None):
        if file_name is None:
            raise FileError(0)  # Raise 'No file specified' Error
        self._file = file_name

        SBIglobals.alert('debug', self,
                         'Preparing File: {0}'.format(self.full))

        self._action = None
        self._check_action(action.lower())  #action must be valid

        self._fd = None
        # Local overwrite takes precedence over Global overwrite
        self._overwrite = SBIglobals.decide_overwrite(overwrite)

        self._check_file()
예제 #4
0
    def copy_file(source, destination, overwrite=False):
        """
            - copy_file()       : Copy a file from one place to another.
                                    - source      (string): Name of the original file
                                                            @Mandatory
                                    - destination (string): Name of the new copied file
                                                            @Mandatory
                                    -overwrite    (bool)  : Ignores if a file with the same name existed before
                                                            @Default: False
                                  @Raises FileError
        """
        # Local overwrite takes precedence over Global overwrite
        overwrite = SBIglobals.decide_overwrite(overwrite)

        if not os.path.exists(source):
            raise FileError(3, source, 'noexists')
        if os.path.exists(destination) and not overwrite:
            raise FileError(3, destination, 'exists')

        shutil.copyfile(src=source, dst=destination)
예제 #5
0
    def __init__(self, database, search_type = 'protein', self_hit = False,
                 hitid_format = 'single', overwrite = None, clean = True):
        '''
        @param:    database
        @pdef:     database to blast upon.
        @ptype:    {String}

        @param:    search_type
        @pdef:     type of database search.
        @pdefault: 'protein'
        @ptype:    {String}

        @@param:   self_hit
        @pdef:     when _True_ if the query is found in the database, it is
                   retrieved.
        @pdefault: _False_
        @ptype:    {Boolean}

        @param:    hitid_format
        @pdef:     format of the name of the hit. If given a wrong option,
                   it defaults to 'single'
        @pdefault: 'single'
        @poptions: 'single' -> first word of the name,
                   'double' -> first two words of the hit name,
                   'all'    -> all the text in the hit name
        @ptype:    {String}

        @param:    overwrite
        @pdef:     For writing actions. Decides whether it can overwrite an
                   existing file.
        @pdefault: _SBIglobals.overwrite_
        @ptype:    {Boolean}

        @param:    clean
        @pdef:     remove the temporary files after the data is read.
        @pdefault: _True_
        @pclash:   if _SBIglobals.debug_ is _True_, clean is _False_
        @ptype:    {Boolean}

        @raises: {BlastError}
        '''
        self._error = BlastError()

        self._search_type = self._check_execution_type(search_type)

        # Blast executable configuration
        if self._EXE is None:
            self._set_default_executable('blast')
            BlastExe._DBFORMATER = BlastExe._CONFIG.get('blast', 'dbformatexe')

        # Database Configuration
        self._idx      = None
        self._database = self._check_database(os.path.abspath(database))

        # Optional execution parameters
        self._parameters = []

        msg  = ['New Blast Executable created.', ]
        msg.append('Blast executable at {0}'.format(self._EXE.full_executable))
        SBIg.alert('debug', self, msg)

        self._self_hit     = self_hit
        self._hitid_format = self._check_hitid_format(hitid_format)
        self._clean_files  = clean

        # Local overwrite takes precedence over Global overwrite
        self._overwrite = SBIg.decide_overwrite(overwrite)
예제 #6
0
    def split(self,
              start_separator=None,
              end_separator=None,
              size=1073741824,
              sections=None,
              overwrite=None):
        """
            > split()   : Splits a given file in multiple files
                            - start_separator (string):  Line startswith. Is a line to mark the line that starts the new file
                                              @Default:  None, only other parameters are considered
                            - end_separator   (string):  Line startswith. Is a line to mark the line that ends the new file
                                              @Default:  None, only other parameters are considered
                            - size            (int):     Max allowed file size. IN BYTES
                                              @Default:  1073741824 (1GB)
                            - sections        (int):     Number of sections to split file into. Overwrites 'size'
                                              @Default:  None
                            - overwrite       (bool):    Overwrite previous files of the same name
                          @Returns an array with the File object of each created file.
                          @Raises FileError
                          @Raises AttributeError if both start_separator and end_separator are defined
        """
        # A defined separator will avoid splitting data
        # For example, uniprot files separate between proteins with '//', then by declaring the separator
        # we can avoid a protein from being split between two files.

        counter = 1
        newfiles = []

        if (size is None and sections is None) or not isinstance(size, int):
            raise FileError(5, self.__name__, 'split')
        if (start_separator is not None and end_separator is not None):
            raise AttributeError(
                'Both start and end separators can not be defined simultaniously\n'
            )

        # section TAKES PRECEDENCE over size...
        if sections is not None:
            size = math.ceil(os.path.getsize(self.full) / float(sections))
        else:
            sections = math.ceil(os.path.getsize(self.full) / size)

        # preparing partition file names
        outputfile = os.path.join(self.dir,
                                  self.prefix + '.{0:003d}' + self.extension)
        if self.is_gziped: outputfile += '.gz'

        # Local overwrite takes precedence over Global overwrite
        overwrite = SBIglobals.decide_overwrite(overwrite)

        SBIglobals.alert(
            'verbose', self,
            'Dividing {0.full} into {1} files (aprox.)'.format(self, sections))

        newfiles.append(
            File(file_name=outputfile.format(counter),
                 action='w',
                 overwrite=overwrite))
        for line in self.descriptor:
            if (start_separator is None or line.startswith(start_separator)
                ) and os.path.getsize(newfiles[-1].full) >= size:
                newfiles[-1].close()
                counter += 1
                newfiles.append(
                    File(file_name=outputfile.format(counter),
                         action='w',
                         overwrite=overwrite))

            newfiles[-1].write(line)

            if (end_separator is None or line.startswith(end_separator)
                ) and os.path.getsize(newfiles[-1].full) >= size:
                newfiles[-1].close()
                counter += 1
                newfiles.append(
                    File(file_name=outputfile.format(counter),
                         action='w',
                         overwrite=overwrite))
        newfiles[-1].close()
        self.close()
        return newfiles