def test_writereadconfiguration_basic(self): filename = "temp.cfg" self.fragmentation.writeConfigurationToFile(filename) otherfrag = Fragmentation(self.molecule) otherfrag.readConfigurationFromFile(filename) for key in otherfrag.values.keys(): for key2 in otherfrag.values[key].keys(): self.assertEqual(self.fragmentation.values[key][key2], otherfrag.values[key][key2]) self.delete_file(filename)
def main(argv=None): if argv is None: argv = sys.argv[1:] # load defaults so we can use them below from config import FragItConfig cfg = FragItConfig() parser = OptionParser(usage=strings.usage, description=strings.description, version=strings.version_str) parser.add_option("-o", "--output", dest="outputfile", type=str, default="", metavar="filename") configuration = OptionGroup(parser, "Configuration") general = OptionGroup(parser, "Fragmentation") output = OptionGroup(parser, "Output") configuration.add_option( "--use-config", dest="useconfigfile", type=str, default="", metavar="filename", help= "Specify configuration file to use. This will ignore other command line parameters." ) configuration.add_option( "--make-config", dest="makeconfigfile", type=str, default="", metavar="filename", help= "Specify a filename to use as a configuration file. Use command line options to modify defaults. It is possible to use this command without specifying an input file to generate a clean configuration file." ) general.add_option( "-m", "--maxfragsize", dest="maxFragmentSize", type=int, default=cfg.getMaximumFragmentSize(), metavar="integer", help="The maximum fragment size allowed [default: %default]") general.add_option( "-g", "--groupcount", dest="groupcount", type=int, default=cfg.getFragmentGroupCount(), metavar="integer", help= "Specify number of consecutive fragments to combine into a single fragment [default: %default]" ) general.add_option( "--disable-protection", dest="disable_protection", action="store_true", default=False, help="Specify this flag to disable the use protection patterns.") general.add_option( "--merge-glycine", action="store_true", dest="merge_glycine", default=False, help= "Merge a glycine to the neighbor fragment when fragmenting proteins.") general.add_option( "--merge-specific", dest="mergespecific", type=int, default=None, metavar="integer", help= "Merge a specific fragment into all other fragments and remove it as a singular fragment." ) general.add_option("--charge-model", dest="charge_model", default=cfg.getChargeModel(), help="Charge model to use [%default]") general.add_option("--combine-fragments", dest="combinefragments", type=str, default="", metavar="list of integers", help="Combines several fragments into one.") output.add_option("--output-format", dest="format", type=str, default=cfg.getWriter(), help="Output format [%default]") output.add_option( "--output-boundaries", dest="boundaries", type=str, default="", metavar="list of floats", help= "Specifies boundaries for multiple layers. Must be used with --central-fragment option" ) output.add_option( "--output-central-fragment", dest="central_fragment", type=int, default=cfg.getCentralFragmentID(), metavar="integer", help= "Specifies the fragment to use as the central one. Used in combination with --output-boundaries to make layered inputs" ) output.add_option( "--output-active-distance", dest="active_atoms_distance", type=float, default=cfg.getActiveAtomsDistance(), metavar="float", help= "Atoms within this distance from --output-central-fragment will be active. Use with --output-buffer-distance to add buffer region between active and frozen parts. [default: %default]" ) output.add_option( "--output-buffer-distance", dest="maximum_buffer_distance", type=float, default=cfg.getBufferDistance(), metavar="float", help= "Maximum distance in angstrom from active fragments from which to include nearby fragments as buffers. This option adds and extends to --output-boundaries. [default: %default]" ) output.add_option( "--output-freeze-backbone", dest="freeze_backbone", action="store_true", default=cfg.getFreezeBackbone(), help="Option to freeze the backbone of the active region.") output.add_option( "--output-jmol-script", dest="output_jmol_script", action="store_true", default=cfg.getWriteJmolScript(), help="Write a complimentary jmol script for visualization.") output.add_option( "--output-pymol-script", dest="output_pymol_script", action="store_true", default=cfg.getWritePymolScript(), help="Write a complimentary pymol script for visualization.") parser.add_option_group(configuration) parser.add_option_group(general) parser.add_option_group(output) (options, args) = parser.parse_args(argv) if len(args) == 0 and len(options.makeconfigfile) > 0: cfg.writeConfigurationToFile(options.makeconfigfile) sys.exit() if len(args) != 1: parser.print_help() sys.exit() infile = args[0] molecule = fileToMol(infile) fragmentation = Fragmentation(molecule) # if there is a config file, read it and ignore other command line options if len(options.useconfigfile) > 0: fragmentation.readConfigurationFromFile(options.useconfigfile) (writer, output_extension) = get_writer_and_extension( fragmentation.getOutputFormat()) else: fragmentation.setChargeModel(options.charge_model) fragmentation.setMaximumFragmentSize(options.maxFragmentSize) fragmentation.setOutputFormat(options.format) if options.groupcount > 1: fragmentation.setFragmentGroupCount(options.groupcount) (writer, output_extension) = get_writer_and_extension(options.format) outfile = "%s%s" % (file_basename(infile), output_extension) if len(options.outputfile) > 0: outfile = options.outputfile # do the fragmentation procedure # 'fragmentation.doFragmentSpecificMerging()' should go somewhere here... fragmentation.setCombineFragments(options.combinefragments) if options.disable_protection: fragmentation.clearProtectPatterns() if options.merge_glycine: fragmentation.enableMergeGlycinePattern() fragmentation.beginFragmentation() fragmentation.doFragmentation() fragmentation.doFragmentMerging() fragmentation.doFragmentCombination() if fragmentation.getFragmentGroupCount() > 1: fragmentation.doFragmentGrouping() fragmentation.finishFragmentation() # write to file out = writer(fragmentation) # set options from command line boundaries = options.boundaries central_fragment = options.central_fragment active_atoms_distance = options.active_atoms_distance maximum_buffer_distance = options.maximum_buffer_distance freeze_backbone = options.freeze_backbone output_pymol_script = options.output_pymol_script output_jmol_script = options.output_jmol_script # set options from config file if len(options.useconfigfile) > 0: boundaries = fragmentation.getBoundaries() central_fragment = fragmentation.getCentralFragmentID() output_pymol_script = fragmentation.getWritePymolScript() output_jmol_script = fragmentation.getWriteJmolScript() freeze_backbone = fragmentation.getFreezeBackbone() maximum_buffer_distance = fragmentation.getBufferDistance() active_atoms_distance = fragmentation.getActiveAtomsDistance() # set the options out.setBoundariesFromString(boundaries) out.setCentralFragmentID(central_fragment) out.setActiveAtomsDistance(active_atoms_distance) out.setBufferMaxDistance(maximum_buffer_distance) if freeze_backbone: out.setFreezeBackbone() if output_pymol_script: out.setPymolOutput(infile, outfile) if output_jmol_script: out.setJmolOutput(infile, outfile) out.setup() out.writeFile(outfile) # write configuration file if len(options.makeconfigfile) > 0: fragmentation.setBoundaries(boundaries) fragmentation.writeConfigurationToFile(options.makeconfigfile)
def main(argv=None): if argv is None: argv = sys.argv[1:] # load defaults so we can use them below from config import FragItConfig cfg = FragItConfig() parser = OptionParser(usage=strings.usage, description=strings.description, version=strings.version_str) parser.add_option("-o", "--output", dest="outputfile", type=str, default="", metavar="filename") configuration = OptionGroup(parser, "Configuration") general = OptionGroup(parser, "Fragmentation") output = OptionGroup(parser, "Output") configuration.add_option("--use-config", dest="useconfigfile", type=str, default="", metavar="filename", help="Specify configuration file to use. This will ignore other command line parameters.") configuration.add_option("--make-config", dest="makeconfigfile", type=str, default="", metavar="filename", help="Specify a filename to use as a configuration file. Use command line options to modify defaults. It is possible to use this command without specifying an input file to generate a clean configuration file.") general.add_option("-m", "--maxfragsize", dest="maxFragmentSize", type=int, default=cfg.getMaximumFragmentSize(),metavar="integer", help="The maximum fragment size allowed [default: %default]") general.add_option("-g", "--groupcount", dest="groupcount", type=int, default=cfg.getFragmentGroupCount(),metavar="integer", help="Specify number of consecutive fragments to combine into a single fragment [default: %default]") general.add_option("--disable-protection", dest="disable_protection", action="store_true", default=False, help="Specify this flag to disable the use protection patterns.") general.add_option("--merge-glycine", action="store_true", dest="merge_glycine", default=False, help="Merge a glycine to the neighbor fragment when fragmenting proteins.") general.add_option("--merge-specific", dest="mergespecific", type=int, default=None, metavar="integer", help="Merge a specific fragment into all other fragments and remove it as a singular fragment.") general.add_option("--charge-model", dest="charge_model", default=cfg.getChargeModel(), help="Charge model to use [%default]") general.add_option("--combine-fragments", dest="combinefragments", type=str, default="",metavar="list of integers", help="Combines several fragments into one.") output.add_option("--output-format", dest="format", type=str, default=cfg.getWriter(), help="Output format [%default]") output.add_option("--output-boundaries", dest="boundaries", type=str, default="",metavar="list of floats", help="Specifies boundaries for multiple layers. Must be used with --central-fragment option") output.add_option("--output-central-fragment", dest="central_fragment", type=int, default=cfg.getCentralFragmentID(), metavar="integer", help="Specifies the fragment to use as the central one. Used in combination with --output-boundaries to make layered inputs") output.add_option("--output-active-distance", dest="active_atoms_distance", type=float, default=cfg.getActiveAtomsDistance(), metavar="float", help="Atoms within this distance from --output-central-fragment will be active. Use with --output-buffer-distance to add buffer region between active and frozen parts. [default: %default]") output.add_option("--output-buffer-distance", dest="maximum_buffer_distance", type=float, default=cfg.getBufferDistance(), metavar="float", help="Maximum distance in angstrom from active fragments from which to include nearby fragments as buffers. This option adds and extends to --output-boundaries. [default: %default]") output.add_option("--output-freeze-backbone", dest="freeze_backbone", action="store_true", default=cfg.getFreezeBackbone(), help="Option to freeze the backbone of the active region.") output.add_option("--output-jmol-script", dest="output_jmol_script", action="store_true", default=cfg.getWriteJmolScript(), help="Write a complimentary jmol script for visualization.") output.add_option("--output-pymol-script", dest="output_pymol_script", action="store_true", default=cfg.getWritePymolScript(), help="Write a complimentary pymol script for visualization.") parser.add_option_group(configuration) parser.add_option_group(general) parser.add_option_group(output) (options, args) = parser.parse_args(argv) if len(args) == 0 and len(options.makeconfigfile) > 0: cfg.writeConfigurationToFile(options.makeconfigfile) sys.exit() if len(args) != 1: parser.print_help() sys.exit() infile = args[0] molecule = fileToMol(infile) fragmentation = Fragmentation(molecule) # if there is a config file, read it and ignore other command line options if len(options.useconfigfile) > 0: fragmentation.readConfigurationFromFile(options.useconfigfile) (writer, output_extension) = get_writer_and_extension(fragmentation.getOutputFormat()) else: fragmentation.setChargeModel(options.charge_model) fragmentation.setMaximumFragmentSize(options.maxFragmentSize) fragmentation.setOutputFormat(options.format) if options.groupcount > 1: fragmentation.setFragmentGroupCount(options.groupcount) (writer, output_extension) = get_writer_and_extension(options.format) outfile = "%s%s" % (file_basename(infile), output_extension) if len(options.outputfile) > 0: outfile = options.outputfile # do the fragmentation procedure # 'fragmentation.doFragmentSpecificMerging()' should go somewhere here... fragmentation.setCombineFragments(options.combinefragments) if options.disable_protection: fragmentation.clearProtectPatterns() if options.merge_glycine: fragmentation.enableMergeGlycinePattern() fragmentation.beginFragmentation() fragmentation.doFragmentation() fragmentation.doFragmentMerging() fragmentation.doFragmentCombination() if fragmentation.getFragmentGroupCount() > 1: fragmentation.doFragmentGrouping() fragmentation.finishFragmentation() # write to file out = writer(fragmentation) # set options from command line boundaries = options.boundaries central_fragment = options.central_fragment active_atoms_distance = options.active_atoms_distance maximum_buffer_distance = options.maximum_buffer_distance freeze_backbone = options.freeze_backbone output_pymol_script = options.output_pymol_script output_jmol_script = options.output_jmol_script # set options from config file if len(options.useconfigfile) > 0: boundaries = fragmentation.getBoundaries() central_fragment = fragmentation.getCentralFragmentID() output_pymol_script = fragmentation.getWritePymolScript() output_jmol_script = fragmentation.getWriteJmolScript() freeze_backbone = fragmentation.getFreezeBackbone() maximum_buffer_distance = fragmentation.getBufferDistance() active_atoms_distance = fragmentation.getActiveAtomsDistance() # set the options out.setBoundariesFromString(boundaries) out.setCentralFragmentID(central_fragment) out.setActiveAtomsDistance(active_atoms_distance) out.setBufferMaxDistance(maximum_buffer_distance) if freeze_backbone: out.setFreezeBackbone() if output_pymol_script: out.setPymolOutput(infile,outfile) if output_jmol_script: out.setJmolOutput(infile,outfile) out.setup() out.writeFile(outfile) # write configuration file if len(options.makeconfigfile) > 0: fragmentation.setBoundaries(boundaries) fragmentation.writeConfigurationToFile(options.makeconfigfile)
def setUp(self): self.filename_pdb = "1UAO.pdb" # for testing, use OpenBabel functionality directly self.molecule = fileToMol(self.filename_pdb) self.fragmentation = Fragmentation(self.molecule)
class TestFragmentationModule(unittest.TestCase): def setUp(self): self.filename_pdb = "1UAO.pdb" # for testing, use OpenBabel functionality directly self.molecule = fileToMol(self.filename_pdb) self.fragmentation = Fragmentation(self.molecule) def tearDown(self): pass def delete_file(self, filename): try: f = open(filename) except IOError: return finally: f.close() os.remove(filename) def test_FragmentationDefaultParameters(self): frg = self.fragmentation self.assertEqual(frg.mol != None, True) def test_FragmentationSetActiveFragments(self): self.fragmentation.setActiveFragments([1, 2, 3]) self.assertEqual(self.fragmentation.active_fragments, [1, 2, 3]) def test_FragmentationApplySmartProtectPatterns(self): self.fragmentation.applySmartProtectPatterns() self.assertEqual(self.fragmentation.getExplicitlyProtectedAtoms(), [1, 2, 3, 4, 10]) def test_FragmentationDetermineFormalCharges(self): self.fragmentation.determineFormalCharges() self.assertAlmostEqual(sum(self.fragmentation.formalCharges), -2) def test_FragmentationGetProtectedAtoms(self): self.assertEqual(self.fragmentation.getExplicitlyProtectedAtoms(), []) def test_FragmentationAddProtectedAtomsAfterProtect(self): self.fragmentation.setProtectedAtoms() self.fragmentation.addExplicitlyProtectedAtoms([44, 55, 67]) self.assertEqual(self.fragmentation.getExplicitlyProtectedAtoms(), [1, 2, 3, 4, 10, 44, 55, 67]) def test_FragmentationAddBrokenBond(self): pass def test_FragmentationIsBondProtected(self): bond_pair = (2, 3) self.assertEqual(self.fragmentation.isBondProtected(bond_pair), False) self.fragmentation.addExplicitlyProtectedAtoms([2]) self.assertEqual(self.fragmentation.isBondProtected(bond_pair), True) def test_FragmentationRealBondBreakerNoProtect(self): bond_atoms = (2, 3) self.fragmentation.realBondBreaker("peptide", bond_atoms) self.assertEqual(self.fragmentation.getExplicitlyBreakAtomPairs(), [(2, 3)]) def test_FragmentationIsValidExplicitBond(self): self.assertRaises(ValueError, self.fragmentation.isValidExplicitBond, (1, 1)) self.assertRaises(ValueError, self.fragmentation.isValidExplicitBond, (2, 4)) def test_FragmentationBreakBondsWithNoProtect(self): self.fragmentation.breakBonds() self.assertEqual(len(self.fragmentation.getExplicitlyBreakAtomPairs()), 9) def test_FragmentationBreakBondsExplcitWithNoProtect(self): self.fragmentation.addExplicitlyBreakAtomPairs([(111, 112)]) self.fragmentation.breakBonds() self.assertEqual(len(self.fragmentation.getExplicitlyBreakAtomPairs()), 10) def test_FragmentationBreakBondsWithProtect(self): self.fragmentation.setProtectedAtoms() self.fragmentation.breakBonds() self.assertEqual(len(self.fragmentation.getExplicitlyBreakAtomPairs()), 8) def test_FragmentationBreakBondsExplcitWithProtect(self): self.fragmentation.setProtectedAtoms() self.fragmentation.addExplicitlyBreakAtomPairs([(111, 112)]) self.fragmentation.breakBonds() self.assertEqual(len(self.fragmentation.getExplicitlyBreakAtomPairs()), 9) def test_FragmentationDetermineFragmentsNoBreaking(self): self.assertRaises(ValueError, self.fragmentation.determineFragments) def test_FragmentationDetermineFragmentsWithBreaking(self): self.fragmentation.breakBonds() self.fragmentation.determineFragments() self.assertEqual(lenOfLists(self.fragmentation.getFragments()), [7, 21, 12, 14, 15, 14, 7, 14, 24, 10]) def test_FragmentationDetermineFragmentsWithBreakingAndGrouping(self): self.fragmentation.setFragmentGroupCount(2) self.fragmentation.breakBonds() self.fragmentation.determineFragments() self.fragmentation.doFragmentGrouping() self.assertEqual(lenOfLists(self.fragmentation.getFragments()), [28, 26, 29, 21, 34]) def test_FragmentationDetermineFragmentsWithBreakingAndGroupingTriple( self): self.fragmentation.setFragmentGroupCount(3) self.fragmentation.breakBonds() self.fragmentation.determineFragments() self.fragmentation.doFragmentGrouping() self.assertEqual(lenOfLists(self.fragmentation.getFragments()), [40, 43, 45, 10]) def test_FragmentationFindFragmentsCastErrors(self): self.assertRaises(ValueError, self.fragmentation.getAtomsInSameFragment, 1, 1) self.assertRaises(ValueError, self.fragmentation.getAtomsInSameFragment, "") def test_FragmentationFindFragmentsNoGrouping(self): self.fragmentation.breakBonds() self.fragmentation.determineFragments() self.assertEqual(self.fragmentation.getAtomsInSameFragment(11, 0), [ 3, 4, 10, 11, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30 ]) self.assertEqual(self.fragmentation.getAtomsInSameFragment(12, 0), [12, 13, 31, 32] + range(35, 43)) def test_FragmentationFindFragmentsNoGroupingWithProtect(self): self.fragmentation.setProtectedAtoms() self.fragmentation.breakBonds() self.fragmentation.determineFragments() self.assertEqual(self.fragmentation.getAtomsInSameFragment(11, 0), range(1, 12) + range(14, 31)) self.assertEqual(self.fragmentation.getAtomsInSameFragment(12, 0), [12, 13, 31, 32] + range(35, 43)) def test_FragmentationFragmentChargeAfterFragment(self): self.fragmentation.determineFormalCharges() self.fragmentation.breakBonds() self.fragmentation.determineFragments() self.fragmentation.determineFragmentCharges() self.assertEqual(self.fragmentation.getFragmentCharges(), [1, 0, -1, 0, -1, 0, 0, 0, 0, -1]) def test_FragmentationFragmentChargeAfterProtectAndFragment(self): self.fragmentation.setProtectedAtoms() self.fragmentation.determineFormalCharges() self.fragmentation.breakBonds() self.fragmentation.determineFragments() self.fragmentation.determineFragmentCharges() self.assertEqual(self.fragmentation.getFragmentCharges(), [1, -1, 0, -1, 0, 0, 0, 0, -1]) def test_FragmentationFragmentChargeAfterFragmentAndGroup(self): self.fragmentation.determineFormalCharges() self.fragmentation.breakBonds() self.fragmentation.determineFragments() self.fragmentation.setFragmentGroupCount(2) self.fragmentation.doFragmentGrouping() self.fragmentation.determineFragmentCharges() self.assertEqual(self.fragmentation.getFragmentCharges(), [1, -1, -1, 0, -1]) def test_FragmentationFragmentChargeAfterProtectFragmentAndGroup(self): self.fragmentation.setProtectedAtoms() self.fragmentation.determineFormalCharges() self.fragmentation.breakBonds() self.fragmentation.determineFragments() self.fragmentation.setFragmentGroupCount(2) self.fragmentation.doFragmentGrouping() self.fragmentation.determineFragmentCharges() self.assertEqual(self.fragmentation.getFragmentCharges(), [0, -1, 0, 0, -1]) def test_FragmentationGetOBAtom(self): test_atom = self.fragmentation.getOBAtom(1) self.assertEqual(type(test_atom), type(openbabel.OBAtom())) def test_FragmentationNameFragments(self): self.fragmentation.breakBonds() self.fragmentation.determineFragments() self.fragmentation.nameFragments() self.assertEqual(self.fragmentation.getFragmentNames(), [ "NH3+", "AMINO", "AMINO", "AMINO", "AMINO", "AMINO", "AMINO", "AMINO", "AMINO", "AMINO" ]) def test_FragmentationNameFragmentsProtect(self): self.fragmentation.setProtectedAtoms() self.fragmentation.breakBonds() self.fragmentation.determineFragments() self.fragmentation.nameFragments() self.assertEqual(self.fragmentation.getFragmentNames(), [ "AMINO", "AMINO", "AMINO", "AMINO", "AMINO", "AMINO", "AMINO", "AMINO", "AMINO" ]) def test_FragmentationNameFragmentsGroupByTwo(self): self.fragmentation.breakBonds() self.fragmentation.setFragmentGroupCount(2) self.fragmentation.determineFragments() self.fragmentation.doFragmentGrouping() self.fragmentation.nameFragments() self.assertEqual(self.fragmentation.getFragmentNames(), ["AMINO", "AMINO", "AMINO", "AMINO", "AMINO"]) def test_writereadconfiguration_basic(self): filename = "temp.cfg" self.fragmentation.writeConfigurationToFile(filename) otherfrag = Fragmentation(self.molecule) otherfrag.readConfigurationFromFile(filename) for key in otherfrag.values.keys(): for key2 in otherfrag.values[key].keys(): self.assertEqual(self.fragmentation.values[key][key2], otherfrag.values[key][key2]) self.delete_file(filename)
def setUp(self): self.molecule = fileToMol("watercluster4.xyz") self.fragmentation = Fragmentation(self.molecule) self.fixtures = 'gamess-fmo-fixtures'
class TestGamessFMOOutputModule(unittest.TestCase): def setUp(self): self.molecule = fileToMol("watercluster4.xyz") self.fragmentation = Fragmentation(self.molecule) self.fixtures = 'gamess-fmo-fixtures' def delete_file(self, filename): try: f = open(filename) except IOError: return finally: f.close() os.remove(filename) def test_water_1(self): filename = "temp.inp" otherfile = self.fixtures + "/water_1.fixture" gamessfmo = GamessFMO(self.fragmentation) gamessfmo.setup() gamessfmo.writeFile(filename) generated = ReadStringListFromFile(filename) fixture = ReadStringListFromFile(otherfile) self.assertEqual(len(generated), len(fixture)) for i in range(len(fixture)): self.assertEqual(generated[i], fixture[i]) self.delete_file(filename) def test_water_2(self): filename = "temp.inp" otherfile = self.fixtures + "/water_2.fixture" self.fragmentation.beginFragmentation() self.fragmentation.doFragmentation() self.fragmentation.finishFragmentation() gamessfmo = GamessFMO(self.fragmentation) gamessfmo.setup() gamessfmo.writeFile(filename) generated = ReadStringListFromFile(filename) fixture = ReadStringListFromFile(otherfile) self.assertEqual(len(generated), len(fixture)) for i in range(len(fixture)): self.assertEqual(generated[i], fixture[i]) self.delete_file(filename) def test_water_3(self): filename = "temp.inp" otherfile = self.fixtures + "/water_3.fixture" self.fragmentation.beginFragmentation() self.fragmentation.doFragmentation() self.fragmentation.finishFragmentation() gamessfmo = GamessFMO(self.fragmentation) gamessfmo.setCentralFragmentID(1) gamessfmo.setup() gamessfmo.writeFile(filename) generated = ReadStringListFromFile(filename) fixture = ReadStringListFromFile(otherfile) self.assertEqual(len(generated), len(fixture)) for i in range(len(fixture)): self.assertEqual(generated[i], fixture[i]) self.delete_file(filename) def test_water_4(self): filename = "temp.inp" otherfile = self.fixtures + "/water_4.fixture" self.fragmentation.beginFragmentation() self.fragmentation.doFragmentation() self.fragmentation.finishFragmentation() gamessfmo = GamessFMO(self.fragmentation) gamessfmo.setCentralFragmentID(1) gamessfmo.setBoundariesFromString("1.0") gamessfmo.setup() gamessfmo.writeFile(filename) generated = ReadStringListFromFile(filename) fixture = ReadStringListFromFile(otherfile) self.assertEqual(len(generated), len(fixture)) for i in range(len(fixture)): self.assertEqual(generated[i], fixture[i]) self.delete_file(filename) def test_water_5(self): filename = "temp.inp" otherfile = self.fixtures + "/water_5.fixture" self.fragmentation.beginFragmentation() self.fragmentation.doFragmentation() self.fragmentation.finishFragmentation() gamessfmo = GamessFMO(self.fragmentation) gamessfmo.setCentralFragmentID(1) gamessfmo.setBoundariesFromString("1.0") gamessfmo.setActiveAtomsDistance(1.0) gamessfmo.setBufferMaxDistance(1.0) gamessfmo.setup() gamessfmo.writeFile(filename) generated = ReadStringListFromFile(filename) fixture = ReadStringListFromFile(otherfile) self.assertEqual(len(generated), len(fixture)) for i in range(len(fixture)): self.assertEqual(generated[i], fixture[i]) self.delete_file(filename)
def setUp(self): self.molecule = fileToMol("1UAO.pdb") self.fragmentation = Fragmentation(self.molecule) self.standardwriter = Standard(Fragmentation)
class TestGamessFMOOutputModule(unittest.TestCase): def setUp(self): self.molecule = fileToMol("watercluster4.xyz") self.fragmentation = Fragmentation(self.molecule) self.fixtures = 'gamess-fmo-fixtures' def delete_file(self,filename): try: f = open(filename) except IOError: return finally: f.close() os.remove(filename) def test_water_1(self): filename = "temp.inp" otherfile = self.fixtures + "/water_1.fixture" gamessfmo = GamessFMO(self.fragmentation) gamessfmo.setup() gamessfmo.writeFile(filename) generated = ReadStringListFromFile(filename) fixture = ReadStringListFromFile(otherfile) self.assertEqual(len(generated), len(fixture)) for i in range(len(fixture)): self.assertEqual(generated[i], fixture[i]) self.delete_file(filename) def test_water_2(self): filename = "temp.inp" otherfile = self.fixtures + "/water_2.fixture" self.fragmentation.beginFragmentation() self.fragmentation.doFragmentation() self.fragmentation.finishFragmentation() gamessfmo = GamessFMO(self.fragmentation) gamessfmo.setup() gamessfmo.writeFile(filename) generated = ReadStringListFromFile(filename) fixture = ReadStringListFromFile(otherfile) self.assertEqual(len(generated), len(fixture)) for i in range(len(fixture)): self.assertEqual(generated[i], fixture[i]) self.delete_file(filename) def test_water_3(self): filename = "temp.inp" otherfile = self.fixtures + "/water_3.fixture" self.fragmentation.beginFragmentation() self.fragmentation.doFragmentation() self.fragmentation.finishFragmentation() gamessfmo = GamessFMO(self.fragmentation) gamessfmo.setCentralFragmentID(1) gamessfmo.setup() gamessfmo.writeFile(filename) generated = ReadStringListFromFile(filename) fixture = ReadStringListFromFile(otherfile) self.assertEqual(len(generated), len(fixture)) for i in range(len(fixture)): self.assertEqual(generated[i], fixture[i]) self.delete_file(filename) def test_water_4(self): filename = "temp.inp" otherfile = self.fixtures + "/water_4.fixture" self.fragmentation.beginFragmentation() self.fragmentation.doFragmentation() self.fragmentation.finishFragmentation() gamessfmo = GamessFMO(self.fragmentation) gamessfmo.setCentralFragmentID(1) gamessfmo.setBoundariesFromString("1.0") gamessfmo.setup() gamessfmo.writeFile(filename) generated = ReadStringListFromFile(filename) fixture = ReadStringListFromFile(otherfile) self.assertEqual(len(generated), len(fixture)) for i in range(len(fixture)): self.assertEqual(generated[i], fixture[i]) self.delete_file(filename) def test_water_5(self): filename = "temp.inp" otherfile = self.fixtures + "/water_5.fixture" self.fragmentation.beginFragmentation() self.fragmentation.doFragmentation() self.fragmentation.finishFragmentation() gamessfmo = GamessFMO(self.fragmentation) gamessfmo.setCentralFragmentID(1) gamessfmo.setBoundariesFromString("1.0") gamessfmo.setActiveAtomsDistance(1.0) gamessfmo.setBufferMaxDistance(1.0) gamessfmo.setup() gamessfmo.writeFile(filename) generated = ReadStringListFromFile(filename) fixture = ReadStringListFromFile(otherfile) self.assertEqual(len(generated), len(fixture)) for i in range(len(fixture)): self.assertEqual(generated[i], fixture[i]) self.delete_file(filename)
class TestFragmentationModule(unittest.TestCase): def setUp(self): self.filename_pdb = "1UAO.pdb" # for testing, use OpenBabel functionality directly self.molecule = fileToMol(self.filename_pdb) self.fragmentation = Fragmentation(self.molecule) def tearDown(self): pass def delete_file(self, filename): try: f = open(filename) except IOError: return finally: f.close() os.remove(filename) def test_FragmentationDefaultParameters(self): frg = self.fragmentation self.assertEqual(frg.mol != None, True) def test_FragmentationSetActiveFragments(self): self.fragmentation.setActiveFragments([1, 2, 3]) self.assertEqual(self.fragmentation.active_fragments, [1, 2, 3]) def test_FragmentationApplySmartProtectPatterns(self): self.fragmentation.applySmartProtectPatterns() self.assertEqual(self.fragmentation.getExplicitlyProtectedAtoms(), [1, 2, 3, 4, 10]) def test_FragmentationDetermineFormalCharges(self): self.fragmentation.determineFormalCharges() self.assertAlmostEqual(sum(self.fragmentation.formalCharges), -2) def test_FragmentationGetProtectedAtoms(self): self.assertEqual(self.fragmentation.getExplicitlyProtectedAtoms(), []) def test_FragmentationAddProtectedAtomsAfterProtect(self): self.fragmentation.setProtectedAtoms() self.fragmentation.addExplicitlyProtectedAtoms([44, 55, 67]) self.assertEqual(self.fragmentation.getExplicitlyProtectedAtoms(), [1, 2, 3, 4, 10, 44, 55, 67]) def test_FragmentationAddBrokenBond(self): pass def test_FragmentationIsBondProtected(self): bond_pair = (2, 3) self.assertEqual(self.fragmentation.isBondProtected(bond_pair), False) self.fragmentation.addExplicitlyProtectedAtoms([2]) self.assertEqual(self.fragmentation.isBondProtected(bond_pair), True) def test_FragmentationRealBondBreakerNoProtect(self): bond_atoms = (2, 3) self.fragmentation.realBondBreaker("peptide", bond_atoms) self.assertEqual(self.fragmentation.getExplicitlyBreakAtomPairs(), [(2, 3)]) def test_FragmentationIsValidExplicitBond(self): self.assertRaises(ValueError, self.fragmentation.isValidExplicitBond, (1, 1)) self.assertRaises(ValueError, self.fragmentation.isValidExplicitBond, (2, 4)) def test_FragmentationBreakBondsWithNoProtect(self): self.fragmentation.breakBonds() self.assertEqual(len(self.fragmentation.getExplicitlyBreakAtomPairs()), 9) def test_FragmentationBreakBondsExplcitWithNoProtect(self): self.fragmentation.addExplicitlyBreakAtomPairs([(111, 112)]) self.fragmentation.breakBonds() self.assertEqual(len(self.fragmentation.getExplicitlyBreakAtomPairs()), 10) def test_FragmentationBreakBondsWithProtect(self): self.fragmentation.setProtectedAtoms() self.fragmentation.breakBonds() self.assertEqual(len(self.fragmentation.getExplicitlyBreakAtomPairs()), 8) def test_FragmentationBreakBondsExplcitWithProtect(self): self.fragmentation.setProtectedAtoms() self.fragmentation.addExplicitlyBreakAtomPairs([(111, 112)]) self.fragmentation.breakBonds() self.assertEqual(len(self.fragmentation.getExplicitlyBreakAtomPairs()), 9) def test_FragmentationDetermineFragmentsNoBreaking(self): self.assertRaises(ValueError, self.fragmentation.determineFragments) def test_FragmentationDetermineFragmentsWithBreaking(self): self.fragmentation.breakBonds() self.fragmentation.determineFragments() self.assertEqual(lenOfLists(self.fragmentation.getFragments()), [7, 21, 12, 14, 15, 14, 7, 14, 24, 10]) def test_FragmentationDetermineFragmentsWithBreakingAndGrouping(self): self.fragmentation.setFragmentGroupCount(2) self.fragmentation.breakBonds() self.fragmentation.determineFragments() self.fragmentation.doFragmentGrouping() self.assertEqual(lenOfLists(self.fragmentation.getFragments()), [28, 26, 29, 21, 34]) def test_FragmentationDetermineFragmentsWithBreakingAndGroupingTriple(self): self.fragmentation.setFragmentGroupCount(3) self.fragmentation.breakBonds() self.fragmentation.determineFragments() self.fragmentation.doFragmentGrouping() self.assertEqual(lenOfLists(self.fragmentation.getFragments()), [40, 43, 45, 10]) def test_FragmentationFindFragmentsCastErrors(self): self.assertRaises(ValueError, self.fragmentation.getAtomsInSameFragment, 1, 1) self.assertRaises(ValueError, self.fragmentation.getAtomsInSameFragment, "") def test_FragmentationFindFragmentsNoGrouping(self): self.fragmentation.breakBonds() self.fragmentation.determineFragments() self.assertEqual( self.fragmentation.getAtomsInSameFragment(11, 0), [3, 4, 10, 11, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30], ) self.assertEqual(self.fragmentation.getAtomsInSameFragment(12, 0), [12, 13, 31, 32] + range(35, 43)) def test_FragmentationFindFragmentsNoGroupingWithProtect(self): self.fragmentation.setProtectedAtoms() self.fragmentation.breakBonds() self.fragmentation.determineFragments() self.assertEqual(self.fragmentation.getAtomsInSameFragment(11, 0), range(1, 12) + range(14, 31)) self.assertEqual(self.fragmentation.getAtomsInSameFragment(12, 0), [12, 13, 31, 32] + range(35, 43)) def test_FragmentationFragmentChargeAfterFragment(self): self.fragmentation.determineFormalCharges() self.fragmentation.breakBonds() self.fragmentation.determineFragments() self.fragmentation.determineFragmentCharges() self.assertEqual(self.fragmentation.getFragmentCharges(), [1, 0, -1, 0, -1, 0, 0, 0, 0, -1]) def test_FragmentationFragmentChargeAfterProtectAndFragment(self): self.fragmentation.setProtectedAtoms() self.fragmentation.determineFormalCharges() self.fragmentation.breakBonds() self.fragmentation.determineFragments() self.fragmentation.determineFragmentCharges() self.assertEqual(self.fragmentation.getFragmentCharges(), [1, -1, 0, -1, 0, 0, 0, 0, -1]) def test_FragmentationFragmentChargeAfterFragmentAndGroup(self): self.fragmentation.determineFormalCharges() self.fragmentation.breakBonds() self.fragmentation.determineFragments() self.fragmentation.setFragmentGroupCount(2) self.fragmentation.doFragmentGrouping() self.fragmentation.determineFragmentCharges() self.assertEqual(self.fragmentation.getFragmentCharges(), [1, -1, -1, 0, -1]) def test_FragmentationFragmentChargeAfterProtectFragmentAndGroup(self): self.fragmentation.setProtectedAtoms() self.fragmentation.determineFormalCharges() self.fragmentation.breakBonds() self.fragmentation.determineFragments() self.fragmentation.setFragmentGroupCount(2) self.fragmentation.doFragmentGrouping() self.fragmentation.determineFragmentCharges() self.assertEqual(self.fragmentation.getFragmentCharges(), [0, -1, 0, 0, -1]) def test_FragmentationGetOBAtom(self): test_atom = self.fragmentation.getOBAtom(1) self.assertEqual(type(test_atom), type(openbabel.OBAtom())) def test_FragmentationNameFragments(self): self.fragmentation.breakBonds() self.fragmentation.determineFragments() self.fragmentation.nameFragments() self.assertEqual( self.fragmentation.getFragmentNames(), ["NH3+", "AMINO", "AMINO", "AMINO", "AMINO", "AMINO", "AMINO", "AMINO", "AMINO", "AMINO"], ) def test_FragmentationNameFragmentsProtect(self): self.fragmentation.setProtectedAtoms() self.fragmentation.breakBonds() self.fragmentation.determineFragments() self.fragmentation.nameFragments() self.assertEqual( self.fragmentation.getFragmentNames(), ["AMINO", "AMINO", "AMINO", "AMINO", "AMINO", "AMINO", "AMINO", "AMINO", "AMINO"], ) def test_FragmentationNameFragmentsGroupByTwo(self): self.fragmentation.breakBonds() self.fragmentation.setFragmentGroupCount(2) self.fragmentation.determineFragments() self.fragmentation.doFragmentGrouping() self.fragmentation.nameFragments() self.assertEqual(self.fragmentation.getFragmentNames(), ["AMINO", "AMINO", "AMINO", "AMINO", "AMINO"]) def test_writereadconfiguration_basic(self): filename = "temp.cfg" self.fragmentation.writeConfigurationToFile(filename) otherfrag = Fragmentation(self.molecule) otherfrag.readConfigurationFromFile(filename) for key in otherfrag.values.keys(): for key2 in otherfrag.values[key].keys(): self.assertEqual(self.fragmentation.values[key][key2], otherfrag.values[key][key2]) self.delete_file(filename)