File: nuclinfo.py

package info (click to toggle)
mdanalysis 2.10.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 116,696 kB
  • sloc: python: 92,135; ansic: 8,156; makefile: 215; sh: 138
file content (912 lines) | stat: -rw-r--r-- 24,177 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
# -*- Mode: python; tab-width: 4; indent-tabs-mode:nil; coding:utf-8 -*-
# vim: tabstop=4 expandtab shiftwidth=4 softtabstop=4
#
# MDAnalysis --- https://www.mdanalysis.org
# Copyright (c) 2006-2017 The MDAnalysis Development Team and contributors
# (see the file AUTHORS for the full list of names)
#
# Released under the Lesser GNU Public Licence, v2.1 or any higher version
#
# Please cite your use of MDAnalysis in published work:
#
# R. J. Gowers, M. Linke, J. Barnoud, T. J. E. Reddy, M. N. Melo, S. L. Seyler,
# D. L. Dotson, J. Domanski, S. Buchoux, I. M. Kenney, and O. Beckstein.
# MDAnalysis: A Python package for the rapid analysis of molecular dynamics
# simulations. In S. Benthall and S. Rostrup editors, Proceedings of the 15th
# Python in Science Conference, pages 102-109, Austin, TX, 2016. SciPy.
# doi: 10.25080/majora-629e541a-00e
#
# N. Michaud-Agrawal, E. J. Denning, T. B. Woolf, and O. Beckstein.
# MDAnalysis: A Toolkit for the Analysis of Molecular Dynamics Simulations.
# J. Comput. Chem. 32 (2011), 2319--2327, doi:10.1002/jcc.21787
#

"""
Nucleic acid analysis --- :mod:`MDAnalysis.analysis.nuclinfo`
=============================================================

:Author: Elizabeth Denning
:Year: 2011
:Copyright: Lesser GNU Public License v2.1+

The module provides functions to analyze nucleic acid structures, in
particular

- backbone dihedrals,
- chi dihedrals,
- AS or CP phase angles,
- Watson-Crick N1-N3 distances, C2-O2 distances, N6-O4 distances, O6-N4 distances.

For applications of this kind of analysis see
:footcite:p:`Denning2011,Denning2012`.

All functions take a :class:`~MDAnalysis.core.universe.Universe` as an
argument together with further parameters that specify the base or bases in
question. Angles are in degrees. The functions use standard CHARMM names for
nucleic acids and atom names.


.. rubric:: References

.. footbibliography::

Distances
---------

.. autofunction:: wc_pair

.. autofunction:: minor_pair

.. autofunction:: major_pair


Phases
------

.. autofunction:: phase_cp

.. autofunction:: phase_as


Dihedral angles
---------------

.. autofunction:: tors

.. autofunction:: tors_alpha

.. autofunction:: tors_beta

.. autofunction:: tors_gamma

.. autofunction:: tors_delta

.. autofunction:: tors_eps

.. autofunction:: tors_zeta

.. autofunction:: tors_chi

.. autofunction:: hydroxyl

.. autofunction:: pseudo_dihe_baseflip

"""
import numpy as np
from math import pi, sin, cos, sqrt, pow

from MDAnalysis.lib import mdamath


def wc_pair(universe, i, bp, seg1="SYSTEM", seg2="SYSTEM"):
    """Watson-Crick basepair distance for residue `i` with residue `bp`.

    The distance of the nitrogen atoms in a Watson-Crick hydrogen bond is
    computed.

    Parameters
    ----------
    universe : Universe
         :class:`~MDAnalysis.core.universe.Universe` containing the trajectory
    i : int
        resid of the first base
    bp : int
        resid of the second base
    seg1 : str (optional)
        segment id for first base ["SYSTEM"]
    seg2 : str (optional)
        segment id for second base ["SYSTEM"]

    Returns
    -------
    float
        Watson-Crick base pair distance


    Notes
    -----
    If failure occurs be sure to check the segment identification.


    .. versionadded:: 0.7.6
    """
    if universe.select_atoms(" resid {0!s} ".format(i)).resnames[0] in [
        "DC",
        "DT",
        "U",
        "C",
        "T",
        "CYT",
        "THY",
        "URA",
    ]:
        a1, a2 = "N3", "N1"
    if universe.select_atoms(" resid {0!s} ".format(i)).resnames[0] in [
        "DG",
        "DA",
        "A",
        "G",
        "ADE",
        "GUA",
    ]:
        a1, a2 = "N1", "N3"
    wc_dist = universe.select_atoms(
        "(segid {0!s} and resid {1!s} and name {2!s}) "
        "or (segid {3!s} and resid {4!s} and name {5!s}) ".format(
            seg1, i, a1, seg2, bp, a2
        )
    )
    wc = mdamath.norm(wc_dist[0].position - wc_dist[1].position)
    return wc


def minor_pair(universe, i, bp, seg1="SYSTEM", seg2="SYSTEM"):
    """Minor-Groove basepair distance for residue `i` with residue `bp`.

    The distance of the nitrogen and oxygen atoms in a Minor-groove hydrogen
    bond is computed.

    Parameters
    ----------
    universe : Universe
         :class:`~MDAnalysis.core.universe.Universe` containing the trajectory
    i : int
        resid of the first base
    bp : int
        resid of the second base
    seg1 : str (optional)
        segment id for first base ["SYSTEM"]
    seg2 : str (optional)
        segment id for second base ["SYSTEM"]

    Returns
    -------
    float
        Minor groove base pair distance

    Notes
    -----
    If failure occurs be sure to check the segment identification.


    .. versionadded:: 0.7.6
    """
    if universe.select_atoms(" resid {0!s} ".format(i)).resnames[0] in [
        "DC",
        "DT",
        "U",
        "C",
        "T",
        "CYT",
        "THY",
        "URA",
    ]:
        a1, a2 = "O2", "C2"
    if universe.select_atoms(" resid {0!s} ".format(i)).resnames[0] in [
        "DG",
        "DA",
        "A",
        "G",
        "ADE",
        "GUA",
    ]:
        a1, a2 = "C2", "O2"
    c2o2_dist = universe.select_atoms(
        "(segid {0!s} and resid {1!s} and name {2!s}) "
        "or (segid {3!s} and resid {4!s} and name {5!s})".format(
            seg1, i, a1, seg2, bp, a2
        )
    )
    c2o2 = mdamath.norm(c2o2_dist[0].position - c2o2_dist[1].position)
    return c2o2


def major_pair(universe, i, bp, seg1="SYSTEM", seg2="SYSTEM"):
    """Major-Groove basepair distance for residue `i` with residue `bp`.

    The distance of the nitrogen and oxygen atoms in a Major-groove hydrogen
    bond is computed.


    Parameters
    ----------
    universe : Universe
         :class:`~MDAnalysis.core.universe.Universe` containing the trajectory
    i : int
        resid of the first base
    bp : int
        resid of the second base
    seg1 : str (optional)
        segment id for first base ["SYSTEM"]
    seg2 : str (optional)
        segment id for second base ["SYSTEM"]

    Returns
    -------
    float
        Major groove base pair distance

    Notes
    -----
    If failure occurs be sure to check the segment identification.


    .. versionadded:: 0.7.6
    """
    if universe.select_atoms(" resid {0!s} ".format(i)).resnames[0] in [
        "DC",
        "DG",
        "C",
        "G",
        "CYT",
        "GUA",
    ]:
        if universe.select_atoms(" resid {0!s} ".format(i)).resnames[0] in [
            "DC",
            "C",
            "CYT",
        ]:
            a1, a2 = "N4", "O6"
        else:
            a1, a2 = "O6", "N4"
    if universe.select_atoms(" resid {0!s} ".format(i)).resnames[0] in [
        "DT",
        "DA",
        "A",
        "T",
        "U",
        "ADE",
        "THY",
        "URA",
    ]:
        if universe.select_atoms(" resid {0!s} ".format(i)).resnames[0] in [
            "DT",
            "T",
            "THY",
            "U",
            "URA",
        ]:
            a1, a2 = "O4", "N6"
        else:
            a1, a2 = "N6", "O4"
    no_dist = universe.select_atoms(
        "(segid {0!s} and resid {1!s} and name {2!s}) "
        "or (segid {3!s} and resid {4!s} and name {5!s}) ".format(
            seg1, i, a1, seg2, bp, a2
        )
    )
    major = mdamath.norm(no_dist[0].position - no_dist[1].position)
    return major


def phase_cp(universe, seg, i):
    """Pseudo-angle describing the phase of the ribose pucker for residue `i` using the CP method.

    The angle is computed by the positions of atoms in the ribose ring.


    Parameters
    ----------
    universe : Universe
         :class:`~MDAnalysis.core.universe.Universe` containing the trajectory
    seg : str
        segment id for base
    i : int
        resid of the first base

    Returns
    -------
    float
        phase angle in degrees


    .. versionadded:: 0.7.6
    """
    atom1 = universe.select_atoms(" atom {0!s} {1!s} O4' ".format(seg, i))
    atom2 = universe.select_atoms(" atom {0!s} {1!s} C1' ".format(seg, i))
    atom3 = universe.select_atoms(" atom {0!s} {1!s} C2' ".format(seg, i))
    atom4 = universe.select_atoms(" atom {0!s} {1!s} C3' ".format(seg, i))
    atom5 = universe.select_atoms(" atom {0!s} {1!s} C4' ".format(seg, i))

    data1 = atom1.positions
    data2 = atom2.positions
    data3 = atom3.positions
    data4 = atom4.positions
    data5 = atom5.positions

    r0 = (data1 + data2 + data3 + data4 + data5) * (1.0 / 5.0)
    r1 = data1 - r0
    r2 = data2 - r0
    r3 = data3 - r0
    r4 = data4 - r0
    r5 = data5 - r0

    R1 = (
        (r1 * sin(2 * pi * 0.0 / 5.0))
        + (r2 * sin(2 * pi * 1.0 / 5.0))
        + (r3 * sin(2 * pi * 2.0 / 5.0))
        + (r4 * sin(2 * pi * 3.0 / 5.0))
        + (r5 * sin(2 * pi * 4.0 / 5.0))
    )

    R2 = (
        (r1 * cos(2 * pi * 0.0 / 5.0))
        + (r2 * cos(2 * pi * 1.0 / 5.0))
        + (r3 * cos(2 * pi * 2.0 / 5.0))
        + (r4 * cos(2 * pi * 3.0 / 5.0))
        + (r5 * cos(2 * pi * 4.0 / 5.0))
    )

    x = np.cross(R1[0], R2[0])
    n = x / sqrt(pow(x[0], 2) + pow(x[1], 2) + pow(x[2], 2))

    r1_d = np.dot(r1, n)
    r2_d = np.dot(r2, n)
    r3_d = np.dot(r3, n)
    r4_d = np.dot(r4, n)
    r5_d = np.dot(r5, n)

    D = (
        (
            (r1_d * sin(4 * pi * 0.0 / 5.0))
            + (r2_d * sin(4 * pi * 1.0 / 5.0))
            + (r3_d * sin(4 * pi * 2.0 / 5.0))
            + (r4_d * sin(4 * pi * 3.0 / 5.0))
            + (r5_d * sin(4 * pi * 4.0 / 5.0))
        )
        * -1
        * sqrt(2.0 / 5.0)
    )

    C = (
        (r1_d * cos(4 * pi * 0.0 / 5.0))
        + (r2_d * cos(4 * pi * 1.0 / 5.0))
        + (r3_d * cos(4 * pi * 2.0 / 5.0))
        + (r4_d * cos(4 * pi * 3.0 / 5.0))
        + (r5_d * cos(4 * pi * 4.0 / 5.0))
    ) * sqrt(2.0 / 5.0)

    phase_ang = (np.arctan2(D, C) + (pi / 2.0)) * 180.0 / pi
    return phase_ang % 360


def phase_as(universe, seg, i):
    """Pseudo-angle describing the phase of the ribose pucker for residue `i` using the AS method

    The angle is computed by the position vector of atoms in the ribose ring.

    Parameters
    ----------
    universe : Universe
         :class:`~MDAnalysis.core.universe.Universe` containing the trajectory
    seg : str
        segment id for base
    i : int
        resid of the first base

    Returns
    -------
    float
        phase angle in degrees


    .. versionadded:: 0.7.6
    """
    angle1 = universe.select_atoms(
        " atom {0!s} {1!s} C1' ".format(seg, i),
        " atom {0!s} {1!s} C2' ".format(seg, i),
        " atom {0!s} {1!s} C3' ".format(seg, i),
        " atom {0!s} {1!s} C4' ".format(seg, i),
    )

    angle2 = universe.select_atoms(
        " atom {0!s} {1!s} C2' ".format(seg, i),
        " atom {0!s} {1!s} C3' ".format(seg, i),
        " atom {0!s} {1!s} C4' ".format(seg, i),
        " atom {0!s} {1!s} O4' ".format(seg, i),
    )

    angle3 = universe.select_atoms(
        " atom {0!s} {1!s} C3' ".format(seg, i),
        " atom {0!s} {1!s} C4' ".format(seg, i),
        " atom {0!s} {1!s} O4' ".format(seg, i),
        " atom {0!s} {1!s} C1' ".format(seg, i),
    )

    angle4 = universe.select_atoms(
        " atom {0!s} {1!s} C4' ".format(seg, i),
        " atom {0!s} {1!s} O4' ".format(seg, i),
        " atom {0!s} {1!s} C1' ".format(seg, i),
        " atom {0!s} {1!s} C2' ".format(seg, i),
    )

    angle5 = universe.select_atoms(
        " atom {0!s} {1!s} O4' ".format(seg, i),
        " atom {0!s} {1!s} C1' ".format(seg, i),
        " atom {0!s} {1!s} C2' ".format(seg, i),
        " atom {0!s} {1!s} C3' ".format(seg, i),
    )

    data1 = angle1.dihedral.value()
    data2 = angle2.dihedral.value()
    data3 = angle3.dihedral.value()
    data4 = angle4.dihedral.value()
    data5 = angle5.dihedral.value()

    B = (
        (
            (data1 * sin(2 * 2 * pi * (1 - 1.0) / 5.0))
            + (data2 * sin(2 * 2 * pi * (2 - 1.0) / 5.0))
            + (data3 * sin(2 * 2 * pi * (3 - 1.0) / 5.0))
            + (data4 * sin(2 * 2 * pi * (4 - 1.0) / 5.0))
            + (data5 * sin(2 * 2 * pi * (5 - 1.0) / 5.0))
        )
        * -2.0
        / 5.0
    )

    A = (
        (
            (data1 * cos(2 * 2 * pi * (1 - 1.0) / 5.0))
            + (data2 * cos(2 * 2 * pi * (2 - 1.0) / 5.0))
            + (data3 * cos(2 * 2 * pi * (3 - 1.0) / 5.0))
            + (data4 * cos(2 * 2 * pi * (4 - 1.0) / 5.0))
            + (data5 * cos(2 * 2 * pi * (5 - 1.0) / 5.0))
        )
        * 2.0
        / 5.0
    )

    phase_ang = np.arctan2(B, A) * 180.0 / pi
    return phase_ang % 360


def tors(universe, seg, i):
    """Calculation of nucleic backbone dihedral angles.

    The dihedral angles are alpha, beta, gamma, delta, epsilon, zeta, chi.

    The dihedral is computed based on position of atoms for resid `i`.

    Parameters
    ----------
    universe : Universe
         :class:`~MDAnalysis.core.universe.Universe` containing the trajectory
    seg : str
        segment id for base
    i : int
        resid of the first base

    Returns
    -------
    [alpha, beta, gamma, delta, epsilon, zeta, chi] : list of floats
        torsion angles in degrees

    Notes
    -----
    If failure occurs be sure to check the segment identification.


    .. versionadded:: 0.7.6

    """
    a = universe.select_atoms(
        " atom {0!s} {1!s} O3' ".format(seg, i - 1),
        " atom {0!s} {1!s} P  ".format(seg, i),
        " atom {0!s} {1!s} O5' ".format(seg, i),
        " atom {0!s} {1!s} C5' ".format(seg, i),
    )

    b = universe.select_atoms(
        " atom {0!s} {1!s} P    ".format(seg, i),
        " atom {0!s} {1!s} O5' ".format(seg, i),
        " atom {0!s} {1!s} C5' ".format(seg, i),
        " atom {0!s} {1!s} C4' ".format(seg, i),
    )

    g = universe.select_atoms(
        " atom {0!s} {1!s} O5' ".format(seg, i),
        " atom {0!s} {1!s} C5' ".format(seg, i),
        " atom {0!s} {1!s} C4' ".format(seg, i),
        " atom {0!s} {1!s} C3' ".format(seg, i),
    )

    d = universe.select_atoms(
        " atom {0!s} {1!s} C5' ".format(seg, i),
        " atom {0!s} {1!s} C4' ".format(seg, i),
        " atom {0!s} {1!s} C3' ".format(seg, i),
        " atom {0!s} {1!s} O3' ".format(seg, i),
    )

    e = universe.select_atoms(
        " atom {0!s} {1!s} C4' ".format(seg, i),
        " atom {0!s} {1!s} C3' ".format(seg, i),
        " atom {0!s} {1!s} O3' ".format(seg, i),
        " atom {0!s} {1!s} P    ".format(seg, i + 1),
    )

    z = universe.select_atoms(
        " atom {0!s} {1!s} C3' ".format(seg, i),
        " atom {0!s} {1!s} O3' ".format(seg, i),
        " atom {0!s} {1!s} P    ".format(seg, i + 1),
        " atom {0!s} {1!s} O5' ".format(seg, i + 1),
    )
    c = universe.select_atoms(
        " atom {0!s} {1!s} O4' ".format(seg, i),
        " atom {0!s} {1!s} C1' ".format(seg, i),
        " atom {0!s} {1!s} N9 ".format(seg, i),
        " atom {0!s} {1!s} C4  ".format(seg, i),
    )
    if len(c) < 4:
        c = universe.select_atoms(
            " atom {0!s} {1!s} O4' ".format(seg, i),
            " atom {0!s} {1!s} C1' ".format(seg, i),
            " atom {0!s} {1!s} N1 ".format(seg, i),
            " atom {0!s} {1!s} C2  ".format(seg, i),
        )

    alpha = a.dihedral.value() % 360
    beta = b.dihedral.value() % 360
    gamma = g.dihedral.value() % 360
    delta = d.dihedral.value() % 360
    epsilon = e.dihedral.value() % 360
    zeta = z.dihedral.value() % 360
    chi = c.dihedral.value() % 360

    return [alpha, beta, gamma, delta, epsilon, zeta, chi]


def tors_alpha(universe, seg, i):
    """alpha backbone dihedral

    The dihedral is computed based on position atoms for resid `i`.

    Parameters
    ----------
    universe : Universe
         :class:`~MDAnalysis.core.universe.Universe` containing the trajectory
    seg : str
        segment id for base
    i : int
        resid of the first base

    Returns
    -------
    alpha : float
        torsion angle in degrees


    .. versionadded:: 0.7.6
    """
    a = universe.select_atoms(
        " atom {0!s} {1!s} O3' ".format(seg, i - 1),
        " atom {0!s} {1!s} P  ".format(seg, i),
        " atom {0!s} {1!s} O5' ".format(seg, i),
        " atom {0!s} {1!s} C5' ".format(seg, i),
    )
    alpha = a.dihedral.value() % 360
    return alpha


def tors_beta(universe, seg, i):
    """beta  backbone dihedral

    The dihedral is computed based on position atoms for resid `i`.

    Parameters
    ----------
    universe : Universe
         :class:`~MDAnalysis.core.universe.Universe` containing the trajectory
    seg : str
        segment id for base
    i : int
        resid of the first base

    Returns
    -------
    beta : float
        torsion angle in degrees


    .. versionadded:: 0.7.6
    """
    b = universe.select_atoms(
        " atom {0!s} {1!s} P    ".format(seg, i),
        " atom {0!s} {1!s} O5' ".format(seg, i),
        " atom {0!s} {1!s} C5' ".format(seg, i),
        " atom {0!s} {1!s} C4' ".format(seg, i),
    )
    beta = b.dihedral.value() % 360
    return beta


def tors_gamma(universe, seg, i):
    """Gamma backbone dihedral

    The dihedral is computed based on position atoms for resid `i`.

    Parameters
    ----------
    universe : Universe
         :class:`~MDAnalysis.core.universe.Universe` containing the trajectory
    seg : str
        segment id for base
    i : int
        resid of the first base

    Returns
    -------
    gamma : float
        torsion angle in degrees


    .. versionadded:: 0.7.6
    """
    g = universe.select_atoms(
        " atom {0!s} {1!s} O5' ".format(seg, i),
        " atom {0!s} {1!s} C5' ".format(seg, i),
        " atom {0!s} {1!s} C4' ".format(seg, i),
        " atom {0!s} {1!s} C3' ".format(seg, i),
    )
    gamma = g.dihedral.value() % 360
    return gamma


def tors_delta(universe, seg, i):
    """delta backbone dihedral

    The dihedral is computed based on position atoms for resid `i`.

    Parameters
    ----------
    universe : Universe
         :class:`~MDAnalysis.core.universe.Universe` containing the trajectory
    seg : str
        segment id for base
    i : int
        resid of the first base

    Returns
    -------
    delta : float
        torsion angle in degrees


    .. versionadded:: 0.7.6
    """
    d = universe.select_atoms(
        " atom {0!s} {1!s} C5' ".format(seg, i),
        " atom {0!s} {1!s} C4' ".format(seg, i),
        " atom {0!s} {1!s} C3' ".format(seg, i),
        " atom {0!s} {1!s} O3' ".format(seg, i),
    )
    delta = d.dihedral.value() % 360
    return delta


def tors_eps(universe, seg, i):
    """Epsilon backbone dihedral

    The dihedral is computed based on position atoms for resid `i`.

    Parameters
    ----------
    universe : Universe
         :class:`~MDAnalysis.core.universe.Universe` containing the trajectory
    seg : str
        segment id for base
    i : int
        resid of the first base

    Returns
    -------
    epsilon : float
        torsion angle in degrees


    .. versionadded:: 0.7.6
    """
    e = universe.select_atoms(
        " atom {0!s} {1!s} C4' ".format(seg, i),
        " atom {0!s} {1!s} C3' ".format(seg, i),
        " atom {0!s} {1!s} O3' ".format(seg, i),
        " atom {0!s} {1!s} P    ".format(seg, i + 1),
    )
    epsilon = e.dihedral.value() % 360
    return epsilon


def tors_zeta(universe, seg, i):
    """Zeta backbone dihedral

    The dihedral is computed based on position atoms for resid `i`.

    Parameters
    ----------
    universe : Universe
         :class:`~MDAnalysis.core.universe.Universe` containing the trajectory
    seg : str
        segment id for base
    i : int
        resid of the first base

    Returns
    -------
    zeta : float
        torsion angle in degrees


    .. versionadded:: 0.7.6
    """
    z = universe.select_atoms(
        " atom {0!s} {1!s} C3' ".format(seg, i),
        " atom {0!s} {1!s} O3' ".format(seg, i),
        " atom {0!s} {1!s} P    ".format(seg, i + 1),
        " atom {0!s} {1!s} O5' ".format(seg, i + 1),
    )
    zeta = z.dihedral.value() % 360
    return zeta


def tors_chi(universe, seg, i):
    """chi nucleic acid dihedral

     The dihedral is computed based on position atoms for resid `i`.

    Parameters
    ----------
    universe : Universe
         :class:`~MDAnalysis.core.universe.Universe` containing the trajectory
    seg : str
        segment id for base
    i : int
        resid of the first base

    Returns
    -------
    chi : float
        torsion angle in degrees


    .. versionadded:: 0.7.6
    """
    c = universe.select_atoms(
        " atom {0!s} {1!s} O4' ".format(seg, i),
        " atom {0!s} {1!s} C1' ".format(seg, i),
        " atom {0!s} {1!s} N9 ".format(seg, i),
        " atom {0!s} {1!s} C4  ".format(seg, i),
    )
    if len(c) < 4:
        c = universe.select_atoms(
            " atom {0!s} {1!s} O4' ".format(seg, i),
            " atom {0!s} {1!s} C1' ".format(seg, i),
            " atom {0!s} {1!s} N1 ".format(seg, i),
            " atom {0!s} {1!s} C2  ".format(seg, i),
        )
    chi = c.dihedral.value() % 360
    return chi


def hydroxyl(universe, seg, i):
    """2-hydroxyl dihedral. Useful only for RNA calculations.

     .. Note:: This dihedral calculation will only work if using atom
               names as documented by charmm force field parameters,
               namely "C1', C2', O2', H2'".

    Parameters
    ----------
    universe : Universe
         :class:`~MDAnalysis.core.universe.Universe` containing the trajectory
    seg : str
        segment id for base
    i : int
        resid of the first base

    Returns
    -------
    hydroxyl_angle : float
        torsion angle in degrees


    .. versionadded:: 0.7.6

    """
    h = universe.select_atoms(
        "atom {0!s} {1!s} C1'".format(seg, i),
        "atom {0!s} {1!s} C2'".format(seg, i),
        "atom {0!s} {1!s} O2'".format(seg, i),
        "atom {0!s} {1!s} H2'".format(seg, i),
    )
    try:
        hydr = h.dihedral.value() % 360
    except ValueError:
        errmsg = (
            f"Resid {i} does not contain atoms C1', C2', O2', H2' but "
            f"atoms {list(h.atoms)}"
        )
        raise ValueError(errmsg) from None

    return hydr


def pseudo_dihe_baseflip(
    universe, bp1, bp2, i, seg1="SYSTEM", seg2="SYSTEM", seg3="SYSTEM"
):
    """pseudo dihedral for flipped bases. Useful only for nucleic acid base flipping

    The dihedral is computed based on position atoms for resid `i`

    .. Note:: This dihedral calculation will only work if using atom names as
              documented by charmm force field parameters.

    Parameters
    ----------
    universe : Universe
        :class:`~MDAnalysis.core.universe.Universe` containing the
        trajectory
    bp1 : int
        resid that base pairs with `bp2`
    bp2 : int
        resid below the base that flips
    i : int
        resid of the base that flips
    segid1 : str (optional)
        segid of resid base pairing with `bp2`
    segid2 : str (optional)
        segid, same as that of segid of flipping resid `i`
    segid3 : str (optional)
        segid of resid `i` that flips

    Returns
    -------
    float
          pseudo dihedral angle in degrees


    .. versionadded:: 0.8.0
    """
    bf1 = universe.select_atoms(
        " ( segid {0!s} and resid {1!s} and nucleicbase ) "
        "or ( segid {2!s} and resid {3!s} and nucleicbase ) ".format(
            seg1, bp1, seg2, bp2
        )
    )
    bf4 = universe.select_atoms(
        "(segid {0!s} and resid {1!s} and nucleicbase) ".format(seg3, i)
    )
    bf2 = universe.select_atoms(
        "(segid {0!s} and resid {1!s} and nucleicsugar) ".format(seg2, bp2)
    )
    bf3 = universe.select_atoms(
        "(segid {0!s} and resid {1!s} and nucleicsugar) ".format(seg3, i)
    )
    x = [
        bf1.center_of_mass(),
        bf2.center_of_mass(),
        bf3.center_of_mass(),
        bf4.center_of_mass(),
    ]
    pseudo = mdamath.dihedral(x[0] - x[1], x[1] - x[2], x[2] - x[3])
    pseudo = np.rad2deg(pseudo) % 360
    return pseudo