/****************************************************************************** # # ##### ##### #### # # ##### #### #### ## # # # # # # ## ## # # # # # # # # # # # # # ## # # # #### # # # # ##### # # # # # # # ### # # ## # # # # # # # # # # ### # # # # # # ####### #### # # ##### #### ### #### ****************************************************************************** /* This file is part of MAPMAKER 3.0b, Copyright 1987-1992, Whitehead Institute for Biomedical Research. All rights reserved. See READ.ME for license. */ #define INC_LIB #define INC_SHELL #define INC_MISC #include "mapm.h" /* internal procedures and variables */ bool try_marker(); /* ERROR KLUDGE Stuff */ #define ESTEPS 200 #define ESTART (-9.9) #define ESTEP 0.1 int errs[ESTEPS], noterrs[ESTEPS], lod[ESTEPS]; int cum_not[ESTEPS], cum_err[ESTEPS]; real ratio[ESTEPS]; void npt_cmds_init() { int i; for (i=0; i999) usage_error(1); if (threshold<0.0 || threshold>100.0) usage_error(2); if (threshold==0.0) threshold=VERY_UNLIKELY; else threshold=-threshold; nomore_args(num_args); run { list=allocate_map_list(maps_to_save,num_loci,SORTED,&map); array(locus,num_loci,int); /* NO CRUNCH?, #loci shouldn't change */ get_list_of_all_loci(seq,locus,&loci_per_map,num_loci); num_orders=0; /* global - deletes all pre-existing orders */ for (i=0; ilocus,map->num_loci,three_pt_window)) { excluded++; continue; } init_rec_fracs(map); converge_to_map(map); insert_map_into_list(list,&map); tried++; } if (tried==0) { sf(ps,COMPARE_NONE,three_pt_threshold); pr(); abort_command(); } map=get_best_map(list); best=map->log_like; i=1; if (threshold!=VERY_UNLIKELY) {/* can you say "abstraction violation" */ while (inum_maps && list->map_list[i]->log_like>(best+threshold)) i++; num_to_print=i; sf(ps,BEST_THRESH,maybe_s(num_to_print),-threshold); pr(); } else { num_to_print=min(maps_to_save,tried); if (num_to_print>1) sf(ps,BEST_ORDERS,num_to_print); else strcpy(ps,BEST_ORDER); pr(); } if (excluded>0) { sf(ps,N_EXCLUDED,excluded); pr(); } print(":\n"); print_list(list,num_to_print); print("order1 is set"); for (i=0; inum_loci; i++) { /* sf(ps,"%s ",rag(loc2str(map->locus[i]))); pr(); */ if (i==0) order_first[0]=map->locus[i]; else order_next[prev]= map->locus[i]; /* lint warning is OK */ prev=map->locus[i]; } nl(); unorder_first[0]=NO_LOCUS; order_next[prev]=NO_LOCUS; num_orders=1; } on_exit { free_map_list(list); relay_messages; } } command ripple() { MAP *map0=NULL, *map; SAVED_LIST *list; int n_loci, window, excluded, tried, num_ok, i, j, k, n; real thresh, best; bool same; mapm_ready(ANY_DATA,3,ONE_ORDER,&n_loci); get_arg(itoken,5,&window); if (!irange(&window,3,99)) usage_error(1); get_arg(rtoken,2.0,&thresh);if (!rrange(&thresh,0.0,100.0)) usage_error(1); if (window>n_loci) error("window size is greater than number of loci in seq"); if (has_fixed_dists(seq)) error("can not use sequence with fixed distances"); nomore_args(num_args); for (i=window, n=1; i>1; i--) n*=i; /* window factorial */ run { map0=allocate_map(n_loci); list=allocate_map_list(n,n_loci,SORTED,&map); get_one_order(seq,map0); init_rec_fracs(map0); print(MAP_DIVIDER); converge_to_map(map0); print_long_map(map0,"Map To Test:"); if (use_three_pt) setup_3pt_data(map0->locus,map0->num_loci,-three_pt_threshold); print(MAP_DIVIDER); sf(ps,"Window-size: %d Log-likelihood Threshold: %.2lf\n", window,thresh); pr(); print("Comparing maps with ALL flanking markers...\n\n"); for (i=0; i<=n_loci-window; i++) { clean_list(list); map=get_map_to_bash(list); make_compare_seq(map0->locus,map0->num_loci,i,window); print("compare "); if (i>0) print("...{"); else print(" {"); for (j=i; jlocus[j]))); if (j!=i+window-1) print(" "); } if (i+window-1num_loci-1) print("}... "); else print("} "); /* to_column(17+(print_names ? 10:5)*window); */ flush(); excluded=tried=0; for_all_orders(seq,map) { if (use_three_pt && /* only threept the compared part */ !three_pt_verify(map->locus+i,window,three_pt_window)) { excluded++; continue; } init_rec_fracs(map); converge_to_map(map); insert_map_into_list(list,&map); tried++; } if (tried==0) { print("(all excluded)\n"); continue; } same=TRUE; map=get_best_map(list); for (j=0; jnum_loci; j++) if (map0->locus[j]!=map->locus[j]) { same=FALSE; break; } best=map->log_like; k=1; while (knum_maps && /* thresh>0 here */ list->map_list[k]->log_like>(best-thresh)) k++; if (!same || k>1) { sf(ps,"\nbest order%s:",maybe_s(k)); pr(); if (excluded>0) { sf(ps," (%d excluded)",excluded); pr(); } nl(); print_list(list,k); nl(); } else print("ok\n"); } print(MAP_DIVIDER); } on_exit { free_map_list(list); free_map(map0); relay_messages; } } #define TRY_NONE "%s - all orders excluded by three-point analysis\n" #define TRY_TOO_MANY "too many loci to try" #define MAX_PAIRED 20 command try() { SAVED_LIST **list=NULL; MAP *map=NULL; int *marker_to_try=NULL, **new_marker=NULL, num_to_try_at_once, num_tries; int num_seq_loci, num_intervals, first_marker, i, j, n, m, next; int **paired_loci=NULL, next_paired, num_paired, num_total, max_paired; bool **exclude_interval=NULL, **zero_placement=NULL, *is_locus_paired=NULL; char *err, token[TOKLEN+1], *intervals, *p; void expand_seq_names(); /* KLUDGE from sequence.c, gotta do better */ char *markers, str[MAX_SEQ_LEN+99]; /* KLUDGE KLUDGE KLUDGE */ mapm_ready(ANY_DATA,2,MAYBE_PERM,&num_seq_loci); strcpy(str,args); markers=str; if (nullstr(args)) usage_error(0); /* NEED SOMETHING LIKE PARSE_LOCUS_ARGS AND CRUNCH */ run { array(marker_to_try,raw.num_markers,int); /* w/paired, num_total */ matrix(new_marker,raw.num_markers,MAX_PAIRED,int); num_tries=0; num_total=0; max_paired=1; for (i=0; i")) break; if (!is_a_locus(token,&m,&err) || !nullstr(err)) { sf(ps,"'%s' is not a locus",token); error(ps); } if (num_total==raw.num_markers) error(TRY_TOO_MANY); marker_to_try[num_total++]=m; if (next==MAX_PAIRED) error(TRY_TOO_MANY); new_marker[num_tries][next++]=m; } if (!streq(token,">")) error("missing '>'"); if (next>1) { /* then it's paired */ for (i=0; ilocus, then setup 3pt */ get_one_order(seq,map); init_rec_fracs(map); /* no ctm */ if (use_three_pt) { for (j=0; jnum_loci; j++) { if (num_total==raw.num_markers) error(TRY_TOO_MANY); marker_to_try[num_total++]=map->locus[j]; } setup_3pt_data(marker_to_try,num_total,-three_pt_threshold); } /* Here we loop over 8 markers to try at a time, calculating the 8 map_lists and printing the results. Each map_list has the results of sticking one locus into all selected intervals of one order. Note that i counts 0..7 and j counts from n...n+7, where n is the 1st-marker of this set of 8 to try. */ n=0; for_all_orders(seq,map) { for (first_marker=0; first_markerunlink= NONE_UNLINKED; for (i=0; inum_loci; i++) excluded[i]=FALSE; num_ok= original_map->num_loci+1; for (num_paired=0; marker[num_paired]!=NO_LOCUS; num_paired++) if (use_three_pt) num_ok=three_pt_exclusions(original_map->locus,original_map->num_loci, marker[num_paired],excluded); if (num_paired==0) send(CRASH); last=original_map->num_loci; best=VERY_UNLIKELY; for (i=0; i<=last; i++) if (!excluded[i]) { mapcpy(map,original_map,TRUE); for (j=0; jlog_like>best) { best=map->log_like; best_i=i; } /* find zero placements. indexing is: orig-loci: 0 1 | 0 1 | 0 1 intervals: 0 1 2 | 0 1 2 | 0 1 2 new-seq: x 0 1 | 0 x 1 | 0 1 x recfracs: 0 1 | 0 1 | 0 1 index: i=0 | i=1 | i=2 */ sex=map->sex_specific; if (i>0 && is_zero(map->rec_frac[i-1],sex)) zero[i]=TRUE; if (irec_frac[i+num_paired-1],sex)) zero[i]=TRUE; if (zero[i]) for (j=1; jrec_frac[i+j],sex)) zero[i]=FALSE; insert_map_into_list(list,&map); } else keep_user_amused("map",++*count,0); if (num_ok>0 && zero[best_i]) { for (i=best_i+1; !excluded[i] && i<=last && list->map_list[i]->log_like-best>ZERO_PLACE && zero[i]; i++) list->map_list[i]->log_like=ZERO_LIKE; for (i=best_i-1; !excluded[i] && i>=0 && list->map_list[i]->log_like-best>ZERO_PLACE && zero[i]; i--) list->map_list[i]->log_like=ZERO_LIKE; } /* INF dist */ mapcpy(map,original_map,TRUE); /* cleans map */ i=map->num_loci-1; /* interval to unlink */ for (j=0; jnum_loci+j,marker[j])) send(CRASH); keep_user_amused("map",++*count,0); map->rec_frac[i][MALE]= UNLINK_ME; map->rec_frac[i][FEMALE]=UNLINK_ME; init_rec_fracs(map); converge_to_map(map); insert_map_into_list(list,&map); return(num_ok==0 ? FALSE:TRUE); } #define ORDER_NO_GROUPS \ "\nNo linkage groups found at specified threshold.\n" #define ORDER_AT "Placing at log-likelihood threshold %.2lf...\n" #define START_CRITERIA \ "Starting Orders: Size %d, Log-Likelihood %.2lf, Searching up to %d subsets\n" #define INF_CRITERIA \ "Informativeness: min #Individuals %d%s, min Distance %s\n" #define LINK_CRITERIA \ "Linkage Groups at min LOD %.2lf, max Distance %s\n" command order_maker() { int *loci=NULL, num_loci, *linkage_group=NULL, num_unlinked, group_size; int starter[3], *subset=NULL, **seed_temp=NULL, subset_size, groups_done; int i, j, k, num, prev, num_to_place, num_unplaced, seed_size, seed_tries; bool seed_ok, found; MAP *order=NULL, *seed_map=NULL; PLACEME **unplaced; real lodbound, thetabound, seed_like; /* args: lod theta start-window start-like max-to-try */ mapm_ready(F2_DATA,3,LIST_SEQ,&num_loci); /* F2 because #indivs */ if (!rtoken(&args,default_lod,&lodbound) || !rrange(&lodbound,0.0,1000.0)) usage_error(1); if (!rtoken(&args,default_theta,&thetabound) || !input_dist(&thetabound)) usage_error(2); get_arg(itoken,5,&seed_size); if (seed_size<3) usage_error(0); get_arg(rtoken,3.0,&seed_like); if (seed_like<=0.0) usage_error(0); get_arg(itoken,50,&seed_tries); if (seed_tries<1) usage_error(0); nomore_args(0); seed_like= -seed_like; sf(ps,LINK_CRITERIA,lodbound,rag(rf2str(thetabound))); pr(); sf(ps,START_CRITERIA,seed_size,-seed_like,seed_tries); pr(); sf(ps,INF_CRITERIA,npt_min_indivs, (npt_codominant ? " (codominant only)":""), rag(rf2str(npt_min_theta))); pr(); if (npt_threshold==npt_first_threshold) /* globals */ sf(ps,"Placement Threshold %.2lf, Npt-Window %d\n",npt_threshold, npt_window); else sf(ps,"Placement Threshold-1 %.2lf, Threshold-2 %.2lf, Npt-Window %d\n", npt_first_threshold,npt_threshold,npt_window); pr(); run { alloc_list_of_all_loci(seq,&loci,&num_loci); array(linkage_group,num_loci,int); order=allocate_map(MAX_MAP_LOCI); seed_map=allocate_map(seed_size); array(subset,num_loci,int); matrix(seed_temp,seed_tries+1,num_loci,int); parray(unplaced,num_loci,PLACEME); for (i=0; iexcluded,MAX_MAP_LOCI+1,bool); unplaced[i]->best_map=allocate_map(MAX_MAP_LOCI); } num_unlinked=num_loci; num_orders=0; /* global - deletes all pre-existing orders */ for (i=0; i=2) { print(MAP_DIVIDER); inv_isort(linkage_group,group_size); groups_done++; sf(ps,"Linkage group %d, %d Markers:\n",groups_done, group_size); pr(); for (i=0; inum_loci; i++) if (order->locus[i]==linkage_group[j]) { found=TRUE; break; } if (!found) unplaced[num_unplaced++]->locus=linkage_group[j]; } if (num_unplaced>0) { /* extend_order is chatty, prints map and places at end */ nl(); sf(ps,ORDER_AT,npt_first_threshold); pr(); extend_order(order,unplaced,&num_unplaced, -npt_first_threshold,TRUE); if (npt_first_threshold!=npt_threshold && num_unplaced>0) { print(SUB_DIVIDER); sf(ps,ORDER_AT,npt_threshold); pr(); extend_order(order,unplaced,&num_unplaced, -npt_threshold,FALSE); } } nl(); sf(ps,"order%d= ",groups_done); pr(); for (i=0; inum_loci; i++) { sf(ps,"%s ",rag(loc2str(order->locus[i]))); pr(); if (i==0) order_first[groups_done-1]=order->locus[i]; else order_next[prev]=order->locus[i]; prev=order->locus[i]; /* lint warning is OK */ } nl(); sf(ps,"other%d= ",groups_done); pr(); for (i=0; ilocus))); pr(); if (i==0) unorder_first[groups_done-1]=unplaced[i]->locus; else order_next[prev]=unplaced[i]->locus; prev=unplaced[i]->locus; /* lint warning is OK */ } nl(); num_orders++; if (print_all_maps) for (i=0; ibest_map->num_loci>0) { print(SUB_DIVIDER); sf(ps,"Best placement of %s:", rag(loc2str(unplaced[i]->locus))); print_special_map(unplaced[i]->best_map,ps, order->num_loci,order->locus); } } /* if group size */ } while (num_unlinked>0); if (groups_done==0) print(ORDER_NO_GROUPS); else print(MAP_DIVIDER); } on_exit { unarray(loci,int); unarray(linkage_group,int); free_map(order); free_map(seed_map); unarray(subset,int); unmatrix(seed_temp,seed_tries,int); for (i=0; iexcluded,bool); free_map(unplaced[i]->best_map); } unparray(unplaced,num_loci,PLACEME); relay_messages; } } command greedy() { int *locus=NULL, num_loci, num_order, *all_loci, total; int i, prev, num, num_unplaced; MAP *order=NULL; PLACEME **unplaced; /* args: lod theta start-window start-like max-to-try */ mapm_ready(F2_DATA,2,LIST_SEQ,&num_order); /* F2 because place_locus */ if (nullstr(args)) usage_error(0); parse_locus_args(&locus,&num_loci); /* error if fails, >=1 */ crunch_locus_list(locus,&num_loci,CRUNCH_WARNINGS,ANY_CHROMS,IN_ARGS); if (npt_threshold==npt_first_threshold) /* globals */ sf(ps,"Placement Threshold %.2lf, Npt-Window %d\n", npt_threshold,npt_window); else sf(ps,"Placement Threshold-1 %.2lf, Threshold-2 %.2lf, Npt-Window %d\n", npt_first_threshold,npt_threshold,npt_window); pr(); if (num_order>=MAX_MAP_LOCI) error("starting order is too big"); run { total= num_order+num_loci; order=allocate_map(total); /* maybe MAX_MAP */ get_list_of_all_loci(seq,order->locus,&order->num_loci,total); array(all_loci,total,int); parray(unplaced,num_loci,PLACEME); for (i=0; iexcluded,total+1,bool); unplaced[i]->best_map=allocate_map(total); } num_orders=0; /* global - deletes all pre-existing orders */ for (i=0; inum_loci; i++) all_loci[total++]=order->locus[i]; for (i=0; ilocus=locus[i]; num_unplaced=num_loci; sf(ps,"%d Markers to order:\n",total); pr(); for (i=0; i0) { print(SUB_DIVIDER); sf(ps,ORDER_AT,npt_threshold); pr(); extend_order(order,unplaced,&num_unplaced,-npt_threshold,FALSE); } nl(); print("order1= "); for (i=0; inum_loci; i++) { sf(ps,"%s ",rag(loc2str(order->locus[i]))); pr(); if (i==0) order_first[0]=order->locus[i]; else order_next[prev]=order->locus[i]; prev=order->locus[i]; /* lint warning is OK */ } nl(); print("other1= "); for (i=0; ilocus))); pr(); if (i==0) unorder_first[0]=unplaced[i]->locus; else order_next[prev]=unplaced[i]->locus; prev=unplaced[i]->locus; } nl(); num_orders=1; if (print_all_maps) for (i=0; ibest_map->num_loci>0) { print(SUB_DIVIDER); sf(ps,"Best placement of %s:",rag(loc2str(unplaced[i]->locus))); print_special_map(unplaced[i]->best_map,ps, order->num_loci,order->locus); } print(MAP_DIVIDER); } on_exit { unarray(locus,int); unarray(all_loci,int); free_map(order); for (i=0; iexcluded,bool); free_map(unplaced[i]->best_map); } unparray(unplaced,num_loci,PLACEME); relay_messages; } }