def test_change_phosphate(self): """Tests conversion OP1 <---> O1P""" convert(input_file=PDB_STRANGE, output_file='converter_out.pdb', phosphate="OP1") ms = load_model('converter_out.pdb', 'B') for resi in ms: self.assertFalse(resi.has_id("O1P")) self.assertTrue(resi.has_id('OP1')) convert(input_file=PDB_STRANGE, output_file='converter_out.pdb', phosphate="O1P") ms = load_model('converter_out.pdb', 'B') for resi in ms: self.assertTrue(resi.has_id("O1P")) self.assertFalse(resi.has_id('OP1'))
def test_change_ribose(self): """Test conversion of ribose nomenclature ' <---> *""" convert(input_file=PDB_STRANGE, output_file='converter_out.pdb', ribose="*") ms = load_model('converter_out.pdb', 'B') for resi in ms: self.assertFalse(resi.has_id("C4'")) self.assertTrue(resi.has_id('C4*')) convert(input_file=PDB_STRANGE, output_file='converter_out.pdb', ribose="'") ms = load_model('converter_out.pdb', 'B') for resi in ms: self.assertTrue(resi.has_id("C4'")) self.assertFalse(resi.has_id('C4*'))
def test_check_fragment_candidate_fitness(self): """Checks whether the fitness of fragment candidates increases""" last_fitness = None m = load_model('test_data/gaps/mini_1h4s_T_gap2.pdb', 'T') cand = m.find_fragment_candidates(m['64'], m['67'], Sequence('GAA')) for i in range(20): if not last_fitness: last_fitness = cand[i].score self.assertTrue(last_fitness <= cand[i].score) last_fitness = cand[i].score
def test_find_secstruc20(self): """Secstruc can be used as an optional search criterion""" hairpin = load_model(RNA_HAIRPIN, 'D') frag = find_fragment(hairpin, 29, 40, 'AUUCGUEAU', number_of_candidates=20, secstruc='(.......)') self.assertEqual(frag[0].fragment_instance.struc.get_secstruc(), ".(.......).")
def _test_write_candidates_with_right_seq(self): """All written candidates should have the right sequence.""" #TODO: reactivate tet for BBBuilder improvement self.candidates.write_fragment_candidates( TEST_DATA_PATH + 'loop_candidates', True, True, False, False) i = 0 for fn in os.listdir(TEST_DATA_PATH + 'loop_candidates'): if fn.endswith('.log'): continue i += 1 #if i>3: continue m = load_model(TEST_DATA_PATH + 'loop_candidates/' + fn) self.assertEqual(m.get_sequence(), Sequence('AGDDGGUUAG'))
def use_moderna_fragment(chain, target_seq, resid1, resid2): try: import moderna except ImportError: raise RuntimeError("ModeRNA is required for processing indels!") mod_model = moderna.load_model(chain, data_type="chain") res5p = resid_to_moderna(resid1) res3p = resid_to_moderna(resid2) moderna.apply_indel(mod_model, res5p, res3p, str(target_seq)) # Back to PDB pdb_model = mod_model.get_structure()[0] mod_chain, = pdb_model.child_list return mod_chain
def test_insert_eliminate(self): """Inserts a 2D fragment into a model.""" helix = load_model(HELIX, 'A') helix = RnaModel(None, None, 'A', 'residues', helix['1':'8'] + helix['73':'80']) mf = ModernaFragment2D(self.motif, \ anchor5=helix['7'], anchor3=helix['74'], \ anchor5_upper=helix['8'], anchor3_upper=helix['73'], \ frag5_upper=self.motif['196'], frag3_upper=self.motif['217'], \ new_sequence=Sequence('C'), \ model=helix) helix.insert_fragment(mf) self.assertEqual(helix.get_sequence(), Sequence('AAAAAAAA_UCUUUUUUU')) self.assertEqual(helix.get_secstruc(), '(((((((().)))))))')
def test_insert_blunt_fragment(self): """Helices can be built at blunt ends.""" model = load_model(HELIX) frag = self.hfb.build_fragment(anchor5=model['40'], anchor3=model['41'], \ sequence=Sequence('AUCGAAUU_AAUUCGAU')) finsert = FragmentInserter() finsert.insert_fragment(frag, model) self.assertEqual( model.get_sequence(), Sequence( 'AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAUCGAAUU_AAUUCGAUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUU' )) self.assertEqual( model.get_secstruc(), '(((((((((((((((((((((((((((((((((((((((((((((((())))))))))))))))))))))))))))))))))))))))))))))))' )
def get_clean_sequence_rna(pdbFile): """ With this function, the residues beloging to the pdb that are represented by a dot (.) are changed by the undefinided nucleotide 'N'. Finally, the fuction returns that sequence Module required: - moderna """ clean_sequence = '' pdb = moderna.load_model(pdbFile) sequence = str(pdb.get_sequence()) for residue in sequence: if residue in ['A', 'G', 'U', 'C']: clean_sequence += residue if residue == '.': clean_sequence += 'N' return clean_sequence
def test_change_sequence(self): """Checks whether sequence is changed into given by user""" m = ModernaStructure('file', MINI_TEMPLATE) #residues_mixed.ent') m.change_sequence('UUUUUUUUUUUUUUU') self.assertEqual(m.get_sequence(), Sequence('UUUUUUUUUUUUUUU')) self.assertRaises(ModernaStructureError, m.change_sequence, 'AAAAA') m.change_sequence('GGGG', '1', '4') self.assertEqual(m.get_sequence(), Sequence('GGGGUUUUUUUUUUU')) m.change_sequence('AAAA', '12') self.assertEqual(m.get_sequence(), Sequence('GGGGUUUUUUUAAAA')) m.change_sequence('CC', None, '2') self.assertEqual(m.get_sequence(), Sequence('CCGGUUUUUUUAAAA')) m.change_sequence(Sequence('UUUUUUUUUUUUUUU')) self.assertEqual(m.get_sequence(), Sequence('UUUUUUUUUUUUUUU')) unk = load_model(PDB_UNK) unk.change_sequence('DAAAPAEA?A7A') self.assertEqual(unk.get_sequence(), Sequence('DAAAPAEA?A7A'))
def test_gap_optimization_example_4(self): m = load_model('test_data/gaps/mini_1h4s_T_gap2.pdb', 'T') cand = m.find_fragment_candidates(m['64'], m['67'], Sequence('GAA')) hit = cand[6] m.insert_fragment(hit.fragment_instance)
def test_insert_best_fragment_quality_1(self): """Insert best indel without a strand break""" m = load_model('test_data/gaps/mini_1h4s_T_gap2.pdb', 'T') cand = m.insert_best_fragment('64', '67', Sequence('GAA')) m.fix_backbone() self.assertEqual(m.get_sequence().seq_with_modifications.find('_'), -1)
def mend_breakpoints(chains, gap): """ :param gap: A list of res_ids, which can be moved to mend the gap. """ #raise NotImplementedError("Error") try: import moderna except ImportError: warnings.warn( "Cannot mend gaps in sequence, because ModeRNA is not installed!") return chains mod_models = {} with fus.make_temp_directory() as tmpdir: log.info("Writing chains %s", chains.values()) #ftup.output_multiple_chains(chains.values(), op.join(tmpdir, "tmp.pdb")) for g in gap: if g[0].chain != g[1].chain: log.warning( "Not mending gap between multiple chains: %s and %s", g[0], g[1]) continue if g[0].chain not in mod_models: try: mod_models[g[0].chain] = moderna.load_model( chains[g[0].chain], data_type="chain" ) #moderna.load_model(op.join(tmpdir, "tmp.pdb"), g[0].chain) except Exception as e: with log_to_exception(log, e): log.error("g is %s, g[0] is %s, g[0].chain is %s", g, g[0], g[0].chain) log.error("chains is %s", chains) raise moderna.fix_backbone(mod_models[g[0].chain], resid_to_moderna(g[0]), resid_to_moderna(g[1])) #moderna.write_model(mod_models[g[0].chain], op.join(tmpdir, "tmp.pdb")) #for chain_id, model in mod_models.items(): # moderna.write_model(model, op.join(tmpdir, "mended_{}.pdb".format(chain_id))) #Load back to Biopython mended_chains = {} for chain_id in chains.keys(): if chain_id in mod_models: mended_chains[chain_id] = mod_models[ chain_id] #Mod models are chain subclasses anyway log.info("Mended:", mended_chains) mended_chains[chain_id].id = chain_id else: mended_chains[chain_id] = chains[chain_id] log.info("mended_chains: %s", mended_chains) # Moderna may replace modified residues with "UNK" for unknown or otherrwise change the code. # We have to replace them back. for chain_id in chains: for res in mended_chains[chain_id]: changed = False for o_res in chains[chain_id]: if o_res.id[1:] == res.id[1:]: log.debug("Changing Moderna residue %s to %s", res, o_res) assert not changed #Only one residue per number+icode res.id = o_res.id res.resname = o_res.resname log.debug("Moderna residue now %s", res) changed = True # Convert back from ModeRNA to Biopython out_chains = {} for k, v in mended_chains.items(): s = v.get_structure()[0] log.error("%s, %s %s", k, s, s.child_dict) assert len(s.child_list) == 1 out_chains[k] = s.child_list[0] out_chains[k].id = k return out_chains
def setUp(self): self.rna = load_model(RNA_HAIRPIN, 'D') self.hfb = HelixFragmentBuilder()
def test_add_to_unk(self): """Should be possible to add modification to unknown residue when ribose is present""" m = load_model(PDB_UNK) for resi in m: add_modification(resi, 'm1G') self.assertEqual(resi.long_abbrev, 'm1G')
def setUp(self): self.rna = load_model(MINI_TEMPLATE, 'A')
def setUp(self): self.rna = load_model(RNA_HAIRPIN, 'D') self.motif = ModernaStructure('file', BULGE_MOTIF)
class WriteFragmentCandidatesTests(TestCase): #KR: load model and candidates only once --> saves lots of time m = load_model(TEST_DATA_PATH + 'gaps/gap7_template.pdb', 'D') candidates = m.find_fragment_candidates(m['515'], m['519'], Sequence('DDG'), 10) #, lir_path='test_data/') #TODO: solve path problem: RNAModel needs to know LIR db file as optional parameter def setUp(self): self.remove_dir() def remove_dir(self, dirname=TEST_DATA_PATH + 'loop_candidates'): if os.access(TEST_DATA_PATH + 'loop_candidates', os.F_OK): for fn in os.listdir(dirname): os.remove(dirname + os.sep + fn) os.rmdir(dirname) def test_candidate_number(self): self.assertEqual(len(self.candidates), 10) def test_write_candidates_options_without_stems(self): """ Options for writing candidates should work 1. without stem residues""" self.candidates.write_fragment_candidates( TEST_DATA_PATH + 'loop_candidates', False, False, False, True) files = [fn for fn in os.listdir(TEST_DATA_PATH + 'loop_candidates')] self.assertEqual(len(files), 11) # check a sample for the right number of residues (3) m = ModernaStructure('file', TEST_DATA_PATH + 'loop_candidates/' + files[1]) self.assertEqual(len(m), 3) def test_write_candidates_options_with_stems(self): """ Options for writing candidates should work 2. with stem residues""" self.candidates.write_fragment_candidates( TEST_DATA_PATH + 'loop_candidates', True, False, False, True) files = [fn for fn in os.listdir(TEST_DATA_PATH + 'loop_candidates')] self.assertEqual(len(files), 11) # check a sample for the right number of residues (5) m = ModernaStructure('file', TEST_DATA_PATH + 'loop_candidates/' + files[1]) self.assertEqual(len(m), 5) def test_write_candidates_options_with_model(self): """ Options for writing candidates should work 3. without model""" self.candidates.write_fragment_candidates( TEST_DATA_PATH + 'loop_candidates', False, True, False, True) files = [fn for fn in os.listdir(TEST_DATA_PATH + 'loop_candidates')] self.assertEqual(len(files), 11) m = ModernaStructure('file', TEST_DATA_PATH + 'loop_candidates/' + files[1]) self.assertEqual(len(m), 10) def _test_write_candidates_with_right_seq(self): """All written candidates should have the right sequence.""" #TODO: reactivate tet for BBBuilder improvement self.candidates.write_fragment_candidates( TEST_DATA_PATH + 'loop_candidates', True, True, False, False) i = 0 for fn in os.listdir(TEST_DATA_PATH + 'loop_candidates'): if fn.endswith('.log'): continue i += 1 #if i>3: continue m = load_model(TEST_DATA_PATH + 'loop_candidates/' + fn) self.assertEqual(m.get_sequence(), Sequence('AGDDGGUUAG'))