-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathassign_bmseid.py
executable file
·135 lines (110 loc) · 3.99 KB
/
assign_bmseid.py
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
#!/usr/bin/python -u
# -*- coding: utf-8 -*-
#
# nmrfam_to_bmrb.py
#
# Copyright 2018 Board of Regents university of Wisconsin - Madison
# Dimitri Maziuk <dmaziuk@bmrb.wisc.edu>
#
# This code is free: reuse what you like but give credit
# convert files created by NMRFAM to NMR-STAR
#
from __future__ import absolute_import
import os
import sys
import json
import re
import sqlite3
import argparse
_HERE = os.path.split( os.path.realpath( __file__ ) )[0]
sys.path.append( _HERE )
import bmrbmb
DBFILE = "bmrbids.sqlt3"
#
#
#
if __name__ == '__main__':
ap = argparse.ArgumentParser( description = "Assign a new BMSE ID or return the existing one" )
ap.add_argument( "-v", "--verbose", default = False, action = "store_true",
help = "print lots of messages to stdout", dest = "verbose" )
ap.add_argument( "-i", "--index", help = "index file", dest = "index", required = True )
ap.add_argument( "-d", "--directory", help = "incoming directory", dest = "directory", default = None )
args = ap.parse_args()
# DB has
# id
# dirname - used to ID the entry directory on the NMRFAM side
# inchi - the proper key, but not convenient for teh above
#
# alatis inchi string is stored in index file
# index file is normally in the entry directory coming from NRFAM
# so we get inchi and the last directory component out of the index
#
indexfile = os.path.realpath( args.index )
with open( indexfile, "rU" ) as f :
dat = json.load( f )
if dat is None : raise Exception( "no entry data: %s" % (indexfile,) )
indir = os.path.split( indexfile )[0]
inchi = None
if "inchi" in dat.keys() :
inchi = dat["inchi"]
else :
if not "sdf" in dat.keys() :
raise Exception( "No sdf in index file" )
sdf = os.path.realpath( os.path.join( indir, dat["sdf"] ) )
if not os.path.exists( sdf ) :
raise IOError( "File not found: %s" % (sdf,) )
mol = bmrbmb.chemcomp.Molecule.from_file( filename = sdf, verbose = args.verbose )
chem_comp = mol.chem_comp
# inchi string is needed for web queries
#
if "descriptors" in chem_comp.keys() :
for i in chem_comp["descriptors"] :
if (i["type"] == "InChI") and (i["program"] == "ALATIS") :
inchi = i["descriptor"]
if inchi is None : raise Exception( "no inchi_code in %s or SDF" % (indexfile,) )
if args.directory is None :
path = os.path.split( os.path.split( indexfile )[0] )[1]
if path == "" : raise Exception( "can't figure dirname from %s" % (indexfile,) )
else :
path = os.path.split( args.directory )[1]
if not os.path.exists( DBFILE ) : raise Exception( "no DB fie %s" % (DBFILE,) )
if args.verbose :
sys.stdout.write( "Get ID for\n InChI %s\n dir %s\n" % (inchi,path,) )
conn = sqlite3.connect( DBFILE )
curs = conn.cursor()
sql = "select id from ids where inchi=?"
# sql = "select * from ids where inchi=?"
bmse = None
curs.execute( sql, (inchi,) )
for row in curs :
bmse = row[0]
# if bmse is None :
# sql = "select id from ids where dirname=?"
# curs.execute( sql, (path,) )
# for row in curs :
# bmse = row[0]
if bmse is not None :
sys.stdout.write( "%s\n" % (bmse,) )
curs.close()
conn.close()
sys.exit( 0 )
bmid = 0
pat = re.compile( r"^bmse(\d+)$" )
sql = "select id from ids"
curs.execute( sql )
for row in curs :
m = pat.search( row[0] )
if m :
if int( m.group( 1 ) ) > bmid :
bmid = int( m.group( 1 ) )
if bmid == 0 : raise Exception( "no bmse id (huh?)" )
bmse = "bmse%06d" % ((bmid + 1),)
sql = "insert into ids (id,dirname,inchi) values (?,?,?)"
curs.execute( sql, (bmse,path,inchi,) )
if curs.rowcount != 1 : raise Exception( "rowcount is %s" % (curs.rowcount,) )
conn.commit()
sys.stdout.write( "BMRB ID for %s is %s\n" % (path,bmse,) )
curs.close()
conn.close()
#
#