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