def test_fill_doublegap(self): """Should insert fragments into correct sites.""" # prepare model t = Template(DOUBLEGAP, 'file', 'A') m = RnaModel() for r in t: m.copy_residue(r) # insert first fragment struc = ModernaStructure('file', FRAGMENT1) f1 = ModernaFragment53(struc, anchor5=m['20'], anchor3=m['24'], \ new_sequence=Sequence('AUA'), keep=keep_first_last) m.insert_fragment(f1) resids = [r.identifier for r in m] expected_resids1 = [ '18', '19', '20', '21', '21A', '23', '24', '27', '28', '29', '30', '31', '32', '33', '34', '35' ] self.assertEqual(resids, expected_resids1) # insert second fragment struc = ModernaStructure('file', FRAGMENT2) f2 = ModernaFragment53(struc, anchor5=m['23'], anchor3=m['28'], \ new_sequence=Sequence('AUUA'), keep=keep_first_last) m.insert_fragment(f2) resids = [r.identifier for r in m] expected_resids2 = [ '18', '19', '20', '21', '21A', '23', '24', '24A', '24B', '27', '28', '29', '30', '31', '32', '33', '34', '35' ] self.assertEqual(resids, expected_resids2)
def test_attributes(self): """object attributes are set up correctly.""" f1 = ModernaFragment53(self.s1, anchor5=self.m['10'], anchor3=self.m['13']) self.assertEqual(len(f1.struc), 4) self.assertEqual(f1.anchor5.fixed_resi, self.m['10']) self.assertEqual(f1.anchor3.fixed_resi, self.m['13']) self.assertEqual(f1.struc.get_sequence(), Sequence("GCGG")) self.assertTrue(str(f1)) # anchor residues self.assertEqual(len(f1.anchor_residues), 2) self.assertEqual(f1.anchor_residues[0].mobile_resi.identifier, '1') self.assertEqual(f1.anchor_residues[1].mobile_resi.identifier, '4') f1.prepare_anchor_residues() self.assertEqual(f1.anchor_residues[0].mobile_resi.identifier, '1') self.assertEqual(f1.anchor_residues[1].mobile_resi.identifier, '4') # non-anchors self.assertEqual(len(f1.nonanchor_residues), 2) # second example f2 = ModernaFragment53(self.s2, anchor5=self.m['1'], anchor3=self.m['4'], new_sequence=Sequence('GL')) self.assertEqual(len(f2.struc), 4) self.assertEqual(f2.struc.get_sequence(), Sequence("GCGG")) self.assertEqual(f2.new_sequence, Sequence("GL"))
def test_superimpose_fragment(self): """Should apply superimposition and return RMSD""" f1 = ModernaFragment53(self.s1, anchor5=self.m['10'],anchor3=self.m['13']) rmsd = f1.superimpose() self.assertAlmostEqual(rmsd,1.0,0) # MM: ? 1.1253267913922658 # second fragment should fit perfectly f2 = ModernaFragment53(self.s2,anchor5=self.m['1'],anchor3=self.m['4'], new_sequence=Sequence('GL')) rmsd = f2.superimpose() self.assertAlmostEqual(rmsd,0.00)
def test_clash(self): """Should not clash with a given piece of structure.""" f1 = ModernaFragment53(self.s1, anchor5=self.m['10'], anchor3=self.m['13']) self.assertFalse(f1.has_clashes(self.m['8':'10'])) f2 = ModernaFragment53(self.s2, anchor5=self.m['1'], anchor3=self.m['4'], new_sequence=Sequence('GL')) self.assertTrue(f2.has_clashes(self.m['1':'4']))
def test_renumber(self): """New numbering should start at anchor5, and then go through alphabet.""" f1 = ModernaFragment53(self.s1, anchor5=self.m['10'], anchor3=self.m['13']) f1.prepare_anchor_residues() f1.renumber() self.assertEqual([r.identifier for r in self.s1],['10','10A','10B','13']) # second example f2 = ModernaFragment53(self.s2,anchor5=self.m['1'], anchor3=self.m['4'], new_sequence=Sequence('GL')) f2.prepare_anchor_residues() f2.renumber() self.assertEqual([r.identifier for r in self.s2], ['1','1A','1B','4'])
def test_get_resi_to_remove(self): """Should return resi identifiers between anchors and of anchors itself.""" f1 = ModernaFragment53(self.s1, anchor5=self.m['10'], anchor3=self.m['13']) result = f1.get_resi_to_remove(self.m) self.assertEqual(result, ['10', '11', '12', '13'])
def _create_frag(self, helix, seq, anchor5, anchor3, upper5, upper3, model): """Returns a ModernaFragment class""" seq = seq.seq_without_breaks if upper5 and upper3: return ModernaFragment2D(helix, \ anchor5=anchor5, anchor3=anchor3, \ anchor5_upper=upper5, \ anchor3_upper=upper3, \ frag5_upper=helix.strand5[-1], \ frag3_upper=helix.strand3[0], \ new_sequence=seq, \ superposition_atoms=HELIX_SUPERPOSITION, model=model ) elif upper5: return ModernaFragment553 elif upper3: return ModernaFragment533 else: return ModernaFragment53(helix, \ anchor5=anchor5, anchor3=anchor3, \ new_sequence=seq, \ sup_r5=HELIX_SUPERPOSITION, \ sup_r3=HELIX_SUPERPOSITION, \ strict=False )
def create_fragment(pdb_path, anchor5=None, anchor3=None, chain_name='A', sequence=None): """*create_fragment(pdb_path, anchor5=None, anchor3=None, chain_name='A', sequence=None)* To insert small pieces of custom PDB structures into models, fragments can be used. This command loads a fragment, and defines one or two connection points, to which the fragment will be inserted by superposition. It returns a ModernaFragment object. To add a ModernaFragment object to a model, the insert_fragment command should be used. The scenarios for adding either involve superposition of a single residue with the model on the 5' end of the fragment, or on the 3' end, or both. :Arguments: * path to pdb file with the fragment * residue to which the 5' end of the fragment will be superimposed * residue to which the 3' end of the fragment will be superimposed * chain name of the fragment in the file (optional; A by default) * sequence that should be modeled onto the fragment upon insertion (optional; the sequence should not include the 1-2 anchor residues, it therefore has to be shorter than the fragment) """ pdb_path = validate_path(pdb_path) if anchor5: anchor5 = validate_resi(anchor5) if anchor3: anchor3 = validate_resi(anchor3) if sequence: sequence = validate_seq(sequence) struc = ModernaStructure(data_type='file', data=pdb_path, chain_name=chain_name) if anchor5 and anchor3: fr = ModernaFragment53(struc, anchor5=anchor5, anchor3=anchor3, new_sequence=sequence, strict=False) elif anchor5: fr = ModernaFragment5(struc, anchor5=anchor5, new_sequence=sequence, strict=False) elif anchor3: fr = ModernaFragment3(struc, anchor3=anchor3, new_sequence=sequence, strict=False) else: raise ModernaError("Anchor residues need to be specified.") return fr
def setUp(self): self.s = ModernaStructure('file', MINI_TEMPLATE) self.t = Template(MINI_TEMPLATE, 'file') self.m = RnaModel(None, None, 'A', 'file', MINI_TEMPLATE) self.f = ModernaFragment53(self.s, anchor5=self.m['1'], anchor3=self.m['14']) self.struc = PDBParser().get_structure('test_struc', MINI_TEMPLATE)
def make_helix_longer(self, helix): #, dest_length): """Adds base pairs to a helix.""" helix2 = self._get_helix() helix2 = ModernaFragment53(helix2, anchor5=helix.strand5[-1], \ anchor3=helix.strand3[0], strict=False) finsert = FragmentInserter() finsert.insert_fragment(helix2, helix) self.renumber_helix(helix)
def test_add_continuous_exact(self): """Model sequence should change accordingly.""" f2 = ModernaFragment53(self.s2, anchor5=self.m['1'], anchor3=self.m['4'], new_sequence=Sequence('GL')) self.m.insert_fragment(f2) self.assertEqual(self.m.get_sequence(), Sequence("GGLGAUUUALCUCAG")) self.assertAlmostEqual(f2.rmsd, 0.000, 2)
def test_add_keep_numbers(self): """Model numbers should change accordingly.""" middle1 = ModernaFragment53(self.s1,anchor5=self.m['9'],anchor3=self.m['14'], keep=keep_first_last) middle1.prepare_anchor_residues() middle1.renumber(self.m) self.m.insert_fragment(middle1) numbers = [r.identifier for r in self.m] expected = ['1', '2', '3', '4', '5', '6', '7', '8','9', '10', '13', '14','15'] self.assertEqual(numbers, expected)
def test_refine(self): """Should change both numbers and sequence, and superimpose.""" f2 = ModernaFragment53(self.s2,anchor5=self.m['1'],anchor3=self.m['4'], new_sequence=Sequence('GL')) f2.superimpose() f2.prepare_anchor_residues() f2.renumber() f2.apply_seq() numbers = [r.identifier for r in self.s2] self.assertEqual(numbers,['1','1A','1B','4']) self.assertEqual(self.s2.get_sequence(), Sequence('GGLG'))
def test_create_long_fragment(self): """Fragments with 26+ residues should be OK.""" t = load_template(RNA_1C0A, 'B') resis = t['5':'45'] long_seq = Sequence("AUAUAUAUAUGCGCGCGCGCAUAUAUAUAUGCGCGCGCG") struc = ModernaStructure('residues', resis) frag = ModernaFragment53(struc, anchor5=t['5'],anchor3=t['45'], new_sequence=long_seq) frag.superimpose() frag.prepare_anchor_residues() frag.renumber() frag.apply_seq() self.assertEqual(struc.get_sequence(), Sequence("CAUAUAUAUAUGCGCGCGCGCAUAUAUAUAUGCGCGCGCGG"))
def test_fill_gap_numbering(self): """Should insert fragment with correct numbering.""" t = Template(DOUBLEGAP, 'file', 'A') m = RnaModel() for r in t: m.copy_residue(r) struc = ModernaStructure('file', FRAGMENT1) f1 = ModernaFragment53(struc, anchor5=m['20'], anchor3=m['24'], new_sequence=Sequence('AUA')) m.insert_fragment(f1) resids = [r.identifier for r in m] expected_resids1 = [ '18', '19', '20', '20A', '20B', '20C', '24', '27', '28', '29', '30', '31', '32', '33', '34', '35' ] self.assertEqual(resids, expected_resids1)
def test_all_records(self): """All records in the entire LIR db should be readable.""" library = StructureLibrary(PATH_TO_LIR_STRUCTURES) f = open(LIR_DATABASE_PATH) for i, line in enumerate(f): print i, line if line.startswith('loop length'): continue line = line.strip().split('\t') if len(line) == 17: st = library.get_structure_part(line[1], line[2], line[3], line[4]) seq = Sequence(line[6]) fr = ModernaFragment53('residues', st[line[3]:line[4]], line[2], st[line[3]], st[line[4]], None, None, None) assert len(fr) - 2 == int(line[0]) fr_seq = fr.get_sequence() print '1', seq, '2', fr_seq assert seq == fr_seq if i % 100 == 0: print i
def test_insert(self): """Inserts fragment into a model""" f1 = ModernaFragment53(self.s1, anchor5=self.m['10'],anchor3=self.m['13']) finsert = FragmentInserter() finsert.insert_fragment(f1, self.m) self.assertEqual(self.m.get_sequence(),Sequence("GCGGAUUUALCGCAG"))
def get_fragment_part(self, name, chain, resi_from, resi_to, \ anchor5, anchor3, sequence, keep, seq): """Returns a ModernaFragment53 from the library.""" struc = self.get_structure_part(name, chain, resi_from, resi_to, seq) return ModernaFragment53(struc, anchor5, anchor3, sequence, keep=keep)