Ejemplo n.º 1
0
    def get_protein_file(self):

        """If requested in input will download pdb (or mmCIF) file
        If the given local file doesn't exist will download pdb (or mmCIF)
        otherwise returns the given path without modifying it
        
        all the paths are converted to absolute paths!"""

        #checks if I got a wrong input
        if self.local != 'no' and self.local != 'yes':
            raise ValueError(f'local cannot be {self.local} it can only be "yes" "no" or omitted')

        #if local = no or if the given path doesn't exist it will download the file in the given format (mmcif or pdb)
        if self.local == 'no' or self.local == None:
            
            #downloads the pdb or mmcif returning it's name
            path = download_pdb.download(protein_id = self.protein_id, file_type = self.protein_filetype, pdir = None)

            #the absolute path of the downloaded file is given to self.protein_filename
            self.protein_filename = auxiliary_functions_path.absolute_filepath(path = path)

        elif self.local == 'yes':

            #protein filename points to an existing file (if it doesn't auxiliary_functions_path.absolute_filepath will raise a FileNotFoundError)
            #the path is updated as absolute path
            self.protein_filename = auxiliary_functions_path.absolute_filepath(path =self.protein_filename)

        else:
            
            raise ValueError(f"local can only be 'yes' or 'no' not {self.local}")
Ejemplo n.º 2
0
    def test_file_not_found(self):

        with mock.patch("HPC_Drug.auxiliary_functions.path.os.path.exists",
                        return_value=False,
                        autospec=True):

            with self.assertRaises(FileNotFoundError):

                path.absolute_filepath(path="NOT_EXIST")
Ejemplo n.º 3
0
    def test_existing_file(self):

        with mock.patch("HPC_Drug.auxiliary_functions.path.os.path.exists",
                        return_value=True,
                        autospec=True):

            with mock.patch(
                    "HPC_Drug.auxiliary_functions.path.os.path.abspath",
                    return_value="abs_path",
                    autospec=True) as mocked_abspath:

                with mock.patch(
                        "HPC_Drug.auxiliary_functions.path.os.path.expanduser",
                        autospec=True) as mocked_expanduser:

                    with mock.patch(
                            "HPC_Drug.auxiliary_functions.path.os.path.expandvars",
                            autospec=True) as mocked_expandvars:

                        output = path.absolute_filepath(path="test")

                        mocked_abspath.assert_called_once()

                        mocked_expanduser.assert_called_once()

                        mocked_expandvars.assert_called_once_with("test")

                        self.assertEqual(output, "abs_path")
Ejemplo n.º 4
0
    def write(self, file_name = None, struct_type = 'biopython'):
        """
        This method writes self.structure on a {self.file_type} file (pdb, cif) using biopython (default) or prody (can only write pdb files)
        If no file_name is given self.pdb_file file will be overwritten otherwise a new file called file_name will be created and
        self.pdb_file will be updated with the new file_name (absolute path)

        file_name :: string, default self.pdb_file

        struct_type :: string, values: biopython (default), prody ; is the kind of structure in self.structure
        """

        if file_name == None:
            file_name = self.pdb_file

        if struct_type == 'prody':

            if self.file_type == 'pdb':

                prody.write_pdb(structure = self.structure, file_name = file_name)

            else:
                raise TypeError("Prody can only write pdb files not mmcif files, change Protein.file_type to pdb before you call this method")

        elif struct_type == 'biopython':

            biopython.write(structure = self.structure, file_type = self.file_type, file_name = file_name)

        self.pdb_file = path.absolute_filepath(path = file_name)
Ejemplo n.º 5
0
def merge_pdb(Protein):
    """
    Will put all the given ligands after the protein and update the ligand resnums
    this function is brutal and memory consuming I should do it better in the future

    both the protein and the ligands should be in PDB files (no check will be done)

    Protein :: HPC_Drug.structures.protein.Protein instance with a valid _ligands value

    return Protein with updated Protein.pdb_file
    """

    protein_file = read_file.read_file(file_name=Protein.pdb_file)

    ligands = Protein.get_ligand_list()

    #get the index of the line with the last ATOM HETATM or TER line
    #and get the resnum of this last residue
    for i in range(len(protein_file) - 1, -1, -1):

        if protein_file[i][0:4] == 'ATOM' or protein_file[i][
                0:6] == 'HETATM' or protein_file[i][0:3] == 'TER':

            #some TER lines are non standard and don't contain the residue number
            try:
                residue_number = int(protein_file[i][22:26].strip())
            except:
                residue_number = int(protein_file[i - 1][22:26].strip())

            index_protein_file = i + 1

            break

    #create the ligands list of strings
    ligand_file = []
    for j in range(len(ligands)):

        residue_number = residue_number + 1

        #update resnum
        ligands[j].resnum = residue_number

        tmp_ligand = read_file.read_file(file_name=ligands[j].pdb_file)

        #update the residue numbers in the file
        for k in range(len(tmp_ligand)):

            tmp_ligand[k] = tmp_ligand[k][:22] + "{0:>4}".format(
                residue_number) + tmp_ligand[k][26:]

        ligand_file = ligand_file + tmp_ligand

    #insert the ligands in the right place of the protein_file list
    protein_file[index_protein_file:index_protein_file] = ligand_file

    #be sure to get the right formatting
    for i in range(len(protein_file)):

        protein_file[i] = protein_file[i].strip('\n') + '\n'

    #overwrite Protein.pdb_file
    write_on_files.write_file(lines=protein_file,
                              file_name=f"{Protein.protein_id}_joined.pdb")

    Protein.pdb_file = path.absolute_filepath(
        path=f"{Protein.protein_id}_joined.pdb")

    return Protein