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 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003
// Chemfiles, a modern library for chemistry file reading and writing
// Copyright (C) 2015-2018 Guillaume Fraux -- BSD licensed
use std::marker::PhantomData;
use std::ops::{Deref, Drop};
use chemfiles_sys as ffi;
use crate::errors::{check, check_not_null, check_success, Error};
use crate::{Atom, AtomMut, AtomRef};
use crate::{Residue, ResidueRef};
/// Possible bond order associated with bonds
#[repr(C)]
#[non_exhaustive]
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
pub enum BondOrder {
/// Unknown or unspecified bond order
Unknown = ffi::chfl_bond_order::CHFL_BOND_UNKNOWN as isize,
/// Single bond
Single = ffi::chfl_bond_order::CHFL_BOND_SINGLE as isize,
/// Double bond
Double = ffi::chfl_bond_order::CHFL_BOND_DOUBLE as isize,
/// Triple bond
Triple = ffi::chfl_bond_order::CHFL_BOND_TRIPLE as isize,
/// Quadruple bond (present in some metals)
Quadruple = ffi::chfl_bond_order::CHFL_BOND_QUADRUPLE as isize,
/// Quintuplet bond (present in some metals)
Quintuplet = ffi::chfl_bond_order::CHFL_BOND_QUINTUPLET as isize,
/// Amide bond (required by some file formats)
Amide = ffi::chfl_bond_order::CHFL_BOND_AMIDE as isize,
/// Aromatic bond (required by some file formats)
Aromatic = ffi::chfl_bond_order::CHFL_BOND_AROMATIC as isize,
}
impl BondOrder {
pub(crate) fn as_raw(self) -> ffi::chfl_bond_order {
match self {
BondOrder::Unknown => ffi::chfl_bond_order::CHFL_BOND_UNKNOWN,
BondOrder::Single => ffi::chfl_bond_order::CHFL_BOND_SINGLE,
BondOrder::Double => ffi::chfl_bond_order::CHFL_BOND_DOUBLE,
BondOrder::Triple => ffi::chfl_bond_order::CHFL_BOND_TRIPLE,
BondOrder::Quadruple => ffi::chfl_bond_order::CHFL_BOND_QUADRUPLE,
BondOrder::Quintuplet => ffi::chfl_bond_order::CHFL_BOND_QUINTUPLET,
BondOrder::Amide => ffi::chfl_bond_order::CHFL_BOND_AMIDE,
BondOrder::Aromatic => ffi::chfl_bond_order::CHFL_BOND_AROMATIC,
}
}
}
impl From<ffi::chfl_bond_order> for BondOrder {
fn from(order: ffi::chfl_bond_order) -> BondOrder {
match order {
ffi::chfl_bond_order::CHFL_BOND_UNKNOWN => BondOrder::Unknown,
ffi::chfl_bond_order::CHFL_BOND_SINGLE => BondOrder::Single,
ffi::chfl_bond_order::CHFL_BOND_DOUBLE => BondOrder::Double,
ffi::chfl_bond_order::CHFL_BOND_TRIPLE => BondOrder::Triple,
ffi::chfl_bond_order::CHFL_BOND_QUADRUPLE => BondOrder::Quadruple,
ffi::chfl_bond_order::CHFL_BOND_QUINTUPLET => BondOrder::Quintuplet,
ffi::chfl_bond_order::CHFL_BOND_AMIDE => BondOrder::Amide,
ffi::chfl_bond_order::CHFL_BOND_AROMATIC => BondOrder::Aromatic,
}
}
}
/// A `Topology` contains the definition of all the atoms in the system, and
/// the liaisons between the atoms (bonds, angles, dihedrals, ...). It will
/// also contain all the residues information if it is available.
#[derive(Debug)]
pub struct Topology {
handle: *mut ffi::CHFL_TOPOLOGY,
}
/// An analog to a reference to a topology (`&Topology`)
#[derive(Debug)]
pub struct TopologyRef<'a> {
inner: Topology,
marker: PhantomData<&'a Topology>,
}
impl<'a> Deref for TopologyRef<'a> {
type Target = Topology;
fn deref(&self) -> &Topology {
&self.inner
}
}
impl Clone for Topology {
fn clone(&self) -> Topology {
unsafe {
let new_handle = ffi::chfl_topology_copy(self.as_ptr());
Topology::from_ptr(new_handle)
}
}
}
impl Topology {
/// Create a `Topology` from a C pointer.
///
/// This function is unsafe because no validity check is made on the pointer,
/// except for it being non-null.
#[inline]
pub(crate) unsafe fn from_ptr(ptr: *mut ffi::CHFL_TOPOLOGY) -> Topology {
check_not_null(ptr);
Topology { handle: ptr }
}
/// Create a borrowed `Topology` from a C pointer.
///
/// This function is unsafe because no validity check is made on the
/// pointer, except for it being non-null, and the caller is responsible
/// for setting the right lifetime
#[inline]
#[allow(clippy::ptr_cast_constness)]
pub(crate) unsafe fn ref_from_ptr<'a>(ptr: *const ffi::CHFL_TOPOLOGY) -> TopologyRef<'a> {
TopologyRef {
inner: Topology::from_ptr(ptr as *mut ffi::CHFL_TOPOLOGY),
marker: PhantomData,
}
}
/// Get the underlying C pointer as a const pointer.
#[inline]
pub(crate) fn as_ptr(&self) -> *const ffi::CHFL_TOPOLOGY {
self.handle
}
/// Get the underlying C pointer as a mutable pointer.
#[inline]
pub(crate) fn as_mut_ptr(&mut self) -> *mut ffi::CHFL_TOPOLOGY {
self.handle
}
/// Get the underlying C pointer as a mutable pointer FROM A SHARED REFERENCE.
///
/// For uses with functions of the C API using mut pointers for both read
/// and write access. Users should check that this does not lead to multiple
/// mutable borrows
#[inline]
#[allow(non_snake_case)]
pub(crate) fn as_mut_ptr_MANUALLY_CHECKING_BORROW(&self) -> *mut ffi::CHFL_TOPOLOGY {
self.handle
}
/// Create a new empty topology.
///
/// # Example
/// ```
/// # use chemfiles::Topology;
/// let topology = Topology::new();
/// assert_eq!(topology.size(), 0);
/// ```
pub fn new() -> Topology {
unsafe { Topology::from_ptr(ffi::chfl_topology()) }
}
/// Get a reference of the atom at the given `index` in this topology.
///
/// # Panics
///
/// If `index` is out of bounds.
///
/// # Example
/// ```
/// # use chemfiles::Topology;
/// let mut topology = Topology::new();
/// topology.resize(6);
///
/// let atom = topology.atom(4);
/// assert_eq!(atom.name(), "");
/// ```
pub fn atom(&self, index: usize) -> AtomRef {
unsafe {
let handle = ffi::chfl_atom_from_topology(self.as_mut_ptr_MANUALLY_CHECKING_BORROW(), index as u64);
Atom::ref_from_ptr(handle)
}
}
/// Get a mutable reference to the atom at the given `index` in this topology.
///
/// # Panics
///
/// If `index` is out of bounds.
///
/// # Example
/// ```
/// # use chemfiles::Topology;
/// let mut topology = Topology::new();
/// topology.resize(6);
///
/// assert_eq!(topology.atom(4).name(), "");
///
/// topology.atom_mut(4).set_name("Fe");
/// assert_eq!(topology.atom(4).name(), "Fe");
/// ```
pub fn atom_mut(&mut self, index: usize) -> AtomMut {
unsafe {
let handle = ffi::chfl_atom_from_topology(self.as_mut_ptr(), index as u64);
Atom::ref_mut_from_ptr(handle)
}
}
/// Get the current number of atoms in this topology.
///
/// # Example
/// ```
/// # use chemfiles::Topology;
/// let mut topology = Topology::new();
/// assert_eq!(topology.size(), 0);
///
/// topology.resize(6);
/// assert_eq!(topology.size(), 6);
/// ```
pub fn size(&self) -> usize {
let mut size = 0;
unsafe {
check_success(ffi::chfl_topology_atoms_count(self.as_ptr(), &mut size));
}
#[allow(clippy::cast_possible_truncation)]
return size as usize;
}
/// Resize this topology to hold `natoms` atoms, inserting dummy atoms if
/// the new size if bigger than the old one.
///
/// # Example
/// ```
/// # use chemfiles::Topology;
/// let mut topology = Topology::new();
/// assert_eq!(topology.size(), 0);
///
/// topology.resize(6);
/// assert_eq!(topology.size(), 6);
/// ```
pub fn resize(&mut self, natoms: usize) {
unsafe {
check_success(ffi::chfl_topology_resize(self.as_mut_ptr(), natoms as u64));
}
}
/// Add an `Atom` at the end of this topology
///
/// # Example
/// ```
/// # use chemfiles::{Topology, Atom};
/// let mut topology = Topology::new();
/// topology.add_atom(&Atom::new("Mg"));
///
/// let atom = topology.atom(0);
/// assert_eq!(atom.name(), "Mg");
/// ```
pub fn add_atom(&mut self, atom: &Atom) {
unsafe {
check_success(ffi::chfl_topology_add_atom(self.as_mut_ptr(), atom.as_ptr()));
}
}
/// Remove an `Atom` from this topology by `index`. This modify all the
/// other atoms indexes.
///
/// # Panics
///
/// If the `index` is out of bounds
///
/// # Example
/// ```
/// # use chemfiles::Topology;
/// let mut topology = Topology::new();
/// topology.resize(9);
/// assert_eq!(topology.size(), 9);
///
/// topology.remove(7);
/// assert_eq!(topology.size(), 8);
/// ```
pub fn remove(&mut self, index: usize) {
unsafe {
check_success(ffi::chfl_topology_remove(self.as_mut_ptr(), index as u64));
}
}
/// Get the number of bonds in the topology.
///
/// # Example
/// ```
/// # use chemfiles::Topology;
/// let mut topology = Topology::new();
/// assert_eq!(topology.bonds_count(), 0);
/// topology.resize(4);
///
/// topology.add_bond(0, 1);
/// topology.add_bond(2, 1);
/// topology.add_bond(2, 3);
/// assert_eq!(topology.bonds_count(), 3);
/// ```
pub fn bonds_count(&self) -> usize {
let mut count = 0;
unsafe {
check_success(ffi::chfl_topology_bonds_count(self.as_ptr(), &mut count));
}
#[allow(clippy::cast_possible_truncation)]
return count as usize;
}
/// Get the number of angles in the topology.
///
/// # Example
/// ```
/// # use chemfiles::Topology;
/// let mut topology = Topology::new();
/// assert_eq!(topology.angles_count(), 0);
/// topology.resize(4);
///
/// topology.add_bond(0, 1);
/// topology.add_bond(2, 1);
/// topology.add_bond(2, 3);
/// assert_eq!(topology.angles_count(), 2);
/// ```
pub fn angles_count(&self) -> usize {
let mut count = 0;
unsafe {
check_success(ffi::chfl_topology_angles_count(self.as_ptr(), &mut count));
}
#[allow(clippy::cast_possible_truncation)]
return count as usize;
}
/// Get the number of dihedral angles in the topology.
///
/// # Example
/// ```
/// # use chemfiles::Topology;
/// let mut topology = Topology::new();
/// assert_eq!(topology.dihedrals_count(), 0);
/// topology.resize(4);
///
/// topology.add_bond(0, 1);
/// topology.add_bond(2, 1);
/// topology.add_bond(2, 3);
/// assert_eq!(topology.dihedrals_count(), 1);
/// ```
pub fn dihedrals_count(&self) -> usize {
let mut count = 0;
unsafe {
check_success(ffi::chfl_topology_dihedrals_count(self.as_ptr(), &mut count));
}
#[allow(clippy::cast_possible_truncation)]
return count as usize;
}
/// Get the number of improper dihedral angles in the topology.
///
/// # Example
/// ```
/// # use chemfiles::Topology;
/// let mut topology = Topology::new();
/// assert_eq!(topology.dihedrals_count(), 0);
/// topology.resize(4);
///
/// topology.add_bond(0, 1);
/// topology.add_bond(0, 2);
/// topology.add_bond(0, 3);
/// assert_eq!(topology.impropers_count(), 1);
/// ```
pub fn impropers_count(&self) -> usize {
let mut count = 0;
unsafe {
check_success(ffi::chfl_topology_impropers_count(self.as_ptr(), &mut count));
}
#[allow(clippy::cast_possible_truncation)]
return count as usize;
}
/// Get the list of bonds in the topology.
///
/// # Example
/// ```
/// # use chemfiles::Topology;
/// let mut topology = Topology::new();
/// topology.resize(4);
///
/// topology.add_bond(0, 1);
/// topology.add_bond(2, 1);
/// topology.add_bond(2, 3);
/// assert_eq!(topology.bonds(), vec![[0, 1], [1, 2], [2, 3]]);
/// ```
pub fn bonds(&self) -> Vec<[usize; 2]> {
let size = self.bonds_count();
let count = size as u64;
let mut bonds = vec![[u64::max_value(); 2]; size];
unsafe {
check_success(ffi::chfl_topology_bonds(self.as_ptr(), bonds.as_mut_ptr(), count));
}
#[allow(clippy::cast_possible_truncation)]
return bonds
.into_iter()
.map(|bond| [bond[0] as usize, bond[1] as usize])
.collect();
}
/// Get the list of angles in the topology.
///
/// # Example
/// ```
/// # use chemfiles::Topology;
/// let mut topology = Topology::new();
/// topology.resize(4);
///
/// topology.add_bond(0, 1);
/// topology.add_bond(2, 1);
/// topology.add_bond(2, 3);
/// assert_eq!(topology.angles(), vec![[0, 1, 2], [1, 2, 3]]);
/// ```
pub fn angles(&self) -> Vec<[usize; 3]> {
let size = self.angles_count();
let count = size as u64;
let mut angles = vec![[u64::max_value(); 3]; size];
unsafe {
check_success(ffi::chfl_topology_angles(self.as_ptr(), angles.as_mut_ptr(), count));
}
#[allow(clippy::cast_possible_truncation)]
return angles
.into_iter()
.map(|angle| [angle[0] as usize, angle[1] as usize, angle[2] as usize])
.collect();
}
/// Get the list of dihedral angles in the topology.
///
/// # Example
/// ```
/// # use chemfiles::Topology;
/// let mut topology = Topology::new();
/// topology.resize(4);
///
/// topology.add_bond(0, 1);
/// topology.add_bond(2, 1);
/// topology.add_bond(2, 3);
///
/// assert_eq!(topology.dihedrals(), vec![[0, 1, 2, 3]]);
/// ```
pub fn dihedrals(&self) -> Vec<[usize; 4]> {
let size = self.dihedrals_count();
let count = size as u64;
let mut dihedrals = vec![[u64::max_value(); 4]; size];
unsafe {
check_success(ffi::chfl_topology_dihedrals(
self.as_ptr(),
dihedrals.as_mut_ptr(),
count,
));
}
#[allow(clippy::cast_possible_truncation)]
return dihedrals
.into_iter()
.map(|dihedral| {
[
dihedral[0] as usize,
dihedral[1] as usize,
dihedral[2] as usize,
dihedral[3] as usize,
]
})
.collect();
}
/// Get the list of improper dihedral angles in the topology.
///
/// # Example
/// ```
/// # use chemfiles::Topology;
/// let mut topology = Topology::new();
/// topology.resize(4);
///
/// topology.add_bond(0, 1);
/// topology.add_bond(0, 2);
/// topology.add_bond(0, 3);
///
/// assert_eq!(topology.impropers(), vec![[1, 0, 2, 3]]);
/// ```
pub fn impropers(&self) -> Vec<[usize; 4]> {
let size = self.impropers_count();
let count = size as u64;
let mut impropers = vec![[u64::max_value(); 4]; size];
unsafe {
check_success(ffi::chfl_topology_impropers(
self.as_ptr(),
impropers.as_mut_ptr(),
count,
));
}
#[allow(clippy::cast_possible_truncation)]
return impropers
.into_iter()
.map(|improper| {
[
improper[0] as usize,
improper[1] as usize,
improper[2] as usize,
improper[3] as usize,
]
})
.collect();
}
/// Remove all existing bonds, angles, dihedral angles and improper
/// dihedral angles in the topology.
///
/// # Example
/// ```
/// # use chemfiles::Topology;
/// let mut topology = Topology::new();
/// assert_eq!(topology.bonds_count(), 0);
/// topology.resize(4);
/// topology.add_bond(0, 1);
/// topology.add_bond(0, 2);
/// assert_eq!(topology.bonds_count(), 2);
/// assert_eq!(topology.angles().len(), 1);
///
/// topology.clear_bonds();
/// assert!(topology.bonds().is_empty());
/// assert!(topology.angles().is_empty());
/// ```
pub fn clear_bonds(&mut self) {
unsafe {
check_success(ffi::chfl_topology_clear_bonds(self.as_mut_ptr()));
}
}
/// Add a bond between the atoms at indexes `i` and `j` in the topology.
///
/// The bond order is set to `BondOrder::Unknown`.
///
/// # Example
/// ```
/// # use chemfiles::{Topology, BondOrder};
/// let mut topology = Topology::new();
/// assert_eq!(topology.bonds_count(), 0);
/// topology.resize(4);
///
/// topology.add_bond(0, 1);
/// topology.add_bond(0, 2);
/// assert_eq!(topology.bonds_count(), 2);
///
/// assert_eq!(topology.bond_order(0, 1), BondOrder::Unknown);
/// ```
pub fn add_bond(&mut self, i: usize, j: usize) {
unsafe {
check_success(ffi::chfl_topology_add_bond(self.as_mut_ptr(), i as u64, j as u64));
}
}
/// Add a bond between the atoms at indexes `i` and `j` in the topology
/// with the given bond `order`.
///
/// # Example
/// ```
/// # use chemfiles::{Topology, BondOrder};
/// let mut topology = Topology::new();
/// assert_eq!(topology.bonds_count(), 0);
/// topology.resize(2);
///
/// topology.add_bond_with_order(0, 1, BondOrder::Double);
/// assert_eq!(topology.bond_order(0, 1), BondOrder::Double);
/// ```
pub fn add_bond_with_order(&mut self, i: usize, j: usize, order: BondOrder) {
unsafe {
check_success(ffi::chfl_topology_bond_with_order(
self.as_mut_ptr(),
i as u64,
j as u64,
order.as_raw(),
));
}
}
/// Get the bond order for the bond between the atoms at indexes `i` and
/// `j`.
///
/// # Example
/// ```
/// # use chemfiles::{Topology, BondOrder};
/// let mut topology = Topology::new();
/// assert_eq!(topology.bonds_count(), 0);
/// topology.resize(2);
///
/// topology.add_bond_with_order(0, 1, BondOrder::Double);
/// assert_eq!(topology.bond_order(0, 1), BondOrder::Double);
/// ```
pub fn bond_order(&self, i: usize, j: usize) -> BondOrder {
let mut order = ffi::chfl_bond_order::CHFL_BOND_UNKNOWN;
unsafe {
check_success(ffi::chfl_topology_bond_order(
self.as_ptr(),
i as u64,
j as u64,
&mut order,
));
}
return order.into();
}
/// Get the bond order for all the bonds in the topology
///
/// # Example
/// ```
/// # use chemfiles::{Topology, BondOrder};
/// let mut topology = Topology::new();
/// assert_eq!(topology.bonds_count(), 0);
/// topology.resize(3);
///
/// topology.add_bond_with_order(0, 1, BondOrder::Double);
/// topology.add_bond_with_order(0, 2, BondOrder::Single);
///
/// assert_eq!(topology.bond_orders(), &[BondOrder::Double, BondOrder::Single]);
/// ```
pub fn bond_orders(&self) -> Vec<BondOrder> {
let size = self.bonds_count();
let count = size as u64;
let mut bonds = vec![BondOrder::Unknown; size];
unsafe {
check_success(ffi::chfl_topology_bond_orders(
self.as_ptr(),
// Casting BondOrder to ffi::chfl_bond_order is safe, as they are
// both `repr(C)` enums with the same values.
bonds.as_mut_ptr().cast(),
count,
));
}
return bonds;
}
/// Remove any existing bond between the atoms at indexes `i` and `j` in
/// this topology.
///
/// This function does nothing if there is no bond between `i` and `j`.
///
/// # Example
/// ```
/// # use chemfiles::Topology;
/// let mut topology = Topology::new();
/// assert_eq!(topology.bonds_count(), 0);
/// topology.resize(4);
///
/// topology.add_bond(0, 1);
/// topology.add_bond(1, 2);
/// assert_eq!(topology.bonds_count(), 2);
///
/// topology.remove_bond(0, 1);
/// assert_eq!(topology.bonds_count(), 1);
///
/// // Removing a bond that does not exists is fine
/// topology.remove_bond(0, 2);
/// assert_eq!(topology.bonds_count(), 1);
/// ```
pub fn remove_bond(&mut self, i: usize, j: usize) {
unsafe {
check_success(ffi::chfl_topology_remove_bond(self.as_mut_ptr(), i as u64, j as u64));
}
}
/// Get a reference to the residue at index `index` from this topology.
///
/// The residue index in the topology is not always the same as the residue
/// `id`.
///
/// # Example
/// ```
/// # use chemfiles::{Topology, Residue};
/// let mut topology = Topology::new();
/// topology.add_residue(&Residue::new("water")).unwrap();
///
/// let residue = topology.residue(0).unwrap();
/// assert_eq!(residue.name(), "water");
/// ```
pub fn residue(&self, index: usize) -> Option<ResidueRef> {
unsafe {
let handle = ffi::chfl_residue_from_topology(self.as_ptr(), index as u64);
if handle.is_null() {
None
} else {
Some(Residue::ref_from_ptr(handle))
}
}
}
/// Get a copy of the residue containing the atom at index `index` in this
/// topology, if any.
///
/// # Example
/// ```
/// # use chemfiles::{Topology, Residue};
/// let mut topology = Topology::new();
/// topology.resize(8);
///
/// let mut residue = Residue::new("water");
/// residue.add_atom(0);
/// residue.add_atom(1);
/// residue.add_atom(2);
/// topology.add_residue(&residue).unwrap();
///
/// let residue = topology.residue_for_atom(0).unwrap();
/// assert_eq!(residue.name(), "water");
///
/// assert!(topology.residue_for_atom(6).is_none());
/// ```
pub fn residue_for_atom(&self, index: usize) -> Option<ResidueRef> {
let handle = unsafe { ffi::chfl_residue_for_atom(self.as_ptr(), index as u64) };
if handle.is_null() {
None
} else {
unsafe { Some(Residue::ref_from_ptr(handle)) }
}
}
/// Get the number of residues in this topology.
///
/// # Example
/// ```
/// # use chemfiles::{Topology, Residue};
/// let mut topology = Topology::new();
/// assert_eq!(topology.residues_count(), 0);
///
/// topology.add_residue(&Residue::with_id("water", 0)).unwrap();
/// topology.add_residue(&Residue::with_id("protein", 1)).unwrap();
/// assert_eq!(topology.residues_count(), 2);
/// ```
pub fn residues_count(&self) -> u64 {
let mut count = 0;
unsafe {
check_success(ffi::chfl_topology_residues_count(self.as_ptr(), &mut count));
}
return count;
}
/// Add a residue to this topology.
///
/// # Errors
///
/// This function fails is the residue `id` is not already in the topology,
/// or if the residue contains atoms that are already in another residue.
///
/// # Example
/// ```
/// # use chemfiles::{Topology, Residue};
/// let mut topology = Topology::new();
/// topology.add_residue(&Residue::new("water")).unwrap();
///
/// let residue = topology.residue(0).unwrap();
/// assert_eq!(residue.name(), "water");
/// ```
pub fn add_residue(&mut self, residue: &Residue) -> Result<(), Error> {
unsafe { check(ffi::chfl_topology_add_residue(self.as_mut_ptr(), residue.as_ptr())) }
}
/// Check if the two residues `first` and `second` from the `topology` are
/// linked together, *i.e.* if there is a bond between one atom in the
/// first residue and one atom in the second one.
///
/// # Example
/// ```
/// # use chemfiles::{Topology, Residue};
/// let mut topology = Topology::new();
///
/// topology.add_residue(&Residue::with_id("water", 0)).unwrap();
/// topology.add_residue(&Residue::with_id("protein", 1)).unwrap();
///
/// let first = topology.residue(0).unwrap();
/// let second = topology.residue(1).unwrap();
/// assert_eq!(topology.are_linked(&first, &second), false);
/// ```
pub fn are_linked(&self, first: &Residue, second: &Residue) -> bool {
let mut linked = 0;
unsafe {
check_success(ffi::chfl_topology_residues_linked(
self.as_ptr(),
first.as_ptr(),
second.as_ptr(),
&mut linked,
));
}
return linked != 0;
}
}
impl Drop for Topology {
fn drop(&mut self) {
unsafe {
let _ = ffi::chfl_free(self.as_ptr().cast());
}
}
}
#[cfg(test)]
mod test {
use super::*;
#[test]
fn clone() {
let mut topology = Topology::new();
assert_eq!(topology.size(), 0);
let copy = topology.clone();
assert_eq!(copy.size(), 0);
topology.resize(10);
assert_eq!(topology.size(), 10);
assert_eq!(copy.size(), 0);
}
#[test]
fn size() {
let mut topology = Topology::new();
assert_eq!(topology.size(), 0);
topology.resize(10);
assert_eq!(topology.size(), 10);
topology.remove(7);
assert_eq!(topology.size(), 9);
topology.add_atom(&Atom::new("Hg"));
assert_eq!(topology.size(), 10);
}
#[test]
fn atoms() {
let mut topology = Topology::new();
topology.add_atom(&Atom::new("Hg"));
topology.add_atom(&Atom::new("Mn"));
topology.add_atom(&Atom::new("W"));
topology.add_atom(&Atom::new("Fe"));
assert_eq!(topology.atom(0).name(), "Hg");
assert_eq!(topology.atom(3).name(), "Fe");
}
#[test]
fn remove() {
let mut topology = Topology::new();
topology.add_atom(&Atom::new("Hg"));
topology.add_atom(&Atom::new("Mn"));
topology.add_atom(&Atom::new("W"));
topology.add_atom(&Atom::new("Fe"));
assert_eq!(topology.atom(0).name(), "Hg");
assert_eq!(topology.atom(2).name(), "W");
topology.remove(1);
assert_eq!(topology.atom(0).name(), "Hg");
assert_eq!(topology.atom(2).name(), "Fe");
}
#[test]
#[should_panic(expected = "out of bounds atomic index in `Topology::remove`")]
fn out_of_bounds_remove() {
let mut topology = Topology::new();
topology.resize(18);
topology.remove(33);
}
#[test]
fn bonds() {
let mut topology = Topology::new();
topology.resize(12);
assert_eq!(topology.bonds_count(), 0);
topology.add_bond(0, 1);
topology.add_bond(9, 2);
topology.add_bond_with_order(3, 7, BondOrder::Aromatic);
assert_eq!(topology.bonds_count(), 3);
assert_eq!(topology.bonds(), vec![[0, 1], [2, 9], [3, 7]]);
let expected = vec![BondOrder::Unknown, BondOrder::Unknown, BondOrder::Aromatic];
assert_eq!(topology.bond_orders(), expected);
assert_eq!(topology.bond_order(0, 1), BondOrder::Unknown);
assert_eq!(topology.bond_order(3, 7), BondOrder::Aromatic);
topology.remove_bond(3, 7);
// Removing unexisting bond is OK if both indexes are in bounds
topology.remove_bond(8, 7);
assert_eq!(topology.bonds_count(), 2);
topology.clear_bonds();
assert_eq!(topology.bonds_count(), 0);
}
#[test]
#[should_panic(expected = "out of bounds atomic index in `Topology::add_bond`")]
fn out_of_bounds_bonds() {
let mut topology = Topology::new();
topology.resize(12);
topology.add_bond(300, 7);
}
#[test]
#[should_panic(expected = "out of bounds atomic index in `Topology::remove_bond`")]
fn out_of_bounds_remove_bond() {
let mut topology = Topology::new();
topology.resize(12);
topology.remove_bond(300, 7);
}
#[test]
#[should_panic(expected = "out of bounds atomic index in `Topology::add_bond`")]
fn out_of_bounds_bonds_with_order() {
let mut topology = Topology::new();
topology.resize(12);
topology.add_bond_with_order(300, 7, BondOrder::Unknown);
}
#[test]
fn angles() {
let mut topology = Topology::new();
topology.resize(12);
assert_eq!(topology.angles_count(), 0);
topology.add_bond(0, 1);
topology.add_bond(1, 2);
topology.add_bond(3, 7);
topology.add_bond(3, 5);
assert_eq!(topology.angles_count(), 2);
assert_eq!(topology.angles(), vec![[0, 1, 2], [5, 3, 7]]);
topology.clear_bonds();
assert_eq!(topology.angles_count(), 0);
}
#[test]
fn dihedrals() {
let mut topology = Topology::new();
topology.resize(12);
assert_eq!(topology.dihedrals_count(), 0);
topology.add_bond(0, 1);
topology.add_bond(1, 2);
topology.add_bond(3, 2);
topology.add_bond(4, 7);
topology.add_bond(4, 5);
topology.add_bond(7, 10);
assert_eq!(topology.dihedrals_count(), 2);
assert_eq!(topology.dihedrals(), vec![[0, 1, 2, 3], [5, 4, 7, 10]]);
topology.clear_bonds();
assert_eq!(topology.dihedrals_count(), 0);
}
#[test]
fn impropers() {
let mut topology = Topology::new();
topology.resize(12);
assert_eq!(topology.impropers_count(), 0);
topology.add_bond(0, 1);
topology.add_bond(0, 2);
topology.add_bond(0, 3);
topology.add_bond(4, 7);
topology.add_bond(4, 5);
topology.add_bond(4, 8);
assert_eq!(topology.impropers_count(), 2);
assert_eq!(topology.impropers(), vec![[1, 0, 2, 3], [5, 4, 7, 8]]);
topology.clear_bonds();
assert_eq!(topology.impropers_count(), 0);
}
#[test]
fn residues() {
let mut topology = Topology::new();
topology.resize(4);
assert_eq!(topology.residues_count(), 0);
let mut residue = Residue::new("Foo");
residue.add_atom(0);
residue.add_atom(2);
topology.add_residue(&residue).unwrap();
assert_eq!(topology.residues_count(), 1);
assert_eq!(topology.residue(0).unwrap().name(), "Foo");
{
let residue = topology.residue_for_atom(2).unwrap();
assert_eq!(residue.name(), "Foo");
}
let mut residue = Residue::new("Bar");
residue.add_atom(3);
topology.add_residue(&residue).unwrap();
assert_eq!(topology.residues_count(), 2);
let first = topology.residue(0).unwrap();
let second = topology.residue(0).unwrap();
assert!(topology.are_linked(&first, &second));
// missing residue
assert!(topology.residue_for_atom(1).is_none());
// out of bounds
assert!(topology.residue_for_atom(67).is_none());
}
}