Module file_ops
[hide private]
[frames] | no frames]

Source Code for Module file_ops

  1  from os import mkdir, chmod 
  2  from re import match 
  3  from string import split 
  4  import sys 
  5   
6 -class file_ops:
7 - def __init__(self, mf):
8 "Class containing the file operations" 9 10 self.mf = mf
11 12
13 - def close_mf_files(self, dir):
14 "Close the mfin, mfdata, mfmodel, mfpar, and run files, and make the run file executable." 15 16 self.mf.mfin.close() 17 self.mf.mfdata.close() 18 self.mf.mfmodel.close() 19 self.mf.mfpar.close() 20 self.mf.run.close() 21 chmod(dir + '/run', 0777)
22 23
24 - def init_log_file(self, title):
25 "Initialize the log file." 26 27 self.mf.log = open('log.stage' + self.mf.data.stage, 'w') 28 self.mf.log.write(title)
29 30
31 - def open_file(self, file_name):
32 "Open the file 'file' and return all the data." 33 34 file = open(file_name, 'r') 35 lines = file.readlines() 36 data = [] 37 i = 0 38 for line in lines: 39 if i != 0: 40 j = i - 1 41 row = split(line) 42 data.append([]) 43 data[j].append(row[0]) 44 data[j].append(row[1]) 45 for k in range(len(row)): 46 if k > 1: 47 data[j].append(float(row[k])) 48 i = i + 1 49 return data
50 51
52 - def open_input(self):
53 "Open the input file." 54 55 try: 56 open("input", 'r') 57 except IOError: 58 print "Input file \"input\" does not exist, quitting script!\n" 59 sys.exit() 60 input = open("input", 'r') 61 return input
62 63
64 - def mkdir(self, dir):
65 "Create the given directory, or exit if the directory exists." 66 67 self.mf.log.write("Making directory " + dir + "\n") 68 try: 69 mkdir(dir) 70 except OSError: 71 print "Directory ./" + dir + " already exists, quitting script.\n" 72 sys.exit()
73 74
75 - def open_mf_files(self, dir):
76 "Open the mfin, mfdata, mfmodel, mfpar, and run files for writing." 77 78 self.mf.mfin = open(dir + '/mfin', 'w') 79 self.mf.mfdata = open(dir + '/mfdata', 'w') 80 self.mf.mfmodel = open(dir + '/mfmodel', 'w') 81 self.mf.mfpar = open(dir + '/mfpar', 'w') 82 self.mf.run = open(dir + '/run', 'w')
83 84
85 - def read_file(self, file_name):
86 "Attempt to read the file, or quit the script if it does not exist." 87 88 try: 89 open(file_name, 'r') 90 except IOError: 91 print "The file '" + file_name + "' does not exist, quitting script.\n\n" 92 sys.exit() 93 file = open(file_name, 'r') 94 return file
95 96
97 - def relax_data(self, file):
98 """Open the relaxation data in the file 'file' and return all the data. 99 100 It is assumed that the file has four columns separated by whitespace. The columns should be: 101 0 - Residue numbers 102 1 - Residue names 103 2 - R1, R2, or NOE values 104 3 - The errors 105 """ 106 107 lines = open(file, 'r') 108 lines = lines.readlines() 109 data = [] 110 i = 0 111 for line in lines: 112 if i != 0: 113 j = i - 1 114 row = split(line) 115 data.append([]) 116 data[j].append(row[0]) 117 data[j].append(row[1]) 118 data[j].append(float(row[2])) 119 data[j].append(float(row[3])) 120 i = i + 1 121 return data
122