Codi per posar un camp d’un sdf al títol de la molècula

Sovint, mentre manipulem fitxers sdf ens trobem que volem canviar el títol de la molècula. Moltes vegades, com a títol hi volem posar algun camp de les dades addicionals de la molècula en qüestió. M’he fet un petit script per fer aquesta tasca de manera automàtica i senzilla.

El programet llegeix l’sdf i, molècula a molècula, en llegeix el camp que volguem, l’assigna com a títol i desa la molècula en un nou fitxer. A més a més, agafa el títol original i el desa en un camp “old_title”.

?View Code PYTHON
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
"""
Script to move a field to an id in a sdf
Alfons Nonell-Canals - April 2010
"""
 
import optparse
import sys
from pybel import *
 
p = optparse.OptionParser()
p.add_option('--sdf', '-i', help='SDF file to modify')
p.add_option('--field', '-f', help='Field to you want to set as molecule title')
options, arguments = p.parse_args()
 
if not options.sdf  or not options.field:
        p.error('An input sdf file and a field are required')
 
molfile = options.sdf
field = options.field
 
outFile = molfile.replace('.sdf','')+'.Mod.sdf'
out = Outputfile('sdf',outFile)
 
for mol in readfile("sdf", molfile):
        oldTitle = mol.title
        mol.data['old_title'] = oldTitle
        newtitle = mol.data[field]
        mol.OBMol.SetTitle(newtitle)
        out.write(mol)

No comments yet.

Write a comment: