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