Skip to content

Commit

Permalink
Improve file reading strategy
Browse files Browse the repository at this point in the history
Pass PDB and CIF files straight to ChemDoodle. For all other files, use Open Babel if available to convert to ChemDoodle JSON and pass that to ChemDoodle.
  • Loading branch information
mcs07 committed Feb 4, 2015
1 parent 9b919c2 commit 9908acd
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 41 deletions.
37 changes: 18 additions & 19 deletions Common.m
Expand Up @@ -59,41 +59,40 @@

NSString *PreviewURL(CFBundleRef bundle, NSURL *url, NSError *error) {
NSString *extension = [[[url path] pathExtension] lowercaseString];
NSString *mol = nil;

// Use Open Babel to generate ChemDoodle JSON from file contents
NSString *options = @"-ocdjson -l 20 -c";
if ([@[@"smiles", @"smi", @"can", @"inchi"] containsObject:extension]) {
options = [options stringByAppendingString:@" --gen2d"];
// Read the raw file contents if ChemDoodle supports the format
if ([@[@"sdf", @"sd", @"mdl", @"mol", @"cif", @"pdb", @"xyz"] containsObject:extension]) {
mol = [NSString stringWithContentsOfURL:url encoding:NSUTF8StringEncoding error:&error];
}
NSString *cdjson = MolDataFromOpenBabel(url, options);

// Read the raw file contents if Open Babel failed or if a cif (for unit cell info)
NSString *raw = nil;
if ((cdjson == nil) || [extension isEqualToString:@"cif"]) {
// Only worth reading the raw file contents if ChemDoodle supports the format
if ([@[@"sdf", @"sd", @"mdl", @"mol", @"cif", @"pdb", @"xyz"] containsObject:extension]) {
raw = [NSString stringWithContentsOfURL:url encoding:NSUTF8StringEncoding error:&error];
// Use Open Babel to generate ChemDoodle JSON from file (unless CIF or PDB)
if (![@[@"cif", @"pdb"] containsObject:extension]) {
NSString *options = @"-ocdjson -l 20 -c";
if ([@[@"smiles", @"smi", @"can", @"inchi"] containsObject:extension]) {
options = [options stringByAppendingString:@" --gen2d"];
}
NSString *cdjson = MolDataFromOpenBabel(url, options);
if (!(cdjson == nil)) {
extension = @"cdjson";
mol = cdjson;
}
}
if (cdjson == nil && raw == nil) {
return nil;
}


// Escape strings to insert into template as javascript variables
raw = EscapeStringForJavascript(raw);
cdjson = EscapeStringForJavascript(cdjson);
mol = EscapeStringForJavascript(mol);

// Load the template file as a string for substitution
NSString *template = TextFromBundle(bundle, @"chemlook.html", error);
NSString *css = TextFromBundle(bundle, @"style.css", error);
NSString *chemdoodle = TextFromBundle(bundle, @"ChemDoodleWeb.js", error);
NSString *js = TextFromBundle(bundle, @"script.js", error);
if (template == nil || chemdoodle == nil || css == nil || js == nil) {
if (mol == nil || extension == nil || template == nil || chemdoodle == nil || css == nil || js == nil) {
return nil;
}

// Insert variables into template
NSString *output = [NSString stringWithFormat:template, css, chemdoodle, raw, cdjson, extension, js];
NSString *output = [NSString stringWithFormat:template, css, chemdoodle, extension, mol, js];
return output;
}

Expand Down
5 changes: 2 additions & 3 deletions chemlook.html
Expand Up @@ -15,9 +15,8 @@
<a href="#transformer" id="threed" class="button active">3D</a>
</div>
<script>
var raw = '%@';
var cdjson = '%@';
var extension = '%@';
var extension = '%@',
molstring = '%@';
%@
</script>
</body>
Expand Down
33 changes: 14 additions & 19 deletions script.js
Expand Up @@ -39,26 +39,21 @@ ChemDoodle.lib.jQuery(document).ready(function($) {
var viewer = new ChemDoodle.ViewerCanvas('viewer', width, height);

// Construct ChemDoodle Molecule from molecule data
if (cdjson !== '') {
// If available, use the ChemDoodle JSON produced by Open Babel
var mol = ChemDoodle.readJSON(cdjson);
// TODO: Support previewing multiple molecules (for now we just take the first)
if (typeof(mol) !== 'undefined') {
mol = mol.molecules[0];
}
}

// If no ChemDoodle JSON, use the raw file contents instead with ChemDoodle reader
if (typeof(mol) === 'undefined' && raw !== '') {
if (extension === 'pdb') {
var mol = ChemDoodle.readPDB(raw);
} else if (extension === 'cif') {
var mol = ChemDoodle.readCIF(raw);
} else if (extension === 'xyz') {
var mol = ChemDoodle.readXYZ(raw);
} else {
var mol = ChemDoodle.readMOL(raw);
var mol = undefined,
unitcell = undefined;
if (extension === 'cdjson') {
var parsed = ChemDoodle.readJSON(molstring);
if (typeof parsed !== 'undefined' && typeof parsed.molecules[0] !== 'undefined') {
mol = parsed.molecules[0];
}
} else if (extension === 'pdb') {
mol = ChemDoodle.readPDB(molstring);
} else if (extension === 'cif') {
var parsed = ChemDoodle.readCIF(molstring);
mol = parsed.molecule;
unitcell = parsed.unitCell;
} else {
mol = ChemDoodle.readMOL(molstring);
}
scaleMol(mol);

Expand Down

0 comments on commit 9908acd

Please sign in to comment.