Package Bio :: Package PopGen :: Package FDist
[hide private]
[frames] | no frames]

Source Code for Package Bio.PopGen.FDist

  1  # Copyright 2007 by Tiago Antao <tiagoantao@gmail.com>.  All rights reserved. 
  2  # This code is part of the Biopython distribution and governed by its 
  3  # license.  Please see the LICENSE file that should have been included 
  4  # as part of this package. 
  5   
  6   
  7  """ 
  8  This module provides code to work with FDist. 
  9   
 10  See http://www.rubic.rdg.ac.uk/~mab/software.html . 
 11   
 12  Classes: 
 13  Record           Holds FDist data. 
 14   
 15  Functions: 
 16  read             Parses a FDist record (file) into a Record object. 
 17   
 18   
 19  Obsolete classes: 
 20  RecordParser     Parses a FDist record (file) into a Record object. 
 21   
 22  _Scanner         Scans a FDist record. 
 23  _RecordConsumer  Consumes FDist data to a Record object. 
 24   
 25   
 26  """ 
 27   
 28   
 29   
 30   
31 -def read(handle):
32 """Parses FDist data into a Record object. 33 34 handle is a file-like object that contains a FDist record. 35 """ 36 record = Record() 37 record.data_org = int(handle.next().rstrip()) 38 record.num_pops = int(handle.next().rstrip()) 39 record.num_loci = int(handle.next().rstrip()) 40 for i in range(record.num_loci): 41 handle.next() 42 num_alleles = int(handle.next().rstrip()) 43 pops_data = [] 44 if record.data_org==0: 45 for j in range(record.num_pops): 46 line_comp = handle.next().rstrip().split(' ') 47 pop_dist = map(lambda x: int(x), line_comp) 48 pops_data.append(pop_dist) 49 else: 50 raise NotImplementedError('1/alleles by rows not implemented') 51 record.loci_data.append((num_alleles, pops_data)) 52 return record
53 54
55 -class Record:
56 """Holds information from a FDist record. 57 58 Members: 59 data_org Data organization (0 pops by rows, 1 alleles by rows). 60 The Record will behave as if data was 0 (converting if needed) 61 62 num_pops Number of populations 63 64 num_loci Number of loci 65 66 loci_data Loci data 67 68 loci_data is a list, where each element represents a locus. Each element 69 is a tuple, the first element is the number of alleles, the second 70 element a list. Each element of the list is the count of each allele 71 per population. 72 73 """
74 - def __init__(self):
75 self.data_org = 0 76 self.num_pops = 0 77 self.num_loci = 0 78 self.loci_data = []
79
80 - def __str__(self):
81 rep = ['0\n'] #We only export in 0 format, even if originally was 1 82 rep.append(str(self.num_pops) + '\n') 83 rep.append(str(self.num_loci) + '\n') 84 rep.append('\n') 85 for locus_data in self.loci_data: 86 num_alleles, pops_data = locus_data 87 rep.append(str(num_alleles) + '\n') 88 for pop_data in pops_data: 89 for allele_count in pop_data: 90 rep.append(str(allele_count) + ' ') 91 rep.append('\n') 92 rep.append('\n') 93 return "".join(rep)
94 95 # Everything below is obsolete 96 97 from Bio.ParserSupport import * 98
99 -class RecordParser(AbstractParser):
100 """Parses FDist data into a Record object (OBSOLETE). 101 102 This function is OBSOLETE; plesae use the read() function in this module 103 instead. 104 """
105 - def __init__(self):
106 self._scanner = _Scanner() 107 self._consumer = _RecordConsumer()
108
109 - def parse(self, handle):
110 self._scanner.feed(handle, self._consumer) 111 return self._consumer.data
112
113 -class _Scanner:
114 """Scans a FDist record (OBSOLETE). 115 116 There is only one record per file. 117 118 This function is OBSOLETE; plesae use the read() function in this module 119 instead. 120 """ 121
122 - def feed(self, handle, consumer):
123 """feed(self, handle, consumer) 124 125 Feed in a FDist unit record for scanning. handle is a file-like 126 object that contains a FDist record. consumer is a 127 Consumer object that will receive events as the report is scanned. 128 129 """ 130 131 consumer.start_record() 132 self.num_pops = None 133 self.num_loci = None 134 self.loci_data = [] 135 136 data_org = int(handle.readline().rstrip()) 137 consumer.data_org(data_org) 138 num_pops = int(handle.readline().rstrip()) 139 consumer.num_pops(num_pops) 140 num_loci = int(handle.readline().rstrip()) 141 consumer.num_loci(num_loci) 142 for i in range(num_loci): 143 handle.readline() 144 num_alleles = int(handle.readline().rstrip()) 145 pops_data = [] 146 if data_org==0: 147 for j in range(num_pops): 148 line_comp = handle.readline().rstrip().split(' ') 149 pop_dist = map(lambda x: int(x), line_comp) 150 pops_data.append(pop_dist) 151 else: 152 raise NotImplementedError('1/alleles by rows not implemented') 153 consumer.new_locus(num_alleles, pops_data) 154 consumer.end_record()
155
156 -class _RecordConsumer(AbstractConsumer):
157 """Consumer that converts a FDist record to a Record object (OBSOLETE). 158 159 Members: 160 data Record with FDist data. 161 162 This function is OBSOLETE; plesae use the read() function in this module 163 instead. 164 """
165 - def __init__(self):
166 self.data = None
167
168 - def start_record(self):
169 self.data = Record()
170
171 - def end_record(self):
172 pass
173
174 - def data_org(self, data_org):
175 self.data.data_org = data_org
176
177 - def num_pops(self, num_pops):
178 self.data.num_pops = num_pops
179
180 - def num_loci(self, num_loci):
181 self.data.num_loci = num_loci
182
183 - def new_locus(self, num_alleles, pop_data):
184 self.data.loci_data.append((num_alleles, pop_data))
185