-
Notifications
You must be signed in to change notification settings - Fork 0
/
NefImporter.py
1420 lines (1185 loc) · 52.8 KB
/
NefImporter.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
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
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
"""
NefImporter - a series of routines for reading a Nef file and examining the contents.
Module Contents
===============
Introduction
Error handling
Examples
Nef File Contents
Introduction
------------
NefImporter consists of two classes: NefImporter - a class for handling the top-level object, and
NefDict for handling individual saveFrames in the dictionary.
NefImporter contains:
initialise initialise a new dictionary
loadFile read in the contents of a .nef file
saveFile save the dictionary to a .nef file
getCategories return the current categories defined in the Nef structure
getSaveFrameNames return the names of the saveFrames with the file
hasSaveFrame return True if the saveFrame exists
getSaveFrame return saveFrame of the given name
addSaveFrame add a new saveFrame to the dictionary
get<name> return the relevant structures of the Nef file
defined by the available categories, where <name> can be:
NmrMetaData
MolecularSystems
ChemicalShiftLists
DistanceRestraintLists
DihedralRestraintLists
RdcRestraintLists
NmrSpectra
PeakRestraintLinks
e.g. yourImport.getChemicalShiftLists()
add<name> add a new saveFrame to the dictionary
defined by the available categories, where <name> can be:
ChemicalShiftList
DistanceRestraintList
DihedralRestraintList
RdcRestraintList
NmrSpectra
PeakLists
LinkageTables
e.g. addChemicalShiftList
toString convert Nef dictionary to a string that can be written to a file
fromString convert string to Nef dictionary
getAttributeNames get a list of the attributes attached to the dictionary
getAttribute return the value of the attribute
hasAttribute return True if the attribute Exists
lastError error code of the last operation
lastErrorString error string of the last operation
NefDict contains handling routines:
getTableNames return a list of the tables in the saveFrame
getTable return table from the saveFrame, it can be returned as an OrderedDict
or as a Pandas DataFrame
hasTable return true of the table exists
setTable set the table - currently not implemented
getAttributeNames get a list of the attributes attached to the saveFrame
getAttribute return the value of the attribute
hasAttribute return True if the attribute Exists
lastError error code of the last operation
lastErrorString error string of the last operation
Error Handling
--------------
Errors can be handled in three different modes:
'silent' errors are handled internally and can be interrogated with saveFrame.lastError
with no logging to the stderr
'standard' errors are handled internally, error messages are logged to stderr.
'strict' errors message are logged to stderr and errors are raised to be trapped by
the calling functions
error handling mode can be set at the instantiation of the object, e.g.
newObject = NefImporter(errorLogging='standard')
Examples
--------
Here are a few examples of using the classes:
# load a Nef file
test = NefImporter(errorLogging=NEF_STANDARD)
test.loadFile('/Users/account/Projects/NefFile.nef')
# get categories
print (test.getCategories())
# get saveFrame names
names = test.getSaveFrameNames(); print(names)
names = test.getSaveFrameNames(returnType=NEF_RETURNALL); print(names)
names = test.getSaveFrameNames(returnType=NEF_RETURNNEF); print (names)
names = test.getSaveFrameNames(returnType=NEF_RETURNOTHER); print (names)
# get a particular saveFrame
sf1 = test.getSaveFrame(names[0])
# convert NefImporter into a string for saving
ts = test.toString()
test.fromString(ts)
# getting tables from a saveFrame
print (sf1.getTableNames())
table = sf1.getTable('nmr_atom', asPandas=True)
print (table)
print (sf1.hasTable('nmr_residue'))
print (sf1.getAttributeNames())
print (sf1.hasAttribute('sf_framecode'))
print (sf1.hasAttribute('nothing'))
print (test.getSaveFrame(name='ccpn_assignment').getTable(name='nmr_residue', asPandas=True))
# saving a file
print ('SAVE ', test.saveFile('/Users/ejb66/PycharmProjects/Sec5Part3testing.nef'))
print (test.lastError)
# test meta creation of category names
print (test.getMolecularSystems())
There are more examples i the __main__ function at the bottom of the module
Nef File Contents
-----------------
More details of the contents of Nef files can be found in GenericStarParser
The general structure of a Nef file is:
::
DataExtent
DataBlock
Item
Loop
SaveFrame
Item
Loop
DataExtent, DataBlock and SaveFrame are Python OrderedDict with an additional 'name' attribute
DataBlocks and SaveFrames are entered in their container using their name as the key.
Loop is an object with a 'columns' list, a 'data' list-of-row-OrderedDict, and a name attribute
set equal to the name of the first column. A loop is entered in its container under each
column name, so that e.g. aSaveFrame['_Loopx.loopcol1'] and aSaveFrame['_Loopx.loopcol2'] both
exist and both correspond to the same loop object.
Items are entered as a string key - string value pair.
the string value can be a dictionary
"""
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
from __future__ import unicode_literals
#=========================================================================================
# Licence, Reference and Credits
#=========================================================================================
__copyright__ = "Copyright (C) CCPN project (http://www.ccpn.ac.uk) 2014 - 2019"
__credits__ = ("Ed Brooksbank, Luca Mureddu, Timothy J Ragan & Geerten W Vuister")
__licence__ = ("CCPN licence. See http://www.ccpn.ac.uk/v3-software/downloads/license")
__reference__ = ("Skinner, S.P., Fogh, R.H., Boucher, W., Ragan, T.J., Mureddu, L.G., & Vuister, G.W.",
"CcpNmr AnalysisAssign: a flexible platform for integrated NMR analysis",
"J.Biomol.Nmr (2016), 66, 111-124, http://doi.org/10.1007/s10858-016-0060-y")
#=========================================================================================
# Last code modification
#=========================================================================================
__modifiedBy__ = "$modifiedBy: Ed Brooksbank $"
__dateModified__ = "$dateModified: 2017-07-07 16:32:41 +0100 (Fri, July 07, 2017) $"
__version__ = "$Revision: 3.0.0 $"
#=========================================================================================
# Created
#=========================================================================================
__author__ = "$Author: Ed Brooksbank $"
__date__ = "$Date: 2017-04-07 10:28:41 +0000 (Fri, April 07, 2017) $"
#=========================================================================================
# Start of code
#=========================================================================================
import os
import sys
import re
import numpy as np
from collections import OrderedDict, namedtuple
from pathlib import Path
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# this is a fix to get the import to work when running as a standalone
# when importing into your own code, it can be safely removed
def import_parents(level=1):
global __package__
import sys
from os import path
import importlib
# pathlib does all this a lot nicer, but don't think it's in python2.7
top = parent = path.dirname(path.abspath(__file__))
package = []
for t in range(level):
package.insert(0, os.path.basename(top))
top = path.dirname(top)
sys.path.append(str(top))
try:
sys.path.remove(str(parent))
except ValueError: # already removed
pass
__package__ = str('.'.join(package))
importlib.import_module(__package__)
if __name__ == '__main__' and __package__ is None:
import_parents(level=1)
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
from . import StarIo
from . import ErrorLog as el
from . import Validator
from . import Specification
MAJOR_VERSION = '1'
MINOR_VERSION = '1'
PATCH_LEVEL = '0'
__nef_version__ = '.'.join((MAJOR_VERSION, MINOR_VERSION))
# __version__ = '.'.join( (__nef_version__, PATCH_LEVEL) )
from . import NEF_ROOT_PATH
NEF_DEFAULT_DICT = os.path.join(NEF_ROOT_PATH, 'mmcif_nef_v1_1.dic')
NEF_CATEGORIES = [('nef_nmr_meta_data', 'get_nmr_meta_data'),
('nef_molecular_system', 'get_molecular_systems'),
('nef_chemical_shift_list', 'get_chemical_shift_lists'),
('nef_distance_restraint_list', 'get_distance_restraint_lists'),
('nef_dihedral_restraint_list', 'get_dihedral_restraint_lists'),
('nef_rdc_restraint_list', 'get_rdc_restraint_lists'),
('nef_nmr_spectrum', 'get_nmr_spectra'),
('nef_peak_restraint_links', 'get_peak_restraint_links')]
NEF_REQUIRED_SAVEFRAME_BY_FRAMECODE = ['nef_nmr_meta_data',
'nef_molecular_system']
NEF_REQUIRED_SAVEFRAME_BY_CATEGORY = ['nef_chemical_shift_list', ]
NEF_ALL_SAVEFRAME_REQUIRED_FIELDS = ['sf_category',
'sf_framecode', ]
MD_REQUIRED_FIELDS = ['sf_category',
'sf_framecode',
'format_name',
'format_version',
'program_name',
'program_version',
'creation_date',
'uuid']
MD_OPTIONAL_FIELDS = ['coordinate_file_name', ]
MD_OPTIONAL_LOOPS = ['nef_related_entries',
'nef_program_script',
'nef_run_history']
MD_RE_REQUIRED_FIELDS = ['database_name',
'database_accession_code']
MD_PS_REQUIRED_FIELDS = ['program_name', ]
MD_RH_REQUIRED_FIELDS = ['run_ordinal',
'program_name']
MD_RH_OPTIONAL_FIELDS = ['program_version',
'script_name',
'script']
MS_REQUIRED_FIELDS = ['sf_category',
'sf_framecode']
MS_REQUIRED_LOOPS = ['nef_sequence']
MS_OPTIONAL_LOOPS = ['nef_covalent_links']
MS_NS_REQUIRED_FIELDS = ['chain_code',
'sequence_code',
'residue_type',
'linking',
'residue_variant']
MS_CL_REQUIRED_FIELDS = ['chain_code_1',
'sequence_code_1',
'residue_type_1',
'atom_name_1',
'chain_code_2',
'sequence_code_2',
'residue_type_2',
'atom_name_2']
CSL_REQUIRED_FIELDS = ['sf_category',
'sf_framecode',
'atom_chem_shift_units']
CSL_REQUIRED_LOOPS = ['nef_chemical_shift']
CSL_CS_REQUIRED_FIELDS = ['chain_code',
'sequence_code',
'residue_type',
'atom_name',
'value']
CSL_CS_OPTIONAL_FIELDS = ['value_uncertainty', ]
DRL_REQUIRED_FIELDS = ['sf_category',
'sf_framecode',
'potential_type']
DRL_REQUIRED_LOOPS = ['nef_distance_restraint']
DRL_OPTIONAL_FIELDS = ['restraint_origin', ]
DRL_DR_REQUIRED_FIELDS = ['ordinal',
'restraint_id',
'chain_code_1',
'sequence_code_1',
'residue_type_1',
'atom_name_1',
'chain_code_2',
'sequence_code_2',
'residue_type_2',
'atom_name_2',
'weight']
DRL_DR_OPTIONAL_FIELDS = ['restraint_combination_id',
'target_value',
'target_value_uncertainty',
'lower_linear_limit',
'lower_limit',
'upper_limit',
'upper_linear_limit']
DIHRL_REQUIRED_FIELDS = ['sf_category',
'sf_framecode',
'potential_type']
DIHRL_REQUIRED_LOOPS = ['nef_dihedral_restraint']
DIHRL_OPTIONAL_FIELDS = ['restraint_origin', ]
DIHRL_DIHR_REQUIRED_FIELDS = ['ordinal',
'restraint_id',
'restraint_combination_id',
'chain_code_1',
'sequence_code_1',
'residue_type_1',
'atom_name_1',
'chain_code_2',
'sequence_code_2',
'residue_type_2',
'atom_name_2',
'chain_code_3',
'sequence_code_3',
'residue_type_3',
'atom_name_3',
'chain_code_4',
'sequence_code_4',
'residue_type_4',
'atom_name_4',
'weight']
DIHRL_DIHR_OPTIONAL_FIELDS = ['target_value',
'target_value_uncertainty',
'lower_linear_limit',
'lower_limit',
'upper_limit',
'upper_linear_limit',
'name']
RRL_REQUIRED_FIELDS = ['sf_category',
'sf_framecode',
'potential_type']
RRL_REQUIRED_LOOPS = ['nef_rdc_restraint']
RRL_OPTIONAL_FIELDS = ['restraint_origin',
'tensor_magnitude',
'tensor_rhombicity',
'tensor_chain_code',
'tensor_sequence_code',
'tensor_residue_type', ]
RRL_RR_REQUIRED_FIELDS = ['ordinal',
'restraint_id',
'chain_code_1',
'sequence_code_1',
'residue_type_1',
'atom_name_1',
'chain_code_2',
'sequence_code_2',
'residue_type_2',
'atom_name_2',
'weight']
RRL_RR_OPTIONAL_FIELDS = ['restraint_combination_id',
'target_value',
'target_value_uncertainty',
'lower_linear_limit',
'lower_limit',
'upper_limit',
'upper_linear_limit',
'scale',
'distance_dependent', ]
PL_REQUIRED_FIELDS = ['sf_category',
'sf_framecode',
'num_dimensions',
'chemical_shift_list']
PL_REQUIRED_LOOPS = ['nef_spectrum_dimension',
'nef_spectrum_dimension_transfer',
'nef_peak']
PL_OPTIONAL_FIELDS = ['experiment_classification',
'experiment_type']
PL_SD_REQUIRED_FIELDS = ['dimension_id',
'axis_unit',
'axis_code']
PL_SD_OPTIONAL_FIELDS = ['spectrometer_frequency',
'spectral_width',
'value_first_point',
'folding',
'absolute_peak_positions',
'is_acquisition', ]
PL_SDT_REQUIRED_FIELDS = ['dimension_1',
'dimension_2',
'transfer_type']
PL_SDT_OPTIONAL_FIELDS = ['is_indirect', ]
PL_P_REQUIRED_FIELDS = ['ordinal',
'peak_id']
PL_P_REQUIRED_ALTERNATE_FIELDS = [['height', 'volume'], ]
PL_P_REQUIRED_FIELDS_PATTERN = ['position_{}',
'chain_code_{}',
'sequence_code_{}',
'residue_type_{}',
'atom_name_{}', ]
PL_P_OPTIONAL_ALTERNATE_FIELDS = {r'(height)' : ['{}_uncertainty', ],
r'(volume)' : ['{}_uncertainty', ],
r'position_([0-9]+)': ['position_uncertainty_{}', ],
}
PL_P_OPTIONAL_FIELDS_PATTERN = ['position_uncertainty_{}', ]
PRLS_REQUIRED_FIELDS = ['sf_category',
'sf_framecode']
PRLS_REQUIRED_LOOPS = ['nef_peak_restraint_link']
PRLS_PRL_REQUIRED_FIELDS = ['nmr_spectrum_id',
'peak_id',
'restraint_list_id',
'restraint_id']
NEF_CATEGORIES_REMOVEPREFIX = {'nef_distance_restraint' : 'distance_restraint',
'nef_molecular_system' : 'molecular_system',
'nef_covalent_links' : 'covalent_links',
'nef_peak_restraint_links' : 'peak_restraint_links',
'nef_run_history' : 'run_history',
'nef_nmr_meta_data' : 'nmr_meta_data',
'nef_rdc_restraint_list' : 'rdc_restraint_list',
'nef_peak_restraint_link' : 'peak_restraint_link',
'nef_nmr_spectrum' : 'nmr_spectrum',
'nef_spectrum_dimension' : 'spectrum_dimension',
'nef_chemical_shift_list' : 'chemical_shift_list',
'nef_sequence' : 'sequence',
'nef_program_script' : 'program_script',
'nef_related_entries' : 'related_entries',
'nef_distance_restraint_list' : 'distance_restraint_list',
'nef_rdc_restraint' : 'rdc_restraint',
'nef_chemical_shift' : 'chemical_shift',
'nef_spectrum_dimension_transfer': 'spectrum_dimension_transfer',
'nef_dihedral_restraint_list' : 'dihedral_restraint_list',
'nef_peak' : 'peak',
'nef_dihedral_restraint' : 'dihedral_restraint'}
NEF_CATEGORIES_INSERTPREFIX = dict((val, key) for key, val in NEF_CATEGORIES_REMOVEPREFIX.items())
NEF_RETURNALL = 'all'
NEF_RETURNNEF = 'nef_'
NEF_RETURNOTHER = 'other'
NEF_PREFIX = 'nef_'
def _tryNumber(value):
if isinstance(value, str):
ll = value.rsplit('`', 2)
if len(ll) == 3:
# name is of form abc`xyz`
try:
return int(ll[1])
except ValueError:
pass
REGEXREMOVEENDQUOTES = u'\`\d*`+?'
_nameFromCategory = namedtuple('_nameFromCategory', ('framecode', 'frameName', 'subname', 'prefix', 'postfix', 'precode', 'postcode', 'category'))
def _saveFrameNameFromCategory(saveFrame: StarIo.NmrSaveFrame):
"""Parse the saveframe name to extract pre- and post- numbering
necessary for restraint and spectrum saveframe names
"""
category = saveFrame['sf_category']
framecode = saveFrame['sf_framecode']
# frameName = framecode[len(category) + 1:]
return _getNameFromCategory(category, framecode)
def _getNameFromCategory(category, framecode):
# check for any occurrences of `n` in the saveframe name and keep for later reference
frameName = framecode[len(category) + 1:]
names = re.split(REGEXREMOVEENDQUOTES, frameName)
if 0 <= len(names) > 3:
raise TypeError('bad splitting of saveframe name {}'.format(framecode))
subName = re.sub(REGEXREMOVEENDQUOTES, '', frameName)
matches = [mm for mm in re.finditer(REGEXREMOVEENDQUOTES, frameName)]
prefix = matches[0].group() if matches and matches[0] and matches[0].span()[0] == 0 else ''
preSerial = _tryNumber(prefix)
postfix = matches[-1].group() if matches and matches[-1] and matches[-1].span()[1] == len(frameName) else ''
postSerial = _tryNumber(postfix)
return _nameFromCategory(framecode, frameName, subName, prefix, postfix, preSerial, postSerial, category)
class NefImporter(el.ErrorLog):
"""Object for accessing Nef data tree.
The Nef data consist of a single NmrStar dataBlock (an OrderedDict),
with (saveFrameName, NmrSaveFrame) key,value pairs
"""
# put functions in here to read the contents of the dict.
# superclassed from DataBlock which is of type StarContainer
def __init__(self,
programName='Unknown',
programVersion='Unknown',
errorLogging=el.NEF_STANDARD,
hidePrefix = True,
):
el.ErrorLog.__init__(self, loggingMode=errorLogging)
# self.name = name
self.programName = programName
self.programVersion = programVersion
self._hidePrefix = hidePrefix
self._validateNefDict = None
self.loadValidateDictionary()
self._validator = Validator.Validator()
self._isValid = False
# No data read so far
self._saveFrameNames = {}
self._nefDict = {}
# self._initialise() # initialise a basic object
self._path = None
@property
def data(self) -> StarIo.NmrDataBlock:
"""Return the NmrDataBlock instance
"""
return self._nefDict
@property
def path(self) -> str:
""":return the path of the last read Nef file (empty if undefined)
"""
return '' if self._path is None else self._path
def _logFunc(self, *args):
"""Simple logger for CifDicConverter using _logError
"""
self._logError(errorString=''.join([str(arg) for arg in args]))
@el.ErrorLog(errorCode=el.NEFERROR_ERRORLOADINGFILE)
def loadValidateDictionary(self, fileName=None, mode='standard'):
"""Load and parse a Nef dictionary file (in star format) to
validate the nef file.
:param fileName: path of Nef dictionary file; defaults to current
definition dictionary file
:param mode:
"""
if fileName is None:
fileName = NEF_DEFAULT_DICT
if not isinstance(fileName, (str, Path)):
raise RuntimeError('Invalid Nef dictionary file %r' % fileName)
fileName = str(fileName) # convert any Path instance, as the downstream routines may fall over
_path = os.path.expanduser(fileName)
_path = os.path.normpath(_path)
if not os.path.isfile(_path):
raise RuntimeError('Nef dictionary file "%s" not found' % fileName)
with open(_path) as fp:
data = fp.read()
converter = Specification.CifDicConverter(data, logger=self._logFunc)
converter.convertToNef()
self._validateNefDict = converter.result
return True
def _doValidate(self) -> bool:
"""Validate the current state of self._nefDict
:return True if nefDict validated successfully
"""
result = self._validator.isValid(self._nefDict, self._validateNefDict)
self._isValid = result
return result
@property
def isValid(self) -> bool:
"""
Check whether the Nef object contains the required information
:return True or False:
"""
return self._isValid
@property
def validErrorLog(self):
"""
Return the error log from checking validity
:return dict:
"""
return self._validator._validation_errors
def _namedToNefDict(self, frame):
# change a saveFrame into a normal OrderedDict
newItem = NefDict(inFrame=frame, errorLogging=self.loggingMode)
for ky in frame.keys():
newItem[ky] = frame[ky]
return newItem
def _removePrefix(self, name):
if self._hidePrefix:
for db in NEF_CATEGORIES_REMOVEPREFIX.keys():
if name.startswith(db):
name = name.replace(db, NEF_CATEGORIES_REMOVEPREFIX[db], 1)
break
return name
def _insertPrefix(self, name):
if self._hidePrefix:
for db in NEF_CATEGORIES_INSERTPREFIX.keys():
if name.startswith(db):
name = name.replace(db, NEF_CATEGORIES_INSERTPREFIX[db], 1)
break
return name
@el.ErrorLog(errorCode=el.NEFERROR_BADLISTTYPE)
def _getListType(self, _listType):
# return a list of '_listType' from the saveFrame,
# used with nefCategory routines below
if self._nefDict and isinstance(self._nefDict, OrderedDict):
sfList = [self._nefDict[db] for db in self._nefDict.keys() if _listType in db]
sfList = [self._namedToNefDict(sf) for sf in sfList]
# if there is only one item then return it, otherwise return the list
if len(sfList) > 1:
return sfList
elif sfList:
return sfList[0]
return None
# routines to get the Nef specific data from the dictionary
def getNmrMetaData(self):
"""
Return the nef_nmr_meta_data saveFrames
:return list or single item:
"""
return self._getListType(NEF_CATEGORIES[0][0])
def getMolecularSystems(self):
"""
Return the nef_molecular_system saveFrames
:return list or single item:
"""
return self._getListType(NEF_CATEGORIES[1][0])
def getChemicalShiftLists(self):
"""
Return the nef_chemical_shift_list saveFrames
:return list or single item:
"""
return self._getListType(NEF_CATEGORIES[2][0])
def getDistanceRestraintLists(self):
"""
Return the nef_distance_restraint_list saveFrames
:return list or single item:
"""
return self._getListType(NEF_CATEGORIES[3][0])
def getDihedralRestraintLists(self):
"""
Return the nef_dihedral_restraint_list saveFrames
:return list or single item:
"""
return self._getListType(NEF_CATEGORIES[4][0])
def getRdcRestraintLists(self):
"""
Return the nef_rdc_restraint_list saveFrames
:return list or single item:
"""
return self._getListType(NEF_CATEGORIES[5][0])
def getNmrSpectra(self):
"""
Return the nef_nmr_spectrum saveFrames
:return list or single item:
"""
return self._getListType(NEF_CATEGORIES[6][0])
def getPeakRestraintLinks(self):
"""
Return the nef_peak_restraint_link saveFrames
:return list or single item:
"""
return self._getListType(NEF_CATEGORIES[7][0])
def _initialise(self):
"""
Initialise a new NefImporter object with a starting saveFrame
"""
nefNmr = 'nef_nmr_meta_data'
nefMol = 'nef_molecular_system'
nefChem = 'nef_chemical_shift_list_1'
self._nefDict[nefNmr] = StarIo.NmrDataBlock()
self._nefDict[nefNmr].update({k: '' for k in MD_REQUIRED_FIELDS})
self._nefDict[nefNmr]['sf_category'] = 'nef_nmr_meta_data'
self._nefDict[nefNmr]['sf_framecode'] = 'nef_nmr_meta_data'
self._nefDict[nefNmr]['format_name'] = 'Nmr_Exchange_Format'
self._nefDict[nefNmr]['format_version'] = __nef_version__
self._nefDict[nefNmr]['program_name'] = self.programName
self._nefDict[nefNmr]['program_version'] = self.programVersion
self._nefDict[nefMol] = StarIo.NmrDataBlock()
self._nefDict[nefMol].update({k: '' for k in MS_REQUIRED_FIELDS})
self._nefDict[nefMol]['sf_category'] = 'nef_molecular_system'
self._nefDict[nefMol]['sf_framecode'] = 'nef_molecular_system'
for l in MS_REQUIRED_LOOPS:
self._nefDict['nef_molecular_system'][l] = []
self.addChemicalShiftList(nefChem, 'ppm')
self._logError(errorCode=el.NEFVALID)
@el.ErrorLog(errorCode=el.NEFERROR_BADADDSAVEFRAME)
def addSaveFrame(self, name, category, required_fields=None, required_loops=None):
"""
Add a new saveFrame to NefImporter
:param name:
:param category:
:param required_fields:
:param required_loops:
"""
name = self._insertPrefix(name)
self._nefDict[name] = StarIo.NmrSaveFrame()
if required_fields is not None:
self._nefDict[name].update({k: '' for k in required_fields})
self._nefDict[name]['sf_category'] = category
self._nefDict[name]['sf_framecode'] = name
if required_loops is not None:
for l in required_loops:
self._nefDict[name][l] = []
return self._nefDict[name]
@el.ErrorLog(errorCode=el.NEFERROR_BADADDSAVEFRAME)
def addChemicalShiftList(self, name, cs_units='ppm'):
name = self._insertPrefix(name)
category = 'nef_chemical_shift_list'
self.addSaveFrame(name=name, category=category,
required_fields=CSL_REQUIRED_FIELDS,
required_loops=CSL_REQUIRED_LOOPS)
self._nefDict[name]['atom_chem_shift_units'] = cs_units
return self._nefDict[name]
@el.ErrorLog(errorCode=el.NEFERROR_BADADDSAVEFRAME)
def addDistanceRestraintList(self, name, potential_type,
restraint_origin=None):
name = self._insertPrefix(name)
category = 'nef_distance_restraint_list'
self.addSaveFrame(name=name, category=category,
required_fields=DRL_REQUIRED_FIELDS,
required_loops=DRL_REQUIRED_LOOPS)
self._nefDict[name]['potential_type'] = potential_type
if restraint_origin is not None:
self._nefDict[name]['restraint_origin'] = restraint_origin
return self._nefDict[name]
@el.ErrorLog(errorCode=el.NEFERROR_BADADDSAVEFRAME)
def addDihedralRestraintList(self, name, potential_type,
restraint_origin=None):
name = self._insertPrefix(name)
category = 'nef_dihedral_restraint_list'
self.addSaveFrame(name=name, category=category,
required_fields=DIHRL_REQUIRED_FIELDS,
required_loops=DIHRL_REQUIRED_LOOPS)
self._nefDict[name]['potential_type'] = potential_type
if restraint_origin is not None:
self._nefDict[name]['restraint_origin'] = restraint_origin
return self._nefDict[name]
@el.ErrorLog(errorCode=el.NEFERROR_BADADDSAVEFRAME)
def addRdcRestraintList(self, name, potential_type,
restraint_origin=None, tensor_magnitude=None,
tensor_rhombicity=None, tensor_chain_code=None,
tensor_sequence_code=None, tensor_residue_type=None):
name = self._insertPrefix(name)
category = 'nef_rdc_restraint_list'
self.addSaveFrame(name=name, category=category,
required_fields=DIHRL_REQUIRED_FIELDS,
required_loops=RRL_REQUIRED_LOOPS)
self._nefDict[name]['potential_type'] = potential_type
if restraint_origin is not None:
self._nefDict[name]['restraint_origin'] = restraint_origin
if tensor_magnitude is not None:
self._nefDict[name]['tensor_magnitude'] = tensor_magnitude
if tensor_rhombicity is not None:
self._nefDict[name]['tensor_rhombicity'] = tensor_rhombicity
if tensor_chain_code is not None:
self._nefDict[name]['tensor_chain_code'] = tensor_chain_code
if tensor_sequence_code is not None:
self._nefDict[name]['tensor_sequence_code'] = tensor_sequence_code
if tensor_residue_type is not None:
self._nefDict[name]['tensor_residue_type'] = tensor_residue_type
return self._nefDict[name]
@el.ErrorLog(errorCode=el.NEFERROR_BADADDSAVEFRAME)
def addPeakList(self, name, num_dimensions, chemical_shift_list,
experiment_classification=None,
experiment_type=None):
name = self._insertPrefix(name)
category = 'nef_nmr_spectrum'
if chemical_shift_list in self:
if self._nefDict[chemical_shift_list]['sf_category'] == 'nef_chemical_shift_list':
self.addSaveFrame(name=name, category=category,
required_fields=PL_REQUIRED_FIELDS,
required_loops=PL_REQUIRED_LOOPS)
self._nefDict[name]['num_dimensions'] = num_dimensions
self._nefDict[name]['chemical_shift_list'] = chemical_shift_list
if experiment_classification is not None:
self._nefDict[name]['experiment_classification'] = experiment_classification
if experiment_type is not None:
self._nefDict[name]['experiment_type'] = experiment_type
return self._nefDict[name]
raise Exception('{} is not a nef_chemical_shift_list.'.format(chemical_shift_list))
raise Exception('{} does not exist.'.format(chemical_shift_list))
@el.ErrorLog(errorCode=el.NEFERROR_BADADDSAVEFRAME)
def addLinkageTable(self):
name = category = 'nef_peak_restraint_links'
return self.addSaveFrame(name=name, category=category,
required_fields=PRLS_REQUIRED_FIELDS,
required_loops=PL_REQUIRED_LOOPS)
@el.ErrorLog(errorCode=el.NEFERROR_BADTOSTRING)
def toString(self):
return self._nefDict.toString()
@el.ErrorLog(errorCode=el.NEFERROR_BADFROMSTRING)
def fromString(self, text, mode='standard'):
# set the Nef from the contents of the string, opposite of toString
dataExtent = StarIo.parseNef(text=text, mode=mode)
if dataExtent:
dbs = [dataExtent[db] for db in dataExtent.keys()]
if dbs:
self._nefDict = dbs[0]
else:
self._logError(errorCode=el.NEFERROR_BADFROMSTRING)
self._nefDict = {}
@el.ErrorLog(errorCode=el.NEFERROR_ERRORLOADINGFILE)
def loadFile(self, fileName=None, mode='standard') -> StarIo.NmrDataBlock:
"""Load and parse Nef-file fileName
:param fileName: path to a Nef-file
:return a NmrDataBlock instance
"""
if not isinstance(fileName, (str, Path)):
raise RuntimeError('Invalid Nef file %r' % fileName)
fileName = str(fileName) # convert any Path instance, as the downstream routines may fall over
_path = os.path.expanduser(fileName)
_path = os.path.normpath(_path)
if not os.path.isfile(_path):
raise RuntimeError('Nef file "%s" not found' % fileName)
nefDataExtent = StarIo.parseNefFile(fileName=fileName, mode=mode)
_dataBlocks = list(nefDataExtent.values())
if len(_dataBlocks) > 1:
raise RuntimeError('More than one datablock in a NEF file is not allowed. Using the first and discarding the rest.\n')
self._nefDict = _dataBlocks[0]
self._path = fileName
self._doValidate()
return self.data
@el.ErrorLog(errorCode=el.NEFERROR_ERRORLOADINGFILE)
def loadText(self, text, mode='standard') -> StarIo.NmrDataBlock:
"""Load and parse Nef-formatted text
:param text: Nef-formatted text
:return a NmrDataBlock instance
"""
nefDataExtent = StarIo.parseNef(text=text, mode=mode)
_dataBlocks = list(nefDataExtent.values())
if len(_dataBlocks) > 1:
raise RuntimeError('More than one datablock in a NEF file is not allowed. Using the first and discarding the rest.\n')
self._nefDict = _dataBlocks[0]
self._path = 'loadedFromText'
self._doValidate()
return self.data
@el.ErrorLog(errorCode=el.NEFERROR_ERRORSAVINGFILE)
def saveFile(self, fileName=None):
with open(fileName, 'w') as op:
op.write(self._nefDict.toString())
return True
@el.ErrorLog(errorCode=el.NEFERROR_BADCATEGORIES)
def getCategories(self):
# return a list of the categories available in a Nef file
return tuple([self._removePrefix(nm[0]) for nm in NEF_CATEGORIES])
@el.ErrorLog(errorCode=el.NEFERROR_SAVEFRAMEDOESNOTEXIST)
def getSaveFrameNames(self, returnType=NEF_RETURNALL):
# return a list of the saveFrames in the file
if not self._nefDict:
return ()
names = [db for db in self._nefDict
if isinstance(self._nefDict[db], StarIo.NmrSaveFrame)]
if returnType == NEF_RETURNNEF:
names = [self._removePrefix(nm) for nm in names if nm and nm.startswith(NEF_PREFIX)]
elif returnType == NEF_RETURNOTHER:
names = [nm for nm in names if nm and not nm.startswith(NEF_PREFIX)]
else:
names = [self._removePrefix(nm) for nm in names]
return tuple(names)
@el.ErrorLog(errorCode=el.NEFERROR_SAVEFRAMEDOESNOTEXIST)
def hasSaveFrame(self, name):
# return True if the saveFrame exists, else False
name = self._insertPrefix(name)
return name in self._nefDict
@el.ErrorLog(errorCode=el.NEFERROR_SAVEFRAMEDOESNOTEXIST)
def getSaveFrame(self, name):
# return the saveFrame 'name'
name = self._insertPrefix(name)
return NefDict(self._nefDict[name], errorLogging=self.loggingMode, hidePrefix=self._hidePrefix)
@el.ErrorLog(errorCode=el.NEFERROR_SAVEFRAMEDOESNOTEXIST)
def deleteSaveFrame(self, name):
# return True if the saveFrame exists (and delete), else False
name = self._insertPrefix(name)
if name in self._nefDict:
del self._nefDict[name]
return True
@el.ErrorLog(errorCode=el.NEFERROR_SAVEFRAMEDOESNOTEXIST)
def renameSaveFrame(self, name, newName):
# return True if the saveFrame exists, else False
name = self._insertPrefix(name)
if name in self._nefDict and newName not in self._nefDict:
saveFrame = self._nefDict[name]
_frameID = _saveFrameNameFromCategory(saveFrame)
framecode, frameName, subName, prefix, postfix, preSerial, postSerial, category = _frameID
newSaveFrameName = '_'.join([category, prefix + newName + postfix])
saveFrame.name = newSaveFrameName
saveFrame['sf_framecode'] = newSaveFrameName
data = [(k, val) for k, val in self._nefDict.items()]