Back to index

python-biopython  1.60
Namespaces | Classes | Functions
Bio.PopGen.GenePop Namespace Reference

Namespaces

namespace  Controller
namespace  EasyController
namespace  FileParser
namespace  LargeFileParser
namespace  Utils

Classes

class  Record

Functions

def get_indiv
def read

Function Documentation

Definition at line 25 of file __init__.py.

00025 
00026 def get_indiv(line):
00027     def int_no_zero(val):
00028         v = int(val)
00029         if v == 0:
00030             return None
00031         return v
00032     indiv_name, marker_line = line.split(',')
00033     markers = marker_line.replace('\t', ' ').split(' ')
00034     markers = [marker for marker in markers if marker!='']
00035     if len(markers[0]) in [2, 4]: #2 digits per allele
00036         marker_len = 2
00037     else:
00038         marker_len = 3
00039     try:
00040         allele_list = [(int_no_zero(marker[0:marker_len]),
00041                        int_no_zero(marker[marker_len:]))
00042                    for marker in markers]
00043     except ValueError: #Haploid
00044         allele_list = [(int_no_zero(marker[0:marker_len]),)
00045                    for marker in markers]
00046     return indiv_name, allele_list, marker_len

Here is the caller graph for this function:

def Bio.PopGen.GenePop.read (   handle)
Parses a handle containing a GenePop file.

   handle is a file-like object that contains a GenePop record.

Definition at line 47 of file __init__.py.

00047 
00048 def read(handle):
00049     """Parses a handle containing a GenePop file.
00050 
00051        handle is a file-like object that contains a GenePop record.
00052     """
00053     record = Record()
00054     record.comment_line = str(handle.next()).rstrip()
00055     #We can now have one loci per line or all loci in a single line
00056     #separated by either space or comma+space...
00057     #We will remove all commas on loci... that should not be a problem
00058     sample_loci_line = str(handle.next()).rstrip().replace(',', '')
00059     all_loci = sample_loci_line.split(' ')
00060     record.loci_list.extend(all_loci)
00061     for line in handle:
00062         line = line.rstrip()
00063         if line.upper()=='POP':
00064             break
00065         record.loci_list.append(line)
00066     else:
00067         raise ValueError('No population data found, file probably not GenePop related')
00068     record.populations.append([])
00069     for line in handle:
00070         line = line.rstrip()
00071         if line.upper()=='POP':
00072             record.populations.append([])
00073         else:
00074             indiv_name, allele_list, record.marker_len = get_indiv(line)
00075             record.populations[-1].append((indiv_name, allele_list))
00076     loci = record.loci_list
00077     for pop in record.populations:
00078         record.pop_list.append(pop[-1][0])
00079         for indiv in pop:
00080             for mk_i in range(len(loci)):
00081                 mk_orig = indiv[1][mk_i]
00082                 mk_real = []
00083                 for al in mk_orig:
00084                     if al == 0:
00085                         mk_real.append(None)
00086                     else:
00087                         mk_real.append(al)
00088                 indiv[1][mk_i] = tuple(mk_real)
00089     return record
00090 

Here is the call graph for this function: