SEXP rph_tree_rename(SEXP treeVec, SEXP oldNamesP, SEXP newNamesP) { int i, numtree = LENGTH(treeVec), treeIdx; TreeNode *tr, *n; SEXP result; Hashtable *hash = hsh_new(20); char *str; for (i=0; i<LENGTH(oldNamesP); i++) { str = smalloc((strlen(CHAR(STRING_ELT(newNamesP, i)))+1)*sizeof(char)); strcpy(str, CHAR(STRING_ELT(newNamesP, i))); hsh_put(hash, CHAR(STRING_ELT(oldNamesP, i)), str); } PROTECT(result = NEW_CHARACTER(numtree)); for (treeIdx=0; treeIdx < numtree; treeIdx++) { tr = rph_tree_new(STRING_ELT(treeVec, treeIdx)); // tr = tr_new_from_string(CHAR(STRING_ELT(treeVec, treeIdx))); for (i=0; i<tr->nnodes; i++) { n = lst_get_ptr(tr->nodes, i); if (n->name[0] != '\0' && (str = hsh_get(hash, n->name)) != (char*)-1) strcpy(n->name, str); } str = tr_to_string(tr, 1); SET_STRING_ELT(result, treeIdx, mkChar(str)); } UNPROTECT(1); return result; }
/* open a file with name out_root.name.maf, or returns it if already open. This is a bit messy because in some cases (splitting by feature) there may be more output files than the OS can handle. But it would be computationally expensive to check and see which files are finished, assuming that the MAF is sorted. So, if it tries to open a file and fails, it the goes through the list of filehandles, finds an open one, closes it, and tries to open the new one again. Repeat until successful. Then, if a filehandle needs to be re-opened, it is opened with append. Again, if this is not successful, it looks for another file to close. If it can't find one the program reports an error and dies. Finally, close_outfiles below checks and makes sure that all files are closed with mafBlock_close_file in the end, so that they get the #eof closer. */ FILE *get_outfile(List *outfileList, Hashtable *outfileHash, String *name, char *out_root, int argc, char *argv[]) { int idx, i; FILE *outfile; char *fname = smalloc((strlen(out_root)+name->length+7)*sizeof(char)); sprintf(fname, "%s.%s.maf", out_root, name->chars); idx = ptr_to_int(hsh_get(outfileHash, fname)); if (idx == -1) { hsh_put(outfileHash, fname, int_to_ptr(lst_size(outfileList))); outfile = mafBlock_open_outfile(fname, argc, argv); while (outfile==NULL) { //too many files are open, close one first for (i=0; i<lst_size(outfileList); i++) { outfile = (FILE*)lst_get_ptr(outfileList, i); if (outfile != NULL) break; } if (i == lst_size(outfileList)) { die("ERROR: too many files open in maf_parse\n"); } else { phast_fclose(outfile); lst_set_ptr(outfileList, i, NULL); } outfile = mafBlock_open_outfile(fname, argc, argv); } lst_push_ptr(outfileList, (void*)outfile); sfree(fname); return outfile; } outfile = (FILE*)lst_get_ptr(outfileList, idx); if (outfile == NULL) { //has already been opened but then closed. outfile = phast_fopen_no_exit(fname, "a"); while (outfile == NULL) { for (i=0; i<lst_size(outfileList); i++) { outfile = (FILE*)lst_get_ptr(outfileList, i); if (outfile != NULL) break; } if (i == lst_size(outfileList)) { die("ERROR: too many files open in maf_parse\n"); } else { phast_fclose(outfile); lst_set_ptr(outfileList, i, NULL); } outfile = phast_fopen_no_exit(fname, "a"); } lst_set_ptr(outfileList, idx, (void*)outfile); } sfree(fname); return outfile; }