def test__repair(self):

        with mock.patch("HPC_Drug.PDB.repair_pdb.repair.repair", return_value = "repaired", autospec = True):

            output = struct_rep.InfoRepair(Protein = "test", repairing_method = "pdbfixer")

            output._repair()

            self.assertEqual(output.Protein, "repaired")
    def test_get_info_and_repair_typeerror(self):

        Protein = mock.Mock()
        Protein.file_type = "WRONG_TYPE"
        
        output = struct_rep.InfoRepair(Protein = Protein, repairing_method = "pdbfixer")

        with self.assertRaises(TypeError):

            output.get_info_and_repair()
    def test__parse_structure(self):

        with mock.patch("HPC_Drug.PDB.structural_information.scan_structure.get_metalbinding_disulf_ligands", return_value = ("protein", "list")):

            output = struct_rep.InfoRepair(Protein = "test", repairing_method = "pdbfixer")

            output._parse_structure()

            self.assertEqual(output.Protein, "protein")
            self.assertEqual(output.organic_ligand_list, "list")
    def test_cif_header(self):

        with mock.patch.object(struct_rep.InfoRepair, "_repair", autospec = True) as mocked_repair:

            with mock.patch.object(struct_rep.InfoRepair, "_parse_header", autospec = True) as mocked_parse:

                output = struct_rep.InfoRepair(Protein = "test", repairing_method = "pdbfixer")

                output._cif()

                mocked_repair.assert_called_once()
                mocked_parse.assert_called_once()
    def test_get_info_and_repair_pdb(self):

        with mock.patch.object(struct_rep.InfoRepair, "_pdb", return_value = ("output", "list"), autospec = True) as mocked_pdb:

            Protein = mock.Mock()
            Protein.file_type = "pdb"
            
            output = struct_rep.InfoRepair(Protein = Protein, repairing_method = "pdbfixer")

            out_out = output.get_info_and_repair()

            mocked_pdb.assert_called_once()

            self.assertEqual(out_out, (output.Protein, output.organic_ligand_list))
    def test_cif_structure(self):

        with mock.patch.object(struct_rep.InfoRepair, "_repair", autospec = True) as mocked_repair:

            with mock.patch.object(struct_rep.InfoRepair, "_parse_header", side_effect = Exception, autospec = True) as mocked_parse:

                with mock.patch.object(struct_rep.InfoRepair, "_pdb", autospec = True) as mocked_pdb:

                    output = struct_rep.InfoRepair(Protein = "test", repairing_method = "pdbfixer")

                    output._cif()

                    mocked_repair.assert_not_called()
                    mocked_parse.assert_called_once()
                    mocked_pdb.assert_called_once()
    def test__init__(self):

        output = struct_rep.InfoRepair(Protein = "protein", repairing_method = "pdbfixer")

        self.assertEqual(output.Protein, "protein")
        self.assertEqual(output.repairing_method, "pdbfixer")
    def execute(self):
        """
        A pipeline that returns a clean and repaired "protein and ions" PDB and
        a PDB file for any not trash organic lingand
        starting from both a PDB, an mmCIF file or a protein id

        returns a Protein instance
        """

        #If requested in input will download pdb file
        #If the given local file doesn't exist raises FileNotFounfError
        #otherwise updates self.protein_filename with the given path
        #all the paths are converted to absolute paths
        self.get_protein_file()
        

        # creating protein instance
        Protein = protein.Protein(protein_id = self.protein_id,
                                    pdb_file = self.protein_filename,
                                    model = self.model,
                                    chain = self.chain,
                                    file_type = self.protein_filetype,
                                    tpg_file = self.protein_tpg_file,
                                    prm_file = self.protein_prm_file)
        

        #Get Protein.substitutions_dict Protein.sulf_bonds
        #repairs the Protein.pdb_file
        #returns a list containing the resnames and resnumbers of organic ligands
        # [[resname, resnum], [...], ...]
        #if there are none will be None item
        Info_rep = structural_information_and_repair.InfoRepair(Protein = Protein, repairing_method = self.repairing_method)

        Protein, ligand_resnames_resnums = Info_rep.get_info_and_repair()

        #remove still present disordered atoms (if any)
        Protein = remove_disordered_atoms.remove_disordered_atoms(Protein = Protein)

        #selects only a selected model and chain (Protein.model Protein.chain)
        Protein = select_model_chain.select_model_chain(Protein = Protein)

        #if the protein was a mmCIF I convert it to PDB
        Protein = mmcif2pdb.mmcif2pdb(Protein = Protein)

        #create the Ligand instances and add them to Protein._ligands
        Protein = get_ligands.get_ligands(Protein = Protein, ligand_resnames_resnums = ligand_resnames_resnums)

        Protein.update_structure(struct_type = "prody")

        prody_select = prody.ProdySelect(structure = Protein.structure)
        
        #gets the protein's structure from the pdb
        #The only HETATM remaining are the metal ions
        Protein.structure = prody_select.protein_and_ions()

        #Write Protein only pdb
        Protein.write(file_name = f"{Protein.protein_id}_protein.pdb", struct_type = 'prody')

        #removes the remaining trash ions
        Protein = remove_trash_metal_ions.remove_trash_metal_ions(Protein = Protein)


        #quick patch, will do it better
        #The structure is put in the reference system of the
        #inertia tensor
        orient_obj = orient.Orient(Protein = Protein)
        _, _, Rot_matrix = orient_obj.calculate_moment_of_intertia_tensor()
        Protein.structure = orient_obj.base_change_structure()
        Protein.write()

        Ligand = Protein.get_ligand_list()
        for i in range(len(Ligand)):
            Ligand[i].update_structure(struct_type = "biopython")

            Ligand[i].structure = orient_obj.base_change_structure(structure = Ligand[i].structure, rot_matrix = Rot_matrix)

            Ligand[i].write()


        return Protein