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
|
// -*- Mode: C++; tab-width: 2; -*-
// vi: set ts=2:
//
#include <BALL/VIEW/DIALOGS/molecularFileDialog.h>
#include <BALL/VIEW/KERNEL/mainControl.h>
#include <BALL/VIEW/KERNEL/message.h>
#include <BALL/FORMAT/INIFile.h>
#include <BALL/FORMAT/PDBFile.h>
#include <BALL/FORMAT/HINFile.h>
#include <BALL/FORMAT/MOLFile.h>
#include <BALL/FORMAT/MOL2File.h>
#include <BALL/FORMAT/SDFile.h>
#include <BALL/FORMAT/antechamberFile.h>
#include <BALL/FORMAT/XYZFile.h>
#include <BALL/MATHS/simpleBox3.h>
#include <BALL/KERNEL/system.h>
#include <QtWidgets/QMenuBar>
#include <QtWidgets/QFileDialog>
#include <QtWidgets/QToolBar>
namespace BALL
{
namespace VIEW
{
MolecularFileDialog::MolecularFileDialog(QWidget *parent, const char* name)
: QWidget(parent),
ModularWidget(name),
save_id_(0),
read_all_pdb_models_(false)
{
#ifdef BALL_VIEW_DEBUG
Log.error() << "new MolecularFileDialog " << this << std::endl;
#endif
// register the widget with the MainControl
registerWidget(this);
setObjectName(name);
hide();
}
MolecularFileDialog::~MolecularFileDialog()
{
#ifdef BALL_VIEW_DEBUG
Log.info() << "Destructing object " << (void *)this
<< " of class MolecularFileDialog" << std::endl;
#endif
}
void MolecularFileDialog::initializeWidget(MainControl&)
{
String description = "Shortcut|File|Open_Structure";
open_id_ = insertMenuEntry(MainControl::FILE_OPEN, tr("&Structure"), this,
SLOT(readFiles()), description, QKeySequence::Open,
tr("Open a molecular file (e.g. PDB,MOL2,SDF)"),
UIOperationMode::MODE_ADVANCED);
setIcon(open_id_, "actions/document-open", true);
description = "Shortcut|File|Save_Structure";
save_id_ = insertMenuEntry(MainControl::FILE, tr("&Save Structure"), this,
SLOT(writeFile()), description, QKeySequence::Save,
tr("Save the highlighted System (e.g. as PDB,MOL2,SDF file)"),
UIOperationMode::MODE_ADVANCED);
setIcon(save_id_, "actions/document-save", true);
}
void MolecularFileDialog::readFiles()
{
QStringList files = QFileDialog::getOpenFileNames(
0,
tr("Choose a molecular file to open"),
getWorkingDir().c_str(),
getSupportedFileFormatsList().c_str());
for (QStringList::Iterator it = files.begin(); it != files.end(); ++it)
{
// construct a name for the system(the filename without the dir path)
openMolecularFile(ascii(*it));
}
}
String MolecularFileDialog::getSupportedFileFormats() const
{
return String("*.pdb *.brk *.ent *.hin *.mol *.mol2 *.sdf *.ac *.xyz");
}
String MolecularFileDialog::getSupportedFileFormatsList() const
{
QString sl;
sl += tr("Molecular files") + " (*.pdb *.brk *.ent *.hin *.mol *.mol2 *.sdf *.ac *.xyz);;";
sl += "PDB " + tr("files") + " (*.pdb *.brk *.ent);;";
sl += "HIN " + tr("files") + " (*.hin);;";
sl += "MOL " + tr("files") + " (*.mol);;";
sl += "MOL2 "+ tr("files") + " (*.mol2);;";
sl += "SD " + tr("files") + " (*.sdf);;";
sl += "AC " + tr("files") + " (*.ac);;";
sl += "XYZ " + tr("files") + " (*.xyz)";
return (String)sl;
}
System* MolecularFileDialog::openMolecularFile(const String& file)
{
vector<String> fields;
String seperators(FileSystem::PATH_SEPARATOR);
// workaround on windows: QT returns the filename in linux style
// but I am not sure, if this will stay this way.
#ifdef BALL_OS_WINDOWS
seperators += "/";
#endif
Position p = file.split(fields, seperators.c_str()) -1;
String filename = fields[p];
setWorkingDirFromFilename_(file);
String extension = fields[filename.split(fields, ".") -1];
filename = filename.getSubstring(0, filename.size() - (extension.size() + 1));
return openMolecularFile(file, extension, filename);
}
System* MolecularFileDialog::openMolecularFile(const String& filename,
const String& filetype,
const String& system_name)
{
bool ok = false;
try
{
if (File::isReadable(filename)) ok = true;
}
catch(...)
{}
if (!ok || filename == "/" || filename == "\\")
{
setStatusbarText((String)tr("Could not open file") + " " + filename, true);
return 0;
}
// TODO substitue by GenericMolFile* file = MolFileFactory::open(filename);
if (filetype.hasSubstring("pdb") || filetype.hasSubstring("PDB") ||
filetype.hasSubstring("brk") || filetype.hasSubstring("BRK") ||
filetype.hasSubstring("ent") || filetype.hasSubstring("ENT"))
{
return readPDBFile(filename, system_name);
}
else if (filetype.hasSubstring("HIN") ||
filetype.hasSubstring("hin"))
{
return readHINFile(filename, system_name);
}
else if (filetype.hasSubstring("MOL2") ||
filetype.hasSubstring("mol2"))
{
return readMOL2File(filename, system_name);
}
else if (filetype.hasSubstring("MOL") ||
filetype.hasSubstring("mol"))
{
return readMOLFile(filename, system_name);
}
else if (filetype.hasSubstring("SDF") ||
filetype.hasSubstring("sdf"))
{
return readSDFile(filename, system_name);
}
else if (filetype.hasSubstring("AC") ||
filetype.hasSubstring("ac"))
{
return readACFile(filename, system_name);
}
else if (filetype.hasSubstring("XYZ") ||
filetype.hasSubstring("xyz"))
{
return readXYZFile(filename, system_name);
}
else
{
setStatusbarText(String(tr("Unknown filetype")) + ": " + filetype, true);
}
return 0;
}
bool MolecularFileDialog::writeFile()
{
list<Composite*>& selection = getMainControl()->getMolecularControlSelection();
if (selection.size() != 1 || !RTTI::isKindOf<System> (*selection.begin()))
{
setStatusbarText((String)tr("Not a single system selected! Aborting writing..."), true);
return false;
}
const System& system = *(const System*) (*selection.begin());
String file_name = getWorkingDir();
file_name += FileSystem::PATH_SEPARATOR;
file_name += system.getName();
file_name += ".pdb";
QString s = QFileDialog::getSaveFileName(
0,
tr("Choose a filename to save the selected system"),
file_name.c_str(),
getSupportedFileFormats().c_str());
if (s == QString::null) return false;
setStatusbarText((String)tr("writing file..."));
String filename = ascii(s);
setWorkingDirFromFilename_(filename);
String filter(filename);
while (filter.has(FileSystem::PATH_SEPARATOR))
{
filter = filter.after(FileSystem::PATH_SEPARATOR);
}
String filename_without_path = filter;
while (filter.has('.'))
{
filter = filter.after(".");
}
bool result = false;
if (filter == "PDB" || filter == "pdb" ||
filter == "ent" || filter == "ENT" ||
filter == "brk" || filter == "BRK")
{
result = writePDBFile(filename, system);
}
else if (filter.hasSubstring("HIN") || filter.hasSubstring("hin"))
{
result = writeHINFile(filename, system);
}
else if (filter.hasSubstring("MOL2") || filter.hasSubstring("mol2"))
{
result = writeMOL2File(filename, system);
}
else if (filter.hasSubstring("MOL") || filter.hasSubstring("mol"))
{
result = writeMOLFile(filename, system);
}
else if (filter.hasSubstring("SDF") || filter.hasSubstring("sdf"))
{
result = writeSDFile(filename, system);
}
else if (filter.hasSubstring("AC") || filter.hasSubstring("ac"))
{
result = writeACFile(filename, system);
}
else if (filter.hasSubstring("XYZ") || filter.hasSubstring("xyz"))
{
result = writeXYZFile(filename, system);
}
else
{
if (filter == filename_without_path)
{
filename += ".pdb";
result = writePDBFile(filename, system);
}
else
{
setStatusbarText((String)tr("Unknown file format, please set the file extension accordingly to type, aborting..."), true);
return false;
}
}
if (!result)
{
return false;
}
setStatusbarText(String(system.countAtoms()) + " " + (String)tr("atoms written to file") + " \"" + filename + "\"", true);
return true;
}
bool MolecularFileDialog::writePDBFile(String filename, const System& system)
{
try
{
PDBFile file(filename, std::ios::out);
file << system;
file.close();
}
catch(Exception::GeneralException& e)
{
Log.error() << e << std::endl;
setStatusbarText((String)tr("Writing of PDB file failed, see logs!"), true);
return false;
}
return true;
}
bool MolecularFileDialog::writeHINFile(String filename, const System& system)
{
try
{
HINFile file(filename, std::ios::out);
file << system;
file.close();
}
catch(Exception::GeneralException& e)
{
Log.error() << e << std::endl;
setStatusbarText((String)tr("Writing of HIN file failed, see logs!"), true);
return false;
}
return true;
}
bool MolecularFileDialog::writeMOLFile(String filename, const System& system)
{
try
{
MOLFile file(filename, std::ios::out);
file << system;
file.close();
}
catch(Exception::GeneralException& e)
{
Log.error() << e << std::endl;
setStatusbarText((String)tr("Writing of MOL file failed, see logs!"), true);
return false;
}
return true;
}
bool MolecularFileDialog::writeMOL2File(String filename, const System& system)
{
try
{
MOL2File file(filename, std::ios::out);
file << system;
file.close();
}
catch(Exception::GeneralException& e)
{
Log.error() << e << std::endl;
setStatusbarText((String)tr("Writing of MOL2 file failed, see logs!"), true);
return false;
}
return true;
}
bool MolecularFileDialog::writeSDFile(String filename, const System& system)
{
try
{
SDFile file(filename, std::ios::out);
file << system;
file.close();
}
catch(Exception::GeneralException& e)
{
Log.error() << e << std::endl;
setStatusbarText((String)tr("Writing of SD file failed, see logs!"), true);
return false;
}
return true;
}
bool MolecularFileDialog::writeACFile(String filename, const System& system)
{
try
{
AntechamberFile file(filename, std::ios::out);
file << system;
file.close();
}
catch(Exception::GeneralException& e)
{
Log.error() << e << std::endl;
setStatusbarText((String)tr("Writing of AC file failed, see logs!"), true);
return false;
}
return true;
}
bool MolecularFileDialog::writeXYZFile(String filename, const System& system)
{
try
{
XYZFile file(filename, std::ios::out);
file << system;
file.close();
}
catch(Exception::GeneralException& e)
{
Log.error() << e << std::endl;
setStatusbarText((String)tr("Writing of XYZ file failed, see logs!"), true);
return false;
}
return true;
}
System* MolecularFileDialog::readPDBFile(String filename, String system_name)
{
setStatusbarText((String)tr("reading PDB file..."), true);
System* system = new System();
try
{
PDBFile pdb_file(filename);
if(read_all_pdb_models_) {
pdb_file.selectAllModels();
}
pdb_file >> *system;
pdb_file.close();
}
catch(Exception::GeneralException& e)
{
Log.error() << e << std::endl;
setStatusbarText((String)tr("Reading of PDB file failed, see logs!"), true);
delete system;
return 0;
}
if (!finish_(filename, system_name, system)) return 0;
return system;
}
System* MolecularFileDialog::readHINFile(String filename, String system_name)
{
bool has_periodic_boundary = false;
SimpleBox3 bounding_box;
setStatusbarText((String)tr("reading HIN file..."), true);
System* system = new System();
try
{
HINFile hin_file(filename);
hin_file >> *system;
has_periodic_boundary = hin_file.hasPeriodicBoundary();
bounding_box = hin_file.getPeriodicBoundary();
hin_file.close();
}
catch(Exception::GeneralException& e)
{
Log.error() << e << std::endl;
setStatusbarText((String)tr("Reading of HIN file failed, see logs!"), true);
delete system;
return 0;
}
// generating bounding box if exists
if (has_periodic_boundary)
{
/*
Box3* simple_box = new Box3;
Vector3 first, second;
bounding_box.get(first.x, first.y, first.z, second.x, second.y, second.z);
Log.info() << "> creating bounding box (" << first << ", " << second << ")" << std::endl;
simple_box->setVertex1(first);
simple_box->setVertex2(second);
simple_box->setColor("ff0000a0");
simple_box->setName("BoundingBox");
simple_box->setProperty(BALL::VIEW::GeometricObject::PROPERTY__OBJECT_TRANSPARENT);
*/
}
if (!finish_(filename, system_name, system)) return 0;
return system;
}
System* MolecularFileDialog::readMOLFile(String filename, String system_name)
{
setStatusbarText((String)tr("reading MOL file..."), true);
System* system = new System();
try
{
MOLFile mol_file(filename);
mol_file >> *system;
mol_file.close();
}
catch(Exception::GeneralException& e)
{
Log.error() << e << std::endl;
setStatusbarText((String)tr("Reading of MOL file failed, see logs!"), true);
delete system;
return 0;
}
if (!finish_(filename, system_name, system)) return 0;
return system;
}
System* MolecularFileDialog::readMOL2File(String filename, String system_name)
{
setStatusbarText((String)tr("reading MOL2 file..."), true);
System* system = new System();
try
{
MOL2File mol2_file(filename);
mol2_file >> *system;
mol2_file.close();
}
catch(Exception::GeneralException& e)
{
setStatusbarText(tr("Reading of MOL2 file failed, see logs!"), true);
Log.error() << e << std::endl;
delete system;
return 0;
}
if (!finish_(filename, system_name, system)) return 0;
return system;
}
System* MolecularFileDialog::readSDFile(String filename, String system_name)
{
setStatusbarText((String)tr("reading SD file..."), true);
System* system = new System();
try
{
SDFile sd_file(filename);
sd_file >> *system;
sd_file.close();
}
catch(Exception::GeneralException& e)
{
setStatusbarText((String)tr("Reading of SD file failed, see logs!"), true);
delete system;
return 0;
}
if (!finish_(filename, system_name, system)) return 0;
return system;
}
System* MolecularFileDialog::readACFile(String filename, String system_name)
{
setStatusbarText((String)tr("reading AC file..."), true);
System* system = new System();
try
{
AntechamberFile ac_file(filename);
ac_file >> *system;
ac_file.close();
}
catch(Exception::GeneralException& e)
{
setStatusbarText((String)tr("Reading of AC file failed, see logs!"), true);
delete system;
return 0;
}
if (!finish_(filename, system_name, system)) return 0;
return system;
}
System* MolecularFileDialog::readXYZFile(String filename, String system_name)
{
setStatusbarText((String)tr("reading XYZ file..."), true);
System* system = new System();
try
{
XYZFile sd_file(filename);
sd_file >> *system;
sd_file.close();
}
catch(Exception::GeneralException& e)
{
setStatusbarText((String)tr("Reading of XYZ file failed, see logs!"), true);
delete system;
return 0;
}
if (!finish_(filename, system_name, system)) return 0;
return system;
}
bool MolecularFileDialog::finish_(const String& filename, const String& system_name, System* system)
{
// writing info to log
setStatusbarText(String(tr("Read")) + " " + String(system->countAtoms()) + " " +
(String)tr("atoms from file") + " \"" + filename + "\"", true);
if (system->getName() == "")
{
system->setName(system_name);
}
if (filename[0] != FileSystem::PATH_SEPARATOR)
{
system->setProperty("FROM_FILE", getWorkingDir() + FileSystem::PATH_SEPARATOR + filename);
}
else
{
system->setProperty("FROM_FILE", filename);
}
// notify tree of a new composite
CompositeMessage* new_message = new CompositeMessage;
new_message->setComposite(*system);
new_message->setCompositeName(system_name);
new_message->setType(CompositeMessage::NEW_COMPOSITE);
notify_(new_message);
return true;
}
void MolecularFileDialog::checkMenu(MainControl& mc)
{
bool busy = mc.compositesAreLocked();
if (save_id_)
save_id_->setEnabled(!busy && mc.getSelectedSystem());
if (open_id_)
open_id_->setEnabled(!busy);
}
MolecularFileDialog::MolecularFileDialog(const MolecularFileDialog& mfd)
: QWidget(),
ModularWidget(mfd)
{}
System* MolecularFileDialog::openFile_(String type)
{
QStringList files = QFileDialog::getOpenFileNames(
0,
tr("Choose a molecular file to open"),
getWorkingDir().c_str(),
"*.*");
System* system = 0;
for (QStringList::Iterator it = files.begin(); it != files.end(); ++it)
{
vector<String> fields;
String seperators(FileSystem::PATH_SEPARATOR);
// workaround on windows: QT returns the filename in linux style
// but I am not sure, if this will stay this way.
#ifdef BALL_OS_WINDOWS
seperators += "/";
#endif
String file = ascii(*it);
Position p = file.split(fields, seperators.c_str()) -1;
String filename = fields[p];
setWorkingDirFromFilename_(file);
// construct a name for the system(the filename without the dir path)
system = openMolecularFile(file, type, filename);
}
return system;
}
System* MolecularFileDialog::openPDBFile()
{
return openFile_("PDB");
}
System* MolecularFileDialog::openHINFile()
{
return openFile_("HIN");
}
System* MolecularFileDialog::openMOLFile()
{
return openFile_("MOL");
}
System* MolecularFileDialog::openMOL2File()
{
return openFile_("MOL2");
}
System* MolecularFileDialog::openSDFile()
{
return openFile_("SDF");
}
System* MolecularFileDialog::openACFile()
{
return openFile_("AC");
}
System* MolecularFileDialog::openXYZFile()
{
return openFile_("XYZ");
}
void MolecularFileDialog::onNotify(Message *message)
{
if (RTTI::isKindOf<ControlSelectionMessage>(message))
{
checkMenu(*getMainControl());
}
}
bool MolecularFileDialog::canHandle(const String& fileformat) const
{
String formats = getSupportedFileFormats();
vector<String> fields;
formats.split(fields, "*. ");
for (Position p = 0; p < fields.size(); p++)
{
String s = fields[p];
if (s == fileformat) return true;
s.toUpper();
if (s == fileformat) return true;
}
return false;
}
bool MolecularFileDialog::openFile(const String& filename)
{
return openMolecularFile(filename) != 0;
}
} } //namespaces
|