Source code for MapParser

## TEMPY is a Python library designed to help the user manipulate and analyse atomic structures and density maps from 3D EM. 
## Copyright (c) 2013 Daven Vasishtan,Irene Farabella, Arun Prasad Pandurangan, Harpal Sahota, Frank Alber and Maya Topf



#--Global imports--#
import numpy
from numpy import array, fromfile, flipud
import struct as binary
import string

#--Local imports--#
from EMMap import * 

[docs]class MapParser: """ A class to read various EM map file types into a Map object instance. """ def __init__(self): ## mapping of numpy type to MRC mode self.numpy2mrc = { ## convert these to int8 numpy.uint8: 0, numpy.bool: 0, numpy.bool_: 0, ## convert these to int16 numpy.int16: 1, numpy.int8: 1, ## convert these to float32 numpy.float32: 2, numpy.float64: 2, numpy.int32: 2, numpy.int: 2, ## convert these to complex64 numpy.complex: 4, numpy.complex64: 4, numpy.complex128: 4, ## convert these to uint16 numpy.uint16: 6, } @staticmethod
[docs] def readMRCHeader(filename, endian = '<'): f = open(filename,'rb') fm_string = endian+(10*'l')+(6*'f')+(3*'l')+(3*'f')+(27*'l')+(3*'f')+(4*'c')+'lfl' header = list(binary.unpack(fm_string, f.read(224))) notes = f.read(800) notes = string.replace(notes, '\x00', '') header.append(notes) header = tuple(header) f.close() return header
@staticmethod
[docs] def get_endian(filename): h = MapParser.readMRCHeader(filename) if 0 <= h[3] <= 6: endian = '<' else: endian = '>' return endian
@staticmethod
[docs] def readMRC(filename): mrc2numpy = { 0: numpy.uint8, 1: numpy.int16, 2: numpy.float32, # 3: complex made of two int16. No such thing in numpy # however, we could manually build a complex array by reading two # int16 arrays somehow. 4: numpy.complex64, 6: numpy.uint16, # according to UCSF } endian = MapParser.get_endian(filename) header = MapParser.readMRCHeader(filename, endian) box_size = tuple(flipud(header[0:3])) origin = header[49:52] #ctrl UCSF apix = header[10]/header[0] map_size = header[0]*header[1]*header[2] f = open(filename,'rb') f.seek(1024) map_data = fromfile(f, dtype=mrc2numpy[header[3]], count=map_size) map_data=map_data.reshape(box_size) map_data=array(map_data, dtype='float64') f.close() return Map(map_data, origin, apix, filename, header=header) #BROKEN
@staticmethod
[docs] def readXPLOR(filename, user_origin=None, user_box_size=None): f = open(filename, 'r') while(True): l = f.readline().split() #print l if(len(l) ==1 and l[0] == '0'): break new_map = [] line = 1 while(True): line = f.readline().split() for dens in line: new_map.append(float(dens)) if len(new_map) >= box_size[0]*box_size[1]*box_size[2]: break new_map = array(new_map) new_map = new_map.reshape(box_size[2], box_size[1], box_size[0]) f.close() return Map(new_map, origin, box_size, apix)
@staticmethod
[docs] def readSitus(filename): f = open(self.filename, 'r') first_line = f.readline().split() apix = float(first_line[0]) origin = map(float, first_line[1:4]) box_size = map(int, first_line[4:7]) new_map = [] line = 1 while(True): line = f.readline().split() for dens in line: new_map.append(float(dens)) if len(new_map) >= box_size[0]*box_size[1]*box_size[2]: break new_map = array(new_map) new_map = new_map.reshape(box_size[2], box_size[1], box_size[0]) f.close() return Map(new_map, origin, box_size, apix)