void cband_solve(dcomplex **a, int n, int m1, int m2, dcomplex *b) { static dcomplex **al; static unsigned long *indx; static int an = 0, am1 = 0; // Allocated sizes dcomplex d; if(an < n) { if(an != 0) { free_cmatrix(al); delete[] indx; } al = cmatrix(n, m1); indx = new unsigned long[n]; an = n; am1 = m1; } if(am1 < m1) { if(am1 != 0) free_cmatrix(al); al = cmatrix(an, m1); am1 = m1; } // LU decompose matrix cbandec(a, n, m1, m2, al, indx, &d); // Solve cbanbks(a, n, m1, m2, al, indx, b); }
/* * Function to be called from Python */ static PyObject* py_smith_waterman_context(PyObject* self, PyObject* args) { char *seq1 = NULL; char *seq2 = NULL; char retstr[100] = {'\0'}; int len1, len2; int i, j; int gap_opening, gap_extension; static int ** similarity = NULL; PyArg_ParseTuple(args, "s#s#ii", &seq1, &len1, &seq2, &len2, &gap_opening, &gap_extension); if (!seq1 || !seq2) { sprintf (retstr, "no seq in py_smith_waterman_context"); return Py_BuildValue("s", retstr); } /* passing a matrix this way is all to painful, so we'll elegantyly hardcode it: */ if ( !similarity) { similarity = imatrix(ASCII_SIZE, ASCII_SIZE); if (!similarity) { sprintf (retstr, "error alloc matrix space"); return Py_BuildValue("s", retstr); } load_sim_matrix (similarity); } /**********************************************************************************/ //int gap_opening = -5; // used in 15_make_maps //int gap_extension = -3; //char gap_character = '-' //int gap_opening = -3; // used in 25_db_migration/06_make_alignments //int gap_extension = 0; char gap_character = '#'; int endgap = 0; int use_endgap = 0; int far_away = -1; int max_i = len1; int max_j = len2; // allocation, initialization int **F = NULL; char **direction = NULL; int *map_i2j = NULL; int *map_j2i = NULL; if ( ! (F= imatrix (max_i+1, max_j+1)) ) { sprintf (retstr, "error alloc matrix space"); return Py_BuildValue("s", retstr); } if ( ! (direction = cmatrix (max_i+1, max_j+1)) ) { sprintf (retstr, "error alloc matrix space"); return Py_BuildValue("s", retstr); } if (! (map_i2j = emalloc( (max_i+1)*sizeof(int))) ) { sprintf (retstr, "error alloc matrix space"); return Py_BuildValue("s", retstr); } if (! (map_j2i = emalloc( (max_j+1)*sizeof(int))) ) { sprintf (retstr, "error alloc matrix space"); return Py_BuildValue("s", retstr); } for (i=0; i<=max_i; i++) map_i2j[i]=far_away; for (j=0; j<=max_j; j++) map_j2i[j]=far_away; int F_max = far_away; int F_max_i = 0; int F_max_j = 0; int penalty = 0; int i_sim, j_sim, diag_sim, max_sim; int i_between_exons = 1; int j_between_exons = 1; // for (i=0; i<=max_i; i++) { if (i > 0) { if (seq1[i-1] == 'B') { i_between_exons = 0; } else if ( seq1[i-1] == 'Z'){ i_between_exons = 1; } } for (j=0; j<=max_j; j++) { if (j > 0) { if (seq2[j-1] == 'B') { j_between_exons = 0; } else if (seq2[j-1] == 'Z') { j_between_exons = 1; } } if ( !i && !j ){ F[0][0] = 0; direction[i][j] = 'd'; continue; } if ( i && j ){ /**********************************/ penalty = 0; if ( direction[i-1][j] == 'i' ) { // gap extension if (j_between_exons) { penalty = 0; } else { if (use_endgap && j==max_j){ penalty = endgap; } else { penalty = gap_extension; } } } else { // gap opening */ if (j_between_exons) { penalty = 0; } else { if (use_endgap && j==max_j){ penalty = endgap; } else{ penalty = gap_opening; } } } i_sim = F[i-1][j] + penalty; /**********************************/ penalty = 0; if ( direction[i][j-1] == 'j' ) { // gap extension if (i_between_exons) { penalty = 0; } else { if (use_endgap && i==max_i){ penalty = endgap; } else{ penalty = gap_extension; } } } else { // gap opening */ if (i_between_exons) { penalty = 0; } else { if (use_endgap && i==max_i){ penalty = endgap; } else { penalty = gap_opening; } } } j_sim = F[i][j-1] + penalty; /**********************************/ diag_sim = F[i-1][j-1] + similarity [seq1[i-1]][seq2[j-1]]; /**********************************/ max_sim = diag_sim; direction[i][j] = 'd'; if ( i_sim > max_sim ){ max_sim = i_sim; direction[i][j] = 'i'; } if ( j_sim > max_sim ) { max_sim = j_sim; direction[i][j] = 'j'; } } else if (j) { penalty = 0; if (j_between_exons) { penalty = 0; } else { if (use_endgap) { penalty = endgap; } else { if ( direction[i][j-1] =='j' ) { penalty = gap_extension; } else { penalty = gap_opening; } } } j_sim = F[i][j-1] + penalty; max_sim = j_sim; direction[i][j] = 'j'; } else if (i) { penalty = 0; if (i_between_exons) { penalty = 0; } else { if ( use_endgap) { penalty = endgap; } else { if ( direction[i-1][j] == 'i' ) { penalty = gap_extension; } else { penalty = gap_opening; } } } i_sim = F[i-1][j] + penalty; max_sim = i_sim; direction[i][j] = 'i'; } if (max_sim < 0.0 ) max_sim = 0.0; F[i][j] = max_sim; if ( F_max < max_sim ) { // TODO{ tie break here */ F_max = max_sim; F_max_i = i; F_max_j = j; } } } i = F_max_i; j = F_max_j; // aln_score = F[i][j] ; while ( i>0 || j >0 ){ if ( i<0 || j<0 ){ sprintf (retstr, "Retracing error"); return Py_BuildValue("s", retstr); } if (direction[i][j] == 'd'){ map_i2j [i-1] = j-1; map_j2i [j-1] = i-1; i-= 1; j-= 1; } else if (direction[i][j] == 'i') { map_i2j [i-1] = far_away; i-= 1 ; } else if (direction[i][j] == 'j') { map_j2i [j-1] = far_away; j-= 1 ; } else{ sprintf (retstr, "Retracing error"); return Py_BuildValue("s", retstr); } } char * aligned_seq_1 = NULL; char * aligned_seq_2 = NULL; /* (lets hope it gets properly freed in the main program */ if (! (aligned_seq_1 = emalloc( (len1+len2)*sizeof(char))) ) { sprintf (retstr, "error alloc array space"); return Py_BuildValue("s", retstr); } if (! (aligned_seq_2 = emalloc( (len1+len2)*sizeof(char))) ) { sprintf (retstr, "error alloc array space"); return Py_BuildValue("s", retstr); } i = 0; j = 0; int done = 0; int pos = 0; while (!done) { if (j>=max_j && i>=max_i){ done = 1; } else if (j<max_j && i<max_i){ if (map_i2j[i] == j){ aligned_seq_1[pos] = seq1[i]; aligned_seq_2[pos] = seq2[j]; i += 1; j += 1; } else if (map_i2j[i] < 0){ aligned_seq_1[pos] = seq1[i]; aligned_seq_2[pos] = gap_character; i += 1; } else if (map_j2i[j] < 0){ aligned_seq_1[pos] = gap_character; aligned_seq_2[pos] = seq2[j]; j += 1; } } else if (j<max_j){ aligned_seq_1[pos] = gap_character; aligned_seq_2[pos] = seq2[j]; j += 1; } else { aligned_seq_1[pos] = seq1[i]; aligned_seq_2[pos] = gap_character; i += 1; } pos ++; } free_imatrix(F); free_cmatrix(direction); free(map_i2j); free(map_j2i); return Py_BuildValue("ss", aligned_seq_1, aligned_seq_2 ); }
/* "read_springfile_sysenv" READS THE SPRINGFILE FOR BOTH THE 'SYSTEM' AND THE 'ENVIRONMENT' */ Sprngmtx *read_springfile_sysenv(char *file,Centroid *SYS,Centroid *ENV, int nss,int nen,int *ntp) { FILE *data; Sprngmtx *foo; char **LIST,nup1[NAME_LNG],nup2[NAME_LNG]; double x; int nn=nss+nen,ok,i,j; if((data=fopen(file,"r"))==NULL){ fprintf(stderr,"\nread_springfile_sysenv: unable to open %s\n\n",file); exit(1);} /* GET THE LIST OF UNIQUE CENTROID NAMES */ LIST=cmatrix(1,nn,0,NAME_LNG); (*ntp)=0; for(i=1;i<=nss;i++){ ok=1; for(j=1;j<=(*ntp);j++){ if(!strcmp(SYS[i].name,LIST[j])){ ok=0; break; } } if(ok==1) strcpy(LIST[++(*ntp)],SYS[i].name); } for(i=1;i<=nen;i++){ ok=1; for(j=1;j<=(*ntp);j++){ if(!strcmp(ENV[i].name,LIST[j])){ ok=0; break; } } if(ok==1) strcpy(LIST[++(*ntp)],ENV[i].name); } foo=(Sprngmtx *)malloc((size_t) sizeof(Sprngmtx *)); foo->name=cmatrix(1,(*ntp),0,NAME_LNG); foo->M=dmatrix(1,(*ntp),1,(*ntp)); for(i=1;i<=(*ntp);i++){ strcpy(foo->name[i],LIST[i]); for(j=i;j<=(*ntp);j++) foo->M[i][j]=foo->M[j][i]=DEFGAM; } free_cmatrix(LIST,1,nn,0,NAME_LNG); /* READ THE FILE */ while(!feof(data)){ fscanf(data,"%s%s%lf",nup1,nup2,&x); for(i=1;i<=(*ntp);i++) if(!strcmp(nup1,foo->name[i])){ for(j=1;j<=(*ntp);j++) if(!strcmp(nup2,foo->name[j])){ foo->M[i][j]=foo->M[j][i]=x; break; } break; } } fclose(data); return foo; }
SEXP rfsrcReadMatrix(SEXP traceFlag, SEXP fName, SEXP rowType, SEXP rowCnt, SEXP tokenDelim, SEXP colHeader, SEXP rowHeader) { FILE *fopen(); FILE *fPtr; char *_fName; SEXP _sexp_rowType ; char **_rowType; uint _rowCnt; char *_tokenDelim; char _colHeadF; char _rowHeadF; SmartBuffer *sb; uint p, i; uint rowCntActual; uint colCntActual; uint sbSizeActual; char flag; setTraceFlag(INTEGER(traceFlag)[0], 0); _fName = (char*) CHAR(STRING_ELT(AS_CHARACTER(fName), 0)); _sexp_rowType = rowType; _rowCnt = INTEGER(rowCnt)[0]; _tokenDelim = (char*) CHAR(STRING_ELT(AS_CHARACTER(tokenDelim), 0)); _colHeadF = (INTEGER(colHeader)[0] != 0) ? TRUE : FALSE; _rowHeadF = (INTEGER(rowHeader)[0] != 0) ? TRUE : FALSE; _rowType = (char**) new_vvector(1, _rowCnt, NRUTIL_CPTR); for (p = 1; p <= _rowCnt; p++) { _rowType[p] = (char*) CHAR(STRING_ELT(AS_CHARACTER(_sexp_rowType), p-1)); if ((strcmp(_rowType[p], "X") != 0) && (strcmp(_rowType[p], "C") != 0) && (strcmp(_rowType[p], "c") != 0) && (strcmp(_rowType[p], "I") != 0) && (strcmp(_rowType[p], "R") != 0)) { Rprintf("\nRF-SRC: *** ERROR *** "); Rprintf("\nRF-SRC: Invalid predictor type: [%10d] = %2s", p, _rowType[p]); Rprintf("\nRF-SRC: Type must be 'C', 'c', 'I', or 'R'."); Rprintf("\nRF-SRC: Please Contact Technical Support."); error("\nRF-SRC: The application will now exit.\n"); } } fPtr = fopen(_fName, "r"); sb = parseLineSB(fPtr, *_tokenDelim, 0); colCntActual = _rowHeadF ? (sb -> tokenCnt - 1) : (sb -> tokenCnt); rowCntActual = 0; if (_colHeadF) { freeSB(sb); sb = parseLineSB(fPtr, *_tokenDelim, 0); sbSizeActual = sb -> size; freeSB(sb); } else { sbSizeActual = sb -> size; freeSB(sb); } ++rowCntActual; flag = TRUE; while (flag) { sb = parseLineSB(fPtr, *_tokenDelim, sbSizeActual); if (sb -> tokenCnt == 0) { flag = FALSE; freeSB(sb); } else { ++rowCntActual; freeSB(sb); } } if (rowCntActual != _rowCnt) { Rprintf("\nRF-SRC: *** ERROR *** "); Rprintf("\nRF-SRC: Inconsistent Predictor Count."); Rprintf("\nRF-SRC: (encountered, expected) = (%10d, %10d)", rowCntActual, _rowCnt); Rprintf("\nRF-SRC: Please Contact Technical Support."); error("\nRF-SRC: The application will now exit.\n"); } fclose(fPtr); if (_colHeadF) { fPtr = fopen(_fName, "r"); sb = parseLineSB(fPtr, *_tokenDelim, sbSizeActual); freeSB(sb); } char **dataMatrix = cmatrix(1, colCntActual, 1, rowCntActual); for (p=1; p <= rowCntActual; p++) { sb = parseLineSB(fPtr, *_tokenDelim, sbSizeActual); if (_rowHeadF) { } for (i = 1; i <= colCntActual; i++) { dataMatrix[i][p] = (char) strtol(sb -> token, NULL, 10); } freeSB(sb); } free_cmatrix(dataMatrix, 1, colCntActual, 1, rowCntActual); return R_NilValue; }
int main(int argc, char **argv) { FILE *namef, *fpOutfast; long totalBases; int i, nSeq, args, n_len; int n_contig, n_reads; fasta *seq, *seqp; char *st, line[2000] = {0}; long Size_q_pdata; char *pdata; int fastq_flag = 1; int name_flag = 0; int *ctg2wgs_index; char **rdnames; if (argc < 2) { printf("Usage: %s <read_name_file> <input_fastq_file> <output_fastq_file\n", argv[0]); exit(1); } args = 1; for (i = 1; i < argc; i++) { if (!strcmp(argv[i], "-fastq")) { sscanf(argv[++i], "%d", &fastq_flag); args += 2; } else if (!strcmp(argv[i], "-name")) { sscanf(argv[++i], "%d", &name_flag); args += 2; } } if ((nSeq = extract_fastq_from_file(argv[args + 1], &pdata, &Size_q_pdata)) == 0) { fprintf(stderr, "Error: unable to extract fastq data from %s\n", argv[args + 1]); exit(1); } if ((seq = decode_fastq(pdata, Size_q_pdata, &nSeq, &totalBases)) == NULL) { fprintf(stderr, "Error: no query data found.\n"); exit(1); } fastaUC(seq, nSeq); if ((namef = fopen(argv[args], "r")) == NULL) { fprintf(stderr, "ERROR unable to open reads file %s\n", argv[args]); exit(1); } n_reads = 0; while(!feof(namef)) { fgets(line, 2000, namef); if (feof(namef)) break; n_reads++; } fclose(namef); printf("reads: %d %s\n", n_reads, argv[args+1]); if ((ctg2wgs_index = (int *)malloc(n_reads * sizeof(int))) == NULL) { fprintf(stderr, "ERROR Out of memory : malloc - ctg2wgs_index\n"); exit(1); } memset(ctg2wgs_index, -1, n_reads*sizeof(int)); rdnames = Read_Index(argv[args], ctg2wgs_index, seq, n_reads, nSeq); /* process contigs one by one */ printf("file name %s \n",argv[args + 2]); if ((fpOutfast = fopen(argv[args + 2], "w")) == NULL) { fprintf(stderr, "ERROR: cannot write to %s\n", argv[args + 2]); exit(1); } n_contig = 0; for (i = 0; i < n_reads; i++) { if (ctg2wgs_index[i] >= 0) { int nline, k, g, rc; int idd = ctg2wgs_index[i]; n_contig++; seqp=seq + idd; n_len = seqp->length; if (fastq_flag == 0) { fprintf(fpOutfast, ">%s\n", seqp->name); nline=n_len / 60; st = seqp->data; for (k = 0; k < nline; k++) { for (g = 0; g < 60; g++, st++) { fprintf(fpOutfast, "%c", *st); } fprintf(fpOutfast, "\n"); } for (g = 0; g < (n_len - (nline * 60)); g++, st++) { fprintf(fpOutfast, "%c", *st); } if ((n_len % 60) != 0) fprintf(fpOutfast, "\n"); } else { if (seqp->name2) fprintf(fpOutfast, "@%s %s\n", seqp->name, seqp->name2); else fprintf(fpOutfast, "@%s\n", seqp->name); for (rc = 0; rc < seqp->length; rc++) { fprintf(fpOutfast, "%c", seqp->data[rc]); } fprintf(fpOutfast, "\n"); if (name_flag) fprintf(fpOutfast, "+%s\n", seqp->name); else fprintf(fpOutfast, "+\n"); for (rc = 0; rc < seqp->length; rc++) { if (seqp->finished) { if ((seqp->data[rc] == 'N') || (seqp->data[rc] == 'X')) putc(2 + 041, fpOutfast); else putc(40 + 041, fpOutfast); } else { putc(seqp->qual[rc] + 041, fpOutfast); } } fprintf(fpOutfast, "\n"); } } else { printf("missed: %d %s\n", i, rdnames[i]); } } fclose(fpOutfast); free(seq->name); free(seq); free(ctg2wgs_index); free_cmatrix(rdnames, 0, n_reads + 1, 0, Max_N_NameBase); printf("Job finished for %d contigs!\n", n_contig); return EXIT_SUCCESS; }
int smith_waterman_2 (int max_i, int max_j, double **similarity, int *map_i2j, int * map_j2i, double * aln_score) { double **F; /*alignment_scoring table*/ char **direction; double gap_opening = -0.2; double gap_extension = -0.1; double endgap = 0.0; double penalty; double i_sim = 0.0, j_sim = 0.0, diag_sim = 0.0, max_sim = 0.0; double F_max; int use_endgap = 1; int F_max_i, F_max_j; int i,j; /* allocate F */ if ( ! (F = dmatrix( max_i+1, max_j+1)) ) return 1; if ( ! (direction = chmatrix ( max_i+1, max_j+1)) ) return 1; /* fill the table */ F_max = FAR_FAR_AWAY; F_max_i = 0; F_max_j = 0; for (i=0; i<= max_i; i++) { for (j=0; j<=max_j; j++) { if ( !i && !j ) { F[0][0] = 0; direction[i][j] = 'd'; continue; } if ( i && j ){ if ( direction[i-1][j] == 'i' ) { /* gap extension */ penalty = (use_endgap&&j==max_j) ? endgap : gap_extension; } else { /* gap opening */ penalty = (use_endgap&&j==max_j) ? endgap : gap_opening; } i_sim = F[i-1][j] + penalty; if ( direction[i][j-1] =='j' ) { penalty = (use_endgap&&i==max_i) ? endgap : gap_extension; } else { penalty = (use_endgap&&i==max_i) ? endgap : gap_opening; } j_sim = F[i][j-1] + penalty; diag_sim = F[i-1][j-1] + similarity [i-1][j-1] ; } else if ( j ) { if ( use_endgap) { penalty = endgap; } else { if ( direction[i][j-1] =='j' ) { penalty = gap_extension; } else { penalty = gap_opening; } } j_sim = F[i][j-1] + penalty; i_sim = diag_sim = options.far_far_away; } else if ( i ) { if ( use_endgap) { penalty = endgap; } else { if ( direction[i-1][j] == 'i' ) { penalty = gap_extension; } else { penalty = gap_opening; } } i_sim = F[i-1][j] + penalty; j_sim = diag_sim = options.far_far_away; } max_sim = diag_sim; direction[i][j] = 'd'; if ( i_sim > max_sim ){ max_sim = i_sim; direction[i][j] = 'i'; } if ( j_sim > max_sim ) { max_sim = j_sim; direction[i][j] = 'j'; } if ( max_sim < 0.0 ) max_sim = 0.0; F[i][j] = max_sim; if ( F_max < max_sim ) { /* TODO: tie break here */ F_max = max_sim; F_max_i = i; F_max_j = j; } } } /*retrace from the maximum element*/ i= max_i; for( i= max_i; i>F_max_i; i--) map_i2j[i-1] = FAR_FAR_AWAY;; for( j= max_j; j>F_max_j; j--) map_j2i[j-1] = FAR_FAR_AWAY;; i = F_max_i; j = F_max_j; *aln_score = F[i][j]; while ( i>0 || j >0 ) { //printf (" %4d %4d %8.3f \n", i, j, F[i][j]); switch ( direction[i][j] ) { case 'd': //printf ( " %4d %4d \n", i, j); map_i2j [i-1] = j-1; map_j2i [j-1] = i-1; i--; j--; break; case 'i': //printf ( " %4d %4d \n", i, -1); map_i2j [i-1] = FAR_FAR_AWAY; i--; break; case 'j': //printf ( " %4d %4d \n", -1, j); map_j2i [j-1] = FAR_FAR_AWAY; j--; break; default: fprintf (stderr, "Retracing error.\n"); } } /* free */ free_dmatrix (F); free_cmatrix (direction); return 0; }
// -------------- WriteNexus ------------------ void WriteNexus(phylo P[], int ntree, sample S, int nsamp, traits T, int ntrf) { // Mesquite style! time_t rawtime; int i, j, q, k, x, pass, present; int makedisc, makecont; float abnd; int nterm = 0; phylo WN[ntree]; char tmp[MAXTAXONLENGTH+10]; for (i = 0; i < ntree; i++) { WN[i] = P[i]; // inefficient to make copy so much, but need to // to create third dimension of taxon array // reassign the pointer to a new space - free this! WN[i].taxon = cmatrix(0, P[0].nnodes-1, 0, MAXTAXONLENGTH+10); } // determine number of terminal taxa - assume all trees contain same taxa for (i = 0; i < P[0].nnodes; i++) { if (P[0].noat[i] == 0) nterm++; } time ( &rawtime ); strncpy(tmp , ctime(&rawtime), 24); printf("#NEXUS\n[output from phylocom, written %s]\n\n", tmp ); printf("BEGIN TAXA;\n"); if (TreeView == 0) printf("TITLE Phylocom_Phylogeny_Taxa;\n"); // Needed for correct Mesquite grammar, but V1.1 busted! Will not read interior names correctly. printf("\tDIMENSIONS NTAX=%d;\n\tTAXLABELS\n\t", nterm); for (i = 0; i < P[0].nnodes; i++) { if (P[0].noat[i] == 0) printf(" %s", P[0].taxon[i]); } printf(";\nEND;\n\n"); if (nsamp > 0) { // Characters printf("BEGIN CHARACTERS;\n\tTITLE Phylocom_Presence_in_Sample;\n\tDIMENSIONS NCHAR=%d;\n\tFORMAT DATATYPE = STANDARD GAP = - MISSING = ? SYMBOLS = \" 0 1\";\n", S.nsamples); printf("\tCHARSTATELABELS\n\t\t"); printf("%d %s", 1, S.pname[0]); for (i = 1; i < S.nsamples; i++) { printf(", %d %s", i+1, S.pname[i]); } printf(";\n\tMATRIX\n"); for (i = 0; i < P[0].nnodes; i++) { if (P[0].noat[i] == 0) { printf("\t%s\t" , P[0].taxon[i]); for (j = 0; j < S.nsamples; j++) { present = 0; for (k = 0; k < S.srec[j]; k++) { if (strcmp(S.taxa[S.id[j][k]], P[0].taxon[i]) == 0) present = 1; } printf("%d", present); } printf("\n"); } } printf(";\nEND;\n\n"); // Abundances as continuous printf("BEGIN CHARACTERS;\n\tTITLE Phylocom_Abundance_in_Sample;\n\tDIMENSIONS NCHAR=%d;\n\tFORMAT DATATYPE = CONTINUOUS GAP = - MISSING = ?;\n", S.nsamples); printf("\tCHARSTATELABELS\n\t\t"); printf("%d %s", 1, S.pname[0]); for (i = 1; i < S.nsamples; i++) { printf(", %d %s", i+1, S.pname[i]); } printf(";\n\tMATRIX\n"); for (i = 0; i < P[0].nnodes; i++) { if (P[0].noat[i] == 0) { printf("\t%s\t" , P[0].taxon[i]); for (j = 0; j < S.nsamples; j++) { abnd = 0.0; for (k = 0; k < S.srec[j]; k++) { if (strcmp(S.taxa[S.id[j][k]], P[0].taxon[i]) == 0) abnd = (float) S.abund[j][k]; } printf(" %f", abnd); } printf("\n"); } } printf(";\nEND;\n\n"); } if (ntrf > 0) { makedisc = 0; makecont = 0; pass = 0; for (i = 0; i < T.ntraits; i++) { if ((T.type[i] == 0) || (T.type[i] == 1) || (T.type[i] == 2)) makedisc++; if (T.type[i] == 3) makecont++; } if (makedisc > 0) { // Discrete Traits printf("BEGIN CHARACTERS;\n\tTITLE Phylocom_Discrete_Traits;\n\tDIMENSIONS NCHAR=%d;\n\tFORMAT DATATYPE = STANDARD GAP = - MISSING = ? SYMBOLS = \" 0 1 2 3 4 5 6 7 8 9\";\n", makedisc); x = 1; printf("\tCHARSTATELABELS\n\t\t"); // first one for (i = 0; i < T.ntraits; i++) { if ((T.type[i] == 0) || (T.type[i] == 1) || (T.type[i] == 2)) { printf("%d %s", x, T.trname[i]); x++; pass = i; break; } } for (i = pass+1; i < T.ntraits; i++) { if ((T.type[i] == 0) || (T.type[i] == 1) || (T.type[i] == 2)) { printf(", %d %s", x, T.trname[i]); x++; } } printf(";\n\tMATRIX\n"); for (i = 0; i < T.ntaxa; i++) { printf("\t%s\t" , T.taxon[i]); for (j = 0; j < T.ntraits; j++) { if ((T.type[j] == 0) || (T.type[j] == 1) || (T.type[j] == 2)) { printf("%d", (int) T.tr[i][j]); } } printf("\n"); } printf(";\nEND;\n\n"); } if (makecont > 0) { // Continous Traits printf("BEGIN CHARACTERS;\n\tTITLE Phylocom_Continuous_Traits;\n\tDIMENSIONS NCHAR=%d;\n\tFORMAT DATATYPE = CONTINUOUS GAP = - MISSING = ?;\n", makecont); x=1; printf("\tCHARSTATELABELS\n\t\t"); // first one for (i = 0; i < T.ntraits; i++) { if (T.type[i] == 3) { printf("%d %s", x, T.trname[i]); x++; pass = i; break; } } for (i = pass+1; i < T.ntraits; i++) { if (T.type[i] == 3) { printf(", %d %s", x, T.trname[i]); x++; } } printf(";\n\tMATRIX\n"); for (i = 0; i < T.ntaxa; i++) { printf("\t%s\t" , T.taxon[i]); for (j = 0; j < T.ntraits; j++) { if (T.type[j] == 3) { printf(" %f", T.tr[i][j]); } } printf("\n"); } printf(";\nEND;\n\n"); } } printf("BEGIN TREES;\n"); if (TreeView == 0) printf("\tTITLE Phylocom_Phylogenies;\n\tLINK Taxa = Phylocom_Phylogeny_Taxa;\n"); // Ditto! printf("\tTRANSLATE\n\t"); for (q = 0; q < ntree; q++) { j = 0; for (i = 0; i < P[0].nnodes; i++) { if (P[0].noat[i] == 0) { j++; if (q == 0) { if (i == P[0].nnodes-1) printf(" %d %s;\n", j, P[0].taxon[i]); else printf(" %d %s,", j, P[0].taxon[i]); } sprintf(tmp, "%d", j); strcpy(WN[q].taxon[i], tmp); } else if ((strcmp(P[q].taxon[i], "") != 0) && \ (strcmp(P[q].taxon[i], ".") != 0)) { strcpy(WN[q].taxon[i], "'"); strcat(WN[q].taxon[i], P[q].taxon[i]); strcat(WN[q].taxon[i], "'"); } else strcpy(WN[q].taxon[i], ""); // test if (strcmp(WN[q].notes[i], "") != 0) printf("%s\n", WN[q].notes[i]); } } for (q = 0; q < ntree; q++) { printf("\tTREE %s = ", WN[q].phyname); Fy2newRec(WN[q]); free_cmatrix(WN[q].taxon, 0, P[0].nnodes-1, 0, MAXTAXONLENGTH+10); } printf("END;\n"); printf("\nBEGIN PHYLOCOM;\n\tTITLE Phylocom_Main;\n\tDATA\n"); for (i = 0; i < S.nsamples; i++) { for (j = 0; j < S.srec[i]; j++) { printf("%s\t%d\t%s\n", S.pname[i], S.abund[i][j], S.taxa[S.id[i][j]]); } } printf(";\nEND;\n"); //free_cmatrix(WN.taxon, 0, P.nnodes-1, 0, MAXTAXONLENGTH+10); }
int needleman_wunsch (int max_i, int max_j, double **similarity, int *map_i2j, int *map_j2i, double * aln_score) { double **F; /*alignment_scoring table*/ char ** direction; double gap_opening = options.gap_open; double gap_extension = options.gap_extend; double endgap = options.endgap; double penalty; double i_sim = 0.0, j_sim = 0.0, diag_sim = 0.0, max_sim = 0.0; int use_endgap = options.use_endgap; int i,j; /* allocate F */ if ( ! (F = dmatrix( max_i+1, max_j+1)) ) return 1; if ( ! (direction = chmatrix ( max_i+1, max_j+1)) ) return 1; /* fill the table */ for (i=0; i<= max_i; i++) { for (j=0; j<=max_j; j++) { if ( !i && !j ) { /* upper left corner */ F[0][0] = 0; direction[i][j] = 'd'; continue; } if ( i && j ){ if ( direction[i-1][j] == 'i' ) { /* gap extension */ penalty = (use_endgap&&j==max_j) ? endgap : gap_extension; } else { /* gap opening */ penalty = (use_endgap&&j==max_j) ? endgap : gap_opening; } i_sim = F[i-1][j] + penalty; if ( direction[i][j-1] =='j' ) { penalty = (use_endgap&&i==max_i) ? endgap : gap_extension; } else { penalty = (use_endgap&&i==max_i) ? endgap : gap_opening; } j_sim = F[i][j-1] + penalty; diag_sim = F[i-1][j-1] + similarity [i-1][j-1] ; } else if ( j ) { if ( use_endgap) { penalty = endgap; } else { if ( direction[i][j-1] =='j' ) { penalty = gap_extension; } else { penalty = gap_opening; } } j_sim = F[i][j-1] + penalty; i_sim = diag_sim = options.far_far_away; } else if ( i ) { if ( use_endgap) { penalty = endgap; } else { if ( direction[i-1][j] == 'i' ) { penalty = gap_extension; } else { penalty = gap_opening; } } i_sim = F[i-1][j] + penalty; j_sim = diag_sim = options.far_far_away; } max_sim = diag_sim; direction[i][j] = 'd'; if ( i_sim > max_sim ){ max_sim = i_sim; direction[i][j] = 'i'; } if ( j_sim > max_sim ) { max_sim = j_sim; direction[i][j] = 'j'; } F[i][j] = max_sim; } } *aln_score = F[max_i][max_j]; /*retrace*/ i = max_i; j = max_j; while ( i>0 || j >0 ) { //printf (" %4d %4d %8.3f \n", i, j, F[i][j]); switch ( direction[i][j] ) { case 'd': //printf ( " %4d %4d \n", i, j); map_i2j [i-1] = j-1; map_j2i [j-1] = i-1; i--; j--; break; case 'i': //printf ( " %4d %4d \n", i, -1); map_i2j [i-1] = options.far_far_away; i--; break; case 'j': //printf ( " %4d %4d \n", -1, j); map_j2i [j-1] = options.far_far_away; j--; break; default: fprintf ( stderr, "Retracing error.\n"); } } /* free */ free_dmatrix (F); free_cmatrix (direction); return 0; }