| 1 |
""" |
| 2 |
Convert a data/dictionary file in STAR2 to CIF2 format. STAR2 and CIF2 differ |
| 3 |
most markedly in their compound data value delimiters (comma and whitespace |
| 4 |
respectively). |
| 5 |
|
| 6 |
We read in the file using STAR2 grammar, then output with CIF2 grammar. |
| 7 |
|
| 8 |
Two optional files may be provided, one giving a top-level comment, and |
| 9 |
another the descriptive text for the dictionary. |
| 10 |
""" |
| 11 |
|
| 12 |
from __future__ import print_function |
| 13 |
from CifFile import ReadCif,CifDic |
| 14 |
import time |
| 15 |
|
| 16 |
def do_conversion(infile,outfile,desc=None,comment=None): |
| 17 |
startread = time.clock() |
| 18 |
incif = ReadCif(infile,grammar="STAR2") |
| 19 |
print('Finished reading %s in %f seconds' % (infile,time.clock() - startread)) |
| 20 |
try: |
| 21 |
incif = CifDic(incif,do_minimum=True) |
| 22 |
except: |
| 23 |
print('Failed to read as CIF dictionary') |
| 24 |
incif.set_grammar("2.0") |
| 25 |
incif.standard = 'Dic' |
| 26 |
incif.SetTemplate("dic_template.dic") |
| 27 |
if comment is None: |
| 28 |
comment_header = \ |
| 29 |
"""############################################################################### |
| 30 |
# # |
| 31 |
# CIF Dictionary # |
| 32 |
# -------------- # |
| 33 |
# # |
| 34 |
# CIF data definitions in DDLm format. # |
| 35 |
# # |
| 36 |
################################################################################ |
| 37 |
""" |
| 38 |
else: |
| 39 |
comment_header = open(comment).read() |
| 40 |
if desc is not None: |
| 41 |
incif.master_block.overwrite=True |
| 42 |
incif.master_block['_description.text'] = open(desc).read() |
| 43 |
incif.master_block.overwrite=False |
| 44 |
|
| 45 |
print('check: {!r}'.format(incif.recurse_child_list('enumeration'))) |
| 46 |
of = open(outfile,"w") |
| 47 |
of.write(incif.WriteOut(comment=comment_header,saves_after='_description.text')) |
| 48 |
of.close() |
| 49 |
print('Finished writing %s in %f seconds' % (outfile,time.clock() - startread)) |
| 50 |
|
| 51 |
if __name__ == "__main__": |
| 52 |
import sys |
| 53 |
comment_file = None |
| 54 |
description_file = None |
| 55 |
if len(sys.argv)<2 or len(sys.argv)>4: |
| 56 |
print('Usage: python star2_to_cif2.py infile description_file comment_file') |
| 57 |
print("""<infile> should be in STAR2 format. The output file will be <infile>+'.cif2' |
| 58 |
Optional description_file contains a description for the dictionary, and optional comment_file |
| 59 |
contains a comment for the dictionary header.""") |
| 60 |
infile = sys.argv[1] |
| 61 |
outfile = infile + '.cif2' |
| 62 |
if len(sys.argv)>2: |
| 63 |
description_file = sys.argv[2] |
| 64 |
if len(sys.argv)>3: |
| 65 |
comment_file = sys.argv[3] |
| 66 |
do_conversion(infile,outfile,desc=description_file,comment=comment_file) |