Example #1
0
def _convert_genepop_to_fdist_big(gp_rec, report_pops=None):
    """Converts a big GenePop record to a FDist one.

       Parameters:
       gp_rec - Genepop Record (Big)

       Returns:
       FDist record.
    """
    fd_rec = Bio.PopGen.FDist.Record()

    fd_rec.data_org = 1
    fd_rec.num_loci = len(gp_rec.loci_list)
    num_loci = len(gp_rec.loci_list)
    loci = []
    for i in range(num_loci):
        loci.append(set())
    pops = []
    work_rec = FileParser.read(gp_rec.fname)
    lParser = work_rec.get_individual()

    def init_pop():
        my_pop = []
        for i in range(num_loci):
            my_pop.append({})
        return my_pop

    curr_pop = init_pop()
    num_pops = 1
    if report_pops:
        report_pops(num_pops)
    while lParser:
        if lParser is not True:
            for loci_pos in range(num_loci):
                for al in lParser[1][loci_pos]:
                    if al is not None:
                        loci[loci_pos].add(al)
                        curr_pop[loci_pos][al] = curr_pop[loci_pos].get(al,
                                                                        0) + 1
        else:
            pops.append(curr_pop)
            num_pops += 1
            if report_pops:
                report_pops(num_pops)
            curr_pop = init_pop()
        lParser = work_rec.get_individual()
    work_rec._handle.close()  # TODO - Needs a proper fix
    pops.append(curr_pop)
    fd_rec.num_pops = num_pops
    for loci_pos in range(num_loci):
        alleles = sorted(loci[loci_pos])
        loci_rec = [len(alleles), []]
        for pop in pops:
            pop_rec = []
            for allele in alleles:
                pop_rec.append(pop[loci_pos].get(allele, 0))
            loci_rec[1].append(pop_rec)
        fd_rec.loci_data.append(tuple(loci_rec))
    return fd_rec
Example #2
0
def _convert_genepop_to_fdist_big(gp_rec, report_pops = None):
    """Converts a big GenePop record to a FDist one.

       Parameters:
       gp_rec - Genepop Record (Big)

       Returns:
       FDist record.
    """
    fd_rec = Bio.PopGen.FDist.Record()

    fd_rec.data_org = 1
    fd_rec.num_loci = len(gp_rec.loci_list)
    num_loci = len(gp_rec.loci_list)
    loci = []
    for i in range(num_loci):
        loci.append(set())
    pops = []
    work_rec = FileParser.read(gp_rec.fname)
    lParser = work_rec.get_individual()

    def init_pop():
        my_pop = []
        for i in range(num_loci):
            my_pop.append({})
        return my_pop

    curr_pop = init_pop()
    num_pops = 1
    if report_pops:
        report_pops(num_pops)
    while lParser:
        if lParser is not True:
            for loci_pos in range(num_loci):
                for al in lParser[1][loci_pos]:
                    if al is not None:
                        loci[loci_pos].add(al)
                        curr_pop[loci_pos][al]= curr_pop[loci_pos].get(al, 0)+1
        else:
            pops.append(curr_pop)
            num_pops += 1
            if report_pops:
                report_pops(num_pops)
            curr_pop = init_pop()
        lParser = work_rec.get_individual()
    work_rec._handle.close()  # TODO - Needs a proper fix
    pops.append(curr_pop)
    fd_rec.num_pops = num_pops
    for loci_pos in range(num_loci):
        alleles = sorted(loci[loci_pos])
        loci_rec = [len(alleles), []]
        for pop in pops:
            pop_rec = []
            for allele in alleles:
                pop_rec.append(pop[loci_pos].get(allele, 0))
            loci_rec[1].append(pop_rec)
        fd_rec.loci_data.append(tuple(loci_rec))
    return fd_rec
Example #3
0
 def countPops(rec):
     f2 = FileParser.read(rec.fname)
     popCnt = 1
     while f2.skip_population():
         popCnt += 1
     return popCnt
Example #4
0
def _convert_genepop_to_fdist_big_old(gp_rec, report_loci=None):
    """Converts a big GenePop record to a FDist one.

       Parameters:
       gp_rec - Genepop Record (Big)

       Returns:
       FDist record.
    """
    fd_rec = Bio.PopGen.FDist.Record()

    def countPops(rec):
        f2 = FileParser.read(rec.fname)
        popCnt = 1
        while f2.skip_population():
            popCnt += 1
        return popCnt

    fd_rec.data_org = 0
    fd_rec.num_loci = len(gp_rec.loci_list)
    work_rec0 = FileParser.read(gp_rec.fname)
    fd_rec.num_pops = countPops(work_rec0)

    num_loci = len(gp_rec.loci_list)
    for lc_i in range(num_loci):
        if report_loci:
            report_loci(lc_i, num_loci)
        work_rec = FileParser.read(gp_rec.fname)
        work_rec2 = FileParser.read(gp_rec.fname)

        alleles = []
        pop_data = []
        lParser = work_rec.get_individual()
        while lParser:
            if lParser is not True:
                for al in lParser[1][lc_i]:
                    if al is not None and al not in alleles:
                        alleles.append(al)
            lParser = work_rec.get_individual()
        #here we go again (necessary...)
        alleles.sort()

        def process_pop(pop_data, alleles, allele_counts):
            allele_array = []  # We need the same order as in alleles
            for allele in alleles:
                allele_array.append(allele_counts.get(allele, 0))
            pop_data.append(allele_array)

        lParser = work_rec2.get_individual()
        allele_counts = {}
        for allele in alleles:
            allele_counts[allele] = 0
        allele_counts[None] = 0
        while lParser:
            if lParser is True:
                process_pop(pop_data, alleles, allele_counts)
                allele_counts = {}
                for allele in alleles:
                    allele_counts[allele] = 0
                allele_counts[None] = 0
            else:
                for al in lParser[1][lc_i]:
                    allele_counts[al] += 1
            lParser = work_rec2.get_individual()
        process_pop(pop_data, alleles, allele_counts)
        fd_rec.loci_data.append((len(alleles), pop_data))
    return fd_rec
Example #5
0
 def countPops(rec):
     f2 = FileParser.read(rec.fname)
     popCnt = 1
     while f2.skip_population():
         popCnt += 1
     return popCnt
Example #6
0
def _convert_genepop_to_fdist_big_old(gp_rec, report_loci = None):
    """Converts a big GenePop record to a FDist one.

       Parameters:
       gp_rec - Genepop Record (Big)

       Returns:
       FDist record.
    """
    fd_rec = Bio.PopGen.FDist.Record()

    def countPops(rec):
        f2 = FileParser.read(rec.fname)
        popCnt = 1
        while f2.skip_population():
            popCnt += 1
        return popCnt

    fd_rec.data_org = 0
    fd_rec.num_loci = len(gp_rec.loci_list)
    work_rec0 = FileParser.read(gp_rec.fname)
    fd_rec.num_pops = countPops(work_rec0)

    num_loci = len(gp_rec.loci_list)
    for lc_i in range(num_loci):
        if report_loci:
            report_loci(lc_i, num_loci)
        work_rec = FileParser.read(gp_rec.fname)
        work_rec2 = FileParser.read(gp_rec.fname)

        alleles = []
        pop_data = []
        lParser = work_rec.get_individual()
        while lParser:
            if lParser is not True:
                for al in lParser[1][lc_i]:
                    if al is not None and al not in alleles:
                        alleles.append(al)
            lParser = work_rec.get_individual()
        #here we go again (necessary...)
        alleles.sort()

        def process_pop(pop_data, alleles, allele_counts):
            allele_array = []  # We need the same order as in alleles
            for allele in alleles:
                allele_array.append(allele_counts.get(allele, 0))
            pop_data.append(allele_array)

        lParser = work_rec2.get_individual()
        allele_counts = {}
        for allele in alleles:
            allele_counts[allele] = 0
        allele_counts[None]=0
        while lParser:
            if lParser is True:
                process_pop(pop_data, alleles, allele_counts)
                allele_counts = {}
                for allele in alleles:
                    allele_counts[allele] = 0
                allele_counts[None]=0
            else:
                for al in lParser[1][lc_i]:
                    allele_counts[al] += 1
            lParser = work_rec2.get_individual()
        process_pop(pop_data, alleles, allele_counts)
        fd_rec.loci_data.append((len(alleles), pop_data))
    return fd_rec