Commit 9450fd05 authored by gpt's avatar gpt
Browse files

GNU_SOURCE removed.

parent ccf9294e
#CC = /usr/local/bin/gcc
CFLAGS = -O3 -Wall -Wno-char-subscripts -Wno-unused-function
CFLAGS = -O3 -Wall
LFLAGS = -lm
all: live-neighbor-joining
all: live-neighbor-joining neighbor-joining
arrays.o:
distance.o:
......
......@@ -134,11 +134,14 @@ dmat* dmat_load(char* filename) {
for (j=0; j<i; j++)
if (fscanf(f,"%lf;",&(R->M[i][j])) != 1) goto EILSEQH;
fclose(f);
free(word);
return R;
ENOMEMH: err = errno; goto STDH;
EILSEQH: err = EILSEQ; goto STDH;
STDH:
fclose(f);
dmat_free(R);
errno = err;
return 0;
......
// Guilherme P. Telles, ?-2017.
#define _GNU_SOURCE
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <string.h>
#include <float.h>
#include <limits.h>
#include <math.h>
#include "graph.h"
#include "dynarray.h"
#include "utils.h"
/**
\brief Allocate a graph.
......@@ -214,105 +213,89 @@ int gr_write_dot(graph* G, char* file, int print_edge_weights) {
/**
\brief Create a Newick representation of a tree.
\param T The tree.
\return A string with the Newick representation of T or NULL if memory
allocation fails somewhere. errno will remain as set by the failing function.
The recusive construction function for tree_newicks().
**/
char* tree_newick(graph *T) {
int i;
char* r;
char* newicks_rec(int k, graph* T, int marked[], char** labels, int l) {
if (T->n == 0)
return asprintf(&r,";") < 0 ? 0 : r;
marked[k] = 1;
if (T->n == 1)
return asprintf(&r,"(0);") < 0 ? 0 : r;
char* r = 0;
char* s = 0;
char* t = 0;
if (T->n == 2)
return asprintf(&r,"(0,1):%.16e;",gr_edge_weight(T,0,1)) < 0 ? 0 : r;
int marked[T->n];
for (i=0; i<T->n; i++)
marked[i] = 0;
/* The recusive construction function: */
char* newick(int k) {
marked[k] = 1;
char* r = 0;
char* s = 0;
char* t = 0;
// Count the unmarked neighbors of k:
int n = 0;
edge* e = T->V[k].N;
while (e) {
if (!marked[e->term])
n++;
e = e->next;
}
int n = 0;
edge* e = T->V[k].N;
while (e) {
if (!marked[e->term])
n++;
e = e->next;
}
if (n == 0) {
// All neighbors are marked, k is a trivial tree:
if (asprintf(&r,"%d",k) < 0) return 0;
if (n == 0) {
// All neighbors are marked, k is a trivial tree:
if (k < l) {
r = malloc((strlen(labels[k])+1)*sizeof(char));
if (!r) return 0;
sprintf(r,"%s",labels[k]);
}
else {
// Join neigboring trees by k into string r. r starts with a dummy char in it:
r = calloc(1,sizeof(char));
r = malloc(((int)log10(k)+2)*sizeof(char));
if (!r) return 0;
sprintf(r,"%d",k);
}
}
else {
// Join neigboring trees by k into string r. r starts with a dummy char in it:
r = calloc(1,sizeof(char));
if (!r) return 0;
e = T->V[k].N;
while (e) {
if (!marked[e->term]) {
t = r;
s = newick(e->term);
if (!s) {
free(t);
return 0;
}
r = 0;
//if (asprintf(&r,"%s,%s:%.16e",t,s,e->w) < 0) {
if (asprintf(&r,"%s,%s:%.1lf",t,s,e->w) < 0) {
free(s);
free(t);
return 0;
}
e = T->V[k].N;
while (e) {
if (!marked[e->term]) {
t = r;
s = newicks_rec(e->term,T,marked,labels,l);
if (!s) {
free(t);
return 0;
}
r = malloc((strlen(t)+strlen(s)+40)*sizeof(char));
if (!r) {
free(s);
free(t);
return 0;
}
e = e->next;
printf("%s\n",r);
sprintf(r,"%s,%s:%.15lf",t,s,e->w);
free(s);
free(t);
}
e = e->next;
}
t = r;
if (k < l) {
t = r;
if (asprintf(&r,"(%s)%d",t+1,k) < 0) {
r = malloc((strlen(t+1)+strlen(labels[k])+3)*sizeof(char));
if (!r) {
free(t);
return 0;
}
free(t);
}
return r;
sprintf(r,"(%s)%s",t+1,labels[k]);
}
else {
r = malloc((strlen(t+1)+3)*sizeof(char));
if (!r) {
free(t);
return 0;
}
sprintf(r,"(%s)",t+1);
}
int c1,c2,dia;
tree_center(T,&c1,&c2,&dia);
char* t = newick(c1);
if (!t) return 0;
if (asprintf(&r,"%s:0.0;",t) < 0) return 0;
free(t);
free(t);
}
return r;
}
......@@ -335,112 +318,67 @@ char* tree_newicks(graph *T, char** labels, int l) {
int i;
char* r;
if (T->n == 0)
return asprintf(&r,";") < 0 ? 0 : r;
if (T->n == 0) {
r = malloc(2*sizeof(char));
if (!r) return 0;
sprintf(r,";");
return r;
}
if (T->n == 1) {
if (l > 0)
return asprintf(&r,"(%s)",labels[0]) < 0 ? 0 : r;
else
return asprintf(&r,"(0)") < 0 ? 0 : r;
if (l > 0) {
r = malloc((strlen(labels[0])+3)*sizeof(char));
if (!r) return 0;
sprintf(r,"(%s)",labels[0]);
return r;
}
else {
r = malloc(4*sizeof(char));
if (!r) return 0;
sprintf(r,"(0)");
return r;
}
}
if (T->n == 2) {
if (l >= 2)
return asprintf(&r,"(%s,%s):%.16e;",labels[0],labels[1],gr_edge_weight(T,0,1)) < 0 ? 0 : r;
else if (l == 1)
return asprintf(&r,"(%s,1):%.16e;",labels[0],gr_edge_weight(T,0,1)) < 0 ? 0 : r;
else
return asprintf(&r,"(0,1):%.16e;",gr_edge_weight(T,0,1)) < 0 ? 0 : r;
}
int marked[T->n];
for (i=0; i<T->n; i++)
marked[i] = 0;
/* The recusive construction function: */
char* newicks(int k) {
marked[k] = 1;
char* r = 0;
char* s = 0;
char* t = 0;
int n = 0;
edge* e = T->V[k].N;
while (e) {
if (!marked[e->term])
n++;
e = e->next;
double w = gr_edge_weight(T,0,1);
if (l >= 2) {
r = malloc((strlen(labels[0])+strlen(labels[1])+40)*sizeof(char));
if (!r) return 0;
sprintf(r,"(%s,%s):%.15lf;",labels[0],labels[1],w);
return r;
}
if (n == 0) {
// All neighbors are marked, k is a trivial tree:
if (k < l) {
if (asprintf(&r,"%s",labels[k]) < 0) return 0;
}
else {
if (asprintf(&r,"%d",k) < 0) return 0;
}
else if (l == 1) {
r = malloc((strlen(labels[0])+40)*sizeof(char));
if (!r)
return 0;
sprintf(r,"(%s,1):%.15lf;",labels[0],w);
return r;
}
else {
// Join neigboring trees by k into string r. r starts with a dummy char in it:
r = calloc(1,sizeof(char));
if (!r) return 0;
e = T->V[k].N;
while (e) {
if (!marked[e->term]) {
t = r;
s = newicks(e->term);
if (!s) {
free(t);
return 0;
}
r = 0;
if (asprintf(&r,"%s,%s:%.16e",t,s,e->w) < 0) {
free(s);
free(t);
return 0;
}
free(s);
free(t);
}
e = e->next;
}
t = r;
if (k < l) {
if (asprintf(&r,"(%s)%s",t+1,labels[k]) < 0) {
free(t);
return 0;
}
}
else {
//if (asprintf(&r,"(%s)%d",t+1,k) < 0) exit(0);
if (asprintf(&r,"(%s)",t+1) < 0) {
free(t);
return 0;
}
}
free(t);
r = malloc(40*sizeof(char));
if (!r)
return 0;
sprintf(r,"(0,1):%.15lf;",w);
return r;
}
return r;
}
int marked[T->n];
for (i=0; i<T->n; i++)
marked[i] = 0;
int c1,c2,dia;
tree_center(T,&c1,&c2,&dia);
char* t = newicks(c1);
if (asprintf(&r,"%s:0.00;",t) < 0) return 0;
free(t);
char* t = newicks_rec(c1,T,marked,labels,l);
r = malloc((strlen(t)+7)*sizeof(char));
if (!r)
return 0;
sprintf(r,"%s:0.00;",t);
free(t);
return r;
}
......@@ -468,6 +406,9 @@ int tree_write_newicks(graph *T, char** labels, int l, char* file) {
fprintf(f,"%s\n",newick);
fclose(f);
free(newick);
return 1;
}
......@@ -557,6 +498,7 @@ int tree_center(graph *T, int* c1, int* c2, int *dia) {
else
*c1 = *c2 = pi[i];
da_free(Q);
free(pi);
return 1;
}
......@@ -52,12 +52,12 @@ typedef struct graph graph;
graph* gr_alloc(char type, unsigned n);
void gr_free(graph *G);
int gr_add_edge(graph *G, unsigned u, unsigned v, double w);
double gr_edge_weight(graph *G, unsigned u, unsigned v);
int gr_write_dot(graph* G, char* file, int print_edge_weights);
char* tree_newick(graph *T);
char* tree_newicks(graph *T, char** labels, int l);
int tree_write_newicks(graph *T, char** labels, int l, char* file);
......
// Guilherme P. Telles, 2017.
#define _GNU_SOURCE
#include <stdlib.h>
#include <stdio.h>
#include <float.h>
......
// Guilherme P. Telles, 2010-2017.
#define _GNU_SOURCE
#include <stdlib.h>
#include <stdio.h>
#include <float.h>
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment