Package Bio :: Package NMR :: Module xpktools
[hide private]
[frames] | no frames]

Source Code for Module Bio.NMR.xpktools

  1  # xpktools.py: A python module containing function definitions and classes 
  2  #          useful for manipulating data from nmrview .xpk peaklist files. 
  3  # 
  4  # ********** INDEX of functions and classes ********** 
  5  # 
  6  #    XpkEntry class: A class suited for handling single lines of 
  7  #        non-header data from an nmrview .xpk file.  This class 
  8  #        provides methods for extracting data by the field name 
  9  #        which is listed in the last line of the peaklist header. 
 10   
 11  import sys 
 12   
 13  # * * * * * INITIALIZATIONS * * * * * 
 14  HEADERLEN=6 
 15  # * * * * * _______________ * * * * * 
 16   
17 -class XpkEntry:
18 # Usage: XpkEntry(xpkentry,xpkheadline) where xpkentry is the line 19 # from an nmrview .xpk file and xpkheadline is the line from 20 # the header file that gives the names of the entries 21 # which is typcially the sixth line of the header (counting fm 1) 22 # Variables are accessed by either their name in the header line as in 23 # self.field["H1.P"] will return the H1.P entry for example. 24 # self.field["entrynum"] returns the line number (1st field of line) 25
26 - def __init__(self,entry,headline):
27 self.fields={} # Holds all fields from input line in a dictionary 28 # keys are data labels from the .xpk header 29 datlist = entry.split() 30 headlist = headline.split() 31 32 i=0 33 for i in range(len(datlist)-1): 34 self.fields[headlist[i]]=datlist[i+1] 35 i=i+1 36 37 try: 38 self.fields["entrynum"]=datlist[0] 39 except IndexError, e: 40 pass
41
42 -class Peaklist:
43 # This class reads in an entire xpk file and returns 44 # Header file lines are available as attributes 45 # The data lines are available as a list
46 - def __init__(self,infn):
47 48 self.data=[] # init the data line list 49 50 infile=open(infn,'r') 51 52 # Read in the header lines 53 self.firstline=infile.readline().split("\012")[0] 54 self.axislabels=infile.readline().split("\012")[0] 55 self.dataset=infile.readline().split("\012")[0] 56 self.sw=infile.readline().split("\012")[0] 57 self.sf=infile.readline().split("\012")[0] 58 self.datalabels=infile.readline().split("\012")[0] 59 60 # Read in the data lines to a list 61 line=infile.readline() 62 while line: 63 self.data.append(line.split("\012")[0]) 64 line=infile.readline()
65
66 - def residue_dict(self,index):
67 # Generate a dictionary idexed by residue number or a nucleus 68 # The nucleus should be given as the input argument in the 69 # same form as it appears in the xpk label line (H1, 15N for example) 70 71 maxres=-1; minres=-1 72 73 # Cast the data lines into the xpentry class 74 self.dict={} 75 for i in range(len(self.data)): 76 line=self.data[i] 77 ind=XpkEntry(line,self.datalabels).fields[index+".L"] 78 key=ind.split(".")[0] 79 80 res=int(key) 81 82 if (maxres==-1): 83 maxres=res 84 if (minres==-1): 85 minres=res 86 87 maxres=max([maxres,res]) 88 minres=min([minres,res]) 89 90 if str(res) in self.dict: 91 # Append additional data to list under same key 92 templst=self.dict[str(res)] 93 templst.append(line) 94 self.dict[str(res)]=templst 95 96 else: 97 # This is a new residue, start a new list 98 self.dict[str(res)]=[line] # Use [] for list type 99 100 self.dict["maxres"]=maxres 101 self.dict["minres"]=minres 102 103 return self.dict
104
105 - def write_header(self,outfn):
106 outfile=_try_open_write(outfn) 107 outfile.write(self.firstline);outfile.write("\012") 108 outfile.write(self.axislabels);outfile.write("\012") 109 outfile.write(self.dataset);outfile.write("\012") 110 outfile.write(self.sw);outfile.write("\012") 111 outfile.write(self.sf);outfile.write("\012") 112 outfile.write(self.datalabels);outfile.write("\012") 113 outfile.close()
114
115 -def _try_open_read(fn):
116 # Try to open a file for reading. Exit on IOError 117 try: 118 infile=open(fn,'r') 119 except IOError, e: 120 print "file", fn, "could not be opened for reading - quitting." 121 sys.exit(0) 122 return infile
123
124 -def _try_open_write(fn):
125 # Try to open a file for writing. Exit on IOError 126 try: 127 infile=open(fn,'w') 128 except IOError, e: 129 print "file", fn, "could not be opened for writing - quitting." 130 sys.exit(0) 131 return infile
132 133
134 -def replace_entry(line,fieldn,newentry):
135 # Replace an entry in a string by the field number 136 # No padding is implemented currently. Spacing will change if 137 # the original field entry and the new field entry are of 138 # different lengths. 139 # This method depends on xpktools._find_start_entry 140 141 start=_find_start_entry(line,fieldn) 142 leng=len(line[start:].split()[0]) 143 newline=line[:start]+str(newentry)+line[(start+leng):] 144 return newline
145
146 -def _find_start_entry(line,n):
147 # find the starting point character for the n'th entry in 148 # a space delimited line. n is counted starting with 1 149 # The n=1 field by definition begins at the first character 150 # This function is used by replace_entry 151 152 infield=0 # A flag that indicates that the counter is in a field 153 154 if (n==1): 155 return 0 # Special case 156 157 # Count the number of fields by counting spaces 158 c=1 159 leng=len(line) 160 161 # Initialize variables according to whether the first character 162 # is a space or a character 163 if (line[0]==" "): 164 infield=0 165 field=0 166 else: 167 infield=1 168 field=1 169 170 171 while (c<leng and field<n): 172 if (infield): 173 if (line[c]==" " and not (line[c-1]==" ")): 174 infield=0 175 else: 176 if (not line[c]==" "): 177 infield=1 178 field=field+1 179 180 c=c+1 181 182 return c-1
183 184
185 -def data_table(fn_list, datalabel, keyatom):
186 # Generate and generate a data table from a list of 187 # input xpk files <fn_list>. The data element reported is 188 # <datalabel> and the index for the data table is by the 189 # nucleus indicated by <keyatom>. 190 191 outlist=[] 192 193 [dict_list,label_line_list]=_read_dicts(fn_list,keyatom) 194 195 # Find global max and min residue numbers 196 minr=dict_list[0]["minres"]; maxr=dict_list[0]["maxres"] 197 198 for dictionary in dict_list: 199 if (maxr < dictionary["maxres"]): 200 maxr = dictionary["maxres"] 201 if (minr > dictionary["minres"]): 202 minr = dictionary["minres"] 203 204 res=minr 205 while res <= maxr: # s.t. res numbers 206 count=0 207 line=str(res) 208 for dictionary in dict_list: # s.t. dictionaries 209 label=label_line_list[count] 210 if str(res) in dictionary: 211 line=line+"\t"+XpkEntry(dictionary[str(res)][0],label).fields[datalabel] 212 else: 213 line=line+"\t"+"*" 214 count=count+1 215 line=line+"\n" 216 outlist.append(line) 217 res=res+1 218 219 return outlist
220
221 -def _sort_keys(dictionary):
222 keys=dictionary.keys() 223 sorted_keys=keys.sort() 224 return sorted_keys
225
226 -def _read_dicts(fn_list, keyatom):
227 # Read multiple files into a list of residue dictionaries 228 dict_list=[]; datalabel_list=[] 229 for fn in fn_list: 230 peaklist=Peaklist(fn); dict=peaklist.residue_dict(keyatom) 231 dict_list.append(dict) 232 datalabel_list.append(peaklist.datalabels) 233 234 return [dict_list, datalabel_list]
235