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 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236
|
## @package docstring
# MMB UI Chimera plugin.
# 2013
# Author: Alex Tek - ICM, Uppsala University
import chimera
import Midas
from chimera import misc, OpenModels, Bond, MolResId
from chimera import colorTable
from Movie.gui import MovieDialog
import threading
import time
import sys
import os
import platform
from collections import OrderedDict
##################################################################################################
# pyMMB initialization
mmbUIPath = os.path.dirname(__file__)
workDir = mmbUIPath
print "MMB INIT ****************** "
print mmbUIPath
if platform.system() == "Darwin":
os.environ['DYLD_FALLBACK_LIBRARY_PATH'] += ":"+mmbUIPath
elif platform.system() == "Windows":
os.environ['PATH'] += ";"+mmbUIPath
import pyMMB
leontisWesthofFileName = os.path.join(mmbUIPath,"parameters.csv")
MMBparameters = pyMMB.MMBparameters
## MMB sessions number
MMB_SESSION_NUMBER = 0
## Flag to know if the polymers are initialized or not
polymersInitialized = False
## Chimera model containing the current biopolymers
currentModel = None
## Chimera's Pseudobond group containing base pairs bonds to display
basePairBonds = None
## Chimera's Pseudobond group containing constraints bonds to display
constraintsBonds = None
## Chimera's Pseudobond group containing atomSprings bonds to display
atomSpringsBonds = None
## Chimera MDMovie dialog used to navigate among simulation frames
movieDialog = None
## Ensemble object containing frames information
ensemble = None
## Current MMB simulation
currentSimulation = None
## Simulation flags
initFlag = False
runFlag = False
## Current BioPolymers in MMB. Key is the chainID
polymers = {}
def setWorkDir( newWD ):
global workDir
workDir = newWD
os.chdir(workDir)
## Synchronise polymers sequences with the ones in MMB
def refreshSequences():
polymers.clear()
seqs = pyMMB.getSequences()
for s in seqs:
if s.chainID in polymers:
raise pyMMB.MMBError("Invalid sequence: " + s.chainID + " already exists.")
BioPolymer.mutateBioPolymerSeq_wrapper(s, currentModel)
polymers[s.chainID] = s
## Use pyMMB to extract sequences from a pdb files
# Populate the polymers global dict
# @param the path of the pdb file
def loadSequencesFromPdb(pdbFileName):
pyMMB.loadSequencesFromPdb(pdbFileName)
refreshSequences();
def importSequencesFromChimera(models=[],selectedAtoms=[]):
ficName = '/tmp/chimeraModels.pdb'
if platform.system() == "Windows":
ficName = os.path.join(os.environ["TEMP"],"chimeraModels.pdb")
selOnly = bool(selectedAtoms)
molecules = [m for m in chimera.openModels.list() if isinstance(m,chimera.Molecule)]
models = [m for m in models if m]
print models
if models:
molecules = [m for m in molecules if m in models]
print molecules
# for m in molecules:
# print m.coordSets
# print m.activeCoordSet
if molecules:
chimera.pdbWrite(molecules,
chimera.Xform.identity(),
# molecules[0].openState.xform,
ficName,
selectedOnly=selOnly,
selectionSet=set(selectedAtoms))
pyMMB.loadSequencesFromPdb(ficName)
refreshSequences();
def initMMB():
global MMBparameters
global polymersInitialized, currentModel, MMB_SESSION_NUMBER
global basePairBonds, constraintsBonds
global initFlag, runFlag, currentSimulation
global ensemble, movieDialog
pyMMB.initMMB(leontisWesthofFileName)
MMBparameters = pyMMB.MMBparameters
pyMMB.cmd("lastStage 1")
polymersInitialized = False
if currentSimulation:
currentSimulation.stop()
currentSimulation = None
# print [currentModel]
# importSequencesFromChimera(models=[currentModel])
if ensemble:
ensemble = None
if movieDialog:
# movieDialog.destroy()
movieDialog = None
initFlag = False
runFlag = False
if currentModel:
currentModel.name = "mmb_"+str(MMB_SESSION_NUMBER)
currentModel = None
if basePairBonds:
basePairBonds.destroy()
# chimera.openModels.close(basePairBonds)
basePairBonds = None
if constraintsBonds:
constraintsBonds.destroy()
# chimera.openModels.close(constraintsBonds)
constraintsBonds = None
MMB_SESSION_NUMBER += 1
## Change one MMB parameter from its name
def setMMBParameter(name, value):
# print name, value
# paramType = eval("type(MMBparameters.%s)" % name)
MMBparameters.__setattr__(name, value)
## Get MMB parameter from its name
def getMMBParameter(name):
return MMBparameters.__getattribute__(name)
initMMB()
##################################################################################################
# Trajectory
## Defines a Molecular model containing several conformations
#
# It is used by the MDMovie Chimera plugin
class MolEnsemble:
## Constructor
# @param mol: a Chimera molecule
def __init__(self, mol):
self.molecule = mol
self.name = "mmb_"+str(MMB_SESSION_NUMBER)
self.startFrame = 1
self.endFrame = len(mol.coordSets)
## The length of the ensemble is the number of conformations
# @return the number of conformations in this ensemble
def __len__(self):
return len(self.molecule.coordSets)
## Subclass of Chimera MovieDialog implementing frame management methods
#
class MyMovieDialog(MovieDialog):
## Constructor
# @param ensemble a MolEnsemble object
# @kw a list of optional named parameters for MovieDialog instanciation
def __init__(self, ensemble, **kw):
MovieDialog.__init__(self, ensemble, **kw)
## Synchronize the dialog display with the ensemble content.
def updateFrames(self):
ensembleSize = len(self.ensemble)
self.endFrame = ensembleSize
self.triggers.activateTrigger(self.MORE_FRAMES, ensembleSize)
## Add a set of coordinates to the ensemble object and update the dialog display
# @param coordArray numpy array containing coordinates to add to the model
def addFrame(self, coordArray):
cs = self.ensemble.molecule.newCoordSet(len(self.ensemble)+1)
chimera.fillCoordSet(cs, self.ensemble.molecule.atoms, coordArray)
self.ensemble.molecule.activeCoordSet = cs
self.updateFrames()
# self.LoadFrame(len(self.ensemble))
# print "wait"
# Midas.wait(1)
##################################################################################################
# TODO: Move the headers to the respective locations in gui.py
## Headers names for mobilizers
mobilizersHeaders=("Id","Mobilizer","Mobility","Chain","Start Res","End Res","Valid","Select")
## Headers names for mobilizers within
mobilizersWithinHeaders=("Id","Mobilizer","Mobility","Chain","Res","Radius","Valid","Select")
## Headers names for constraints
constraintsHeaders=("Id","Chain1","Res1","Atom1"," ", "Chain2", "Res2", "Atom2", "Valid", "Select")
## Colors
# validationColors = {True:"green", False:"red"}
# validationStates = {True:"disable", False:"normal"}
## Types of polymers. Indices match MMB types enum
PolyTypes = ['RNA','protein','DNA','Unassigned']
## Indicate how to initialize coordinates.
# New: Use MMB coordinates generator.
# PDB: Extract coordinates from a PDB file.
Topologies = ['New','PDB']
## Types of pair interaction according to Leontis & Westhof.
PairTypes = ["WatsonCrick", "Hoogsteen", "SugarEdge", "HelicalStackingA3", "HelicalStackingA5"]
## Possible bond orientations according to Leontis & Westhof.
BondOrient = ["Cis", "Trans"]
## Default parameters for a Sequence.
Defaults = {
"chainID": 'X',
"sequence": '',
"firstResNum": 1,
"polyType":0,
"topo":'New'
}
## Unicode representation of Leontis & Westhof interactions
LeontisWesthofSymbols = {
("WatsonCrick", "Cis"): u"\u25CF",
("WatsonCrick", "Trans"):u"\u25CB",
("Hoogsteen", "Cis"): u"\u25A0",
("Hoogsteen", "Trans"):u"\u25A1",
("SugarEdge", "Cis"): u"\u25B6",
("SugarEdge", "Trans"):u"\u25B7"
}
allConstraintsRestraints = OrderedDict()
## Mobility Types
MobilityTypes = ["Default", "Rigid", "Free", "Torsion"]
## List of current base pairs, synchronized with MMB
basePairs = []
allConstraintsRestraints["basePairs"] = basePairs
## List of current mobilizers, synchronized with MMB
mobilizers = []
allConstraintsRestraints["mobilizers"] = mobilizers
## List of current mobilizersWithin, synchronized with MMB
mobilizersWithin = []
allConstraintsRestraints["mobilizersWithin"] = mobilizersWithin
## List of current rootMobilizers, synchronized with MMB
rootMobilizers = []
allConstraintsRestraints["rootMobilizers"] = rootMobilizers
## List of current contacts, synchronized with MMB
contacts = []
allConstraintsRestraints["contacts"] = contacts
## List of current contactsWithin, synchronized with MMB
contactsWithin = []
allConstraintsRestraints["contactsWithin"] = contactsWithin
## List of constraints, synchronized with MMB
constraints = []
allConstraintsRestraints["constraints"] = constraints
## List of atomSprings, synchronized with MMB
atomSprings = []
allConstraintsRestraints["atomSprings"] = atomSprings
## List of current AllResiduesWithin, synchronized with MMB
allResiduesWithins = []
allConstraintsRestraints["allResiduesWithins"] = allResiduesWithins
## List of current includeAllNonBondAtomsInResidues, synchronized with MMB
includeAllNonBondAtomsInResidues = []
allConstraintsRestraints["includeAllNonBondAtomsInResidues"] = includeAllNonBondAtomsInResidues
## List of current Threadings, synchronized with MMB
threadings = []
allConstraintsRestraints["threadings"] = threadings
## List of current GappedThreadings, synchronized with MMB
gappedThreadings = []
allConstraintsRestraints["gappedThreadings"] = gappedThreadings
## List of current density stretches, synchronized with MMB
densities = []
allConstraintsRestraints["densities"] = densities
## Current density map model
densityMapModel = None
## Name of the pdb file chosen by the user.
currentPdb = ""
##################################################################################################
## Defines a generic MMB object (mother class for interactions, constraints etc.)
#
class MMB_UI_Object:
def __init__(self, model=currentModel, mmbID=-1, new=False):
self.model = model
self.mmbID = mmbID
self.new = new
self.show = True
def setAttribute(self, attrName, value):
if getattr(self, attrName) == value:
return
if attrName.startswith("res"):
value = int(value.split()[1])
setattr(self, attrName, value)
def mmbAdd(self):
if self.new:
pyMMB.cmd(str(self))
else:
pass
def mmbUpdate(self):
pass
def mmbDelete(self):
pass
def updateRepresentation(self):
pass
def updateRepresentationColor(self):
pass
def getChimeraSelection(self):
return [None]
def removeRepresentation(self):
res = self.getChimeraSelection()
if res:
for r in res:
if r:
r.label = ""
r.labelColor = None
r.ribbonColor = None
for a in r.atoms:
a.color = None
## Add a constraint to current chimera selection
# @param constraint to select
def addToSelection(self):
sel = self.getChimeraSelection()
if sel:
chimera.selection.addCurrent([s for s in sel if s])
## Remove a constraint from current chimera selection
# @param constraint to deselect
def removeFromSelection(self):
sel = self.getChimeraSelection()
if sel:
chimera.selection.removeCurrent([s for s in sel if s])
##################################################################################################
## Represents a biopolymer as in MMB
#
class BioPolymer(pyMMB.BioPolymerSeq, MMB_UI_Object):
representativeAtom = {"RNA": "C4'","protein": "CA","DNA": "C4'"}
## Constructor
def __init__(self,chainID='',sequence='', firstResNum=1, polyType=3,pdbFileName='',pdbChainID='', loadFromPdb=False, activePhysics=True, new=False, valid=True):
self.chainID = chainID
self.sequence = sequence
self.firstResNum = firstResNum
self.polyType = polyType
self.pdbFileName = pdbFileName
self.pdbChainID = pdbChainID
self.loadFromPdb = loadFromPdb
self.activePhysics = activePhysics
self.initialization(currentModel, new, valid)
def initialization(self, model, new, valid):
self.topo = "New"
self.show = True
if self.loadFromPdb:
self.topo = "PDB"
self.new = new
if self.new:
self.topo = "New"
self.loadFromPdb = False
self.valid = valid
self.select = False
self.idsList = None
# self.lastResNum = self.firstResNum + len(self.sequence) - 1
@classmethod
## Mutate a MobilizerWithin_wrapper object's type to MobilizerWithin
def mutateBioPolymerSeq_wrapper(cls, BioPolymerSeq_wrapper_object, model=currentModel, new=False, valid=True):
BioPolymerSeq_wrapper_object.__class__ = cls
BioPolymerSeq_wrapper_object.initialization(model, new, valid)
## Length of the polymer == length of the sequence
# @return the sequence's length
def __len__(self):
return len(sequence)
def setAttribute(self, attrName, value):
if getattr(self, attrName) == value:
return
if attrName == "polyType":
setattr(self, attrName, PolyTypes.index(value))
return
if attrName in ["topo","sequence"]:
setattr(self, attrName, value)
if self.topo == "New":
self.loadFromPdb = False
else:
self.loadFromPdb = True
return
setattr(self, attrName, value)
# def mmbUpdate(self):
# pyMMB.updatePolymer(self.chainID, self.sequence, self.pdbFileName, self.loadFromPdb)
def mmbDelete(self):
pyMMB.deletePolymer(self.chainID)
## Add the polymer to MMB
def mmbAdd(self):
pyMMB.cmd(PolyTypes[self.polyType]+" "+self.chainID+" "+str(self.firstResNum)+" "+self.sequence)
## Returns the representative atom name
def getRepresentativeAtom(self):
return BioPolymer.representativeAtom[PolyTypes[self.polyType]]
## Builds a list of ids
# @return a list of ids
def getIdsList(self, start=-1, end=sys.maxint):
# if not self.idsList:
self.idsList = []
for res in currentModel.residues:
if res.id.chainId == self.chainID and res.id.position >= start and res.id.position < end:
self.idsList.append(res.id.position)
self.idsList = sorted(self.idsList)
return self.idsList
## Builds a string composed of the residue's 1-letter code and id
# @param id id of the wanted residue
# @return a string like this: A 23
def getResNameId(self, id):
return self.getResName(id) + " " + str(id)
## Returns the residue id 1-letter name or " " if the id is outside the sequence's ids range
# @param id id of the wanted residue
# @return 1-letter name or " "
def getResName(self, id):
idsList = self.getIdsList()
if id in idsList:
return self.sequence[idsList.index(id)]
else:
return ""
## Build a list of resname + id strings
# @return a list containing "resname id" strings
def getResNamesIDsList(self, start=-1):
z = zip(self.sequence, self.getIdsList(start))
return [rName+" "+str(rId) for rName, rId in z]
## Return a list of Chimera Residue objects from start to end (included)
# @param start id of first residue
# @param end id of last residue (included)
# @return a list of Chimera Residue objects
def getChimeraResidues(self, start=None, end=None):
if not currentModel:
return []
if not end: end = sys.maxint
selAdd = []
for res in currentModel.residues:
if res.id.chainId == self.chainID:
if res.id.position >= start and res.id.position <= end:
selAdd.append(res)
return selAdd
def updateRepresentation(self):
pass
def getChimeraSelection(self):
return self.getChimeraResidues()
def getRootMobilizer(self):
return pyMMB.getRootMobilizer(self.chainID)
def matchCoordinatesFromChimera(self):
import cStringIO
out = cStringIO.StringIO()
chimera.pdbWrite([currentModel], currentModel.openState.xform, out)
pyMMB.matchCoordinatesFromContent(self.chainID, out)
def show_hide(self):
modelID = currentModel.id
# print ":.%s#%i" % (self.chainID, modelID)
if self.show:
self.show = False
# chimera.runCommand("~display #%i:.%s" % (modelID, self.chainID))
chimera.runCommand("~ribbon #%i:.%s" % (modelID, self.chainID))
else:
self.show = True
# chimera.runCommand("display #%i:.%s" % (modelID, self.chainID))
chimera.runCommand("ribbon #%i:.%s" % (modelID, self.chainID))
## Check if all polymers have been validated in MMB
def checkSequences():
for p in polymers.values():
if not p.valid:
return False
return True
## Validate all polymers that needs it
def validateSequences():
for p in polymers.values():
if not p.valid:
p.mmbUpdate()
p.valid = True
## Initialize the polymers with the coordinates contained in the pdb file.
# Open a new model in Chimera containing all the biopolymers.
# @param polymers dictionary of Biopolymer objects with chainIDs as keys
# @param the path of the pdb file
def initPolymers(polymers, pdbFileName):
global polymersInitialized
#pyMMB.clearPolymers()
for p in polymers.values():
pdbfile = p.pdbFileName
if not p.loadFromPdb:
pdbfile = ""
pyMMB.initializePolymer(p.chainID, pdbfile)
polymersInitialized = True
def loadPolymers():
global currentModel
if os.getcwd() == "/" :
os.chdir (os.environ['HOME']);
ficName = str(MMBparameters.workingDirectory) + '/mmb.pdb'
#ficName = 'mmb.pdb'
pyMMB.writeDefaultPdb(ficName)
currentModel = chimera.openModels.open(ficName)[0]
# print "CurrentModel coordSets: "
# print currentModel.coordSets
for m in chimera.openModels.list():
if m.id != currentModel.id:
m.display = False
## Module method returning a list of the polymers sorted by chain Id
# @return a list of the biopolymers sorted by chain ID
def sortedPolymers():
return [polymers[k] for k in sorted(polymers.keys())]
## Module method returning a sorted list of the biopolymer's chain ids.
def sortedChainsIDs():
return sorted(polymers.keys())
## Module method returning a sorted list of the nucleic acids's chain ids.
def sortedNucleicAcidsIDs():
return sorted([k for k in polymers.keys() if PolyTypes[polymers[k].polyType] in ("RNA","DNA") ])
## Return a list of atoms ids and names for a residue
def getAtomsList(chainID, resID, model=currentModel):
res = model.findResidue(MolResId(chainID, resID))
if not res:
return []
return [a.name for a in res.atoms]
## Return Chimera residues within a radius around the CA or C4 of :resID.chaindID
def getResiduesWithin(chainID, resID, radius):
repAtom = polymers[chainID].getRepresentativeAtom()
chimera.runCommand("sel :%i.%s@%s z<%.1f & @%s" % (resID, chainID, repAtom, radius, repAtom))
return chimera.selection.currentResidues()
##################################################################################################
## Defines a base pair interaction as in MMB
#
# Some methods are used as callbacks for Tk widgets.
# Others are used to get information from Chimera
class BasePairInteraction(pyMMB.BaseInteraction_wrapper, MMB_UI_Object):
## Constructor
def __init__(self, model, mmbID, poly1, res1, edge1, poly2, res2, edge2, bondOrient,
select=False, valid=True, newPair=False, chimBond=None):
## the Chimera model containing the residues of the interaction.
MMB_UI_Object.__init__(self, model, mmbID)
self.poly1 = poly1
self.res1 = res1
self.edge1 = edge1
self.poly2 = poly2
self.res2 = res2
self.edge2 = edge2
self.bondOrient = bondOrient
self.initialization(model, newPair, valid, select, chimBond)
def initialization(self, model=currentModel, new=False, valid=True, select=False, chimBond=None):
self.model = model
self.select = select
self.valid = valid
self.new = new
## The Chimera pseudobond used to display the interaction
self.chimeraBond= chimBond
@classmethod
## Mutate a MobilizerWithin_wrapper object's type to MobilizerWithin
def mutateBaseInteraction_wrapper(cls, BaseInteraction_wrapper_object, model=currentModel, new=False, valid=True):
BaseInteraction_wrapper_object.__class__ = cls
BaseInteraction_wrapper_object.initialization(model, new, valid)
## Build an interaction with default values
# @return a BasePairInteraction object with default values
@classmethod
def emptyPair(cls):
ch1 = ""
ch2 = ""
try:
ch1 = sortedNucleicAcidsIDs()[0]
ch2 = sortedNucleicAcidsIDs()[0]
except IndexError:
pass
return cls(currentModel,
-1,
ch1, 0, PairTypes[0],
ch2, 0, PairTypes[0],
BondOrient[0], newPair=True)
## Return Chimera residue objects of the two residues
# @return a tuple containing the two residues. An element can be a NoneType.
def getChimeraResidues(self):
res1 = self.model.findResidue(MolResId(self.poly1, self.res1))
res2 = self.model.findResidue(MolResId(self.poly2, self.res2))
return res1, res2
## Return Chimera atom objects for the C5' atoms of the residues
# @return a tuple containing the two atomss. An element can be a NoneType.
def getChimeraAtoms(self):
res1,res2 = self.getChimeraResidues()
atom1 = None
atom2 = None
if res1:
atom1 = res1.findAtom(polymers[self.poly1].getRepresentativeAtom())
if res2:
atom2 = res2.findAtom(polymers[self.poly2].getRepresentativeAtom())
return atom1, atom2
## Return a list of chimera selectables for the residues and the bond
# @return a list of chimera selectables. Can contain None values
def getChimeraSelection(self):
return list(self.getChimeraResidues()) + [self.chimeraBond]
## Build a string composed of the residues 1-letter name and the Leontis&Westhof symbol.
# @return a string like: A O U
def getLabel(self):
res1 = polymers[self.poly1].getResName(self.res1)
res2 = polymers[self.poly2].getResName(self.res2)
if self.edge1 == self.edge2:
symbol = ""
if (self.edge1, self.bondOrient) in LeontisWesthofSymbols:
symbol = LeontisWesthofSymbols[(self.edge1, self.bondOrient)]
return res1 + " - " + symbol + " - " + res2
symbol1 = ""
symbol2 = ""
if (self.edge1, self.bondOrient) in LeontisWesthofSymbols:
symbol1 = LeontisWesthofSymbols[(self.edge1, self.bondOrient)]
if (self.edge2, self.bondOrient) in LeontisWesthofSymbols:
symbol2 = LeontisWesthofSymbols[(self.edge2, self.bondOrient)]
return res1 + " - " + symbol1 + " " + symbol2 + " - " + res2
## Update the bond display in Chimera. Create a new bond if necessary.
def updateRepresentation(self):
global basePairBonds
b = self.chimeraBond
if b:
b.reuse(*(self.getChimeraAtoms()))
b.label = self.getLabel()
self.updateRepresentationColor()
return
# if new bond, check atoms
a1, a2 = self.getChimeraAtoms()
if a1 and a2:
b = basePairBonds.newPseudoBond(a1,a2)
b.display = 1
b.drawMode = Bond.Wire
b.label = self.getLabel()
self.chimeraBond = b
self.updateRepresentationColor()
## Update the bond's color according to state of the interaction in the GUI.
# New interaction: blue
# Non validated interaction: orange
# Normal state: black
def updateRepresentationColor(self):
if self.new:
self.chimeraBond.color = colorTable.getColorByName("blue")
return
if not self.valid:
self.chimeraBond.color = colorTable.getColorByName("orange")
return
self.chimeraBond.color = colorTable.getColorByName("black")
## Synchronize the base pair interactions with MMB
# Update bond display as well
def refreshBasePairInteractions():
"""
String format:
MMBid ch1 resN1 edge1 ch2 resN2 edge2 orientation
"""
global basePairBonds
interactions = pyMMB.getBaseInteractions()
basePairs[:] = []
for i in interactions:
BasePairInteraction.mutateBaseInteraction_wrapper(i, currentModel)
basePairs.append(i)
refreshBasePairBonds()
## Update base pair bonds display
def refreshBasePairBonds():
global basePairBonds
if basePairBonds:
basePairBonds.deleteAll()
else:
basePairBonds = misc.getPseudoBondGroup('BasePairs', modelID=OpenModels.Default, hidden=False)
basePairBonds.name = "MMB Base Pairs"
basePairBonds.lineType = chimera.Dash
basePairBonds.lineWidth = 3
basePairBonds.color = colorTable.getColorByName("black")
[p.updateRepresentation() for p in basePairs]
##################################################################################################
## Defines a Nucleic Acid Duplex. Only for the gui as no such data structure exists in MMB.
# Some methods are used as callbacks for Tk widgets.
# Others are used to get information from Chimera
class NucleicAcidDuplex(MMB_UI_Object):
## Constructor
def __init__(self, model=currentModel, mmbID=-1, chain1="", resStart1=0, resEnd1=0,
chain2="", resStart2=0, resEnd2=0, new=True):
MMB_UI_Object.__init__(self, model, mmbID)
self.chainID1 = chain1
self.residueStart1 = resStart1
self.residueEnd1 = resEnd1
self.chainID2 = chain2
self.residueStart2 = resStart2
self.residueEnd2 = resEnd2
self.initialization(model, new)
def initialization(self, model=currentModel, new=True, selection=None, valid=True):
self.model = model
self.new = new
self.selection = selection
self.valid = valid
def mmbAdd(self):
cmdTxt = "nucleicAcidDuplex %s %i %i %s %i %i" % ( self.chainID1,
self.residueStart1,
self.residueEnd1,
self.chainID2,
self.residueStart2,
self.residueEnd2
)
pyMMB.cmd(cmdTxt)
## Initialize the residue when changing chain
def setAttribute(self, attrName, value):
if attrName == "chain1":
setattr(self, "residueStart1", 0)
setattr(self, "residueStart2", 0)
elif attrName == "chain2":
setattr(self, "residueStart1", 0)
setattr(self, "residueStart2", 0)
MMB_UI_Object.setAttribute(self, attrName, value)
def getResStart1NameId(self):
if self.residueStart1 == 0:
return ""
poly = polymers[self.chainID1]
return poly.getResNameId(self.residueStart1)
def getResStart2NameId(self):
if self.residueStart2 == 0:
return ""
poly = polymers[self.chainID2]
return poly.getResNameId(self.residueStart2)
def getResEnd1NameId(self):
if self.residueEnd1 == 0:
return ""
poly = polymers[self.chainID1]
return poly.getResNameId(self.residueEnd1)
def getResEnd2NameId(self):
if self.residueEnd2 == 0:
return ""
poly = polymers[self.chainID2]
return poly.getResNameId(self.residueEnd2)
def getChimeraSelection(self):
if self.chainID1 == "" and self.chainID2 == "":
return []
sel = []
poly = polymers[self.chainID1]
sel += poly.getChimeraResidues(self.residueStart1, self.residueEnd1)
if self.chainID2:
poly = polymers[self.chainID2]
sel += poly.getChimeraResidues(self.residueStart2, self.residueEnd2)
return sel
##################################################################################################
mobiColors = {"Default":"orange", "Rigid":"dim gray", "Free":"yellow", "Torsion":"green"}
## Defines a MMB's mobilizer
# Some methods are used as callbacks for Tk widgets.
# Others are used to get information from Chimera
class Mobilizer(pyMMB.MobilizerStretch_wrapper, MMB_UI_Object):
## Constructor
def __init__(self, model=currentModel, mmbID=-1, mobility="Default", chain="All", resStart=0, resEnd=-1, new=True):
MMB_UI_Object.__init__(self, model, mmbID)
self.mobility = mobility
self.chainID = chain
self.resStart = resStart
self.resEnd = resEnd
self.initialization(model, new)
def initialization(self, model=currentModel, new=False, valid=True):
self.model = model
self.valid = valid
self.new = new
@classmethod
## Mutate a MobilizerWithin_wrapper object's type to MobilizerWithin
def mutateMobilizerStretch_wrapper(cls, MobilizerStretch_wrapper_object, model=currentModel, new=False, valid=True):
MobilizerStretch_wrapper_object.__class__ = cls
MobilizerStretch_wrapper_object.initialization(model, new, valid)
def setAttribute(self, attrName, value):
if getattr(self, attrName) == value:
return
if attrName == "resStart" and value == "All":
self.resStart = 0
self.resEnd = -1
return
if attrName == "resStart":
value = int(value.split()[1])
self.resStart = value
if value > self.resEnd:
self.resEnd = value
return
if attrName.startswith("res"):
value = int(value.split()[1])
setattr(self, attrName, value)
def getResStartNameId(self):
if self.resStart == 0:
return "All"
poly = polymers[self.chainID]
return poly.getResNameId(self.resStart)
def getResEndNameId(self):
if self.resEnd == -1:
return ""
poly = polymers[self.chainID]
return poly.getResNameId(self.resEnd)
## Return a list of Chimera residue objects
# @return a list containing the residues.
def getChimeraSelection(self):
if self.chainID == "All":
return currentModel.residues;
poly = polymers[self.chainID]
if self.resStart == "All":
return poly.getChimeraResidues(start=poly.firstResNum)
return poly.getChimeraResidues(self.resStart, self.resEnd)
def updateRepresentation(self):
resSel = self.getChimeraSelection()
# for r in resSel:
# r.ribbonDisplay = True
self.updateRepresentationColor(resSel)
## Update the color according to the mobility
def updateRepresentationColor(self, resSel):
color = colorTable.getColorByName(mobiColors[self.mobility])
for r in resSel:
r.ribbonColor = color
for a in r.atoms:
a.color = color
## Synchronize the mobilizers list with MMB
def refreshMobilizerStretches():
global mobilizers
mmbMobilizers = pyMMB.getMobilizerStretches()
mobilizers[:] = []
for m in mmbMobilizers:
Mobilizer.mutateMobilizerStretch_wrapper(m, currentModel, False)
mobilizers.append(m)
##################################################################################################
## Defines a MMB's mobilizerWithin
# Some methods are used as callbacks for Tk widgets.
# Others are used to get information from Chimera
class MobilizerWithin(pyMMB.MobilizerWithin_wrapper, MMB_UI_Object):
## Constructor
def __init__(self, model=currentModel, mmbID=-1, mobility="Default", chain="", res=0, radius=0.4, new=True):
MMB_UI_Object.__init__(self, model, mmbID)
self.mobility = mobility
self.radius = radius
self.chainID = chain
self.resID = res
self.initialization(model, new)
def initialization(self, model=currentModel, new=True, selection=None, valid=True):
self.model = model
self.new = new
self.selection = selection
self.valid = valid
@classmethod
## Mutate a MobilizerWithin_wrapper object's type to MobilizerWithin
def mutateMobilizerWithin_wrapper(cls, MobilizerWithin_wrapper_object, model=currentModel, new=False, selection=None, valid=True):
MobilizerWithin_wrapper_object.__class__ = cls
MobilizerWithin_wrapper_object.initialization(model, new, selection, valid)
## Initialize the residue when changing chain
def setAttribute(self, attrName, value):
if attrName.startswith("chain"):
setattr(self, "resID", 0)
MMB_UI_Object.setAttribute(self, attrName, value)
def getResNameId(self):
if self.resID == 0:
return ""
poly = polymers[self.chainID]
return poly.getResNameId(self.resID)
def getChimeraSelection(self):
if self.resID != 0 and self.radius > 0:
# resWithin = pyMMB.getResiduesWithin(self.chainID, self.resID, self.radius)
# return [self.model.findResidue( MolResId(x[0], x[1])) for x in resWithin]
# chimera.selection.addCurrent(self.selection)
return getResiduesWithin(self.chainID, self.resID, self.radius*10.0)
def updateRepresentation(self):
resSel = self.getChimeraSelection()
self.updateRepresentationColor(resSel)
## Update the color according to the mobility
def updateRepresentationColor(self, resSel):
if resSel == None:
return
color = colorTable.getColorByName(mobiColors[self.mobility])
for r in resSel:
r.ribbonColor = color
for a in r.atoms:
a.color = color
## Synchronize the mobilizersWithin list with MMB
def refreshMobilizerWithin():
global mobilizersWithin
mmbMobilizers = pyMMB.getMobilizersWithin()
mobilizersWithin[:] = []
for m in mmbMobilizers:
MobilizerWithin.mutateMobilizerWithin_wrapper(m, currentModel)
mobilizersWithin.append(m)
##################################################################################################
## Defines a constraint as in MMB
#
class Constraint(pyMMB.Constraint_wrapper, MMB_UI_Object):
##Constructor
def __init__(self, model=currentModel, mmbID=-1,
chain1="", res1=0, atom1="",
chain2="Ground", res2=0, atom2="",
new=True, valid=True):
MMB_UI_Object.__init__(self, model, mmbID)
self.chain1 = chain1
self.res1 = res1
self.atom1 = atom1.replace("*","'")
self.chain2 = chain2
self.res2 = res2
self.atom2 = atom2.replace("*","'")
self.initialization(model, new, valid)
def initialization(self, model=currentModel, new=False, valid=True):
self.model = model
self.atom1 = self.atom1.replace("*","'")
self.atom2 = self.atom2.replace("*","'")
self.valid = valid
self.new = new
self.chimeraBond = None
if not self.chain2.isalpha():
self.chain2 = "Ground"
# if self.res1 == 0:
# self.res1 = ""
# if self.res2 == 0:
# self.res2 = ""
@classmethod
## Mutate a MobilizerWithin_wrapper object's type to MobilizerWithin
def mutateConstraint_wrapper(cls, Constraint_wrapper_object, model=currentModel, new=False, valid=True):
Constraint_wrapper_object.__class__ = cls
Constraint_wrapper_object.initialization(model, new, valid)
## Return an MMB command for this constraint
def __str__(self):
# if self.chain1=="":
# return "rootMobilizer Weld"
# if self.res1=="":
# return "rootMobilizer " + self.chain1 + " Weld"
com = "constraint " + self.chain1 + " " + str(self.res1) + " " + self.atom1
if self.chain2 == "Ground":
return "constrainToGround " + self.chain1 + " " + str(self.res1)
return com + " Weld " + self.chain2 + " " + str(self.res2) + " " + self.atom2
def setAttribute(self, attrName, value):
if getattr(self, attrName) == value:
return
# if the attribute is res1 or res2 we set the corresponding atom to ""
if attrName.startswith("res"):
nAttr = attrName.split("s")[1]
atomAttr = "atom"+nAttr
setattr(self, atomAttr, "")
if value:
value = int(value.split()[1])
else:
value = 0
# if the attribute is chain1 or chain2 we set the corresponding res,atom to 0,""
elif attrName.startswith("chain"):
nAttr = attrName.split("n")[1]
setattr(self, "res"+nAttr, 0)
setattr(self, "atom"+nAttr, "")
setattr(self, attrName, value)
def getRes1AtomsList(self):
return getAtomsList(self.chain1, self.res1, self.model)
def getRes2AtomsList(self):
return getAtomsList(self.chain2, self.res2, self.model)
def getRes1NameId(self):
if self.res1 == 0:
return ""
return polymers[self.chain1].getResNameId(self.res1)
def getRes2NameId(self):
if self.chain2 == "Ground" or self.res2 == 0:
return ""
return polymers[self.chain2].getResNameId(self.res2)
def getChimeraSelection(self):
if self.chain1 == 0 or self.res1 == 0:
return
res1 = self.model.findResidue(MolResId(self.chain1, self.res1))
selection = [self.chimeraBond]
if self.atom1 != "":
selection.append(res1.findAtom(self.atom1))
else:
selection.append(res1)
if self.res2 == 0:
return selection
res2 = self.model.findResidue(MolResId(self.chain2, self.res2))
if self.atom2 != "":
selection.append(res2.findAtom(self.atom2))
else:
selection.append(res2)
return selection
## Update the bond display in Chimera. Create a new bond if necessary.
def updateRepresentation(self):
global constraintsBonds
# Contraint to ground
if self.chain2 == "Ground" and self.res1:
poly = polymers[self.chain1]
resSel = poly.getChimeraResidues(self.res1, self.res1)
for r in resSel:
r.label = u"\u22A5"
self.updateRepresentationColor()
return
# No bond
if self.res2 == 0:
if self.chimeraBond:
constraintsBonds.deletePseudoBond(self.chimeraBond)
self.chimeraBond = None
return
res1 = self.model.findResidue(MolResId(self.chain1, self.res1))
res2 = self.model.findResidue(MolResId(self.chain2, self.res2))
a1 = self.atom1
a2 = self.atom2
if self.atom1 == "":
a1 = polymers[self.chain1].getRepresentativeAtom()
if self.atom2 == "":
a2 = polymers[self.chain2].getRepresentativeAtom()
b = self.chimeraBond
if b:
b.reuse(res1.findAtom(a1), res2.findAtom(a2))
b.label = "||"
self.updateRepresentationColor()
return
b = constraintsBonds.newPseudoBond(res1.findAtom(a1), res2.findAtom(a2))
b.display = 1
b.drawMode = Bond.Wire
b.label = "||"
self.chimeraBond = b
self.updateRepresentationColor()
## Update the bond's color according to state of the interaction in the GUI.
# New constraint: blue
# Non validated constraint: orange
# Normal state: black
def updateRepresentationColor(self):
if self.chain2 == "Ground":
poly = polymers[self.chain1]
resSel = poly.getChimeraResidues(self.res1, self.res1)
for r in resSel:
r.labelColor = colorTable.getColorByName("black")
r.ribbonColor = colorTable.getColorByName("black")
for a in r.atoms:
a.color = colorTable.getColorByName("black")
return
if self.new:
self.chimeraBond.color = colorTable.getColorByName("blue")
return
if not self.valid:
self.chimeraBond.color = colorTable.getColorByName("orange")
return
self.chimeraBond.color = colorTable.getColorByName("black")
## Synchronize the constraints list with MMB
def refreshConstraints():
global constraints
mmbConstraints = pyMMB.getConstraints()
constraints[:] = []
for c in mmbConstraints:
Constraint.mutateConstraint_wrapper(c, currentModel)
constraints.append(c)
refreshConstraintsBonds()
## Update base pair bonds display
def refreshConstraintsBonds():
global constraintsBonds
if constraintsBonds:
constraintsBonds.deleteAll()
else:
constraintsBonds = misc.getPseudoBondGroup('constraints', modelID=OpenModels.Default, hidden=False)
constraintsBonds.name = "MMB Constraints"
constraintsBonds.lineWidth = 2
constraintsBonds.color = colorTable.getColorByName("black")
[p.updateRepresentation() for p in constraints]
##################################################################################################
## Defines an includeAllResiduesWithin command as in MMB
#
class AllResiduesWithin(pyMMB.AllResiduesWithin_wrapper, MMB_UI_Object):
def __init__(self, model=None, mmbID=-1, chain="", residue=0, radius=1.0, valid=True, new=False,select=False):
MMB_UI_Object.__init__(self, model, mmbID)
self.mmbID = mmbID
self.chain = chain
self.residue = residue
self.radius = radius
self.initialization(model, new, valid, select)
def initialization(self, model=currentModel, new=False, valid=True, select=False):
self.model = model
self.valid = valid
self.new = new
self.select = select
@classmethod
## Mutate a MobilizerWithin_wrapper object's type to MobilizerWithin
def mutateAllResiduesWithin_wrapper(cls, AllResiduesWithin_wrapper_object, model=currentModel, new=False, valid=True):
AllResiduesWithin_wrapper_object.__class__ = cls
AllResiduesWithin_wrapper_object.initialization(model, new, valid)
def getResNameId(self):
if self.residue == 0:
return ""
poly = polymers[self.chain]
return poly.getResNameId(self.residue)
def getChimeraSelection(self):
if self.residue != 0 and self.radius > 0:
# resWithin = pyMMB.getResiduesWithin(self.chain, self.residue, self.radius)
# return [self.model.findResidue( MolResId(x[0], x[1])) for x in resWithin]
# chimera.selection.addCurrent(self.selection)
return getResiduesWithin(self.chain, self.residue, self.radius*10.0)
def updateRepresentation(self):
resSel = self.getChimeraSelection()
if resSel:
for r in resSel:
for a in r.atoms:
a.display = True
def __str__(self):
return "includeAllResiduesWithin %.1f %s %i" % (self.radius, self.chain, self.residue)
def refreshAllResiduesWithins():
global allResiduesWithins
allResiduesWithins[:] = []
mmbAllResiduesWithins = pyMMB.getAllResiduesWithins()
for a in mmbAllResiduesWithins:
AllResiduesWithin.mutateAllResiduesWithin_wrapper(a, currentModel)
allResiduesWithins.append(a)
##################################################################################################
## Defines an includeAllNonBondAtomsInResidue command as in MMB
#
class IncludeAllNonBondAtomsInResidue(pyMMB.IncludeAllNonBondAtomsInResidue_wrapper, MMB_UI_Object):
def __init__(self, model=None, mmbID=-1, chain="", residue=0, valid=True, new=False,select=False):
self.model = model
self.mmbID = mmbID
self.chain = chain
self.residue = residue
self.initialization(model, new, valid, select)
def initialization(self, model=currentModel, new=False, valid=True, select=False):
self.model = model
self.valid = valid
self.new = new
self.select = select
self.resEnd = self.residue
@classmethod
## Mutate a MobilizerWithin_wrapper object's type to MobilizerWithin
def mutateIncludeAllNonBondAtomsInResidue_wrapper(cls, IncludeAllNonBondAtomsInResidue_wrapper_object, model=currentModel, new=False, valid=True):
IncludeAllNonBondAtomsInResidue_wrapper_object.__class__ = cls
IncludeAllNonBondAtomsInResidue_wrapper_object.initialization(model, new, valid)
def setAttribute(self, attrName, value):
MMB_UI_Object.setAttribute(self, attrName, value)
if attrName == "residue" and self.residue > self.resEnd:
self.resEnd = self.residue
def getResStartNameId(self):
if self.residue == 0:
return ""
poly = polymers[self.chain]
return poly.getResNameId(self.residue)
def getResEndNameId(self):
if self.resEnd == 0:
return ""
poly = polymers[self.chain]
return poly.getResNameId(self.resEnd)
def getChimeraSelection(self):
if self.chain != "":
poly = polymers[self.chain]
return poly.getChimeraResidues(self.residue, self.resEnd)
def updateRepresentation(self):
resSel = self.getChimeraSelection()
if resSel:
for r in resSel:
for a in r.atoms:
a.display = True
def __str__(self):
return "includeAllNonBondAtomsInResidues %s %i %i" % (self.chain, self.residue, self.resEnd)
def refreshIncludeAllNonBondAtomsInResidues():
global includeAllNonBondAtomsInResidues
includeAllNonBondAtomsInResidues[:] = []
mmbIncludeAllNonBondAtomsInResidues = pyMMB.getIncludeAllNonBondAtomsInResidues()
for a in mmbIncludeAllNonBondAtomsInResidues:
IncludeAllNonBondAtomsInResidue.mutateIncludeAllNonBondAtomsInResidue_wrapper(a, currentModel)
includeAllNonBondAtomsInResidues.append(a)
##################################################################################################
## Defines a MMB's Contact stretch
# Some methods are used as callbacks for Tk widgets.
# Others are used to get information from Chimera
class Contact(pyMMB.ContactStretch_wrapper, MMB_UI_Object):
typesList = ["AllAtomSterics", "AllHeavyAtomSterics", "SelectedAtoms"]
## Constructor
def __init__(self, model=currentModel, mmbID=-1, contactScheme="AllAtomSterics", chain="", resStart=0, resEnd=-1, new=True):
MMB_UI_Object.__init__(self, model, mmbID)
self.contactScheme = contactScheme
self.chainID = chain
self.resStart = resStart
self.resEnd = resEnd
self.initialization(model, new)
def initialization(self, model=currentModel, new=False, valid=True):
self.model = model
self.valid = valid
self.new = new
@classmethod
## Mutate a ContactStretch_wrapper object's type to Contact
def mutateContactStretch_wrapper(cls, ContactStretch_wrapper_object, model=currentModel, new=False, valid=True):
ContactStretch_wrapper_object.__class__ = cls
ContactStretch_wrapper_object.initialization(model, new, valid)
def setAttribute(self, attrName, value):
if getattr(self, attrName) == value:
return
if attrName == "resStart" and value == "All":
self.resStart = 0
self.resEnd = -1
return
if attrName == "resStart":
value = int(value.split()[1])
self.resStart = value
if value > self.resEnd:
self.resEnd = value
return
if attrName.startswith("res"):
value = int(value.split()[1])
setattr(self, attrName, value)
def getResStartNameId(self):
if self.resStart == 0:
return "All"
poly = polymers[self.chainID]
return poly.getResNameId(self.resStart)
def getResEndNameId(self):
if self.resEnd == -1:
return ""
poly = polymers[self.chainID]
return poly.getResNameId(self.resEnd)
## Return a list of Chimera residue objects
# @return a list containing the residues.
def getChimeraSelection(self):
if self.chainID == "":
return []
poly = polymers[self.chainID]
if self.resStart == "All":
return poly.getChimeraResidues(start=poly.firstResNum)
return poly.getChimeraResidues(self.resStart, self.resEnd)
# ## Synchronize the contacts list with MMB
def refreshContactStretches():
global contacts
mmbContacts = pyMMB.getContactStretches()
contacts[:] = []
for c in mmbContacts:
Contact.mutateContactStretch_wrapper(c, currentModel, False)
contacts.append(c)
##################################################################################################
## Defines a MMB's contactWithin
# Some methods are used as callbacks for Tk widgets.
# Others are used to get information from Chimera
class ContactWithin(pyMMB.ContactWithin_wrapper, MMB_UI_Object):
## Constructor
def __init__(self, model=currentModel, mmbID=-1, contactScheme="AllAtomSterics", chain="", res=0, radius=0.4, new=True):
MMB_UI_Object.__init__(self, model, mmbID)
self.contactScheme = contactScheme
self.radius = radius
self.chainID = chain
self.resID = res
self.initialization(model, new)
def initialization(self, model=currentModel, new=True, selection=None, valid=True):
self.model = model
self.new = new
self.selection = selection
self.valid = valid
@classmethod
## Mutate a ContactWithin_wrapper object's type to ContactWithin
def mutateContactWithin_wrapper(cls, ContactWithin_wrapper_object, model=currentModel, new=False, selection=None, valid=True):
ContactWithin_wrapper_object.__class__ = cls
ContactWithin_wrapper_object.initialization(model, new, selection, valid)
## Initialize the residue when changing chain
def setAttribute(self, attrName, value):
if attrName.startswith("chain"):
setattr(self, "resID", 0)
MMB_UI_Object.setAttribute(self, attrName, value)
def getResNameId(self):
if self.resID == 0:
return ""
poly = polymers[self.chainID]
return poly.getResNameId(self.resID)
def getChimeraSelection(self):
if self.resID != 0 and self.radius > 0:
# resWithin = pyMMB.getResiduesWithin(self.chainID, self.resID, self.radius)
# return [self.model.findResidue( MolResId(x[0], x[1])) for x in resWithin]
return getResiduesWithin(self.chainID, self.resID, self.radius*10.0)
## Synchronize the contactsWithin list with MMB
def refreshContactWithin():
global contactsWithin
mmbContacts = pyMMB.getContactsWithin()
contactsWithin[:] = []
for m in mmbContacts:
ContactWithin.mutateContactWithin_wrapper(m, currentModel)
contactsWithin.append(m)
def selectionIntersection(sel1, sel2):
sel1_IDs = set([(a.residue.id.position,a.name) for a in sel1])
sel2_IDs = set([(a.residue.id.position,a.name) for a in sel2])
intersect = sel1_IDs.intersection(sel2_IDs)
sel1_final = [a for a in sel1 if (a.residue.id.position,a.name) in intersect]
sel2_final = [a for a in sel2 if (a.residue.id.position,a.name) in intersect]
return sel1_final, sel2_final
##################################################################################################
## Defines a MMB's threading
# Some methods are used as callbacks for Tk widgets.
# Others are used to get information from Chimera
class Threading(pyMMB.ThreadingStruct_wrapper, MMB_UI_Object):
## Constructor
def __init__(self, model=currentModel, mmbID=-1, chain1="", resStart1=0, resEnd1=0,
chain2="", resStart2=0, resEnd2=0,
forceConstant=3.0, backboneOnly=False, new=True):
MMB_UI_Object.__init__(self, model, mmbID)
self.chainID1 = chain1
self.residueStart1 = resStart1
self.residueEnd1 = resEnd1
self.chainID2 = chain2
self.residueStart2 = resStart2
self.residueEnd2 = resEnd2
self.forceConstant = forceConstant
self.backboneOnly = backboneOnly
self.initialization(model, new)
def initialization(self, model=currentModel, new=True, selection=None, valid=True):
self.model = model
self.new = new
self.selection = selection
self.valid = valid
@classmethod
## Mutate a ThreadingStruct_wrapper object's type to Threading
def mutateThreadingStruct_wrapper(cls, ThreadingStruct_wrapper_object, model=currentModel, new=False, selection=None, valid=True):
ThreadingStruct_wrapper_object.__class__ = cls
ThreadingStruct_wrapper_object.initialization(model, new, selection, valid)
## Initialize the residue when changing chain
def setAttribute(self, attrName, value):
if attrName == "chain1":
setattr(self, "residueStart1", 0)
setattr(self, "residueStart2", 0)
elif attrName == "chain2":
setattr(self, "residueStart1", 0)
setattr(self, "residueStart2", 0)
MMB_UI_Object.setAttribute(self, attrName, value)
def getResStart1NameId(self):
if self.residueStart1 == 0:
return ""
poly = polymers[self.chainID1]
return poly.getResNameId(self.residueStart1)
def getResStart2NameId(self):
if self.residueStart2 == 0:
return ""
poly = polymers[self.chainID2]
return poly.getResNameId(self.residueStart2)
def getResEnd1NameId(self):
if self.residueEnd1 == 0:
return ""
poly = polymers[self.chainID1]
return poly.getResNameId(self.residueEnd1)
def getResEnd2NameId(self):
if self.residueEnd2 == 0:
return ""
poly = polymers[self.chainID2]
return poly.getResNameId(self.residueEnd2)
def getChimeraSelection1(self):
sel = []
if self.chainID1:
poly = polymers[self.chainID1]
sel += poly.getChimeraResidues(self.residueStart1, self.residueEnd1)
return sel
def getChimeraSelection2(self):
sel = []
if self.chainID2:
poly = polymers[self.chainID2]
sel += poly.getChimeraResidues(self.residueStart2, self.residueEnd2)
return sel
def getChimeraSelection(self):
if self.chainID1 == "" and self.chainID2 == "":
return []
sel = []
sel += self.getChimeraSelection1()
sel += self.getChimeraSelection2()
return sel
def matchSelections(self):
selRes1 = self.getChimeraSelection1()
selRes2 = self.getChimeraSelection2()
sel1 = []
[sel1.extend(r.atoms) for r in selRes1]
sel2 = []
[sel2.extend(r.atoms) for r in selRes2]
inter1, inter2 = selectionIntersection(sel1, sel2)
Midas.match(inter1, inter2, move="chains")
## Synchronize the mobilizersWithin list with MMB
def refreshThreadings():
global threadings
mmbThreadings = pyMMB.getThreadingStructs()
threadings[:] = []
for m in mmbThreadings:
Threading.mutateThreadingStruct_wrapper(m, currentModel)
threadings.append(m)
##################################################################################################
## Defines a MMB's GappedThreading
# Some methods are used as callbacks for Tk widgets.
# Others are used to get information from Chimera
class GappedThreading(pyMMB.GappedThreadingStruct_wrapper, MMB_UI_Object):
## Constructor
def __init__(self, model=currentModel, mmbID=-1, chain1="",
chain2="",
forceConstant=3.0, backboneOnly=False, new=True):
MMB_UI_Object.__init__(self, model, mmbID)
self.chainID1 = chain1
self.chainID2 = chain2
self.forceConstant = forceConstant
self.backboneOnly = backboneOnly
self.initialization(model, new)
def initialization(self, model=currentModel, new=True, selection=None, valid=True):
self.model = model
self.new = new
self.selection = selection
self.valid = valid
@classmethod
## Mutate a GappedThreadingStruct_wrapper object's type to Threading
def mutateGappedThreadingStruct_wrapper(cls, GappedThreadingStruct_wrapper_object, model=currentModel, new=False, selection=None, valid=True):
GappedThreadingStruct_wrapper_object.__class__ = cls
GappedThreadingStruct_wrapper_object.initialization(model, new, selection, valid)
## Initialize the residue when changing chain
def setAttribute(self, attrName, value):
MMB_UI_Object.setAttribute(self, attrName, value)
def getChimeraSelection1(self):
sel = []
if self.chainID1:
poly = polymers[self.chainID1]
sel += poly.getChimeraResidues()
return sel
def getChimeraSelection2(self):
sel = []
if self.chainID2:
poly = polymers[self.chainID2]
sel += poly.getChimeraResidues()
return sel
def getChimeraSelection(self):
if self.chainID1 == "" and self.chainID2 == "":
return []
sel = []
sel += self.getChimeraSelection1()
sel += self.getChimeraSelection2()
return sel
def matchSelections(self):
selRes1 = self.getChimeraSelection1()
selRes2 = self.getChimeraSelection2()
sel1 = []
[sel1.extend(r.atoms) for r in selRes1]
sel2 = []
[sel2.extend(r.atoms) for r in selRes2]
inter1, inter2 = selectionIntersection(sel1, sel2)
Midas.match(inter1, inter2, move="chains")
## Synchronize the mobilizersWithin list with MMB
def refreshGappedThreadings():
global gappedThreadings
mmbThreadings = pyMMB.getGappedThreadingStructs()
gappedThreadings[:] = []
for m in mmbThreadings:
GappedThreading.mutateGappedThreadingStruct_wrapper(m, currentModel)
gappedThreadings.append(m)
##################################################################################################
## Defines a MMB's AtomSpring
# Some methods are used as callbacks for Tk widgets.
# Others are used to get information from Chimera
class AtomSpring(pyMMB.AtomSpring_wrapper, MMB_UI_Object):
## Constructor
def __init__(self, model=currentModel, mmbID=-1, chain1="",
res1 = 0,
atom1 = "",
chain2="",
res2 = 0,
atom2 = "",
tether = False,
toGround = False,
deadLength = 0.0,
forceConstant=3.0,
new=True):
MMB_UI_Object.__init__(self, model, mmbID)
self.atom1 = atom1
self.atom2 = atom2
self.res1 = res1
self.res2 = res2
self.chain1 = chain1
self.chain2 = chain2
self.toGround = toGround
self.tether = tether
self.forceConstant= forceConstant
self.deadLength = deadLength
self.initialization(model, new)
def initialization(self, model=currentModel, new=True, selection=None, valid=True):
self.model = model
self.new = new
self.selection = selection
self.valid = valid
self.chimeraBond = None
@classmethod
## Mutate a AtomSpring_wrapper object's type to AtomSpring
def mutateAtomSpring_wrapper(cls, AtomSpring_wrapper_object, model=currentModel, new=False, selection=None, valid=True):
AtomSpring_wrapper_object.__class__ = cls
AtomSpring_wrapper_object.initialization(model, new, selection, valid)
def setAttribute(self, attrName, value):
if getattr(self, attrName) == value:
return
# if the attribute is res1 or res2 we set the corresponding atom to ""
if "res" in attrName:
nAttr = attrName[-1]
atomAttr = "atom"+nAttr
setattr(self, atomAttr, "")
if value:
value = int(value.split()[1])
else:
value = 0
# if the attribute is chain1 or chain2 we set the corresponding res,atom to 0,""
elif "Chain" in attrName:
nAttr = attrName.split("n")[1]
setattr(self, "res"+nAttr, 0)
setattr(self, "atom"+nAttr, "")
setattr(self, attrName, value)
def getRes1AtomsList(self):
return getAtomsList(self.chain1, self.res1, self.model)
def getRes2AtomsList(self):
return getAtomsList(self.chain2, self.res2, self.model)
def getRes1NameId(self):
if self.res1 == 0:
return ""
return polymers[self.chain1].getResNameId(self.res1)
def getRes2NameId(self):
if self.res2 == 0:
return ""
return polymers[self.chain2].getResNameId(self.res2)
def getChimeraSelection(self):
if self.chain1 == 0 or self.res1 == 0:
return
res1 = self.model.findResidue(MolResId(self.chain1, self.res1))
selection = [self.chimeraBond]
if self.atom1 != "":
selection.append(res1.findAtom(self.atom1))
else:
selection.append(res1)
if self.res2 == 0:
return selection
res2 = self.model.findResidue(MolResId(self.chain2, self.res2))
if self.atom2 != "":
selection.append(res2.findAtom(self.atom2))
else:
selection.append(res2)
return selection
## Update the bond display in Chimera. Create a new bond if necessary.
def updateRepresentation(self):
global atomSpringsBonds
# No bond
if self.res2 == 0:
if self.chimeraBond:
atomSpringsBonds.deletePseudoBond(self.chimeraBond)
self.chimeraBond = None
return
res1 = self.model.findResidue(MolResId(self.chain1, self.res1))
res2 = self.model.findResidue(MolResId(self.chain2, self.res2))
a1 = self.atom1
a2 = self.atom2
if self.atom1 == "":
a1 = polymers[self.chain1].getRepresentativeAtom()
if self.atom2 == "":
a2 = polymers[self.chain2].getRepresentativeAtom()
b = self.chimeraBond
if b:
b.reuse(res1.findAtom(a1), res2.findAtom(a2))
# b.label = "||"
self.updateRepresentationColor()
return
b = atomSpringsBonds.newPseudoBond(res1.findAtom(a1), res2.findAtom(a2))
b.display = 1
b.drawMode = Bond.Spring
# b.label = "||"
self.chimeraBond = b
self.updateRepresentationColor()
## Update the bond's color according to state of the interaction in the GUI.
# New atomspring: blue
# Non validated atomspring: orange
# Normal state: black
def updateRepresentationColor(self):
if self.new:
self.chimeraBond.color = colorTable.getColorByName("blue")
return
if not self.valid:
self.chimeraBond.color = colorTable.getColorByName("orange")
return
self.chimeraBond.color = colorTable.getColorByName("black")
## Synchronize the atomsprings list with MMB
def refreshAtomSprings():
global atomSprings
mmbAtomsprings = pyMMB.getAtomSprings()
atomSprings[:] = []
for c in mmbAtomsprings:
AtomSpring.mutateAtomSpring_wrapper(c, currentModel)
atomSprings.append(c)
refreshAtomSpringsBonds()
## Update base pair bonds display
def refreshAtomSpringsBonds():
global atomSpringsBonds
if atomSpringsBonds:
atomSpringsBonds.deleteAll()
else:
atomSpringsBonds = misc.getPseudoBondGroup('atomsprings', modelID=OpenModels.Default, hidden=False)
atomSpringsBonds.name = "MMB AtomSprings"
atomSpringsBonds.lineWidth = 2
atomSpringsBonds.color = colorTable.getColorByName("black")
[p.updateRepresentation() for p in atomSprings]
##################################################################################################
class RootMobilizer(MMB_UI_Object):
rootMobilizerTypes = ["Free", "Weld"]
## Constructor
def __init__(self, model=currentModel, mmbID=-1, chainID="", rootMobilizer=""):
MMB_UI_Object.__init__(self, model, mmbID)
self.chainID = chainID
self.rootMobilizer = rootMobilizer
self.new = False
self.valid = True
def __str__(self):
return "rootMobilizer " + self.chainID + " " + self.rootMobilizer
def mmbUpdate(self):
pyMMB.setRootMobilizer(self.chainID, self.rootMobilizer)
def getChimeraSelection(self):
if self.chainID == "":
return []
sel = []
poly = polymers[self.chainID]
sel += poly.getChimeraResidues()
return sel
def updateRepresentation(self):
if self.chainID:
r = self.getChimeraSelection()[0]
r.label = u"\u22A5"
self.updateRepresentationColor()
def updateRepresentationColor(self):
if self.chainID:
r = self.getChimeraSelection()[0]
r.labelColor = colorTable.getColorByName("black")
r.ribbonColor = colorTable.getColorByName("black")
for a in r.atoms:
a.color = colorTable.getColorByName("black")
def refreshRootMobilizers():
global rootMobilizers
rootMobilizers[:] = []
for p in polymers.values():
mobi = p.getRootMobilizer()
rootMobilizers.append(RootMobilizer(chainID=p.chainID, rootMobilizer=mobi))
##################################################################################################
## Defines a MMB's Density stretch
# Some methods are used as callbacks for Tk widgets.
# Others are used to get information from Chimera
class Density(pyMMB.DensityStretch_wrapper, MMB_UI_Object):
## Constructor
def __init__(self, model=currentModel, mmbID=-1, chain="", resStart=0, resEnd=-1, new=True):
MMB_UI_Object.__init__(self, model, mmbID)
self.chainID = chain
self.resStart = resStart
self.resEnd = resEnd
self.initialization(model, new)
def initialization(self, model=currentModel, new=False, valid=True):
self.model = model
self.valid = valid
self.new = new
@classmethod
## Mutate a DensityStretch_wrapper object's type to Density
def mutateDensityStretch_wrapper(cls, DensityStretch_wrapper_object, model=currentModel, new=False, valid=True):
DensityStretch_wrapper_object.__class__ = cls
DensityStretch_wrapper_object.initialization(model, new, valid)
def setAttribute(self, attrName, value):
if getattr(self, attrName) == value:
return
if attrName == "resStart" and value == "All":
self.resStart = 0
self.resEnd = -1
return
if attrName == "resStart":
value = int(value.split()[1])
self.resStart = value
if value > self.resEnd:
self.resEnd = value
return
if attrName.startswith("res"):
value = int(value.split()[1])
setattr(self, attrName, value)
def getResStartNameId(self):
if self.resStart == 0:
return "All"
poly = polymers[self.chainID]
return poly.getResNameId(self.resStart)
def getResEndNameId(self):
if self.resEnd == -1:
return ""
poly = polymers[self.chainID]
return poly.getResNameId(self.resEnd)
## Return a list of Chimera residue objects
# @return a list containing the residues.
def getChimeraSelection(self):
if self.chainID == "":
return []
poly = polymers[self.chainID]
if self.resStart == "All":
return poly.getChimeraResidues(start=poly.firstResNum)
return poly.getChimeraResidues(self.resStart, self.resEnd)
# ## Synchronize the densitys list with MMB
def refreshDensityStretches():
global densities
mmbDensities = pyMMB.getDensityStretches()
densities[:] = []
for c in mmbDensities:
Density.mutateDensityStretch_wrapper(c, currentModel, False)
densities.append(c)
##################################################################################################
## Open a density map (Xplor format only) and set MMB's parameters accordingly
def openDensityMap(filepath):
global densityMapModel
if densityMapModel and filepath == MMBparameters.densityFileName:
return
try:
newModel = chimera.openModels.open(filepath)[0]
except Exception as e:
return
if densityMapModel:
densityMapModel.close()
densityMapModel = newModel
MMBparameters.densityFileName = filepath
##################################################################################################
## Display molecules with MMB information
def applyMMBVisualization():
from chimera import runCommand as rc
rc("~display")
rc("color none")
rc("~rlabel")
rc("ribspline bspline")
rc("ribbackbone")
rc("ribscale 'Chimera default'")
for array in allConstraintsRestraints.values():
for command in array:
command.updateRepresentation()
rc("window")
##################################################################################################
## Commands to string
def commandsToStr():
buff = ""
buff += "loadSequencesFromPdb mmb.pdb"
if (allResiduesWithins + includeAllNonBondAtomsInResidues) or getMMBParameter("physicsRadius")>0:
buff += ("setDefaultMDParameters\n\n")
for p in polymers.values():
if p.activePhysics == False:
buff += "deactivatePhysics "+p.chainID+"\n"
buff += "\n"
for array in allConstraintsRestraints.values():
for command in array:
buff += (str(command)+"\n")
buff += ("\n")
# print MMBparameters.nonDefaultParameters
for p in MMBparameters.nonDefaultParameters:
if p != "converged":
buff += p + " " + str(getMMBParameter(p)) + "\n"
return buff
## Dump commands
def dumpCommands(fileName):
buff = commandsToStr()
fileOut = open(fileName, "w")
fileOut.write(buff)
fileOut.close()
return buff
# def resetMMB(keepCurrentFrame=False):
# if keepCurrentFrame:
##################################################################################################
# Bindings with pyMMB
## Execute a set of MMB commands.
# @param a list of MMB commands lines
def sendMMBCmds(cmdsLines):
ignore = ["RNA","DNA","protein"]
ignored = []
notSupported = ["read"]
for l in cmdsLines:
l = l.strip()
if not l:
continue
tmp = [l.startswith(s) for s in ignore]
if True in tmp:
print "Ignored the following command:"
print l
ignored.append(l)
continue
tmp = [l.startswith(s) for s in notSupported]
if True in tmp:
return ">>> "+l.split()[0]
if l.startswith("loadSequencesFromPdb"):
if polymersInitialized:
print "Ignored the following command:"
print l
ignored.append(l)
continue
cmd = l.split()
if ( len(cmd) == 1 ):
fileName = "last.0.pdb"
else:
fileName = cmd[1]
fileName = workDir+'/'+fileName
l = 'loadSequencesFromPdb '+fileName
pyMMB.cmd(l)
return ignored
## Add an MMB object to MMB using its MMB command returned by str(mmbObj)
# @param mmbObj the object to add
def addMMBObject(mmbObj):
pyMMB.cmd(str(mmbObj))
## Clear MMB forces and constraints
def clearForcesAndConstraints():
pyMMB.clearForcesAndConstraints()
def setDefaultMDParameters():
pyMMB.cmd("setDefaultMDParameters")
##################################################################################################
# MMB simulation control
import timeit
## Asynchronous control of MMB simulation
class MMBSimulationThread(threading.Thread):
test = 0
## Constructor
# @param callback a function called after of each frame
# @param maxFrame maximum number of frame to compute
def __init__(self, maxFrame = 0, endCallback = None):
threading.Thread.__init__(self)
self.running = threading.Event()
self.running.set()
self.callbacks = []
if endCallback and not hasattr(endCallback, '__call__'):
raise MMBError("MMBSimulationThread::addCallback", "Argument is not callable")
self.endCallback = endCallback
self.maxFrame = maxFrame
self.currentFrame = 0
pyMMB.MMBparameters.converged = False
MMBSimulationThread.test += 1
print "Ready to run"
def run(self):
global runFlag
runFlag = True
t0 = time.clock()
print "HELLO:", MMBSimulationThread.test
while self.currentFrame < self.maxFrame and not pyMMB.MMBparameters.converged:
print "Running"
pyMMB.runOneStep()
self.currentFrame += 1
for cb in self.callbacks:
cb()
self.running.wait()
runFlag = False
print time.clock() - t0
self.endCallback()
def addCallback(self, callback):
if callback in self.callbacks:
return
if not hasattr(callback, '__call__'):
raise MMBError("MMBSimulationThread::addCallback", "Argument is not callable")
self.callbacks.append(callback)
def pause(self):
self.running.clear()
def restart(self):
self.running.set()
def stop(self):
self.currentFrame = self.maxFrame
def addLastFrameFromMMB(movieDialog):
global currentModel
pos = pyMMB.getSystemCoordinates()
# print len(pos)
movieDialog.addFrame(pos)
# cs = currentModel.newCoordSet(len(currentModel.coordSets))
# chimera.fillCoordSet(cs, currentModel.atoms, pos)
# currentModel.activeCoordSet = cs
# movieDialog.updateFrames()
## Init MMB simulation and display the MDMovie dialog
def initSimulation():
global polymers
for p in polymers.values():
p.matchCoordinatesFromChimera()
pyMMB.cmd("numReportingIntervals 1000")
pyMMB.postInitialize()
pyMMB.initializeBodies()
pyMMB.initDynamics()
global currentModel
cs = currentModel.activeCoordSet
chimera.fillCoordSet(cs, currentModel.atoms, pyMMB.getSystemCoordinates())
chimera.runCommand("window #"+str(currentModel.id))
global ensemble
if not ensemble:
ensemble = MolEnsemble(currentModel)
global movieDialog
if not movieDialog:
movieDialog = MyMovieDialog(ensemble, externalEnsemble=True)
global initFlag
initFlag = True
## Run n intervals of the simulation.
# Each new frame is added to the MDMovie dialog
# The callback function is called after each frame
def runIntervals(n=1, callback=None, endCallback=None):
"""
n is the number of intervals to compute
"""
global currentSimulation
global movieDialog
if currentSimulation and currentSimulation.isAlive():
raise pyMMB.MMBError("MMB_UI.runIntervals", "A simulation is currently running.")
currentSimulation = MMBSimulationThread(maxFrame=n, endCallback=endCallback)
currentSimulation.addCallback(lambda md=movieDialog: addLastFrameFromMMB(md))
currentSimulation.addCallback(callback)
currentSimulation.start()
## Pause a simulation
def pauseSimulation():
global currentSimulation
if currentSimulation and currentSimulation.isAlive():
currentSimulation.pause()
else:
raise pyMMB.MMBError("MMB_UI.pauseSimulation", "No simulation is currently running.")
## Continue the current paused simulation
def continueSimulation():
global currentSimulation
if currentSimulation and not currentSimulation.isAlive():
raise pyMMB.MMBError("MMB_UI.continueSimulation", "No simulation is currently running.")
currentSimulation.restart()
## Stop a simulation
def stopSimulation():
global currentSimulation
if currentSimulation and currentSimulation.isAlive():
currentSimulation.stop()
else:
raise pyMMB.MMBError("MMB_UI.stopSimulation", "No simulation is currently running.")
## Return the number of satisfied and unsatisfied base pairs in a tuple
# @return a tuple with the numbers
def getSatisfiedBasePairs():
oks = pyMMB.getNumSatisfiedBasePairs()
notOks = pyMMB.getNumUnSatisfiedBasePairs()
return (oks, notOks)
def getCurrentFrameNumber():
if currentSimulation:
return currentSimulation.currentFrame
return 0
|