Functions in Spider/Spiderutils.py

Back

Reading and writing SPIDER document files
Filenames and file numbers
SPIDER file types and other tests
Utilities for finding and running SPIDER
Other utilities (fileReadLines)

# Reading and writing SPIDER document files

readdoc (filename [, column=1, keys=0])
Returns the contents of a Spider document file. The output is either a list or a dictionary (see Python documentation about these data types).
keys can be either 0, 1, or 'all' (in quotes)

writedoc (filename [, columns=None, lines=None, headers=None, keys=None, mode='w'])
Writes data to a file in Spider document file format.
Data can be organized as a set of columns or a set of lines.
(or as a dictionary, for compatibility with readdoc)
The call to writedoc must use EITHER the columns OR lines keyword.
Data must be integer or float.
filename: must have the data extension.
columns: a list of lists; each doc file column is a list of numbers.
lines: a list of lists; each doc file line is a list of numbers.
headers: a list of strings for column headings.
keys: a list of integers. Default starts with first key = 1.
mode='w' (default): deletes previously existing file. mode='a' appends to file.

numberOfColumns (docfile )
Returns the number of columns in a document file.

makeDocfileHeader (filename [, batext=None])
A utility used by writedoc; it returns the comment line at the start of a doc file.
>>> makeDocfileHeader('sel001.dat')
' ;dat/dat   14-MAR-2006 AT 20:03:49   sel001.dat\n'

getDocfileHeaders (filename )
Tries to get the column headings of a doc file. Returns a list of strings.
>>> getDocfileHeaders('defocus.dat')
['MICROGRAPH', 'DEFOCUS', 'ASTIG.ANG', 'ASTIG.MAG', 'CUTOFF.FREQ']

nowisthetime ()
A utility used by makeDocfileHeader; it returns a tuple of (date, time, 12-digit ID)
>>> nowisthetime()
('14-MAR-2006', '20:05:34', '060314200534')

# Filenames and file numbers

makeSpiderFilename (filename, n)
Returns a filename with a number substituted for asterisks.
Substitutes the first set of asterisks it finds (i.e. leftmost).
If the number of asterisks is too small for the number of digits, the filename is extended.
>>> makeSpiderFilename('mic***.dat', 123)
'mic123.dat'

filenumber (file)
Returns the number from a filename as an integer.

getfilenumber (filename)
Returns the number from a filename as a string with leading zeroes.
The file number must immediately precede the extension dot. Returns "" if no number found.
>>> getfilenumber("mic0089.ext")
'0089' 

name2template (filename [, all=0])
Replaces number in a filename with asterisks.
If filename has more than one number, it replaces number nearest the extension. If all is not equal to zero, all numbers are replaced.
>>> name2template('img001_051.ext')
'img001_***.ext'
>>> name2template('img001_051.ext', all=1)
'img***_***.ext'

template2filename (template [, n=0])
Replaces asterisks with number. Like makeSpiderFilename, except that template2filename can also get the number from a filename.
>>> template2filename('pic***.dat', 3)
'pic003.dat'
>>> template2filename('pic***.dat', 'doc00003.dat')
'pic003.dat'

numberlist2string (numberlist )
Given a list of integers, it returns a string of numbers, hyphenating runs of consecutive numbers. This can be useful for SPIDER commands that take file numbers represented by commas and hyphens.
>>> numberlist2string([1,2,3,4,8,11,12,13])
'1-4,8,11-13'

range2list (numberstring )
Performs the inverse of numberlist2string. Given a string of file numbers, it returns a list.
>>> range2list('1-4,8,11-13')
[1, 2, 3, 4, 8, 11, 12, 13]

# SPIDER file types and other tests
NB: these are not guaranteed 100% correct, but they make a commendable effort

istextfile (filename)
Returns 1 if input is a text file, 0 if it thinks it is a binary file.

isSpiderDocfile (filename)
Returns 1 if input is a SPIDER document file.

isSpiderBatchfile (filename)
Returns 1 if input is a SPIDER batch file. More of a 'guess', since there are too many variants to be sure.

isSpiderProcedurefile (file)
Returns 1 if input is a SPIDER procedure, i.e., called by another batch file.

isSpiderBin (filename)
Returns a nonzero value if input is a SPIDER binary file.
Possible return values: 'image', 'volume', 'stack', 'Fourier' (or zero)

isSpiderImage (file)
Returns 1 if input is a SPIDER 2D image.

isSpiderStack (file)
Returns 1 if input is a SPIDER stack (of 2D images).

isInt (f)
Returns 1 if input is an integer.

isDictionary (d)
Returns 1 if input is a Python dictionary.

isListofLists (d)
Returns 1 if input is a Python list, whose first member is also a list.

# Utilities for finding and running SPIDER

findSpider ()
Returns a path to an executable SPIDER (or the empty string if none found).
Scripts run in a shell that cannot automatically run SPIDER with the command 'spider'. They first need to locate the full path to the program, such as '/usr/local/bin/spider'.
findSpider calls findProgram to locate SPIDER, then tests it with testSpider.
spider = findSpider()

findProgram (prog)
Uses the Unix which command to locate a program. Returns the full path to the program (or the empty string if none found).
prog: the program to search for

testSpider (spider)
Returns 1 if the input is a working SPIDER executable, otherwise returns zero. Creates a small batch file and runs it.
spider: the full path of the SPIDER executable

runSpider (spider, batch, dataext)
A convenience function for running a SPIDER batch file in Python. It returns the output SPIDER writes to the terminal.
spider: the full path of the SPIDER executable
batch: batch filename (with extension)
dataext: data file extension
>>> spider = findSpider()
>>> out = runSpider(spider, 'b01.bat', 'dat')
>>> print out
 
  

 \__`O O'__/        SPIDER  --  COPYRIGHT
 ,__xXXXx___        HEALTH RESEARCH INC., ALBANY, NY.
  __xXXXx__
 /  /xxx\  \        VERSION:  UNIX  14.16 ISSUED: 03/30/2006
   /     \          DATE:    13-APR-2006    AT  13:39:37



 Results file: results.bat.0                                                                   
 Running: /net/bali/usr1/spider/bin/spider4mp.14.16.0                                                                                                                     
 STOP **** SPIDER NORMAL STOP ****

# Other utilities

fileReadLines (filename)
Reads the contents of a text file into a list. Each item in the list is a line from the file.
>>> b = fileReadLines("select.dat")

spiderInfo (filename)
Returns [type, (dimensions), (stats) ] from a SPIDER header.
If there are no stats (max, min, avg, stdev) in the header, it just returns [type, (dimensions)]
type can be "image", "volume", "Fourier", or "stack"
>>> spiderInfo('vol02.hcc')
['volume', (75, 75, 75), (0.01032, -0.0079, 6.6719e-05, 0.0012)]

getSpiderHeader (filename)
Returns the first 27 values of the SPIDER header (with Fortran indices, i.e., starting at 1)
>>> hdr = getSpiderHeader('vol02.hcc')
>>> hdr[12]  # check nsam
75.0

list2int (alist)
Converts a list of strings to integers.
>>> list2int(['1.0000', '2.000', '9.000'])
[1, 2, 9]

list2float (alist)
Converts a list of strings to floating point values.
>>> list2float(['1.2000', '2.000', '0.991'])
[1.2, 2.0, 0.991]

stripComment (line)
Strips batch file comments, i.e., removes anything after the first semicolon.


last updated: April 13, 2006