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 53 - data_org Data organization (0 pops by rows, 1 alleles by rows). 54 The Record will behave as if data was 0 (converting if needed) 55 56 - num_pops Number of populations 57 58 - num_loci Number of loci 59 60 - loci_data Loci data 61 62 loci_data is a list, where each element represents a locus. Each element 63 is a tuple, the first element is the number of alleles, the second 64 element a list. Each element of the list is the count of each allele 65 per population. 66 """
67 - def __init__(self):
68 self.data_org = 0 69 self.num_pops = 0 70 self.num_loci = 0 71 self.loci_data = []
72
73 - def __str__(self):
74 rep = ['0\n'] # We only export in 0 format, even if originally was 1 75 rep.append(str(self.num_pops) + '\n') 76 rep.append(str(self.num_loci) + '\n') 77 rep.append('\n') 78 for locus_data in self.loci_data: 79 num_alleles, pops_data = locus_data 80 rep.append(str(num_alleles) + '\n') 81 for pop_data in pops_data: 82 for allele_count in pop_data: 83 rep.append(str(allele_count) + ' ') 84 rep.append('\n') 85 rep.append('\n') 86 return "".join(rep)
87