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
22
23
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(handle.next()).rstrip())
31 record.num_pops = int(str(handle.next()).rstrip())
32 record.num_loci = int(str(handle.next()).rstrip())
33 for i in range(record.num_loci):
34 handle.next()
35 num_alleles = int(str(handle.next()).rstrip())
36 pops_data = []
37 if record.data_org==0:
38 for j in range(record.num_pops):
39 line_comp = str(handle.next()).rstrip().split(' ')
40 pop_dist = map(lambda x: int(x), 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
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 """
68 self.data_org = 0
69 self.num_pops = 0
70 self.num_loci = 0
71 self.loci_data = []
72
74 rep = ['0\n']
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