File: structure.py

package info (click to toggle)
python-cogent 1.5.3-2
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 16,424 kB
  • ctags: 24,343
  • sloc: python: 134,200; makefile: 100; ansic: 17; sh: 10
file content (54 lines) | stat: -rw-r--r-- 1,596 bytes parent folder | download
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
#!/usr/bin/env python

import os
from cogent.format.pdb import PDBWriter, PDBXWriter
from cogent.format.xyzrn import XYZRNWriter
from cogent.parse.record import FileFormatError

__author__ = "Marcin Cieslik"
__copyright__ = "Copyright 2007-2012, The Cogent Project"
__credits__ = ["Marcin Cieslik"]
__license__ = "GPL"
__version__ = "1.5.3"
__maintainer__ = "Marcin Cieslik"
__email__ = "mpc4p@virginia.edu"
__status__ = "Development"

def save_to_filename(entities, filename, format, **kw):
    """Saves a  structure in a specified format into a given file name.
    Arguments:
        - entities: structure or entities to be written
        - filename: name of the structure file
        - format: structure file format
    """
    f = open(filename, 'w')
    try:
        write_to_file(f, entities, format, **kw)
    except Exception:
        try:
            os.unlink(filename)
        except Exception:
            pass
        raise
    f.close()

def write_to_file(f, entities, format, **kw):
    """Saves a structure in a specified format into a given file handle.
    Arguments:
        - entities: structure or entities to be written
        - filename: name of the structure file
        - format: structure file format
    """
    format = format.lower()
    if format not in WRITERS:
        raise FileFormatError("Unsupported file format %s" % format)
    writer = WRITERS[format]
    writer(f, entities, **kw)

# to add a new file format add it's suffix and class name here
WRITERS = {
        'pdb': PDBWriter,
        'pdbx': PDBXWriter,
        'xyzrn': XYZRNWriter
        }