Difference between revisions of "Developer:Perl Tutorial"

From Open Babel
Jump to: navigation, search
 
Line 22: Line 22:
 
     notatend = obconversion.Read(obmol);
 
     notatend = obconversion.Read(obmol);
 
}
 
}
 +
</pre>
 +
 +
<h2>Add and Delete Atoms</h2>
 +
 +
This script shows an example of deleting and modifying atoms to transform one structure to a related one. It operates on a set of substituted thiophenes, deletes the sulfur atom (note that R1 and R2 may contain sulfur, so the SMARTS pattern is designed to constrain to the ring sulfur), etc.
 +
 +
[[Image:Tutorial-Thiophene.png]]
 +
[[Image:Tutorial-Transform.png]]
 +
 +
<pre>
 +
use Chemistry::OpenBabel;
 +
 +
my $obMol = new Chemistry::OpenBabel::OBMol;
 +
my $obConversion = new Chemistry::OpenBabel::OBConversion;
 +
my $filename = shift @ARGV;
 +
 +
$obConversion->SetInAndOutFormats("xyz", "mol");
 +
$obConversion->ReadFile($obMol, $filename);
 +
 +
for (1..$obMol->NumAtoms()) {
 +
    $atom = $obMol->GetAtom($_);
 +
    # look to see if this atom is a thiophene sulfur atom
 +
    if ($atom->MatchesSMARTS("[#16D2]([#6D3H1])[#6D3H1]")) {
 +
$sulfurIdx = $atom->GetIdx();
 +
    # see if this atom is one of the carbon atoms bonded to a thiophene sulfur
 +
    } elsif ($atom->MatchesSMARTS("[#6D3H1]([#16D2][#6D3H1])[#6]") ) {
 +
if ($c2Idx == 0) { $c2Idx = $atom->GetIdx(); }
 +
else {$c5Idx = $atom->GetIdx(); }
 +
    }
 +
}
 +
 +
# Get the actual atom objects -- indexing will change as atoms are added and deleted!
 +
$sulfurAtom = $obMol->GetAtom($sulfurIdx);
 +
$c2Atom = $obMol->GetAtom($c2Idx);
 +
$c5Atom = $obMol->GetAtom($c5Idx);
 +
 +
$obMol->DeleteAtom($sulfurAtom);
 +
 +
$obMol->DeleteHydrogens($c2Atom);
 +
$obMol->DeleteHydrogens($c5Atom);
 +
 +
$c2Atom->SetAtomicNum(1);
 +
$c5Atom->SetAtomicNum(1);
 +
 +
$obConversion->WriteFile($obMol, "$filename.mol");
 
</pre>
 
</pre>
  
 
[[Category:Developer]]
 
[[Category:Developer]]

Revision as of 12:15, 12 May 2006

This tutorial is aimed at developers looking to use Open Babel with Perl and the Chemistry::OpenBabel module. It will focus on the object-oriented application programmers' interface (API). There is also documentation on the full API. Although Open Babel is written in C++, it also has wrappers to Perl and Python programming languages.

The tutorial is also available in other programming languages.

Please feel free to comment on or improve this tutorial by e-mailing to openbabel-scripting

Output Molecular Weight for a Multi-Molecule SDF File

Let's say we want to print out the molecular weights of every molecule in an SD file. Why? Well, we might want to plot a histogram of the distribution, or see whether the average of the distribution is significantly different (in the statistical sense) compared to another SD file.

use Chemistry::OpenBabel;

my $obconversion = new Chemistry::OpenBabel::OBConversion;
$obconversion.SetInFormat("sdf");
my $obmol = new Chemistry::OpenBabel::OBMol;

$notatend = obconversion.ReadFile(obmol, "../xsaa.sdf")
while (notatend) {
    print obmol.GetMolWt(), "\n";
    obmol.Clear();
    notatend = obconversion.Read(obmol);
}

Add and Delete Atoms

This script shows an example of deleting and modifying atoms to transform one structure to a related one. It operates on a set of substituted thiophenes, deletes the sulfur atom (note that R1 and R2 may contain sulfur, so the SMARTS pattern is designed to constrain to the ring sulfur), etc.

Tutorial-Thiophene.png Tutorial-Transform.png

use Chemistry::OpenBabel;

my $obMol = new Chemistry::OpenBabel::OBMol;
my $obConversion = new Chemistry::OpenBabel::OBConversion;
my $filename = shift @ARGV;

$obConversion->SetInAndOutFormats("xyz", "mol");
$obConversion->ReadFile($obMol, $filename);

for (1..$obMol->NumAtoms()) {
    $atom = $obMol->GetAtom($_);
    # look to see if this atom is a thiophene sulfur atom
    if ($atom->MatchesSMARTS("[#16D2]([#6D3H1])[#6D3H1]")) {
	$sulfurIdx = $atom->GetIdx();
    # see if this atom is one of the carbon atoms bonded to a thiophene sulfur
    } elsif ($atom->MatchesSMARTS("[#6D3H1]([#16D2][#6D3H1])[#6]") ) {
	if ($c2Idx == 0) { $c2Idx = $atom->GetIdx(); }
	else {$c5Idx = $atom->GetIdx(); }
    }
}

# Get the actual atom objects -- indexing will change as atoms are added and deleted!
$sulfurAtom = $obMol->GetAtom($sulfurIdx);
$c2Atom = $obMol->GetAtom($c2Idx);
$c5Atom = $obMol->GetAtom($c5Idx);

$obMol->DeleteAtom($sulfurAtom);

$obMol->DeleteHydrogens($c2Atom);
$obMol->DeleteHydrogens($c5Atom);

$c2Atom->SetAtomicNum(1);
$c5Atom->SetAtomicNum(1);

$obConversion->WriteFile($obMol, "$filename.mol");