path = HYPHY_BASE_DIRECTORY + "TemplateBatchFiles" + DIRECTORY_SEPARATOR + "Utility" + DIRECTORY_SEPARATOR + "ReadDelimitedFiles.bf"; ExecuteAFile (path); /* crashes if we try to use ReadCSVTable() - probably matrix is too big */ g2p = 0; SetDialogPrompt ("Select ancestral g2p (reduced) file:"); fscanf (PROMPT_FOR_FILE, "Lines", g2p); SetDialogPrompt ("Select file containing Newick tree strings:"); fscanf (PROMPT_FOR_FILE, "Lines", newicks); SetDialogPrompt ("Select a file to record output:"); fprintf (PROMPT_FOR_FILE, CLEAR_FILE, KEEP_OPEN); fprintf (LAST_FILE_PATH, "tree.rep,map.rep,node.name,g2p,fpr,start.days,end.days\n"); ACCEPT_ROOTED_TREES = 1; ACCEPT_BRANCH_LENGTHS = 1; UseModel (USE_NO_MODEL); last_line = 0; for (treerep = 0; treerep < Columns(newicks); treerep = treerep + 1) { /* convert Newick string to HyPhy associative list object */ Tree mytree = newicks[treerep]; tre = mytree^1; /* make a new list keyed by node name */ _al = {}; for (i = 0; i < Abs(tre); i = i + 1) { key = (tre[i])["Name"]; _al[key] = {}; (_al[key])["Depth"] = (tre[i])["Depth"]; (_al[key])["Length"] = (tre[i])["Length"]; if ((tre[i])["Depth"] > 0) { parent = (tre[i])["Parent"]; (_al[key])["Parent"] = (tre[parent])["Name"]; } (_al[key])["Children"] = {}; for (child = 0; child < Abs((tre[i])["Children"]); child = child+1) { ((_al[key])["Children"]) [child] = (tre[((tre[i])["Children"])[child]])["Name"]; } } /* output dates */ match = "" + treerep+","; matchlen = Abs(match); enteredBlock = 0; for (ln = last_line; ln < Columns(g2p); ln = ln+1) { line = g2p[ln]; /* if this line corresponds to current tree replicate */ if (line[0][matchlen-1] == match) { enteredBlock = 1; /* 0,0,Node0,0.10260696359,20.9 */ items = splitOnRegExp (g2p[ln],"\\,"); sample_rep = items["1"]; nodename = items["2"]; /* calculate chronological time of branch termini */ vals = _al[nodename]; total_t = 0.; while (Abs(vals["Children"]) > 0) { /* follow the first child branch up the tree */ nextnode = (vals["Children"])[0]; vals = _al[nextnode]; total_t = total_t + vals["Length"]; } /* extract days since January 1, 1900 from tip name */ index = nextnode$"[0-9]+$"; ExecuteCommands("days_since = "+nextnode[index[0]][index[1]]+";"); end_date = days_since - total_t; start_date = end_date - (_al[nodename])["Length"]; fprintf (LAST_FILE_PATH, treerep, ",", sample_rep, ",", nodename, ",", items["3"], ",", items["4"]); fprintf (LAST_FILE_PATH, ",", start_date, ",", end_date, "\n"); /* remember the last point we looked so that we won't have to go back through the whole file contents next time */ last_line = ln; } else { if (enteredBlock) { break; } } } } fprintf (LAST_FILE_PATH, CLOSE_FILE);