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  Deprecated 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(str(handle.next()).rstrip()) 38 record.num_pops = int(str(handle.next()).rstrip()) 39 record.num_loci = int(str(handle.next()).rstrip()) 40 for i in range(record.num_loci): 41 handle.next() 42 num_alleles = int(str(handle.next()).rstrip()) 43 pops_data = [] 44 if record.data_org==0: 45 for j in range(record.num_pops): 46 line_comp = str(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 deprecated 96 97 from Bio.ParserSupport import * 98
99 -class RecordParser(AbstractParser):
100 """Parses FDist data into a Record object (DEPRECATED). 101 102 This class is DEPRECATED; please use the read() function in this 103 module instead. 104 """
105 - def __init__(self):
106 import warnings 107 from Bio import BiopythonDeprecationWarning 108 warnings.warn("Bio.PopGen.FDist.RecordParser is deprecated. Please use the read() function in this module instead.", 109 BiopythonDeprecationWarning) 110 self._scanner = _Scanner() 111 self._consumer = _RecordConsumer()
112
113 - def parse(self, handle):
114 self._scanner.feed(handle, self._consumer) 115 return self._consumer.data
116
117 -class _Scanner:
118 """Scans a FDist record (DEPRECATED). 119 120 There is only one record per file. 121 122 This class is DEPRECATED; please use the read() function in this 123 module instead. 124 """ 125
126 - def __init__(self):
127 import warnings 128 from Bio import BiopythonDeprecationWarning 129 warnings.warn("Bio.PopGen.FDist._Scanner is deprecated. Please use the read() function in this module instead.", 130 BiopythonDeprecationWarning)
131
132 - def feed(self, handle, consumer):
133 """feed(self, handle, consumer) 134 135 Feed in a FDist unit record for scanning. handle is a file-like 136 object that contains a FDist record. consumer is a 137 Consumer object that will receive events as the report is scanned. 138 139 """ 140 141 consumer.start_record() 142 self.num_pops = None 143 self.num_loci = None 144 self.loci_data = [] 145 146 data_org = int(handle.readline().rstrip()) 147 consumer.data_org(data_org) 148 num_pops = int(handle.readline().rstrip()) 149 consumer.num_pops(num_pops) 150 num_loci = int(handle.readline().rstrip()) 151 consumer.num_loci(num_loci) 152 for i in range(num_loci): 153 handle.readline() 154 num_alleles = int(handle.readline().rstrip()) 155 pops_data = [] 156 if data_org==0: 157 for j in range(num_pops): 158 line_comp = handle.readline().rstrip().split(' ') 159 pop_dist = map(lambda x: int(x), line_comp) 160 pops_data.append(pop_dist) 161 else: 162 raise NotImplementedError('1/alleles by rows not implemented') 163 consumer.new_locus(num_alleles, pops_data) 164 consumer.end_record()
165
166 -class _RecordConsumer(AbstractConsumer):
167 """Consumer that converts a FDist record to a Record object (DEPRECATED). 168 169 Members: 170 data Record with FDist data. 171 172 This class is DEPRECATED; please use the read() function in this module 173 instead. 174 """
175 - def __init__(self):
176 import warnings 177 from Bio import BiopythonDeprecationWarning 178 warnings.warn("Bio.PopGen.FDist._RecordConsumer is deprecated. Please use the read() function in this module instead.", 179 BiopythonDeprecationWarning) 180 self.data = None
181
182 - def start_record(self):
183 self.data = Record()
184
185 - def end_record(self):
186 pass
187
188 - def data_org(self, data_org):
189 self.data.data_org = data_org
190
191 - def num_pops(self, num_pops):
192 self.data.num_pops = num_pops
193
194 - def num_loci(self, num_loci):
195 self.data.num_loci = num_loci
196
197 - def new_locus(self, num_alleles, pop_data):
198 self.data.loci_data.append((num_alleles, pop_data))
199