int main(int argc, char* argv[]) { char *maf_fname = NULL, *out_root_fname = "maf_parse", *masked_fn = NULL; String *refseq = NULL, *currRefseq; int opt_idx, startcol = 1, endcol = -1, include = 1, splitInterval = -1; char c, outfilename[1000], splitFormat[100]="%s%.1i.maf", *group_tag = NULL; List *order_list = NULL, *seqlist_str = NULL, *cats_to_do_str=NULL, *cats_to_do=NULL; MafBlock *block; FILE *mfile, *outfile=NULL, *masked_file=NULL; int useRefseq=TRUE, currLen=-1, blockIdx=0, currSize, sortWarned=0; int lastIdx = 0, currStart=0, by_category = FALSE, i, pretty_print = FALSE; int lastStart = -1, gffSearchIdx=0; GFF_Set *gff = NULL, *gffSub; GFF_Feature *feat; CategoryMap *cm = NULL; int base_mask_cutoff = -1, stripILines=FALSE, stripELines=FALSE;//, numspec=0; List *outfileList=NULL; Hashtable *outfileHash=NULL;//, *specNameHash=NULL; msa_format_type output_format = MAF; MSA *msa = NULL;//, **catMsa; char *mask_features_spec_arg=NULL; List *mask_features_spec=NULL; struct option long_opts[] = { {"start", 1, 0, 's'}, {"end", 1, 0, 'e'}, {"seqs", 1, 0, 'l'}, {"exclude", 0, 0, 'x'}, {"order", 1, 0, 'O'}, {"split", 1, 0, 'S'}, {"out-root", 1, 0, 'r'}, {"out-root-digits", 1, 0, 'd'}, {"no-refseq", 0, 0, 'n'}, {"features", 1, 0, 'g'}, {"by-category", 0, 0, 'L'}, {"do-cats", 1, 0, 'C'}, {"catmap", 1, 0, 'c'}, {"by-group", 1, 0, 'P'}, {"mask-bases", 1, 0, 'b'}, {"masked-file", 1, 0, 'm'}, {"strip-i-lines", 0, 0, 'I'}, {"strip-e-lines", 0, 0, 'E'}, {"mask-features", 1, 0, 'M'}, {"help", 0, 0, 'h'}, {0, 0, 0, 0} }; while ((c = getopt_long(argc, argv, "s:e:l:O:r:S:d:g:c:P:b:o:m:M:pLnxEIh", long_opts, &opt_idx)) != -1) { switch(c) { case 's': startcol = get_arg_int(optarg); break; case 'e': endcol = get_arg_int(optarg); break; case 'l': seqlist_str = get_arg_list(optarg); break; case 'O': order_list = get_arg_list(optarg); break; case 'x': include = FALSE; break; case 'S': splitInterval = atoi(optarg); break; case 'r': out_root_fname = optarg; break; case 'd': sprintf(splitFormat, "%%s%%.%si.%%s", optarg); break; case 'n': useRefseq = FALSE; break; case 'g': gff = gff_read_set(phast_fopen(optarg, "r")); gff_sort(gff); stripILines=TRUE; stripELines=TRUE; break; case 'c': cm = cm_new_string_or_file(optarg); break; case 'C': cats_to_do_str = get_arg_list(optarg); break; case 'L': by_category = TRUE; break; case 'P': group_tag = optarg; break; case 'b': base_mask_cutoff = atoi(optarg); break; case 'm': masked_fn = optarg; break; case 'M': mask_features_spec_arg = optarg; break; case 'E': stripELines=TRUE; break; case 'I': stripILines=TRUE; break; case 'o': output_format = msa_str_to_format(optarg); if (output_format == UNKNOWN_FORMAT) die("ERROR: bad output format. Try \"maf_parse -h\" for help.\n"); if (output_format != MAF) die("Sorry, only MAF format output has been implemented right now.\n"); break; case 'p': pretty_print = TRUE; break; case 'h': print_usage(); exit(0); case '?': die("Bad argument. Try 'maf_parse -h' for help.\n"); } } if (optind >= argc) die("Missing alignment filename. Try 'maf_parse -h' for help.\n"); else if (optind == argc - 1) maf_fname = argv[optind]; else die("ERROR: Too many arguments. Try 'maf_parse -h' for help.\n"); set_seed(-1); if (startcol < 1 || (endcol != -1 && endcol < startcol)) die("ERROR: must have 1 <= start <= end <= [msa_length]\n"); if ((group_tag != NULL || by_category) && gff == NULL) die("ERROR: --by-category and --by-group require --features. Try \"maf_parse -h\"" " for help.\n"); if (group_tag != NULL && by_category) die("ERROR: --by-category and --by-group cannot be used together. Try \"maf_parse -h\"" " for help.\n"); if (splitInterval != -1 && gff != NULL) die("ERROR: can't use --split and --features together. Try \"maf_parse -h\"" "for help\n"); if (group_tag != NULL || by_category) { outfileList = lst_new_ptr(10); outfileHash = hsh_new(100); } if (gff != NULL && cm == NULL) cm = cm_new_from_features(gff); if (cats_to_do_str != NULL) { cats_to_do = cm_get_category_str_list(cm, cats_to_do_str, FALSE); if (gff != NULL) gff_filter_by_type(gff, cats_to_do, 0, NULL); } if (masked_fn != NULL) { if (base_mask_cutoff == -1) die("ERROR: need to use --mask-bases with --masked-file"); masked_file = phast_fopen(masked_fn, "w"); } if (mask_features_spec_arg != NULL) { if (gff==NULL) die("ERROR: need --features with --mask-features"); mask_features_spec = lst_new_ptr(10); str_split(str_new_charstr(mask_features_spec_arg), ",", mask_features_spec); for (i=0; i < lst_size(mask_features_spec); i++) { fprintf(stderr, "masking species %s within features\n", ((String*)lst_get_ptr(mask_features_spec, i))->chars); } } /* Check to see if --do-cats names a feature which is length 1. If so, set output_format to SS ? or FASTA ? */ mfile = phast_fopen(maf_fname, "r"); block = mafBlock_read_next(mfile, NULL, NULL); if (splitInterval == -1 && gff==NULL) { //TODO: do we want to copy header from original MAF in this case? mafBlock_open_outfile(NULL, argc, argv); } while (block != NULL) { if (order_list != NULL) mafBlock_reorder(block, order_list); if (seqlist_str != NULL) mafBlock_subSpec(block, seqlist_str, include); if (mafBlock_numSpec(block)==0 || mafBlock_all_gaps(block)) goto get_next_block; if (stripILines) mafBlock_strip_iLines(block); if (stripELines) mafBlock_strip_eLines(block); if (base_mask_cutoff != -1) mafBlock_mask_bases(block, base_mask_cutoff, masked_file); //TODO: still need to implement (either here or elsewhere) // if (indel_mask_cutoff != -1) // mafBlock_mask_indels(block, indel_mask_cutoff, mfile); if (useRefseq) { //get refseq and check that it is consistent in MAF file currRefseq = mafBlock_get_refSpec(block); if (refseq == NULL) refseq = str_new_charstr(currRefseq->chars); else if (str_compare(refseq, currRefseq)!=0) die("Error: refseq not consistent in MAF (got %s, %s)\n", refseq->chars, currRefseq->chars); } if (startcol != 1 || endcol != -1) if (0 == mafBlock_trim(block, startcol, endcol, refseq, useRefseq ? 0 : lastIdx)) goto get_next_block; currSize = mafBlock_get_size(block, refseq); if (useRefseq) { currStart = mafBlock_get_start(block, refseq); if (currStart < lastIdx && sortWarned == 0) { fprintf(stderr, "Warning: input MAF not sorted with respect to refseq. Output files may not represent contiguous alignments. (%i, %i)\n", lastIdx, currStart); sortWarned = 1; } } else currStart = lastIdx; if (currStart < lastStart) gffSearchIdx = 0; lastStart = currStart; lastIdx = currStart + currSize; //split by length if (splitInterval != -1) { if (currLen == -1 || currLen+currSize > splitInterval) { sprintf(outfilename, splitFormat, out_root_fname, ++blockIdx, msa_suffix_for_format(output_format)); if (output_format == MAF) { if (outfile != NULL) mafBlock_close_outfile(outfile); outfile = mafBlock_open_outfile(outfilename, argc, argv); } else if (output_format != MAF && msa != NULL) { // msa_print_to_filename(msa, outfilename, output_format, pretty_print); msa_free(msa); msa = NULL; } currLen = 0; } currLen += currSize; } else outfile = stdout; if (gff != NULL && mask_features_spec != NULL) { gffSub = gff_subset_range_overlap_sorted(gff, currStart+1, lastIdx, &gffSearchIdx); if (gffSub != NULL) { mafBlock_mask_region(block, gffSub, mask_features_spec); gff_free_set(gffSub); } mafBlock_print(outfile, block, pretty_print); } else if (gff != NULL) { gffSub = gff_subset_range_overlap_sorted(gff, currStart+1, lastIdx, &gffSearchIdx); if (gffSub != NULL) { if (by_category) gff_group_by_feature(gffSub); else if (group_tag != NULL) gff_group(gffSub, group_tag); gff_sort(gffSub); gff_flatten_within_groups(gffSub); for (i=0; i<lst_size(gffSub->features); i++) { feat = (GFF_Feature*)lst_get_ptr(gffSub->features, i); MafBlock *subBlock = mafBlock_copy(block); mafBlock_trim(subBlock, feat->start, feat->end, refseq, 0); if (by_category) outfile = get_outfile(outfileList, outfileHash, feat->feature, out_root_fname, argc, argv); else if (group_tag != NULL) outfile = get_outfile(outfileList, outfileHash, gff_group_name(gffSub, feat), out_root_fname, argc, argv); else outfile = stdout; if (output_format == MAF) mafBlock_print(outfile, subBlock, pretty_print); // else msa_add_mafBlock(msa); mafBlock_free(subBlock); } gff_free_set(gffSub); } } else { if (output_format == MAF) mafBlock_print(outfile, block, pretty_print); // else msa = msa_add_mafBlock(mafBlock, msa, ); } get_next_block: mafBlock_free(block); block = mafBlock_read_next(mfile, NULL, NULL); } if (masked_file != NULL) fclose(masked_file); if (output_format == MAF) { if (by_category || group_tag != NULL) close_outfiles(outfileList, outfileHash); else if (outfile!=NULL) mafBlock_close_outfile(outfile); } else { msa_print(stdout, msa, output_format, pretty_print); msa_free(msa); } if (gff != NULL) gff_free_set(gff); phast_fclose(mfile); return 0; }
int main(int argc, char *argv[]) { char c; int opt_idx; GFF_Set *gff; List *include = NULL; char *groupby = "transcript_id", *exongroup_tag = NULL; int unique = FALSE, sort = FALSE, simplebed = FALSE, fix_start_stop = FALSE, add_utrs = FALSE, add_introns = FALSE, add_signals = FALSE; enum {GFF, BED, GENEPRED, WIG} output_format = GFF; FILE *discards_f = NULL, *groups_f = NULL; struct option long_opts[] = { {"output", 1, 0, 'o'}, {"include-only", 1, 0, 'i'}, {"include-groups", 1, 0, 'l'}, {"groupby", 1, 0, 'g'}, {"exongroup", 1, 0, 'e'}, {"add-utrs", 0, 0, 'U'}, {"add-introns", 0, 0, 'I'}, {"add-signals", 0, 0, 'S'}, {"fix-start-stop", 0, 0, 'f'}, {"unique", 0, 0, 'u'}, {"sort", 0, 0, 's'}, {"simplebed", 0, 0, 'b'}, {"discards", 1, 0, 'd'}, {"help", 0, 0, 'h'}, {0, 0, 0, 0} }; while ((c = (char)getopt_long(argc, argv, "o:i:l:g:e:d:UISfusbh", long_opts, &opt_idx)) != -1) { switch (c) { case 'o': if (!strcmp("bed", optarg)) output_format = BED; else if (!strcmp("genepred", optarg)) output_format = GENEPRED; else if (!strcmp("wig", optarg)) output_format = WIG; else if (strcmp("gff", optarg)) die("ERROR: bad output format.\n"); break; case 'i': include = get_arg_list(optarg); break; case 'l': groups_f = phast_fopen(optarg, "r"); break; case 'g': groupby = optarg; break; case 'e': exongroup_tag = optarg; break; case 'U': add_utrs = TRUE; break; case 'I': add_introns = TRUE; break; case 'S': add_signals = TRUE; break; case 'f': fix_start_stop = TRUE; break; case 'u': unique = TRUE; break; case 'b': simplebed = TRUE; output_format = BED; break; case 'd': discards_f = phast_fopen(optarg, "w+"); break; case 's': sort = TRUE; break; case 'h': usage(argv[0]); case '?': die("Bad argument. Try '%s -h'.\n", argv[0]); } } if (optind != argc - 1) die("Input filename required. Try '%s -h'.\n", argv[0]); set_seed(-1); gff = gff_read_set(phast_fopen(argv[optind], "r")); if (lst_size(gff->features) == 0) exit(0); /* helps avoid unexpected behavior below */ /* filter by type */ if (include != NULL) gff_filter_by_type(gff, include, FALSE, discards_f); /* group */ gff_group(gff, groupby); /* utrs, introns, & signals */ if (add_utrs) gff_create_utrs(gff); if (add_introns) gff_create_introns(gff); if (add_signals) gff_create_signals(gff); /* subgroup */ if (exongroup_tag != NULL) gff_exon_group(gff, exongroup_tag); /* filter by group */ if (groups_f != NULL) { String *s = str_new(STR_LONG_LEN); List *groups = lst_new_ptr(10000); str_slurp(s, groups_f); str_split(s, NULL, groups); gff_filter_by_group(gff, groups); lst_free_strings(groups); lst_free(groups); str_free(s); } /* sort */ if (sort) gff_sort(gff); /* make unique */ if (unique) gff_remove_overlaps(gff, discards_f); if (fix_start_stop) gff_fix_start_stop(gff); if (output_format == BED) gff_print_bed(stdout, gff, !simplebed); else if (output_format == GENEPRED) gff_print_genepred(stdout, gff); else if (output_format == WIG) wig_print(stdout, gff); else gff_print_set(stdout, gff); gff_free_set(gff); return 0; }
int main(int argc, char *argv[]) { char c; char *msa_fname = NULL; int opt_idx, i, old_nnodes; MSA *msa; List *pruned_names = lst_new_ptr(5), *tmpl; BDPhyloHmm *bdphmm; GFF_Set *predictions; int found = FALSE; List *ignore_types = lst_new_ptr(1); struct option long_opts[] = { {"refseq", 1, 0, 'M'}, {"msa-format", 1, 0, 'i'}, {"refidx", 1, 0, 'r'}, {"rho", 1, 0, 'R'}, {"phi", 1, 0, 'p'}, {"transitions", 1, 0, 't'}, {"expected-length", 1, 0, 'E'}, {"target-coverage", 1, 0, 'C'}, {"seqname", 1, 0, 'N'}, {"idpref", 1, 0, 'P'}, {"indel-model", 1, 0, 'I'}, {"indel-history", 1, 0, 'H'}, {"help", 0, 0, 'h'}, {0, 0, 0, 0} }; /* arguments and defaults for options */ FILE *refseq_f = NULL, *msa_f = NULL; msa_format_type msa_format = UNKNOWN_FORMAT; TreeModel *source_mod; double rho = DEFAULT_RHO, mu = DEFAULT_MU, nu = DEFAULT_NU, phi = DEFAULT_PHI, gamma = -1, omega = -1, alpha_c = -1, beta_c = -1, tau_c = -1, alpha_n = -1, beta_n = -1, tau_n = -1; int set_transitions = FALSE, refidx = 1, estim_phi = TRUE, estim_gamma = TRUE, estim_omega = TRUE; char *seqname = NULL, *idpref = NULL; IndelHistory *ih = NULL; while ((c = getopt_long(argc, argv, "R:t:p:E:C:r:M:i:N:P:I:H:h", long_opts, &opt_idx)) != -1) { switch (c) { case 'R': rho = get_arg_dbl_bounds(optarg, 0, 1); break; case 't': if (optarg[0] != '~') estim_gamma = estim_omega = FALSE; else optarg = &optarg[1]; set_transitions = TRUE; tmpl = get_arg_list_dbl(optarg); if (lst_size(tmpl) != 2) die("ERROR: bad argument to --transitions.\n"); mu = lst_get_dbl(tmpl, 0); nu = lst_get_dbl(tmpl, 1); if (mu <= 0 || mu >= 1 || nu <= 0 || nu >= 1) die("ERROR: bad argument to --transitions.\n"); lst_free(tmpl); break; case 'p': if (optarg[0] != '~') estim_phi = FALSE; else optarg = &optarg[1]; phi = get_arg_dbl_bounds(optarg, 0, 1); break; case 'E': if (optarg[0] != '~') estim_omega = FALSE; else optarg = &optarg[1]; omega = get_arg_dbl_bounds(optarg, 1, INFTY); mu = 1/omega; break; case 'C': if (optarg[0] != '~') estim_gamma = FALSE; else optarg = &optarg[1]; gamma = get_arg_dbl_bounds(optarg, 0, 1); break; case 'r': refidx = get_arg_int_bounds(optarg, 0, INFTY); break; case 'M': refseq_f = phast_fopen(optarg, "r"); break; case 'i': msa_format = msa_str_to_format(optarg); if (msa_format == UNKNOWN_FORMAT) die("ERROR: unrecognized alignment format.\n"); break; case 'N': seqname = optarg; break; case 'P': idpref = optarg; break; case 'I': tmpl = get_arg_list_dbl(optarg); if (lst_size(tmpl) != 3 && lst_size(tmpl) != 6) die("ERROR: bad argument to --indel-model.\n"); alpha_n = lst_get_dbl(tmpl, 0); beta_n = lst_get_dbl(tmpl, 1); tau_n = lst_get_dbl(tmpl, 2); if (lst_size(tmpl) == 6) { alpha_c = lst_get_dbl(tmpl, 3); beta_c = lst_get_dbl(tmpl, 4); tau_c = lst_get_dbl(tmpl, 5); } else { alpha_c = alpha_n; beta_c = beta_n; tau_c = tau_n; } if (alpha_c <= 0 || alpha_c >= 1 || beta_c <= 0 || beta_c >= 1 || tau_c <= 0 || tau_c >= 1 || alpha_n <= 0 || alpha_n >= 1 || beta_n <= 0 || beta_n >= 1 || tau_n <= 0 || tau_n >= 1) die("ERROR: bad argument to --indel-model.\n"); break; case 'H': fprintf(stderr, "Reading indel history from %s...\n", optarg); ih = ih_new_from_file(phast_fopen(optarg, "r")); break; case 'h': printf("%s", HELP); exit(0); case '?': die("Bad argument. Try 'dless -h'.\n"); } } if (optind != argc - 1) die("Missing alignment file or model file. Try 'dless -h'.\n"); if (set_transitions && (gamma != -1 || omega != -1)) die("ERROR: --transitions and --target-coverage/--expected-length cannot be used together.\n"); if ((gamma != -1 && omega == -1) || (gamma == -1 && omega != -1)) die("ERROR: --target-coverage and --expecteed-length must be used together.\n"); set_seed(-1); if (gamma != -1) nu = gamma/(1-gamma) * mu; fprintf(stderr, "Reading tree model from %s...\n", argv[optind]); source_mod = tm_new_from_file(phast_fopen(argv[optind], "r"), 1); if (source_mod->nratecats > 1) die("ERROR: rate variation not currently supported.\n"); if (source_mod->order > 0) die("ERROR: only single nucleotide models are currently supported.\n"); if (!tm_is_reversible(source_mod)) phast_warning("WARNING: p-value computation assumes reversibility and your model is non-reversible.\n"); /* read alignment */ msa_f = phast_fopen(argv[optind], "r"); fprintf(stderr, "Reading alignment from %s...\n", argv[optind]); if (msa_format == UNKNOWN_FORMAT) msa_format = msa_format_for_content(msa_f, 1); if (msa_format == MAF) { msa = maf_read(msa_f, refseq_f, 1, NULL, NULL, NULL, -1, TRUE, NULL, NO_STRIP, FALSE); } else msa = msa_new_from_file_define_format(msa_f, msa_format, NULL); if (msa_alph_has_lowercase(msa)) msa_toupper(msa); msa_remove_N_from_alph(msa); if (msa->ss == NULL) { fprintf(stderr, "Extracting sufficient statistics...\n"); ss_from_msas(msa, 1, TRUE, NULL, NULL, NULL, -1, 0); } else if (msa->ss->tuple_idx == NULL) die("ERROR: ordered representation of alignment required unless --suff-stats.\n"); /* prune tree, if necessary */ old_nnodes = source_mod->tree->nnodes; tm_prune(source_mod, msa, pruned_names); if (lst_size(pruned_names) == (old_nnodes + 1) / 2) die("ERROR: no match for leaves of tree in alignment (leaf names must match alignment names).\n"); if (lst_size(pruned_names) > 0) { fprintf(stderr, "WARNING: pruned away leaves of tree with no match in alignment ("); for (i = 0; i < lst_size(pruned_names); i++) fprintf(stderr, "%s%s", ((String*)lst_get_ptr(pruned_names, i))->chars, i < lst_size(pruned_names) - 1 ? ", " : ").\n"); } /* this has to be done after pruning tree */ tr_name_ancestors(source_mod->tree); /* also make sure match for reference sequence in tree */ if (refidx > 0) { for (i = 0, found = FALSE; !found && i < source_mod->tree->nnodes; i++) { TreeNode *n = lst_get_ptr(source_mod->tree->nodes, i); if (!strcmp(n->name, msa->names[refidx-1])) found = TRUE; } if (!found) die("ERROR: no match for reference sequence in tree.\n"); } /* checks for indel model */ if (alpha_c > 0) { if (ih == NULL) { fprintf(stderr, "Reconstructing indel history by parsimony...\n"); ih = ih_reconstruct(msa, source_mod->tree); } else { if (ih->ncols != msa->length) die("ERROR: indel history doesn't seem to match alignment.\n"); if (ih->tree->nnodes != source_mod->tree->nnodes) die("ERROR: indel history doesn't seem to match tree model.\n"); } } bdphmm = bd_new(source_mod, rho, mu, nu, phi, alpha_c, beta_c, tau_c, alpha_n, beta_n, tau_n, estim_gamma, estim_omega, estim_phi); /* compute emissions */ phmm_compute_emissions(bdphmm->phmm, msa, FALSE); /* add emissions for indel model, if necessary */ if (alpha_c > 0) { fprintf(stderr, "Adjusting emissions for indels...\n"); bd_add_indel_emissions(bdphmm, ih); } /* postprocess for missing data (requires special handling) */ fprintf(stderr, "Adjusting emissions for missing data...\n"); bd_handle_missing_data(bdphmm, msa); if (estim_gamma || estim_omega || estim_phi) { fprintf(stderr, "Estimating free parameters...\n"); bd_estimate_transitions(bdphmm, msa); } /* set seqname and idpref, if necessary */ if (seqname == NULL || idpref == NULL) { /* derive default from file name root */ String *tmp = str_new_charstr(msa_fname); if (!str_equals_charstr(tmp, "-")) { str_remove_path(tmp); str_root(tmp, '.'); if (idpref == NULL) idpref = copy_charstr(tmp->chars); str_root(tmp, '.'); /* apply one more time for double suffix */ if (seqname == NULL) seqname = tmp->chars; } else if (seqname == NULL) seqname = "refseq"; } /* obtain predictions */ fprintf(stderr, "Running Viterbi algorithm...\n"); predictions = phmm_predict_viterbi(bdphmm->phmm, seqname, NULL, idpref, NULL); lst_push_ptr(ignore_types, str_new_charstr("nonconserved")); gff_filter_by_type(predictions, ignore_types, TRUE, NULL); /* score predictions */ fprintf(stderr, "Scoring predictions...\n"); bd_score_predictions(bdphmm, predictions); /* can free emissions now */ for (i = 0; i < bdphmm->phmm->hmm->nstates; i++) sfree(bdphmm->phmm->emissions[i]); sfree(bdphmm->phmm->emissions); bdphmm->phmm->emissions = NULL; /* convert GFF to coord frame of reference sequence and adjust coords by idx_offset, if necessary */ if (refidx != 0 || msa->idx_offset != 0) msa_map_gff_coords(msa, predictions, 0, refidx, msa->idx_offset); if (refidx != 0) gff_flatten(predictions); /* necessary because coord conversion might create overlapping features (can happen in deletions in reference sequence) */ /* now output predictions */ fprintf(stderr, "Writing GFF to stdout...\n"); gff_print_set(stdout, predictions); fprintf(stderr, "Done.\n"); return 0; }