def switch_alleles_loop(mappable, family, parent, starting_map, NR_matrix_threshold, reps, MST_grouping_threshold = .0000001, map_type = 'mst', MST_exe = MST_exe):    
    for xx in range(reps):
        ini_map, sq_sum_lod, sq_NR_matrix, R, NR, lgs_longer_than_1 = switchAlleles.calculate_switch_stats(mappable, starting_map, 'mst', MST_grouping_threshold)
        where_pairs = numpy.where(sq_NR_matrix > NR_matrix_threshold)
        LGs_to_switch = switchAlleles.find_LGs_to_switch(lgs_longer_than_1, where_pairs)
        loci_to_switch = switchAlleles.find_loci_to_switch(ini_map, LGs_to_switch)
        switched_alleles_file = '/olympus/WORK/WAPLES/Stacks_mapping/Chum_data/mst/switched_alleles_{}_iter{}.txt'.format(family, xx)
        switchAlleles.update_switched_alleles_file(switched_alleles_file, loci_to_switch)
        mappable = switchAlleles.switch_alleles(loci_to_switch, mappable)
        if len(loci_to_switch) == 0:
            print("found zero loci to switch, ending loop")
            break
        print("Starting MSTmap on family: {}, rep: {}".format(family, xx))
        #run MSTmap
        MSTmap_infile = '/olympus/WORK/WAPLES/Stacks_mapping/Chum_data/mst/chum_{}_iter{}_mstmap.txt'.format(family, xx)
        MSTmap_logfile = '/olympus/WORK/WAPLES/Stacks_mapping/Chum_data/mst/chum_{}_iter{}_mstmap.log'.format(family, xx)
        out_linkage_map_file = '/olympus/WORK/WAPLES/Stacks_mapping/Chum_data/mst/chum_{}_iter{}_mstmap.map'.format(family, xx)
        chum_fams.write_mstmap(parent, MSTmap_infile, mappable)
        run_MSTmap(MST_exe = MST_exe, INFILE = MSTmap_infile, OUTFILE = out_linkage_map_file, LOGFILE = MSTmap_logfile)
        print("Finished MSTmap on family: {}, rep: {}".format(family, xx))
        #renew name
        starting_map = out_linkage_map_file

################################


# Calculate some summary stats from the existing recent linkage map
ini_map, sq_sum_lod, sq_NR_matrix, R, NR, lgs_longer_than_1 = switchAlleles.calculate_switch_stats(mappable, in_linkage_map_file, 'mst')
# Determine which pairs of LGs are potential switches
# Perhaps come up with a way to estiamte NR_threshold here.
where_pairs = numpy.where(sq_NR_matrix > NR_matrix_threshold)
# Determines which LGs and loci to switch 
LGs_to_switch = switchAlleles.find_LGs_to_switch(lgs_longer_than_1, where_pairs)
loci_to_switch = switchAlleles.find_loci_to_switch(ini_map, LGs_to_switch)
# updates switched alleles file
switched_alleles_file = '/olympus/WORK/WAPLES/Stacks_mapping/Chum_data/mst/switched_alleles_08_iter{}.txt'.format(xx)
switchAlleles.update_switched_alleles_file(switched_alleles_file, loci_to_switch)
# updates mappapble
mappable = switchAlleles.switch_alleles(loci_to_switch, mappable)
#run MSTmap
MSTmap_infile = '/home/ipseg/Data/chum/data/chum_08_iter{}_mstmap.txt'.format(xx)
MSTmap_logfile = '/home/ipseg/Data/chum/data/chum_08_iter{}_mstmap.log'.format(xx)
out_linkage_map_file = '/home/ipseg/Data/chum/data/chum_08_iter{}_mstmap.map'.format(xx)
chum_fams.write_mstmap('CMUW10X_0008', MSTmap_infile, mappable)
MSTmap_call_string = "{} {} {}".format(MST_exe, MSTmap_infile, out_linkage_map_file)
with open(MSTmap_logfile, 'w') as log:
    subprocess.call(shlex.split(MSTmap_call_string),shell=False, universal_newlines=True, stdout=log)
#renew name
in_linkage_map_file = out_linkage_map_file