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