Back to index

python-biopython  1.60
Public Member Functions | Public Attributes | Private Attributes
Bio.PopGen.GenePop.FileParser.FileRecord Class Reference

List of all members.

Public Member Functions

def __init__
def __str__
def start_read
def skip_header
def seek_position
def skip_population
def get_individual
def remove_population
def remove_locus_by_position
def remove_loci_by_position
def remove_locus_by_name
def remove_loci_by_name

Public Attributes

 comment_line
 loci_list
 fname
 current_pop
 current_ind

Private Attributes

 _handle

Detailed Description

Holds information from a GenePop record.

Members:
marker_len         The marker length (2 or 3 digit code per allele).    

comment_line       Comment line.

loci_list          List of loci names.

Functions:
get_individual     Returns the next individual of the current population.

skip_population    Skips the current population.

skip_population skips the individuals of the current population, returns
True if there are more populations.

get_individual returns an individual of the current population (or None
if the list ended).
Each individual is a pair composed by individual
name and a list of alleles (2 per marker or 1 for haploid data).
Examples
        ('Ind1', [(1,2),    (3,3), (200,201)]
        ('Ind2', [(2,None), (3,3), (None,None)]
        ('Other1', [(1,1),  (4,3), (200,200)]

Definition at line 36 of file FileParser.py.


Constructor & Destructor Documentation

Definition at line 65 of file FileParser.py.

00065 
00066     def __init__(self, fname):
00067         self.comment_line    = ""
00068         self.loci_list       = []
00069         self.fname           = fname
00070         self.start_read()


Member Function Documentation

Returns (reconstructs) a GenePop textual representation.

   This might take a lot of memory.
   Marker length will be 3.

Definition at line 71 of file FileParser.py.

00071 
00072     def __str__(self):
00073         """Returns (reconstructs) a GenePop textual representation.
00074 
00075            This might take a lot of memory.
00076            Marker length will be 3.
00077         """
00078         marker_len = 3
00079         rep  = [self.comment_line + '\n']
00080         rep.append('\n'.join(self.loci_list) + '\n')
00081         current_pop = self.current_pop
00082         current_ind = self.current_ind
00083         self._handle.seek(0)
00084         self.skip_header()
00085         rep.append('Pop\n')
00086         more = True
00087         while more:
00088             res = self.get_individual()
00089             if res == True:
00090                 rep.append('Pop\n')
00091             elif res == False:
00092                 more = False
00093             else:
00094                 name, markers = res
00095                 rep.append(name)
00096                 rep.append(',')
00097                 for marker in markers:
00098                     rep.append(' ')
00099                     for al in marker:
00100                         if al == None:
00101                             al = '0'
00102                         aStr = str(al)
00103                         while len(aStr)<marker_len:
00104                             aStr = "".join(['0', aStr])
00105                         rep.append(aStr)
00106                 rep.append('\n')
00107         self.seek_position(current_pop, current_ind)
00108         return "".join(rep)
00109 

Here is the call graph for this function:

Gets the next individual.

   Returns individual information if there are more individuals
   in the current population.
   Returns True if there are no more individuals in the current
   population, but there are more populations. Next read will
   be of the following pop.
   Returns False if at end of file.

Definition at line 166 of file FileParser.py.

00166 
00167     def get_individual(self):
00168         """Gets the next individual.
00169 
00170            Returns individual information if there are more individuals
00171            in the current population.
00172            Returns True if there are no more individuals in the current
00173            population, but there are more populations. Next read will
00174            be of the following pop.
00175            Returns False if at end of file.
00176         """
00177         marker_len = None
00178         for line in self._handle:
00179             line = line.rstrip()
00180             if line.upper()=='POP':
00181                 self.current_pop += 1
00182                 self.current_ind = 0
00183                 return True
00184             else:
00185                 self.current_ind += 1
00186                 indiv_name, allele_list, ignore = get_indiv(line)
00187                 return (indiv_name, allele_list)
00188         return False

Here is the call graph for this function:

Here is the caller graph for this function:

def Bio.PopGen.GenePop.FileParser.FileRecord.remove_loci_by_name (   self,
  names,
  fname 
)
Removes a loci list (by name).

   names - names
   fname - file to be created with loci removed

Definition at line 327 of file FileParser.py.

00327 
00328     def remove_loci_by_name(self, names, fname):
00329         """Removes a loci list (by name).
00330 
00331            names - names
00332            fname - file to be created with loci removed
00333         """
00334         positions = []
00335         for i in range(len(self.loci_list)):
00336             if self.loci_list[i] in names:
00337                 positions.append(i)
00338         self.remove_loci_by_position(positions, fname)
00339         #If here than locus not existent... Maybe raise exception?
00340         #   Although it should be Ok... Just a boolean return, maybe?
00341 

Here is the call graph for this function:

def Bio.PopGen.GenePop.FileParser.FileRecord.remove_loci_by_position (   self,
  positions,
  fname 
)
Removes a set of loci by position.

   positions - positions
   fname - file to be created with locus removed

Definition at line 271 of file FileParser.py.

00271 
00272     def remove_loci_by_position(self, positions, fname):
00273         """Removes a set of loci by position.
00274 
00275            positions - positions
00276            fname - file to be created with locus removed
00277         """
00278         old_rec = read(self.fname)
00279         f = open(fname, "w")
00280         f.write(self.comment_line + "\n")
00281         loci_list = old_rec.loci_list
00282         positions.sort()
00283         positions.reverse()
00284         for pos in positions:
00285             del loci_list[pos]
00286         for locus in loci_list:
00287             f.write(locus + "\n")
00288         l_parser = old_rec.get_individual()
00289         f.write("POP\n")
00290         while l_parser:
00291             if l_parser == True:
00292                 f.write("POP\n")
00293             else:
00294                 name, markers = l_parser
00295                 f.write(name + ",")
00296                 marker_pos = 0
00297                 for marker in markers:
00298                     if marker_pos in positions:
00299                         marker_pos += 1
00300                         continue
00301                     marker_pos += 1
00302                     f.write(' ')
00303                     for al in marker:
00304                         if al == None:
00305                             al = '0'
00306                         aStr = str(al)
00307                         while len(aStr)<3:
00308                             aStr = "".join(['0', aStr])
00309                         f.write(aStr)
00310                 f.write('\n')
00311 
00312             l_parser = old_rec.get_individual()
00313         f.close()

Here is the call graph for this function:

Here is the caller graph for this function:

Removes a locus by name.

   name - name
   fname - file to be created with locus removed

Definition at line 314 of file FileParser.py.

00314 
00315     def remove_locus_by_name(self, name, fname):
00316         """Removes a locus by name.
00317 
00318            name - name
00319            fname - file to be created with locus removed
00320         """
00321         for i in range(len(self.loci_list)):
00322             if self.loci_list[i] == name:
00323                 self.remove_locus_by_position(i, fname)
00324                 return
00325         #If here than locus not existent... Maybe raise exception?
00326         #   Although it should be Ok... Just a boolean return, maybe?
    

Here is the call graph for this function:

Removes a locus by position.

   pos - position
   fname - file to be created with locus removed

Definition at line 231 of file FileParser.py.

00231 
00232     def remove_locus_by_position(self, pos, fname):
00233         """Removes a locus by position.
00234 
00235            pos - position
00236            fname - file to be created with locus removed
00237         """
00238         old_rec = read(self.fname)
00239         f = open(fname, "w")
00240         f.write(self.comment_line + "\n")
00241         loci_list = old_rec.loci_list
00242         del loci_list[pos]
00243         for locus in loci_list:
00244             f.write(locus + "\n")
00245         l_parser = old_rec.get_individual()
00246         f.write("POP\n")
00247         while l_parser:
00248             if l_parser == True:
00249                 f.write("POP\n")
00250             else:
00251                 name, markers = l_parser
00252                 f.write(name + ",")
00253                 marker_pos = 0
00254                 for marker in markers:
00255                     if marker_pos == pos:
00256                         marker_pos += 1
00257                         continue
00258                     marker_pos += 1
00259                     f.write(' ')
00260                     for al in marker:
00261                         if al == None:
00262                             al = '0'
00263                         aStr = str(al)
00264                         while len(aStr)<3:
00265                             aStr = "".join(['0', aStr])
00266                         f.write(aStr)
00267                 f.write('\n')
00268 
00269             l_parser = old_rec.get_individual()
00270         f.close()

Here is the call graph for this function:

Here is the caller graph for this function:

Removes a population (by position).

   pos - position
   fname - file to be created with population removed

Definition at line 189 of file FileParser.py.

00189 
00190     def remove_population(self, pos, fname):
00191         """Removes a population (by position).
00192 
00193            pos - position
00194            fname - file to be created with population removed
00195         """
00196         old_rec = read(self.fname)
00197         f = open(fname, "w")
00198         f.write(self.comment_line + "\n")
00199         for locus in old_rec.loci_list:
00200             f.write(locus + "\n")
00201         curr_pop = 0
00202         l_parser = old_rec.get_individual()
00203         start_pop = True
00204         while l_parser:
00205             if curr_pop == pos:
00206                 old_rec.skip_population()
00207                 curr_pop += 1
00208             else:
00209                 if l_parser == True:
00210                     curr_pop += 1
00211                     start_pop = True
00212                 else:
00213                     if start_pop:
00214                         f.write("POP\n")
00215                         start_pop = False
00216                     name, markers = l_parser
00217                     f.write(name + ",")
00218                     for marker in markers:
00219                         f.write(' ')
00220                         for al in marker:
00221                             if al == None:
00222                                 al = '0'
00223                             aStr = str(al)
00224                             while len(aStr)<3:
00225                                 aStr = "".join(['0', aStr])
00226                             f.write(aStr)
00227                     f.write('\n')
00228         
00229             l_parser = old_rec.get_individual()
00230         f.close()
    

Here is the call graph for this function:

def Bio.PopGen.GenePop.FileParser.FileRecord.seek_position (   self,
  pop,
  indiv 
)
Seeks a certain position in the file.

   pop   - pop position (0 is first)
   indiv - individual in pop

Definition at line 140 of file FileParser.py.

00140 
00141     def seek_position(self, pop, indiv):
00142         """Seeks a certain position in the file.
00143 
00144            pop   - pop position (0 is first)
00145            indiv - individual in pop
00146         """
00147         self._handle.seek(0)
00148         self.skip_header()
00149         while pop>0:
00150             self.skip_population()
00151             pop -= 1
00152         while indiv>0:
00153             self.get_individual()
00154             indiv -= 1

Here is the call graph for this function:

Here is the caller graph for this function:

Skips the Header. To be done after a re-open.

Definition at line 132 of file FileParser.py.

00132 
00133     def skip_header(self):
00134         """Skips the Header. To be done after a re-open."""
00135         self.current_pop = 0
00136         self.current_ind = 0
00137         for line in self._handle:
00138             if line.rstrip().upper()=="POP":
00139                 return

Here is the caller graph for this function:

Definition at line 155 of file FileParser.py.

00155 
00156     def skip_population(self):
00157         "Skips the current population. Returns true if there is another pop."
00158         for line in self._handle:
00159             if line=="":
00160                 return False
00161             line = line.rstrip()
00162             if line.upper()=='POP':
00163                 self.current_pop += 1
00164                 self.current_ind = 0
00165                 return True

Here is the caller graph for this function:

Starts parsing a file containing a GenePop file.

Definition at line 110 of file FileParser.py.

00110 
00111     def start_read(self):
00112         """Starts parsing a file containing a GenePop file.
00113         """
00114         self._handle = open(self.fname)
00115         self.comment_line = self._handle.readline().rstrip()
00116         #We can now have one loci per line or all loci in a single line
00117         #separated by either space or comma+space...
00118         #We will remove all commas on loci... that should not be a problem
00119         sample_loci_line = self._handle.readline().rstrip().replace(',', '')
00120         all_loci = sample_loci_line.split(' ')
00121         self.loci_list.extend(all_loci)
00122         for line in self._handle:
00123             line = line.rstrip()
00124             if line.upper()=='POP':
00125                 break
00126             self.loci_list.append(line)
00127         else:
00128             raise ValueError('No population data found, file probably not GenePop related')
00129         #self._after_pop = True
00130         self.current_pop = 0
00131         self.current_ind = 0


Member Data Documentation

Definition at line 113 of file FileParser.py.

Definition at line 66 of file FileParser.py.

Definition at line 130 of file FileParser.py.

Definition at line 129 of file FileParser.py.

Definition at line 68 of file FileParser.py.

Definition at line 67 of file FileParser.py.


The documentation for this class was generated from the following file: