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
// Chemfiles, a modern library for chemistry file reading and writing // Copyright (C) 2015-2017 Guillaume Fraux // // This Source Code Form is subject to the terms of the Mozilla Public // License, v. 2.0. If a copy of the MPL was not distributed with this // file, You can obtain one at http://mozilla.org/MPL/2.0/ use std::ops::Drop; use std::u64; use chemfiles_sys::*; use errors::{check, Error}; use {Atom, Residue}; use Result; /// 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. pub struct Topology { handle: *const CHFL_TOPOLOGY, } impl Clone for Topology { fn clone(&self) -> Topology { unsafe { let new_handle = chfl_topology_copy(self.as_ptr()); Topology::from_ptr(new_handle).expect("Out of memory when copying a Topology") } } } 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] #[doc(hidden)] pub unsafe fn from_ptr(ptr: *const CHFL_TOPOLOGY) -> Result<Topology> { if ptr.is_null() { Err(Error::null_ptr()) } else { Ok(Topology { handle: ptr }) } } /// Get the underlying C pointer as a const pointer. #[inline] #[doc(hidden)] pub fn as_ptr(&self) -> *const CHFL_TOPOLOGY { self.handle } /// Get the underlying C pointer as a mutable pointer. #[inline] #[doc(hidden)] pub fn as_mut_ptr(&mut self) -> *mut CHFL_TOPOLOGY { self.handle as *mut CHFL_TOPOLOGY } /// Create a new empty topology. /// /// # Example /// ``` /// # use chemfiles::Topology; /// let topology = Topology::new().unwrap(); /// assert_eq!(topology.size(), Ok(0)); /// ``` pub fn new() -> Result<Topology> { unsafe { let handle = chfl_topology(); Topology::from_ptr(handle) } } /// Get a copy of the atom at index `index` from this topology. /// /// # Example /// ``` /// # use chemfiles::Topology; /// let mut topology = Topology::new().unwrap(); /// topology.resize(6).unwrap(); /// /// let atom = topology.atom(4).unwrap(); /// assert_eq!(atom.name(), Ok(String::new())); /// ``` pub fn atom(&self, index: u64) -> Result<Atom> { unsafe { let handle = chfl_atom_from_topology(self.as_ptr(), index); Atom::from_ptr(handle) } } /// Get the current number of atoms in this topology. /// /// # Example /// ``` /// # use chemfiles::Topology; /// let mut topology = Topology::new().unwrap(); /// assert_eq!(topology.size(), Ok(0)); /// /// topology.resize(6).unwrap(); /// assert_eq!(topology.size(), Ok(6)); /// ``` pub fn size(&self) -> Result<u64> { let mut natoms = 0; unsafe { try!(check(chfl_topology_atoms_count(self.as_ptr(), &mut natoms))); } return Ok(natoms); } /// 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().unwrap(); /// assert_eq!(topology.size(), Ok(0)); /// /// topology.resize(6).unwrap(); /// assert_eq!(topology.size(), Ok(6)); /// ``` pub fn resize(&mut self, natoms: u64) -> Result<()> { unsafe { try!(check(chfl_topology_resize(self.as_mut_ptr(), natoms))); } return Ok(()); } /// Add an `Atom` at the end of this topology /// /// # Example /// ``` /// # use chemfiles::{Topology, Atom}; /// let mut topology = Topology::new().unwrap(); /// topology.add_atom(&Atom::new("Mg").unwrap()).unwrap(); /// /// let atom = topology.atom(0).unwrap(); /// assert_eq!(atom.name(), Ok(String::from("Mg"))); /// ``` pub fn add_atom(&mut self, atom: &Atom) -> Result<()> { unsafe { try!(check(chfl_topology_add_atom(self.as_mut_ptr(), atom.as_ptr()))); } return Ok(()); } /// Remove an `Atom` from this topology by index. This modify all the other /// atoms indexes. /// /// # Example /// ``` /// # use chemfiles::Topology; /// let mut topology = Topology::new().unwrap(); /// topology.resize(9).unwrap(); /// assert_eq!(topology.size(), Ok(9)); /// /// topology.remove(7).unwrap(); /// assert_eq!(topology.size(), Ok(8)); /// ``` pub fn remove(&mut self, index: u64) -> Result<()> { unsafe { try!(check(chfl_topology_remove(self.as_mut_ptr(), index))); } return Ok(()); } /// Get the number of bonds in the topology. /// /// # Example /// ``` /// # use chemfiles::{Topology, Atom}; /// let mut topology = Topology::new().unwrap(); /// assert_eq!(topology.bonds_count(), Ok(0)); /// /// topology.add_atom(&Atom::new("F").unwrap()).unwrap(); /// topology.add_atom(&Atom::new("F").unwrap()).unwrap(); /// topology.add_atom(&Atom::new("F").unwrap()).unwrap(); /// topology.add_atom(&Atom::new("F").unwrap()).unwrap(); /// /// topology.add_bond(0, 1).unwrap(); /// topology.add_bond(2, 1).unwrap(); /// topology.add_bond(2, 3).unwrap(); /// assert_eq!(topology.bonds_count(), Ok(3)); /// ``` pub fn bonds_count(&self) -> Result<u64> { let mut res = 0; unsafe { try!(check(chfl_topology_bonds_count(self.as_ptr(), &mut res))); } return Ok(res); } /// Get the number of angles in the topology. /// /// # Example /// ``` /// # use chemfiles::{Topology, Atom}; /// let mut topology = Topology::new().unwrap(); /// assert_eq!(topology.angles_count(), Ok(0)); /// /// topology.add_atom(&Atom::new("F").unwrap()).unwrap(); /// topology.add_atom(&Atom::new("F").unwrap()).unwrap(); /// topology.add_atom(&Atom::new("F").unwrap()).unwrap(); /// topology.add_atom(&Atom::new("F").unwrap()).unwrap(); /// /// topology.add_bond(0, 1).unwrap(); /// topology.add_bond(2, 1).unwrap(); /// topology.add_bond(2, 3).unwrap(); /// assert_eq!(topology.angles_count(), Ok(2)); /// ``` pub fn angles_count(&self) -> Result<u64> { let mut res = 0; unsafe { try!(check(chfl_topology_angles_count(self.as_ptr(), &mut res))); } return Ok(res); } /// Get the number of dihedral angles in the topology. /// /// # Example /// ``` /// # use chemfiles::{Topology, Atom}; /// let mut topology = Topology::new().unwrap(); /// assert_eq!(topology.dihedrals_count(), Ok(0)); /// /// topology.add_atom(&Atom::new("F").unwrap()).unwrap(); /// topology.add_atom(&Atom::new("F").unwrap()).unwrap(); /// topology.add_atom(&Atom::new("F").unwrap()).unwrap(); /// topology.add_atom(&Atom::new("F").unwrap()).unwrap(); /// /// topology.add_bond(0, 1).unwrap(); /// topology.add_bond(2, 1).unwrap(); /// topology.add_bond(2, 3).unwrap(); /// assert_eq!(topology.dihedrals_count(), Ok(1)); /// ``` pub fn dihedrals_count(&self) -> Result<u64> { let mut res = 0; unsafe { try!(check(chfl_topology_dihedrals_count(self.as_ptr(), &mut res))); } return Ok(res); } /// Get the number of improper dihedral angles in the topology. /// /// # Example /// ``` /// # use chemfiles::{Topology, Atom}; /// let mut topology = Topology::new().unwrap(); /// assert_eq!(topology.dihedrals_count(), Ok(0)); /// /// topology.add_atom(&Atom::new("F").unwrap()).unwrap(); /// topology.add_atom(&Atom::new("F").unwrap()).unwrap(); /// topology.add_atom(&Atom::new("F").unwrap()).unwrap(); /// topology.add_atom(&Atom::new("F").unwrap()).unwrap(); /// /// topology.add_bond(0, 1).unwrap(); /// topology.add_bond(0, 2).unwrap(); /// topology.add_bond(0, 3).unwrap(); /// assert_eq!(topology.impropers_count(), Ok(1)); /// ``` pub fn impropers_count(&self) -> Result<u64> { let mut res = 0; unsafe { try!(check(chfl_topology_impropers_count(self.as_ptr(), &mut res))); } return Ok(res); } /// Get the list of bonds in the topology. /// /// # Example /// ``` /// # use chemfiles::{Topology, Atom}; /// let mut topology = Topology::new().unwrap(); /// topology.add_atom(&Atom::new("F").unwrap()).unwrap(); /// topology.add_atom(&Atom::new("F").unwrap()).unwrap(); /// topology.add_atom(&Atom::new("F").unwrap()).unwrap(); /// topology.add_atom(&Atom::new("F").unwrap()).unwrap(); /// /// topology.add_bond(0, 1).unwrap(); /// topology.add_bond(2, 1).unwrap(); /// topology.add_bond(2, 3).unwrap(); /// assert_eq!(topology.bonds(), Ok(vec![[0, 1], [1, 2], [2, 3]])); /// ``` pub fn bonds(&self) -> Result<Vec<[u64; 2]>> { let nbonds = try!(self.bonds_count()); let mut res = vec![[u64::MAX; 2]; nbonds as usize]; unsafe { try!(check(chfl_topology_bonds(self.handle, res.as_mut_ptr(), nbonds))); } return Ok(res); } /// Get the list of angles in the topology. /// /// # Example /// ``` /// # use chemfiles::{Topology, Atom}; /// let mut topology = Topology::new().unwrap(); /// topology.add_atom(&Atom::new("F").unwrap()).unwrap(); /// topology.add_atom(&Atom::new("F").unwrap()).unwrap(); /// topology.add_atom(&Atom::new("F").unwrap()).unwrap(); /// topology.add_atom(&Atom::new("F").unwrap()).unwrap(); /// /// topology.add_bond(0, 1).unwrap(); /// topology.add_bond(2, 1).unwrap(); /// topology.add_bond(2, 3).unwrap(); /// assert_eq!(topology.angles(), Ok(vec![[0, 1, 2], [1, 2, 3]])); /// ``` pub fn angles(&self) -> Result<Vec<[u64; 3]>> { let nangles = try!(self.angles_count()); let mut res = vec![[u64::MAX; 3]; nangles as usize]; unsafe { try!(check(chfl_topology_angles(self.as_ptr(), res.as_mut_ptr(), nangles))); } return Ok(res); } /// Get the list of dihedral angles in the topology. /// /// # Example /// ``` /// # use chemfiles::{Topology, Atom}; /// let mut topology = Topology::new().unwrap(); /// topology.add_atom(&Atom::new("F").unwrap()).unwrap(); /// topology.add_atom(&Atom::new("F").unwrap()).unwrap(); /// topology.add_atom(&Atom::new("F").unwrap()).unwrap(); /// topology.add_atom(&Atom::new("F").unwrap()).unwrap(); /// /// topology.add_bond(0, 1).unwrap(); /// topology.add_bond(2, 1).unwrap(); /// topology.add_bond(2, 3).unwrap(); /// /// assert_eq!(topology.dihedrals(), Ok(vec![[0, 1, 2, 3]])); /// ``` pub fn dihedrals(&self) -> Result<Vec<[u64; 4]>> { let ndihedrals = try!(self.dihedrals_count()); let mut res = vec![[u64::MAX; 4]; ndihedrals as usize]; unsafe { try!(check(chfl_topology_dihedrals(self.as_ptr(), res.as_mut_ptr(), ndihedrals))); } return Ok(res); } /// Get the list of improper dihedral angles in the topology. /// /// # Example /// ``` /// # use chemfiles::{Topology, Atom}; /// let mut topology = Topology::new().unwrap(); /// topology.add_atom(&Atom::new("F").unwrap()).unwrap(); /// topology.add_atom(&Atom::new("F").unwrap()).unwrap(); /// topology.add_atom(&Atom::new("F").unwrap()).unwrap(); /// topology.add_atom(&Atom::new("F").unwrap()).unwrap(); /// /// topology.add_bond(0, 1).unwrap(); /// topology.add_bond(0, 2).unwrap(); /// topology.add_bond(0, 3).unwrap(); /// /// assert_eq!(topology.impropers(), Ok(vec![[1, 0, 2, 3]])); /// ``` pub fn impropers(&self) -> Result<Vec<[u64; 4]>> { let nimpropers = try!(self.impropers_count()); let mut res = vec![[u64::MAX; 4]; nimpropers as usize]; unsafe { try!(check(chfl_topology_impropers(self.as_ptr(), res.as_mut_ptr(), nimpropers))); } return Ok(res); } /// Add a bond between the atoms at indexes `i` and `j` in the topology. /// /// # Example /// ``` /// # use chemfiles::{Topology, Atom}; /// let mut topology = Topology::new().unwrap(); /// assert_eq!(topology.bonds_count(), Ok(0)); /// /// topology.add_atom(&Atom::new("F").unwrap()).unwrap(); /// topology.add_atom(&Atom::new("F").unwrap()).unwrap(); /// topology.add_atom(&Atom::new("F").unwrap()).unwrap(); /// /// topology.add_bond(0, 1).unwrap(); /// topology.add_bond(0, 2).unwrap(); /// assert_eq!(topology.bonds_count(), Ok(2)); /// ``` pub fn add_bond(&mut self, i: u64, j: u64) -> Result<()> { unsafe { try!(check(chfl_topology_add_bond(self.as_mut_ptr(), i, j))); } Ok(()) } /// 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, Atom}; /// let mut topology = Topology::new().unwrap(); /// assert_eq!(topology.bonds_count(), Ok(0)); /// /// topology.add_atom(&Atom::new("F").unwrap()).unwrap(); /// topology.add_atom(&Atom::new("F").unwrap()).unwrap(); /// topology.add_atom(&Atom::new("F").unwrap()).unwrap(); /// /// topology.add_bond(0, 1).unwrap(); /// topology.add_bond(1, 2).unwrap(); /// assert_eq!(topology.bonds_count(), Ok(2)); /// /// topology.remove_bond(0, 1).unwrap(); /// assert_eq!(topology.bonds_count(), Ok(1)); /// /// // Removing a bond that does not exists /// topology.remove_bond(0, 2).unwrap(); /// assert_eq!(topology.bonds_count(), Ok(1)); /// ``` pub fn remove_bond(&mut self, i: u64, j: u64) -> Result<()> { unsafe { try!(check(chfl_topology_remove_bond(self.as_mut_ptr(), i, j))); } Ok(()) } /// Get a copy of 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().unwrap(); /// topology.add_residue(&Residue::new("water").unwrap()).unwrap(); /// /// let residue = topology.residue(0).unwrap(); /// assert_eq!(residue.name(), Ok(String::from("water"))); /// ``` pub fn residue(&self, index: u64) -> Result<Residue> { unsafe { let handle = chfl_residue_from_topology(self.as_ptr(), index); Residue::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().unwrap(); /// topology.resize(8).unwrap(); /// /// let mut residue = Residue::new("water").unwrap(); /// residue.add_atom(0).unwrap(); /// residue.add_atom(1).unwrap(); /// residue.add_atom(2).unwrap(); /// topology.add_residue(&residue).unwrap(); /// /// let residue = topology.residue_for_atom(0).unwrap().unwrap(); /// assert_eq!(residue.name(), Ok(String::from("water"))); /// /// let residue = topology.residue_for_atom(6).unwrap(); /// assert!(residue.is_none()); /// ``` pub fn residue_for_atom(&self, index: u64) -> Result<Option<Residue>> { let handle = unsafe { chfl_residue_for_atom(self.as_ptr(), index) }; if handle.is_null() { let natoms = try!(self.size()); if index >= natoms { let result = unsafe { Residue::from_ptr(handle).map(Some) }; assert!(result.is_err()); result } else { // Not out of bounds, there is no residue for this atom Ok(None) } } else { let residue = unsafe { try!(Residue::from_ptr(handle)) }; Ok(Some(residue)) } } /// Get the number of residues in this topology. /// /// # Example /// ``` /// # use chemfiles::{Topology, Residue}; /// let mut topology = Topology::new().unwrap(); /// assert_eq!(topology.residues_count(), Ok(0)); /// /// topology.add_residue(&Residue::with_id("water", 0).unwrap()).unwrap(); /// topology.add_residue(&Residue::with_id("protein", 1).unwrap()).unwrap(); /// assert_eq!(topology.residues_count(), Ok(2)); /// ``` pub fn residues_count(&self) -> Result<u64> { let mut res = 0; unsafe { try!(check(chfl_topology_residues_count(self.as_ptr(), &mut res))); } Ok(res) } /// Add a residue to this topology. /// /// The residue `id` must not already be in the topology, and the residue /// must contain only atoms that are not already in another residue. /// /// # Example /// ``` /// # use chemfiles::{Topology, Residue}; /// let mut topology = Topology::new().unwrap(); /// topology.add_residue(&Residue::new("water").unwrap()).unwrap(); /// /// let residue = topology.residue(0).unwrap(); /// assert_eq!(residue.name(), Ok(String::from("water"))); /// ``` pub fn add_residue(&mut self, residue: &Residue) -> Result<()> { unsafe { try!(check(chfl_topology_add_residue(self.as_mut_ptr(), residue.as_ptr()))); } Ok(()) } /// 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().unwrap(); /// /// topology.add_residue(&Residue::with_id("water", 0).unwrap()).unwrap(); /// topology.add_residue(&Residue::with_id("protein", 1).unwrap()).unwrap(); /// /// let first = topology.residue(0).unwrap(); /// let second = topology.residue(1).unwrap(); /// assert_eq!(topology.are_linked(&first, &second), Ok(false)); /// ``` pub fn are_linked(&self, first: &Residue, second: &Residue) -> Result<bool> { let mut res = 0; unsafe { try!(check(chfl_topology_residues_linked( self.as_ptr(), first.as_ptr(), second.as_ptr(), &mut res ))); } Ok(res != 0) } } impl Drop for Topology { fn drop(&mut self) { unsafe { let status = chfl_topology_free(self.as_mut_ptr()); debug_assert_eq!(status, chfl_status::CHFL_SUCCESS); } } } #[cfg(test)] mod test { use super::*; use {Atom, Residue}; #[test] fn clone() { let mut topology = Topology::new().unwrap(); assert_eq!(topology.size(), Ok(0)); let copy = topology.clone(); assert_eq!(copy.size(), Ok(0)); topology.resize(10).unwrap(); assert_eq!(topology.size(), Ok(10)); assert_eq!(copy.size(), Ok(0)); } #[test] fn size() { let mut topology = Topology::new().unwrap(); assert_eq!(topology.size(), Ok(0)); topology.resize(10).unwrap(); assert_eq!(topology.size(), Ok(10)); topology.remove(7).unwrap(); assert_eq!(topology.size(), Ok(9)); topology.add_atom(&Atom::new("Hg").unwrap()).unwrap(); assert_eq!(topology.size(), Ok(10)); } #[test] fn atoms() { let mut topology = Topology::new().unwrap(); topology.add_atom(&Atom::new("Hg").unwrap()).unwrap(); topology.add_atom(&Atom::new("Mn").unwrap()).unwrap(); topology.add_atom(&Atom::new("W").unwrap()).unwrap(); topology.add_atom(&Atom::new("Fe").unwrap()).unwrap(); assert_eq!(topology.atom(0).unwrap().name(), Ok(String::from("Hg"))); assert_eq!(topology.atom(3).unwrap().name(), Ok(String::from("Fe"))); } #[test] fn bonds() { let mut topology = Topology::new().unwrap(); for _ in 0..12 { topology.add_atom(&Atom::new("S").unwrap()).unwrap(); } assert_eq!(topology.bonds_count(), Ok(0)); topology.add_bond(0, 1).unwrap(); topology.add_bond(9, 2).unwrap(); topology.add_bond(3, 7).unwrap(); assert_eq!(topology.bonds_count(), Ok(3)); assert_eq!(topology.bonds(), Ok(vec![[0, 1], [2, 9], [3, 7]])); topology.remove_bond(3, 7).unwrap(); // Removing unexisting bond is OK topology.remove_bond(8, 7).unwrap(); assert_eq!(topology.bonds_count(), Ok(2)); } #[test] fn angles() { let mut topology = Topology::new().unwrap(); for _ in 0..12 { topology.add_atom(&Atom::new("S").unwrap()).unwrap(); } assert_eq!(topology.angles_count(), Ok(0)); topology.add_bond(0, 1).unwrap(); topology.add_bond(1, 2).unwrap(); topology.add_bond(3, 7).unwrap(); topology.add_bond(3, 5).unwrap(); assert_eq!(topology.angles_count(), Ok(2)); assert_eq!(topology.angles(), Ok(vec![[0, 1, 2], [5, 3, 7]])); } #[test] fn dihedrals() { let mut topology = Topology::new().unwrap(); for _ in 0..12 { topology.add_atom(&Atom::new("S").unwrap()).unwrap(); } assert_eq!(topology.dihedrals_count(), Ok(0)); topology.add_bond(0, 1).unwrap(); topology.add_bond(1, 2).unwrap(); topology.add_bond(3, 2).unwrap(); topology.add_bond(4, 7).unwrap(); topology.add_bond(4, 5).unwrap(); topology.add_bond(7, 10).unwrap(); assert_eq!(topology.dihedrals_count(), Ok(2)); assert_eq!(topology.dihedrals(), Ok(vec![[0, 1, 2, 3], [5, 4, 7, 10]])); } #[test] fn impropers() { let mut topology = Topology::new().unwrap(); for _ in 0..12 { topology.add_atom(&Atom::new("S").unwrap()).unwrap(); } assert_eq!(topology.dihedrals_count(), Ok(0)); topology.add_bond(0, 1).unwrap(); topology.add_bond(0, 2).unwrap(); topology.add_bond(0, 3).unwrap(); topology.add_bond(4, 7).unwrap(); topology.add_bond(4, 5).unwrap(); topology.add_bond(4, 8).unwrap(); assert_eq!(topology.impropers_count(), Ok(2)); assert_eq!(topology.impropers(), Ok(vec![[1, 0, 2, 3], [5, 4, 7, 8]])); } #[test] fn residues() { let mut topology = Topology::new().unwrap(); topology.resize(4).unwrap(); assert_eq!(topology.residues_count(), Ok(0)); let mut residue = Residue::new("Foo").unwrap(); residue.add_atom(0).unwrap(); residue.add_atom(2).unwrap(); topology.add_residue(&residue).unwrap(); assert_eq!(topology.residues_count(), Ok(1)); assert_eq!(topology.residue(0).unwrap().name(), Ok("Foo".into())); let residue = topology.residue_for_atom(2).unwrap().unwrap(); assert_eq!(residue.name(), Ok("Foo".into())); let mut residue = Residue::new("Bar").unwrap(); residue.add_atom(3).unwrap(); topology.add_residue(&residue).unwrap(); assert_eq!(topology.residues_count(), Ok(2)); let first = topology.residue(0).unwrap(); let second = topology.residue(0).unwrap(); assert_eq!(topology.are_linked(&first, &second), Ok(true)); let missing = topology.residue_for_atom(1).unwrap(); assert!(missing.is_none()); assert!(topology.residue_for_atom(67).is_err()); } }