Trees | Indices | Help |
---|
|
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 12 import string 13 14 # * * * * * INITIALIZATIONS * * * * * 15 HEADERLEN=6 16 # * * * * * _______________ * * * * * 1719 # Usage: XpkEntry(xpkentry,xpkheadline) where xpkentry is the line 20 # from an nmrview .xpk file and xpkheadline is the line from 21 # the header file that gives the names of the entries 22 # which is typcially the sixth line of the header (counting fm 1) 23 # Variables are accessed by either their name in the header line as in 24 # self.field["H1.P"] will return the H1.P entry for example. 25 # self.field["entrynum"] returns the line number (1st field of line) 264228 self.fields={} # Holds all fields from input line in a dictionary 29 # keys are data labels from the .xpk header 30 datlist = string.split(entry) 31 headlist = string.split(headline) 32 33 i=0 34 for i in range(len(datlist)-1): 35 self.fields[headlist[i]]=datlist[i+1] 36 i=i+1 37 38 try: 39 self.fields["entrynum"]=datlist[0] 40 except IndexError, e: 41 pass44 # This class reads in an entire xpk file and returns 45 # Header file lines are available as attributes 46 # The data lines are available as a list11548 49 self.data=[] # init the data line list 50 51 infile=open(infn,'r') 52 53 # Read in the header lines 54 self.firstline=string.split(infile.readline(),"\012")[0] 55 self.axislabels=string.split(infile.readline(),"\012")[0] 56 self.dataset=string.split(infile.readline(),"\012")[0] 57 self.sw=string.split(infile.readline(),"\012")[0] 58 self.sf=string.split(infile.readline(),"\012")[0] 59 self.datalabels=string.split(infile.readline(),"\012")[0] 60 61 # Read in the data lines to a list 62 line=infile.readline() 63 while line: 64 self.data.append(string.split(line,"\012")[0]) 65 line=infile.readline()6668 # Generate a dictionary idexed by residue number or a nucleus 69 # The nucleus should be given as the input argument in the 70 # same form as it appears in the xpk label line (H1, 15N for example) 71 72 maxres=-1; minres=-1 73 74 # Cast the data lines into the xpentry class 75 self.dict={} 76 for i in range(len(self.data)): 77 line=self.data[i] 78 ind=XpkEntry(line,self.datalabels).fields[index+".L"] 79 key=string.split(ind,".")[0] 80 81 res=string.atoi(key) 82 83 if (maxres==-1): 84 maxres=res 85 if (minres==-1): 86 minres=res 87 88 maxres=max([maxres,res]) 89 minres=min([minres,res]) 90 91 if self.dict.has_key(str(res)): 92 # Append additional data to list under same key 93 templst=self.dict[str(res)] 94 templst.append(line) 95 self.dict[str(res)]=templst 96 97 else: 98 # This is a new residue, start a new list 99 self.dict[str(res)]=[line] # Use [] for list type 100 101 self.dict["maxres"]=maxres 102 self.dict["minres"]=minres 103 104 return self.dict105107 outfile=_try_open_write(outfn) 108 outfile.write(self.firstline);outfile.write("\012") 109 outfile.write(self.axislabels);outfile.write("\012") 110 outfile.write(self.dataset);outfile.write("\012") 111 outfile.write(self.sw);outfile.write("\012") 112 outfile.write(self.sf);outfile.write("\012") 113 outfile.write(self.datalabels);outfile.write("\012") 114 outfile.close()117 # Try to open a file for reading. Exit on IOError 118 try: 119 infile=open(fn,'r') 120 except IOError, e: 121 print "file", fn, "could not be opened for reading - quitting." 122 sys.exit(0) 123 return infile124126 # Try to open a file for writing. Exit on IOError 127 try: 128 infile=open(fn,'w') 129 except IOError, e: 130 print "file", fn, "could not be opened for writing - quitting." 131 sys.exit(0) 132 return infile133 134136 # Replace an entry in a string by the field number 137 # No padding is implemented currently. Spacing will change if 138 # the original field entry and the new field entry are of 139 # different lengths. 140 # This method depends on xpktools._find_start_entry 141 142 start=_find_start_entry(line,fieldn) 143 leng=len(string.splitfields(line[start:])[0]) 144 newline=line[:start]+str(newentry)+line[(start+leng):] 145 return newline146148 # find the starting point character for the n'th entry in 149 # a space delimited line. n is counted starting with 1 150 # The n=1 field by definition begins at the first character 151 # This function is used by replace_entry 152 153 infield=0 # A flag that indicates that the counter is in a field 154 155 if (n==1): 156 return 0 # Special case 157 158 # Count the number of fields by counting spaces 159 c=1 160 leng=len(line) 161 162 # Initialize variables according to whether the first character 163 # is a space or a character 164 if (line[0]==" "): 165 infield=0 166 field=0 167 else: 168 infield=1 169 field=1 170 171 172 while (c<leng and field<n): 173 if (infield): 174 if (line[c]==" " and not (line[c-1]==" ")): 175 infield=0 176 else: 177 if (not line[c]==" "): 178 infield=1 179 field=field+1 180 181 c=c+1 182 183 return c-1184 185187 # Generate and generate a data table from a list of 188 # input xpk files <fn_list>. The data element reported is 189 # <datalabel> and the index for the data table is by the 190 # nucleus indicated by <keyatom>. 191 192 outlist=[] 193 194 [dict_list,label_line_list]=_read_dicts(fn_list,keyatom) 195 196 # Find global max and min residue numbers 197 minr=dict_list[0]["minres"]; maxr=dict_list[0]["maxres"] 198 199 for dict in dict_list: 200 if (maxr < dict["maxres"]): 201 maxr = dict["maxres"] 202 if (minr > dict["minres"]): 203 minr = dict["minres"] 204 205 res=minr 206 while res <= maxr: # s.t. res numbers 207 count=0 208 line=str(res) 209 for dict in dict_list: # s.t. dictionaries 210 label=label_line_list[count] 211 if ( dict.has_key(str(res)) ): 212 line=line+"\t"+XpkEntry(dict[str(res)][0],label).fields[datalabel] 213 else: 214 line=line+"\t"+"*" 215 count=count+1 216 line=line+"\n" 217 outlist.append(line) 218 res=res+1 219 220 return outlist221 226228 # Read multiple files into a list of residue dictionaries 229 dict_list=[]; datalabel_list=[] 230 for fn in fn_list: 231 peaklist=Peaklist(fn); dict=peaklist.residue_dict(keyatom) 232 dict_list.append(dict) 233 datalabel_list.append(peaklist.datalabels) 234 235 return [dict_list, datalabel_list]236
Trees | Indices | Help |
---|
Generated by Epydoc 3.0.1 on Thu Feb 7 11:51:40 2008 | http://epydoc.sourceforge.net |