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 (old) and 
11  http://www.maths.bris.ac.uk/~mamab/ (new) for downloading the 
12  FDist tool by Mark Beaumont. 
13   
14  Classes: 
15  Record           Holds FDist data. 
16   
17  Functions: 
18  read             Parses a FDist record (file) into a Record object. 
19   
20   
21  """ 
22   
23   
24 -def read(handle):
25 """Parses FDist data into a Record object. 26 27 handle is a file-like object that contains a FDist record. 28 """ 29 record = Record() 30 record.data_org = int(str(next(handle)).rstrip()) 31 record.num_pops = int(str(next(handle)).rstrip()) 32 record.num_loci = int(str(next(handle)).rstrip()) 33 for i in range(record.num_loci): 34 next(handle) 35 num_alleles = int(str(next(handle)).rstrip()) 36 pops_data = [] 37 if record.data_org==0: 38 for j in range(record.num_pops): 39 line_comp = str(next(handle)).rstrip().split(' ') 40 pop_dist = [int(x) for x in line_comp] 41 pops_data.append(pop_dist) 42 else: 43 raise NotImplementedError('1/alleles by rows not implemented') 44 record.loci_data.append((num_alleles, pops_data)) 45 return record
46 47
48 -class Record(object):
49 """Holds information from a FDist record. 50 51 Members: 52 data_org Data organization (0 pops by rows, 1 alleles by rows). 53 The Record will behave as if data was 0 (converting if needed) 54 55 num_pops Number of populations 56 57 num_loci Number of loci 58 59 loci_data Loci data 60 61 loci_data is a list, where each element represents a locus. Each element 62 is a tuple, the first element is the number of alleles, the second 63 element a list. Each element of the list is the count of each allele 64 per population. 65 """
66 - def __init__(self):
67 self.data_org = 0 68 self.num_pops = 0 69 self.num_loci = 0 70 self.loci_data = []
71
72 - def __str__(self):
73 rep = ['0\n'] # We only export in 0 format, even if originally was 1 74 rep.append(str(self.num_pops) + '\n') 75 rep.append(str(self.num_loci) + '\n') 76 rep.append('\n') 77 for locus_data in self.loci_data: 78 num_alleles, pops_data = locus_data 79 rep.append(str(num_alleles) + '\n') 80 for pop_data in pops_data: 81 for allele_count in pop_data: 82 rep.append(str(allele_count) + ' ') 83 rep.append('\n') 84 rep.append('\n') 85 return "".join(rep)
86