Back to index

python-biopython  1.60
__init__.py
Go to the documentation of this file.
00001 # Copyright 2007 by Tiago Antao <tiagoantao@gmail.com>.  All rights reserved.
00002 # This code is part of the Biopython distribution and governed by its
00003 # license.  Please see the LICENSE file that should have been included
00004 # as part of this package.
00005 
00006 
00007 """
00008 This module provides code to work with FDist.
00009 
00010 See http://www.rubic.rdg.ac.uk/~mab/software.html .
00011 
00012 Classes:
00013 Record           Holds FDist data.
00014 
00015 Functions:
00016 read             Parses a FDist record (file) into a Record object.
00017 
00018 
00019 """
00020 
00021 
00022 
00023 
00024 def read(handle):
00025     """Parses FDist data into a Record object.
00026 
00027        handle is a file-like object that contains a FDist record.
00028     """
00029     record = Record()
00030     record.data_org = int(str(handle.next()).rstrip())
00031     record.num_pops = int(str(handle.next()).rstrip())
00032     record.num_loci = int(str(handle.next()).rstrip())
00033     for i in range(record.num_loci):
00034         handle.next()
00035         num_alleles = int(str(handle.next()).rstrip())
00036         pops_data = []
00037         if record.data_org==0:
00038             for j in range(record.num_pops):
00039                 line_comp = str(handle.next()).rstrip().split(' ')
00040                 pop_dist = map(lambda x: int(x), line_comp)
00041                 pops_data.append(pop_dist)
00042         else:
00043             raise NotImplementedError('1/alleles by rows not implemented')
00044         record.loci_data.append((num_alleles, pops_data))
00045     return record
00046 
00047 
00048 class Record(object):
00049     """Holds information from a FDist record.
00050 
00051     Members:
00052     data_org    Data organization (0 pops by rows, 1 alleles by rows).
00053                 The Record will behave as if data was 0 (converting if needed)
00054     
00055     num_pops       Number of populations
00056     
00057     num_loci       Number of loci
00058     
00059     loci_data      Loci data
00060     
00061     loci_data is a list, where each element represents a locus. Each element
00062     is a tuple, the first element is the number of alleles, the second
00063     element a list. Each element of the list is the count of each allele
00064     per population.
00065     
00066     """
00067     def __init__(self):
00068         self.data_org    = 0
00069         self.num_pops    = 0
00070         self.num_loci    = 0
00071         self.loci_data   = []
00072         
00073     def __str__(self):
00074         rep  = ['0\n'] #We only export in 0 format, even if originally was 1
00075         rep.append(str(self.num_pops) + '\n')
00076         rep.append(str(self.num_loci) + '\n')
00077         rep.append('\n')
00078         for locus_data in self.loci_data:
00079             num_alleles, pops_data = locus_data
00080             rep.append(str(num_alleles) + '\n')
00081             for pop_data in pops_data:
00082                 for allele_count in pop_data:
00083                     rep.append(str(allele_count) + ' ')
00084                 rep.append('\n')
00085             rep.append('\n')
00086         return "".join(rep)
00087