gde_linux/SUPPORT/CAP2.c

2224 lines
58 KiB
C
Raw Normal View History

2023-04-12 03:39:54 +08:00
/* CONTIG ASSEMBLY PROGRAM (CAP)
copyright (c) 1991 Xiaoqiu Huang
The distribution of the program is granted provided no charge
is made and the copyright notice is included.
Proper attribution of the author as the source of the software
would be appreciated:
"A Contig Assembly Program Based on Sensitive Detection of
Fragment Overlaps" (submitted to Genomics, 1991)
Xiaoqiu Huang
Department of Computer Science
Michigan Technological University
Houghton, MI 49931
2023-04-12 03:41:11 +08:00
E-mail: huang@cs.mtu.edu
2023-04-12 03:39:54 +08:00
The CAP program uses a dynamic programming algorithm to compute
the maximal-scoring overlapping alignment between two fragments.
Fragments in random orientations are assembled into contigs by a
greedy approach in order of the overlap scores. CAP is efficient
in computer memory: a large number of arbitrarily long fragments
can be assembled. The time requirement is acceptable; for example,
CAP took 4 hours to assemble 1015 fragments of a total of 252 kb
nucleotides on a Sun SPARCstation SLC. The program is written in C
and runs on Sun workstations.
Below is a description of the parameters in the #define section of CAP.
Two specially chosen sets of substitution scores and indel penalties
are used by the dynamic programming algorithm: heavy set for regions
of low sequencing error rates and light set for fragment ends of high
sequencing error rates. (Use integers only.)
Heavy set: Light set:
MATCH = 2 MATCH = 2
MISMAT = -6 LTMISM = -3
EXTEND = 4 LTEXTEN = 2
In the initial assembly, any overlap must be of length at least OVERLEN,
and any overlap/containment must be of identity percentage at least
PERCENT. After the initial assembly, the program attempts to join
contigs together using weak overlaps. Two contigs are merged if the
score of the overlapping alignment is at least CUTOFF. The value for
CUTOFF is chosen according to the value for MATCH.
DELTA is a parameter in necessary conditions for overlap/containment.
Those conditions are used to quickly reject pairs of fragments that
could not possibly have an overlap/containment relationship.
The dynamic programming algorithm is only applied to pairs of fragments
that pass the screening. A large value for DELTA means stringent
conditions, where the value for DELTA is a real number at least 8.0.
POS5 and POS3 are fragment positions such that the 5' end between base 1
and base POS5, and the 3' end after base POS3 are of high sequencing
error rates, say more than 5%. For mismatches and indels occurring in
the two ends, light penalties are used.
A file of input fragments looks like:
>G019uabh
ATACATCATAACACTACTTCCTACCCATAAGCTCCTTTTAACTTGTTAAA
GTCTTGCTTGAATTAAAGACTTGTTTAAACACAAAAATTTAGAGTTTTAC
TCAACAAAAGTGATTGATTGATTGATTGATTGATTGATGGTTTACAGTAG
GACTTCATTCTAGTCATTATAGCTGCTGGCAGTATAACTGGCCAGCCTTT
AATACATTGCTGCTTAGAGTCAAAGCATGTACTTAGAGTTGGTATGATTT
ATCTTTTTGGTCTTCTATAGCCTCCTTCCCCATCCCCATCAGTCTTAATC
AGTCTTGTTACGTTATGACTAATCTTTGGGGATTGTGCAGAATGTTATTT
TAGATAAGCAAAACGAGCAAAATGGGGAGTTACTTATATTTCTTTAAAGC
>G028uaah
CATAAGCTCCTTTTAACTTGTTAAAGTCTTGCTTGAATTAAAGACTTGTT
TAAACACAAAATTTAGACTTTTACTCAACAAAAGTGATTGATTGATTGAT
TGATTGATTGATGGTTTACAGTAGGACTTCATTCTAGTCATTATAGCTGC
TGGCAGTATAACTGGCCAGCCTTTAATACATTGCTGCTTAGAGTCAAAGC
ATGTACTTAGAGTTGGTATGATTTATCTTTTTGGTCTTCTATAGCCTCCT
TCCCCATCCCATCAGTCT
>G022uabh
TATTTTAGAGACCCAAGTTTTTGACCTTTTCCATGTTTACATCAATCCTG
TAGGTGATTGGGCAGCCATTTAAGTATTATTATAGACATTTTCACTATCC
CATTAAAACCCTTTATGCCCATACATCATAACACTACTTCCTACCCATAA
GCTCCTTTTAACTTGTTAAAGTCTTGCTTGAATTAAAGACTTGTTTAAAC
ACAAAATTTAGACTTTTACTCAACAAAAGTGATTGATTGATTGATTGATT
GATTGAT
>G023uabh
AATAAATACCAAAAAAATAGTATATCTACATAGAATTTCACATAAAATAA
ACTGTTTTCTATGTGAAAATTAACCTAAAAATATGCTTTGCTTATGTTTA
AGATGTCATGCTTTTTATCAGTTGAGGAGTTCAGCTTAATAATCCTCTAC
GATCTTAAACAAATAGGAAAAAAACTAAAAGTAGAAAATGGAAATAAAAT
GTCAAAGCATTTCTACCACTCAGAATTGATCTTATAACATGAAATGCTTT
TTAAAAGAAAATATTAAAGTTAAACTCCCCTATTTTGCTCGTTTTTGCTT
ATCTAAAATACATTCTGCACAATCCCCAAAGATTGATCATACGTTAC
>G006uaah
ACATAAAATAAACTGTTTTCTATGTGAAAATTAACCTANNATATGCTTTG
CTTATGTTTAAGATGTCATGCTTTTTATCAGTTGAGGAGTTCAGCTTAAT
AATCCTCTAAGATCTTAAACAAATAGGAAAAAAACTAAAAGTAGAAAATG
GAAATAAAATGTCAAAGCATTTCTACCACTCAGAATTGATCTTATAACAT
GAAATGCTTTTTAAAAGAAAATATTAAAGTTAAACTCCCC
A string after ">" is the name of the following fragment.
Only the five upper-case letters A, C, G, T and N are allowed
to appear in fragment data. No other characters are allowed.
A common mistake is the use of lower case letters in a fragment.
To run the program, type a command of form
2023-04-12 03:41:11 +08:00
cap file_of_fragments
2023-04-12 03:39:54 +08:00
The output goes to the terminal screen. So redirection of the
output into a file is necessary. The output consists of three parts:
overview of contigs at fragment level, detailed display of contigs
at nucleotide level, and consensus sequences.
'+' = direct orientation; '-' = reverse complement
The output of CAP on the sample input data looks like:
#Contig 1
#G022uabh+(0)
TATTTTAGAGACCCAAGTTTTTGACCTTTTCCATGTTTACATCAATCCTGTAGGTGATTG
GGCAGCCATTTAAGTATTATTATAGACATTTTCACTATCCCATTAAAACCCTTTATGCCC
ATACATCATAACACTACTTCCTACCCATAAGCTCCTTTTAACTTGTTAAAGTCTTGCTTG
AATTAAAGACTTGTTTAAACACAAAA-TTTAGACTTTTACTCAACAAAAGTGATTGATTG
ATTGATTGATTGATTGAT
#G028uaah+(145)
CATAAGCTCCTTTTAACTTGTTAAAGTCTTGCTTGAATTAAAGACTTGTTTAAACACAAA
A-TTTAGACTTTTACTCAACAAAAGTGATTGATTGATTGATTGATTGATTGATGGTTTAC
AGTAGGACTTCATTCTAGTCATTATAGCTGCTGGCAGTATAACTGGCCAGCCTTTAATAC
ATTGCTGCTTAGAGTCAAAGCATGTACTTAGAGTTGGTATGATTTATCTTTTTGGTCTTC
TATAGCCTCCTTCCCCATCCC-ATCAGTCT
#G019uabh+(120)
ATACATCATAACACTACTTCCTACCCATAAGCTCCTTTTAACTTGTTAAAGTCTTGCTTG
AATTAAAGACTTGTTTAAACACAAAAATTTAGAGTTTTACTCAACAAAAGTGATTGATTG
ATTGATTGATTGATTGATGGTTTACAGTAGGACTTCATTCTAGTCATTATAGCTGCTGGC
AGTATAACTGGCCAGCCTTTAATACATTGCTGCTTAGAGTCAAAGCATGTACTTAGAGTT
GGTATGATTTATCTTTTTGGTCTTCTATAGCCTCCTTCCCCATCCCCATCAGTCTTAATC
AGTCTTGTTACGTTATGACT-AATCTTTGGGGATTGTGCAGAATGTTATTTTAGATAAGC
AAAA-CGAGCAAAAT-GGGGAGTT-A-CTT-A-TATTT-CTTT-AAA--GC
#G023uabh-(426)
GTAACGT-ATGA-TCAATCTTTGGGGATTGTGCAGAATGT-ATTTTAGATAAGCAAAAAC
GAGCAAAATAGGGGAGTTTAACTTTAATATTTTCTTTTAAAAAGCATTTCATGTTATAAG
ATCAATTCTGAGTGGTAGAAATGCTTTGACATTTTATTTCCATTTTCTACTTTTAGTTTT
TTTCCTATTTGTTTAAGATCGTAGAGGATTATTAAGCTGAACTCCTCAACTGATAAAAAG
CATGACATCTTAAACATAAGCAAAGCATATTTTTAGGTTAATTTTCACATAGAAAACAGT
TTATTTTATGTGAAATTCTATGTAGATATACTATTTTTTTGGTATTTATT
#G006uaah-(496)
GGGGAGTTTAACTTTAATATTTTCTTTTAAAAAGCATTTCATGTTATAAGATCAATTCTG
AGTGGTAGAAATGCTTTGACATTTTATTTCCATTTTCTACTTTTAGTTTTTTTCCTATTT
GTTTAAGATCTTAGAGGATTATTAAGCTGAACTCCTCAACTGATAAAAAGCATGACATCT
TAAACATAAGCAAAGCATATNNT-AGGTTAATTTTCACATAGAAAACAGTTTATTTTATG
T
Slight modifications by S. Smith on Mon Feb 17 10:18:34 EST 1992.
These changes allow for command line arguements for several
of the hard coded parameters, as well as a slight modification to
the output routine to support GDE format. Changes are commented
as:
Mod by S.S.
*/
2023-04-12 03:41:11 +08:00
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int OVERLEN; /* Minimum length of any overlap */
float PERCENT; /* Minimum identity percentage of any overlap */
#define CUTOFF 50 /* cutoff score for overlap or containment */
#define DELTA 9.0 /* Jump increment in check for overlap */
#define MATCH 2 /* score of a match */
#define MISMAT -6 /* score of a mismatch */
#define LTMISM -3 /* light score of a mismatch */
#define OPEN 0 /* gap open penalty */
#define EXTEND 4 /* gap extension penalty */
#define LTEXTEN 2 /* light gap extension penalty */
#define POS5 30 /* Sequencing errors often occur before base POS5 */
#define POS3 475 /* Sequencing errors often occur after base POS3 */
#define LINELEN 60 /* length of one printed line */
#define NAMELEN 20 /* length of printed fragment name */
#define TUPLELEN 4 /* Maximum length of words for lookup table */
#define SEQLEN 2000 /* initial size of an array for an output fragment */
2023-04-12 03:39:54 +08:00
static int over_len;
static float per_cent;
2023-04-12 03:41:11 +08:00
typedef struct OVERLAP /* of 5' and 3' segments */
{
int number; /* index of 3' segment */
int host; /* index of 5' segment */
int ind; /* used in reassembly */
int stari; /* start position of 5' suffix */
int endi; /* end position of 5' suffix */
int starj; /* start position of 3' prefix */
int endj; /* end position of 3' prefix */
short orienti; /* orientation of 5' segment: 0= rev. */
short orientj; /* orientation of 3' segment: 0= rev. */
int score; /* score of overlap alignment */
int length; /* length of alignment */
int match; /* number of matches in alignment */
short kind; /* 0 = containment; 1 = overlap */
int *script; /* script for constructing alignment */
struct OVERLAP *next;
2023-04-12 03:39:54 +08:00
} over, *overptr;
2023-04-12 03:41:11 +08:00
struct SEG {
char *name; /* name string */
int len; /* length of segment name */
char *seq; /* segment sequence */
char *rev; /* reverse complement */
int length; /* length of sequence */
short kind; /* 0 = contain; 1 = overlap */
int *lookup; /* lookup table */
int *pos; /* location list */
overptr list; /* list of overlapping edges */
} *segment; /* array of segment records */
int seg_num; /* The number of segments */
overptr *edge; /* set of overlapping edges */
int edge_num; /* The number of overlaps */
struct CONS /* 1 = itself; 0 = reverse complement */
{
short isfive[2]; /* is 5' end free? */
short isthree[2]; /* is 3' end free? */
short orient[2]; /* orientation of 3' segment */
int group; /* contig serial number */
int next[2]; /* pointer to 3' adjacent segment */
int other; /* the other end of the contig */
int child; /* for the containment tree */
int brother;
int father;
overptr node[2]; /* pointers to overlapping edges */
} *contigs; /* list of contigs */
struct TTREE /* multiple alignment tree */
{
short head; /* head = 1 means the head of a contig */
short orient; /* orientation */
int begin; /* start position of previous segment */
int *script; /* alignment script */
int size; /* length of script */
int next; /* list of overlap segments */
int child; /* list of child segments */
int brother; /* list of sibling segments */
} *mtree;
int vert[128]; /* converted digits for 'A','C','G','T' */
int vertc[128]; /* for reverse complement */
int tuple; /* tuple = TUPLELEN - 1 */
int base[TUPLELEN]; /* locations of a lookup table */
int power[TUPLELEN]; /* powers of 4 */
typedef struct OUT {
char *line; /* one print line */
char *a; /* pointer to slot in line */
char c; /* current char */
char *seq; /* pointer to sequence */
int length; /* length of segment */
int id; /* index of segment */
int *s; /* pointer to script vector */
int size; /* size of script vector */
int op; /* current operation */
char name[NAMELEN + 2]; /* name of segment */
2023-04-12 03:39:54 +08:00
short done; /* indicates if segment is done */
2023-04-12 03:41:11 +08:00
int loc; /* position of next segment symbol */
char kind; /* type of next symbol of segment */
char up; /* type of upper symbol of operation */
char dw; /* type of lower symbol of operation */
int offset; /* relative to consensus sequence */
int linesize; /* size of array line */
struct OUT *child; /* pointer to child subtree */
struct OUT *brother; /* pointer to brother node */
struct OUT *link; /* for operation linked list */
struct OUT *father; /* pointer to father node */
} row, *rowptr; /* node for segment */
2023-04-12 03:39:54 +08:00
rowptr *work; /* a set of working segments */
rowptr head, tail; /* first and last nodes of op list */
2023-04-12 03:41:11 +08:00
struct VX {
int id; /* Segment index */
short kind; /* overlap or containment */
overptr list; /* list of overlapping edges */
} *piece; /* array of segment records */
char *allconsen, *allconpt; /* Storing consensus sequences */
char *ckalloc(size_t size);
main(argc, argv) int argc;
2023-04-12 03:39:54 +08:00
char *argv[];
2023-04-12 03:41:11 +08:00
{
int M; /* Sequence length */
int V[128][128], Q, R; /* Weights */
int V1[128][128], R1; /* Light weights */
int total; /* Total of segment lengths */
int number; /* The number of segments */
char *sequence; /* Storing all segments */
char *reverse; /* Storing all reverse segments */
int symbol, prev; /* The next character */
FILE *Ap, *ckopen(); /* For the sequence file */
char *ckalloc(size_t amount); /* space-allocating function */
register int i, j, k; /* index variables */
2023-04-12 03:39:54 +08:00
/* Mod by S.S. */
int jj;
2023-04-12 03:41:11 +08:00
short heading; /* 1: heading; 0: sequence */
2023-04-12 03:39:54 +08:00
/*
* Mod by S.S.
*
if ( argc != 2 )
fatalf("The proper form of command is: \n%s file_of_fragments",
argv[0]);
*/
2023-04-12 03:41:11 +08:00
if (argc != 4)
2023-04-12 03:39:54 +08:00
fatalf("usage: %s file_of_fragments MIN_OVERLAP PERCENT_MATCH",
2023-04-12 03:41:11 +08:00
argv[0]);
sscanf(argv[2], "%d", &OVERLEN);
sscanf(argv[3], "%d", &jj);
PERCENT = (float)jj / 100.0;
if (PERCENT < 0.25) PERCENT = 0.25;
if (PERCENT > 1.0) PERCENT = 1.0;
if (OVERLEN < 1) OVERLEN = 1;
if (OVERLEN > 100) OVERLEN = 100;
2023-04-12 03:39:54 +08:00
/* determine number of segments and total lengths */
j = 0;
Ap = ckopen(argv[1], "r");
prev = '\n';
2023-04-12 03:41:11 +08:00
for (total = 3, number = 0; (symbol = getc(Ap)) != EOF; total++) {
if (symbol == '>' && prev == '\n') number++;
2023-04-12 03:39:54 +08:00
prev = symbol;
}
2023-04-12 03:41:11 +08:00
(void)fclose(Ap);
2023-04-12 03:39:54 +08:00
total += number * 20;
/* allocate space for segments */
2023-04-12 03:41:11 +08:00
sequence = (char *)ckalloc(total * sizeof(char));
reverse = (char *)ckalloc(total * sizeof(char));
allconpt = allconsen = (char *)ckalloc(total * sizeof(char));
segment = (struct SEG *)ckalloc(number * sizeof(struct SEG));
2023-04-12 03:39:54 +08:00
/* read the segments into sequence */
M = 0;
Ap = ckopen(argv[1], "r");
number = -1;
heading = 0;
prev = '\n';
2023-04-12 03:41:11 +08:00
for (i = 0, k = total; (symbol = getc(Ap)) != EOF;) {
if (symbol != '\n') {
2023-04-12 03:39:54 +08:00
sequence[++i] = symbol;
2023-04-12 03:41:11 +08:00
switch (symbol) {
case 'A':
reverse[--k] = 'T';
break;
case 'a':
reverse[--k] = 't';
break;
case 'T':
reverse[--k] = 'A';
break;
case 't':
reverse[--k] = 'a';
break;
case 'C':
reverse[--k] = 'G';
break;
case 'c':
reverse[--k] = 'g';
break;
case 'G':
reverse[--k] = 'C';
break;
case 'g':
reverse[--k] = 'c';
break;
default:
reverse[--k] = symbol;
break;
2023-04-12 03:39:54 +08:00
}
}
2023-04-12 03:41:11 +08:00
if (symbol == '>' && prev == '\n') {
2023-04-12 03:39:54 +08:00
heading = 1;
2023-04-12 03:41:11 +08:00
if (number >= 0) {
2023-04-12 03:39:54 +08:00
segment[number].length = i - j - 1;
segment[number].rev = &(reverse[k]);
2023-04-12 03:41:11 +08:00
if (i - j - 1 > M) M = i - j - 1;
2023-04-12 03:39:54 +08:00
}
number++;
j = i;
segment[number].name = &(sequence[i]);
segment[number].kind = 1;
segment[number].list = 0;
}
2023-04-12 03:41:11 +08:00
if (heading && symbol == '\n') {
2023-04-12 03:39:54 +08:00
heading = 0;
segment[number].len = i - j;
segment[number].seq = &(sequence[i]);
j = i;
}
prev = symbol;
}
segment[number].length = i - j;
reverse[--k] = '>';
segment[number].rev = &(reverse[k]);
2023-04-12 03:41:11 +08:00
if (i - j > M) M = i - j;
2023-04-12 03:39:54 +08:00
seg_num = ++number;
2023-04-12 03:41:11 +08:00
(void)fclose(Ap);
2023-04-12 03:39:54 +08:00
Q = OPEN;
R = EXTEND;
R1 = LTEXTEN;
/* set match and mismatch weights */
2023-04-12 03:41:11 +08:00
for (i = 0; i < 128; i++)
for (j = 0; j < 128; j++)
if ((i | 32) == (j | 32))
2023-04-12 03:39:54 +08:00
V[i][j] = V1[i][j] = MATCH;
2023-04-12 03:41:11 +08:00
else {
2023-04-12 03:39:54 +08:00
V[i][j] = MISMAT;
V1[i][j] = LTMISM;
}
2023-04-12 03:41:11 +08:00
for (i = 0; i < 128; i++) V['N'][i] = V[i]['N'] = MISMAT + 1;
2023-04-12 03:39:54 +08:00
V1['N']['N'] = MISMAT + 1;
over_len = OVERLEN;
per_cent = PERCENT;
edge_num = 0;
INIT(M);
MAKE();
2023-04-12 03:41:11 +08:00
PAIR(V, V1, Q, R, R1);
2023-04-12 03:39:54 +08:00
ASSEM();
REPAIR();
FORM_TREE();
/* GRAPH(); */
SHOW();
}
2023-04-12 03:41:11 +08:00
static int (*v)[128]; /* substitution scores */
static int q, r; /* gap penalties */
static int qr; /* qr = q + r */
static int (*v1)[128]; /* light substitution scores */
static int r1; /* light extension penalty */
static int qr1; /* qr1 = q + r1 */
2023-04-12 03:39:54 +08:00
2023-04-12 03:41:11 +08:00
static int SCORE;
static int STARI;
static int STARJ;
static int ENDI;
static int ENDJ;
2023-04-12 03:39:54 +08:00
2023-04-12 03:41:11 +08:00
static int *CC, *DD; /* saving matrix scores */
static int *RR, *SS; /* saving start-points */
static int *S; /* saving operations for diff */
2023-04-12 03:39:54 +08:00
/* The following definitions are for function diff() */
2023-04-12 03:41:11 +08:00
int diff();
static int zero = 0; /* int type zero */
2023-04-12 03:39:54 +08:00
2023-04-12 03:41:11 +08:00
#define gap(k) ((k) <= 0 ? 0 : q + r * (k)) /* k-symbol indel score */
2023-04-12 03:39:54 +08:00
2023-04-12 03:41:11 +08:00
static int *sapp; /* Current script append ptr */
static int last; /* Last script op appended */
2023-04-12 03:39:54 +08:00
2023-04-12 03:41:11 +08:00
static int no_mat; /* number of matches */
2023-04-12 03:39:54 +08:00
2023-04-12 03:41:11 +08:00
static int no_mis; /* number of mismatches */
2023-04-12 03:39:54 +08:00
2023-04-12 03:41:11 +08:00
static int al_len; /* length of alignment */
2023-04-12 03:39:54 +08:00
/* Append "Delete k" op */
2023-04-12 03:41:11 +08:00
#define DEL(k) \
{ \
al_len += k; \
if (last < 0) \
last = sapp[-1] -= (k); \
else \
last = *sapp++ = -(k); \
}
2023-04-12 03:39:54 +08:00
/* Append "Insert k" op */
2023-04-12 03:41:11 +08:00
#define INS(k) \
{ \
al_len += k; \
if (last < 0) { \
sapp[-1] = (k); \
*sapp++ = last; \
} \
else \
last = *sapp++ = (k); \
}
2023-04-12 03:39:54 +08:00
/* Append "Replace" op */
2023-04-12 03:41:11 +08:00
#define REP \
{ \
last = *sapp++ = 0; \
al_len += 1; \
}
2023-04-12 03:39:54 +08:00
2023-04-12 03:41:11 +08:00
INIT(M) int M;
{
register int j; /* row and column indices */
char *ckalloc(); /* space-allocating function */
2023-04-12 03:39:54 +08:00
/* allocate space for all vectors */
2023-04-12 03:41:11 +08:00
j = (M + 1) * sizeof(int);
CC = (int *)ckalloc(j);
DD = (int *)ckalloc(j);
RR = (int *)ckalloc(j);
SS = (int *)ckalloc(j);
S = (int *)ckalloc(2 * j);
2023-04-12 03:39:54 +08:00
}
/* Make a lookup table for words of lengths up to TUPLELEN in each sequence.
The value of a word is used as an index to the table. */
MAKE()
2023-04-12 03:41:11 +08:00
{
int hash[TUPLELEN]; /* values of words of lengths up to TUPLELEN */
int *table; /* pointer to a lookup table */
int *loc; /* pointer to a table of sequence locations */
int size; /* size of a lookup table */
int limit, digit, step; /* temporary variables */
register int i, j, k, p, q; /* index varibles */
char *ckalloc(); /* space-allocating function */
char *A; /* pointer to a sequence */
int M; /* length of a sequence */
2023-04-12 03:39:54 +08:00
tuple = TUPLELEN - 1;
2023-04-12 03:41:11 +08:00
for (i = 0; i < 128; i++) vert[i] = 4;
2023-04-12 03:39:54 +08:00
vert['A'] = vert['a'] = 0;
vert['C'] = vert['c'] = 1;
vert['G'] = vert['g'] = 2;
vert['T'] = vert['t'] = 3;
vertc['A'] = vertc['a'] = 3;
vertc['C'] = vertc['c'] = 2;
vertc['G'] = vertc['g'] = 1;
vertc['T'] = vertc['t'] = 0;
2023-04-12 03:41:11 +08:00
for (i = 0, j = 1, size = 0; i <= tuple; i++, j *= 4) {
2023-04-12 03:39:54 +08:00
base[i] = size;
power[i] = j;
2023-04-12 03:41:11 +08:00
size = (size + 1) * 4;
2023-04-12 03:39:54 +08:00
}
2023-04-12 03:41:11 +08:00
for (j = 0; j <= tuple; j++) hash[j] = 0;
2023-04-12 03:39:54 +08:00
/* make a lookup table for each sequence */
2023-04-12 03:41:11 +08:00
for (i = 0; i < seg_num; i++) {
2023-04-12 03:39:54 +08:00
A = segment[i].seq;
M = segment[i].length;
2023-04-12 03:41:11 +08:00
table = segment[i].lookup = (int *)ckalloc(size * sizeof(int));
loc = segment[i].pos = (int *)ckalloc((M + 1) * sizeof(int));
for (j = 0; j < size; j++) table[j] = 0;
for (k = 0, j = 1; j <= M; j++)
if ((digit = vert[A[j]]) != 4) {
for (p = tuple; p > 0; p--)
hash[p] = 4 * (hash[p - 1] + 1) + digit;
2023-04-12 03:39:54 +08:00
hash[0] = digit;
step = j - k;
limit = tuple <= step ? tuple : step;
2023-04-12 03:41:11 +08:00
for (p = 0; p < limit; p++)
if (!table[(q = hash[p])]) table[q] = 1;
if (step > tuple) {
loc[(p = j - tuple)] =
table[(q = hash[tuple])];
2023-04-12 03:39:54 +08:00
table[q] = p;
}
}
else
k = j;
}
}
/* Perform pair-wise comparisons of sequences. The sequences not
satisfying the necessary condition for overlap are rejected quickly.
Those that do satisfy the condition are verified with a dynamic
programming algorithm to see if overlaps exist. */
2023-04-12 03:41:11 +08:00
PAIR(V, V1, Q, R, R1) int V[][128], V1[][128], Q, R, R1;
{
int endi, endj, stari, starj; /* endpoint and startpoint */
short orienti, orientj; /* orientation of segments */
short iscon; /* containment condition */
int score; /* the max score */
int count, limit; /* temporary variables */
register int i, j, d; /* row and column indices */
char *ckalloc(); /* space-allocating function */
int rl, cl;
2023-04-12 03:39:54 +08:00
char *A, *B;
2023-04-12 03:41:11 +08:00
int M, N;
2023-04-12 03:39:54 +08:00
overptr node1;
2023-04-12 03:41:11 +08:00
int total; /* total number of pairs */
int hit; /* number of pairs satisfying cond. */
int CHECK();
2023-04-12 03:39:54 +08:00
v = V;
q = Q;
r = R;
qr = q + r;
v1 = V1;
r1 = R1;
qr1 = q + r1;
total = hit = 0;
2023-04-12 03:41:11 +08:00
limit = 2 * (seg_num - 1);
for (orienti = 0, d = 0; d < limit; d++) {
2023-04-12 03:39:54 +08:00
i = d / 2;
orienti = 1 - orienti;
A = orienti ? segment[i].seq : segment[i].rev;
M = segment[i].length;
2023-04-12 03:41:11 +08:00
for (j = i + 1; j < seg_num; j++) {
2023-04-12 03:39:54 +08:00
B = segment[j].seq;
orientj = 1;
N = segment[j].length;
total += 1;
2023-04-12 03:41:11 +08:00
if (CHECK(orienti, i, j)) {
2023-04-12 03:39:54 +08:00
hit += 1;
SCORE = 0;
2023-04-12 03:41:11 +08:00
big_pass(A, B, M, N, orienti, orientj);
if (SCORE) {
2023-04-12 03:39:54 +08:00
score = SCORE;
stari = ++STARI;
starj = ++STARJ;
endi = ENDI;
endj = ENDJ;
rl = endi - stari + 1;
cl = endj - starj + 1;
sapp = S;
last = 0;
al_len = no_mat = no_mis = 0;
2023-04-12 03:41:11 +08:00
(void)diff(&A[stari] - 1, &B[starj] - 1,
rl, cl, q, q);
iscon = stari == 1 && endi == M ||
starj == 1 && endj == N;
if (no_mat >= al_len * per_cent &&
(al_len >= over_len || iscon)) {
node1 = (overptr)ckalloc(
(int)sizeof(over));
if (iscon)
node1->kind =
0; /* containment */
else {
node1->kind = 1;
edge_num++;
} /* overlap */
if (endi == M &&
(endj != N ||
starj != 1)) /*i is 5'*/
{
2023-04-12 03:39:54 +08:00
node1->number = j;
node1->host = i;
node1->stari = stari;
node1->endi = endi;
2023-04-12 03:41:11 +08:00
node1->orienti =
orienti;
2023-04-12 03:39:54 +08:00
node1->starj = starj;
node1->endj = endj;
2023-04-12 03:41:11 +08:00
node1->orientj =
orientj;
2023-04-12 03:39:54 +08:00
}
2023-04-12 03:41:11 +08:00
else /* j is 5' */
{
2023-04-12 03:39:54 +08:00
node1->number = i;
node1->host = j;
node1->stari = starj;
node1->endi = endj;
2023-04-12 03:41:11 +08:00
node1->orienti =
orientj;
2023-04-12 03:39:54 +08:00
node1->starj = stari;
node1->endj = endi;
2023-04-12 03:41:11 +08:00
node1->orientj =
orienti;
2023-04-12 03:39:54 +08:00
}
node1->score = score;
node1->length = al_len;
node1->match = no_mat;
2023-04-12 03:41:11 +08:00
count =
node1->number == i ? j : i;
node1->next =
segment[count].list;
2023-04-12 03:39:54 +08:00
segment[count].list = node1;
2023-04-12 03:41:11 +08:00
if (!node1->kind)
2023-04-12 03:39:54 +08:00
segment[count].kind = 0;
}
}
}
}
}
}
/* Return 1 if two sequences satisfy the necessary condition for overlap,
and 0 otherwise. Parameters first and second are the indices of segments,
and parameter orient indicates the orientation of segment first. */
2023-04-12 03:41:11 +08:00
int CHECK(orient, first, second)
short orient;
int first, second;
{
int limit, bound; /* maximum number of jumps */
int small; /* smaller of limit and bound */
float delta; /* cutoff factor */
float cut; /* cutoff score */
register int i; /* index variable */
int t, q; /* temporary variables */
int JUMP();
int RJUMP();
int JUMPC();
int RJUMPC();
2023-04-12 03:39:54 +08:00
delta = DELTA;
2023-04-12 03:41:11 +08:00
if (orient)
2023-04-12 03:39:54 +08:00
limit = JUMP(CC, first, second, 1);
else
limit = JUMPC(CC, first, second);
bound = RJUMP(DD, second, first, orient);
small = limit <= bound ? limit : bound;
cut = 0;
2023-04-12 03:41:11 +08:00
for (i = 1; i <= small; i++) {
if ((t = DD[i] - 1) >= over_len && t >= cut &&
(q = CC[i] - 1) >= over_len && q >= cut)
2023-04-12 03:39:54 +08:00
return (1);
cut += delta;
}
2023-04-12 03:41:11 +08:00
if (DD[bound] >= delta * (bound - 1) ||
CC[limit] >= delta * (limit - 1))
2023-04-12 03:39:54 +08:00
return (1);
limit = JUMP(CC, second, first, orient);
2023-04-12 03:41:11 +08:00
if (orient)
2023-04-12 03:39:54 +08:00
bound = RJUMP(DD, first, second, 1);
else
bound = RJUMPC(DD, first, second);
small = limit <= bound ? limit : bound;
cut = 0;
2023-04-12 03:41:11 +08:00
for (i = 1; i <= small; i++) {
if ((t = DD[i] - 1) >= over_len && t >= cut &&
(q = CC[i] - 1) >= over_len && q >= cut)
2023-04-12 03:39:54 +08:00
return (1);
cut += delta;
}
return (0);
}
/* Compute a vector of lengths of jumps */
2023-04-12 03:41:11 +08:00
int JUMP(H, one, two, orient)
int H[], one, two;
2023-04-12 03:39:54 +08:00
short orient;
2023-04-12 03:41:11 +08:00
{
char *A, *B; /* pointers to two sequences */
int M, N; /* lengths of two sequences */
int *table; /* pointer to a lookup table */
int *loc; /* pointer to a location table */
int value; /* value of a word */
int maxd; /* maximum length of an identical diagonal */
int d; /* length of current identical diagonal */
int s; /* length of jumps */
int k; /* number of jumps */
register int i, j, p; /* index variables */
2023-04-12 03:39:54 +08:00
A = segment[one].seq;
M = segment[one].length;
table = segment[one].lookup;
loc = segment[one].pos;
B = orient ? segment[two].seq : segment[two].rev;
N = segment[two].length;
2023-04-12 03:41:11 +08:00
for (s = 1, k = 1; s <= N; k++) {
2023-04-12 03:39:54 +08:00
maxd = 0;
2023-04-12 03:41:11 +08:00
for (value = -1, d = 0, j = s; d <= tuple && j <= N; j++, d++) {
if (vert[B[j]] == 4) break;
2023-04-12 03:39:54 +08:00
value = 4 * (value + 1) + vert[B[j]];
2023-04-12 03:41:11 +08:00
if (!table[value]) break;
2023-04-12 03:39:54 +08:00
}
2023-04-12 03:41:11 +08:00
if (d > tuple) {
for (p = table[value]; p; p = loc[p]) {
2023-04-12 03:39:54 +08:00
d = tuple + 1;
2023-04-12 03:41:11 +08:00
for (i = p + d, j = s + d; i <= M && j <= N;
i++, j++, d++)
if (A[i] != B[j] && vert[A[i]] != 4 &&
vert[B[j]] != 4)
break;
if (maxd < d) maxd = d;
if (j > N) break;
2023-04-12 03:39:54 +08:00
}
}
2023-04-12 03:41:11 +08:00
else
2023-04-12 03:39:54 +08:00
maxd = d;
s += maxd + 1;
H[k] = s;
}
return (k - 1);
}
/* Compute a vector of lengths of jumps for reverse complement of one */
2023-04-12 03:41:11 +08:00
int JUMPC(H, one, two)
int H[], one, two;
{
char *A, *B; /* pointers to two sequences */
int M, N; /* lengths of two sequences */
int *table; /* pointer to a lookup table */
int *loc; /* pointer to a location table */
int value; /* value of a word */
int maxd; /* maximum length of an identical diagonal */
int d; /* length of current identical diagonal */
int s; /* length of jumps */
int k; /* number of jumps */
register int i, j, p; /* index variables */
2023-04-12 03:39:54 +08:00
A = segment[one].rev;
M = segment[one].length;
table = segment[one].lookup;
loc = segment[one].pos;
B = segment[two].seq;
N = segment[two].length;
2023-04-12 03:41:11 +08:00
for (s = 1, k = 1; s <= N; k++) {
2023-04-12 03:39:54 +08:00
maxd = 0;
2023-04-12 03:41:11 +08:00
for (value = 0, d = 0, j = s; d <= tuple && j <= N; j++, d++) {
if (vert[B[j]] == 4) break;
2023-04-12 03:39:54 +08:00
value += vertc[B[j]] * power[d];
2023-04-12 03:41:11 +08:00
if (!table[value + base[d]]) break;
2023-04-12 03:39:54 +08:00
}
2023-04-12 03:41:11 +08:00
if (d > tuple) {
for (p = table[value + base[tuple]]; p; p = loc[p]) {
2023-04-12 03:39:54 +08:00
d = tuple + 1;
2023-04-12 03:41:11 +08:00
for (i = M + 2 - p, j = s + d; i <= M && j <= N;
i++, j++, d++)
if (A[i] != B[j] && vert[A[i]] != 4 &&
vert[B[j]] != 4)
break;
if (maxd < d) maxd = d;
if (j > N) break;
2023-04-12 03:39:54 +08:00
}
}
2023-04-12 03:41:11 +08:00
else
2023-04-12 03:39:54 +08:00
maxd = d;
s += maxd + 1;
H[k] = s;
}
return (k - 1);
}
/* Compute a vector of lengths of reverse jumps */
2023-04-12 03:41:11 +08:00
int RJUMP(H, one, two, orient)
int H[], one, two;
2023-04-12 03:39:54 +08:00
short orient;
2023-04-12 03:41:11 +08:00
{
char *A, *B; /* pointers to two sequences */
int N; /* length of a sequence */
int *table; /* pointer to a lookup table */
int *loc; /* pointer to a location table */
int value; /* value of a word */
int maxd; /* maximum length of an identical diagonal */
int d; /* length of current identical diagonal */
int s; /* length of jumps */
int k; /* number of jumps */
register int i, j, p; /* index variables */
2023-04-12 03:39:54 +08:00
A = segment[one].seq;
table = segment[one].lookup;
loc = segment[one].pos;
B = orient ? segment[two].seq : segment[two].rev;
N = segment[two].length;
2023-04-12 03:41:11 +08:00
for (s = 1, k = 1; s <= N; k++) {
2023-04-12 03:39:54 +08:00
maxd = 0;
2023-04-12 03:41:11 +08:00
for (value = 0, d = 0, j = N + 1 - s; d <= tuple && j >= 1;
j--, d++) {
if (vert[B[j]] == 4) break;
2023-04-12 03:39:54 +08:00
value += vert[B[j]] * power[d];
2023-04-12 03:41:11 +08:00
if (!table[value + base[d]]) break;
2023-04-12 03:39:54 +08:00
}
2023-04-12 03:41:11 +08:00
if (d > tuple) {
for (p = table[value + base[tuple]]; p; p = loc[p]) {
2023-04-12 03:39:54 +08:00
d = tuple + 1;
2023-04-12 03:41:11 +08:00
for (i = p - 1, j = N + 1 - s - d;
i >= 1 && j >= 1; i--, j--, d++)
if (A[i] != B[j] && vert[A[i]] != 4 &&
vert[B[j]] != 4)
break;
if (maxd < d) maxd = d;
if (j < 1) break;
2023-04-12 03:39:54 +08:00
}
}
2023-04-12 03:41:11 +08:00
else
2023-04-12 03:39:54 +08:00
maxd = d;
s += maxd + 1;
H[k] = s;
}
return (k - 1);
}
/* Compute a vector of lengths of reverse jumps for reverse complement */
2023-04-12 03:41:11 +08:00
int RJUMPC(H, one, two)
int H[], one, two;
{
char *A, *B; /* pointers to two sequences */
int M, N; /* lengths of two sequences */
int *table; /* pointer to a lookup table */
int *loc; /* pointer to a location table */
int value; /* value of a word */
int maxd; /* maximum length of an identical diagonal */
int d; /* length of current identical diagonal */
int s; /* length of jumps */
int k; /* number of jumps */
register int i, j, p; /* index variables */
2023-04-12 03:39:54 +08:00
A = segment[one].rev;
M = segment[one].length;
table = segment[one].lookup;
loc = segment[one].pos;
B = segment[two].seq;
N = segment[two].length;
2023-04-12 03:41:11 +08:00
for (s = 1, k = 1; s <= N; k++) {
2023-04-12 03:39:54 +08:00
maxd = 0;
2023-04-12 03:41:11 +08:00
for (value = -1, d = 0, j = N + 1 - s; d <= tuple && j >= 1;
j--, d++) {
if (vert[B[j]] == 4) break;
2023-04-12 03:39:54 +08:00
value = 4 * (value + 1) + vertc[B[j]];
2023-04-12 03:41:11 +08:00
if (!table[value]) break;
2023-04-12 03:39:54 +08:00
}
2023-04-12 03:41:11 +08:00
if (d > tuple) {
for (p = table[value]; p; p = loc[p]) {
2023-04-12 03:39:54 +08:00
d = tuple + 1;
i = M - p - tuple;
2023-04-12 03:41:11 +08:00
for (j = N - s - tuple; i >= 1 && j >= 1;
i--, j--, d++)
if (A[i] != B[j] && vert[A[i]] != 4 &&
vert[B[j]] != 4)
break;
if (maxd < d) maxd = d;
if (j < 1) break;
2023-04-12 03:39:54 +08:00
}
}
2023-04-12 03:41:11 +08:00
else
2023-04-12 03:39:54 +08:00
maxd = d;
s += maxd + 1;
H[k] = s;
}
return (k - 1);
}
/* Construct contigs */
ASSEM()
2023-04-12 03:41:11 +08:00
{
char *ckalloc(); /* space-allocating function */
register int i, j, k; /* index variables */
overptr node1, x, y; /* temporary pointer */
int five, three; /* indices of 5' and 3' segments */
short orienti; /* orientation of 5' segment */
short orientj; /* orientation of 3' segment */
short sorted; /* boolean variable */
contigs = (struct CONS *)ckalloc(seg_num * sizeof(struct CONS));
for (i = 0; i < seg_num; i++) {
2023-04-12 03:39:54 +08:00
contigs[i].isfive[0] = contigs[i].isthree[0] = 1;
contigs[i].isfive[1] = contigs[i].isthree[1] = 1;
contigs[i].other = i;
contigs[i].group = contigs[i].child = -1;
contigs[i].brother = contigs[i].father = -1;
}
2023-04-12 03:41:11 +08:00
for (i = 0; i < seg_num; i++)
if (!segment[i].kind)
for (;;) {
for (y = segment[i].list; y->kind; y = y->next)
2023-04-12 03:39:54 +08:00
;
2023-04-12 03:41:11 +08:00
for (x = y->next; x != 0; x = x->next)
if (!x->kind && x->score > y->score)
2023-04-12 03:39:54 +08:00
y = x;
2023-04-12 03:41:11 +08:00
for (j = y->number;
(k = contigs[j].father) != -1; j = k)
2023-04-12 03:39:54 +08:00
;
2023-04-12 03:41:11 +08:00
if (j != i) {
2023-04-12 03:39:54 +08:00
contigs[i].father = j = y->number;
contigs[i].brother = contigs[j].child;
contigs[j].child = i;
contigs[i].node[1] = y;
break;
}
2023-04-12 03:41:11 +08:00
else {
if (segment[i].list->number ==
y->number)
2023-04-12 03:39:54 +08:00
segment[i].list = y->next;
2023-04-12 03:41:11 +08:00
else {
for (x = segment[i].list;
x->next->number !=
y->number;)
2023-04-12 03:39:54 +08:00
x = x->next;
x->next = y->next;
}
2023-04-12 03:41:11 +08:00
for (x = segment[i].list;
x != 0 && x->kind; x = x->next)
2023-04-12 03:39:54 +08:00
;
2023-04-12 03:41:11 +08:00
if (x == 0) {
2023-04-12 03:39:54 +08:00
segment[i].kind = 1;
break;
}
}
}
2023-04-12 03:41:11 +08:00
edge = (overptr *)ckalloc(edge_num * sizeof(overptr));
for (j = 0, i = 0; i < seg_num; i++)
if (segment[i].kind)
for (node1 = segment[i].list; node1 != 0;
node1 = node1->next)
if (segment[node1->number].kind)
2023-04-12 03:39:54 +08:00
edge[j++] = node1;
edge_num = j;
2023-04-12 03:41:11 +08:00
for (i = edge_num - 1; i > 0; i--) {
2023-04-12 03:39:54 +08:00
sorted = 1;
2023-04-12 03:41:11 +08:00
for (j = 0; j < i; j++)
if (edge[j]->score < edge[j + 1]->score) {
2023-04-12 03:39:54 +08:00
node1 = edge[j];
2023-04-12 03:41:11 +08:00
edge[j] = edge[j + 1];
edge[j + 1] = node1;
2023-04-12 03:39:54 +08:00
sorted = 0;
}
2023-04-12 03:41:11 +08:00
if (sorted) break;
2023-04-12 03:39:54 +08:00
}
2023-04-12 03:41:11 +08:00
for (k = 0; k < edge_num; k++) {
2023-04-12 03:39:54 +08:00
five = edge[k]->host;
three = edge[k]->number;
orienti = edge[k]->orienti;
orientj = edge[k]->orientj;
2023-04-12 03:41:11 +08:00
if (contigs[five].isthree[orienti] &&
contigs[three].isfive[orientj] &&
contigs[five].other != three) {
2023-04-12 03:39:54 +08:00
contigs[five].isthree[orienti] = 0;
contigs[three].isfive[orientj] = 0;
contigs[five].next[orienti] = three;
contigs[five].orient[orienti] = orientj;
contigs[five].node[orienti] = edge[k];
contigs[three].isthree[(j = 1 - orientj)] = 0;
contigs[five].isfive[(i = 1 - orienti)] = 0;
contigs[three].next[j] = five;
contigs[three].orient[j] = i;
contigs[three].node[j] = edge[k];
i = contigs[three].other;
j = contigs[five].other;
contigs[i].other = j;
contigs[j].other = i;
}
}
}
REPAIR()
2023-04-12 03:41:11 +08:00
{
int endi, endj, stari, starj; /* endpoint and startpoint */
short orienti, orientj; /* orientation of segments */
short isconi, isconj; /* containment condition */
int score; /* the max score */
int i, j, f, d, e; /* row and column indices */
char *ckalloc(); /* space-allocating function */
2023-04-12 03:39:54 +08:00
char *A, *B;
2023-04-12 03:41:11 +08:00
int M, N;
2023-04-12 03:39:54 +08:00
overptr node1;
2023-04-12 03:41:11 +08:00
int piece_num; /* The number of pieces */
int count, limit;
int number;
int hit;
piece = (struct VX *)ckalloc(seg_num * sizeof(struct VX));
for (j = 0, i = 0; i < seg_num; i++)
if (segment[i].kind &&
(contigs[i].isfive[1] || contigs[i].isfive[0]))
2023-04-12 03:39:54 +08:00
piece[j++].id = i;
piece_num = j;
2023-04-12 03:41:11 +08:00
for (i = 0; i < piece_num; i++) {
2023-04-12 03:39:54 +08:00
piece[i].kind = 1;
piece[i].list = 0;
}
2023-04-12 03:41:11 +08:00
limit = 2 * (piece_num - 1);
2023-04-12 03:39:54 +08:00
hit = number = 0;
2023-04-12 03:41:11 +08:00
for (orienti = 0, d = 0; d < limit; d++) {
2023-04-12 03:39:54 +08:00
i = piece[(e = d / 2)].id;
orienti = 1 - orienti;
A = orienti ? segment[i].seq : segment[i].rev;
M = segment[i].length;
2023-04-12 03:41:11 +08:00
for (f = e + 1; f < piece_num; f++) {
2023-04-12 03:39:54 +08:00
j = piece[f].id;
B = segment[j].seq;
orientj = 1;
N = segment[j].length;
SCORE = 0;
hit++;
2023-04-12 03:41:11 +08:00
big_pass(A, B, M, N, orienti, orientj);
if (SCORE > CUTOFF) {
2023-04-12 03:39:54 +08:00
score = SCORE;
stari = ++STARI;
starj = ++STARJ;
endi = ENDI;
endj = ENDJ;
isconi = stari == 1 && endi == M;
isconj = starj == 1 && endj == N;
2023-04-12 03:41:11 +08:00
node1 = (overptr)ckalloc((int)sizeof(over));
if (isconi || isconj)
node1->kind = 0; /* containment */
else {
node1->kind = 1;
number++;
} /* overlap */
if (endi == M && !isconj) /*i is 5'*/
{
2023-04-12 03:39:54 +08:00
node1->number = j;
node1->host = i;
node1->ind = f;
node1->stari = stari;
node1->endi = endi;
node1->orienti = orienti;
node1->starj = starj;
node1->endj = endj;
node1->orientj = orientj;
}
2023-04-12 03:41:11 +08:00
else /* j is 5' */
{
2023-04-12 03:39:54 +08:00
node1->number = i;
node1->host = j;
node1->ind = e;
node1->stari = starj;
node1->endi = endj;
node1->orienti = orientj;
node1->starj = stari;
node1->endj = endi;
node1->orientj = orienti;
}
node1->score = score;
count = node1->number == i ? f : e;
node1->next = piece[count].list;
piece[count].list = node1;
2023-04-12 03:41:11 +08:00
if (!node1->kind) piece[count].kind = 0;
2023-04-12 03:39:54 +08:00
}
}
}
REASSEM(piece_num, number);
}
/* Construct contigs */
2023-04-12 03:41:11 +08:00
REASSEM(piece_num, number) int piece_num, number;
{
char *ckalloc(); /* space-allocating function */
int i, j, k, d; /* index variables */
overptr node1, x, y; /* temporary pointer */
int five, three; /* indices of 5' and 3' segments */
short orienti; /* orientation of 5' segment */
short orientj; /* orientation of 3' segment */
short sorted; /* boolean variable */
for (d = 0; d < piece_num; d++)
if (!piece[d].kind)
for (i = piece[d].id;;) {
for (y = piece[d].list; y->kind; y = y->next)
2023-04-12 03:39:54 +08:00
;
2023-04-12 03:41:11 +08:00
for (x = y->next; x != 0; x = x->next)
if (!x->kind && x->score > y->score)
2023-04-12 03:39:54 +08:00
y = x;
2023-04-12 03:41:11 +08:00
for (j = y->number;
(k = contigs[j].father) != -1; j = k)
2023-04-12 03:39:54 +08:00
;
2023-04-12 03:41:11 +08:00
if (j != i &&
RECONCILE(y, &piece_num, &number)) {
2023-04-12 03:39:54 +08:00
contigs[i].father = j = y->number;
contigs[i].brother = contigs[j].child;
contigs[j].child = i;
contigs[i].node[1] = y;
segment[i].kind = 0;
break;
}
2023-04-12 03:41:11 +08:00
else {
if (piece[d].list->number == y->number)
2023-04-12 03:39:54 +08:00
piece[d].list = y->next;
2023-04-12 03:41:11 +08:00
else {
for (x = piece[d].list;
x->next->number !=
y->number;)
2023-04-12 03:39:54 +08:00
x = x->next;
x->next = y->next;
}
2023-04-12 03:41:11 +08:00
for (x = piece[d].list;
x != 0 && x->kind; x = x->next)
2023-04-12 03:39:54 +08:00
;
2023-04-12 03:41:11 +08:00
if (x == 0) {
2023-04-12 03:39:54 +08:00
piece[d].kind = 1;
break;
}
}
}
2023-04-12 03:41:11 +08:00
if (number > edge_num)
edge = (overptr *)ckalloc(number * sizeof(overptr));
for (j = 0, d = 0; d < piece_num; d++)
if (piece[d].kind)
for (node1 = piece[d].list; node1 != 0;
node1 = node1->next)
if (piece[node1->ind].kind) edge[j++] = node1;
2023-04-12 03:39:54 +08:00
edge_num = j;
2023-04-12 03:41:11 +08:00
for (i = edge_num - 1; i > 0; i--) {
2023-04-12 03:39:54 +08:00
sorted = 1;
2023-04-12 03:41:11 +08:00
for (j = 0; j < i; j++)
if (edge[j]->score < edge[j + 1]->score) {
2023-04-12 03:39:54 +08:00
node1 = edge[j];
2023-04-12 03:41:11 +08:00
edge[j] = edge[j + 1];
edge[j + 1] = node1;
2023-04-12 03:39:54 +08:00
sorted = 0;
}
2023-04-12 03:41:11 +08:00
if (sorted) break;
2023-04-12 03:39:54 +08:00
}
2023-04-12 03:41:11 +08:00
for (k = 0; k < edge_num; k++) {
2023-04-12 03:39:54 +08:00
five = edge[k]->host;
three = edge[k]->number;
orienti = edge[k]->orienti;
orientj = edge[k]->orientj;
2023-04-12 03:41:11 +08:00
if (contigs[five].isthree[orienti] &&
contigs[three].isfive[orientj] &&
contigs[five].other != three) {
2023-04-12 03:39:54 +08:00
contigs[five].isthree[orienti] = 0;
contigs[three].isfive[orientj] = 0;
contigs[five].next[orienti] = three;
contigs[five].orient[orienti] = orientj;
contigs[five].node[orienti] = edge[k];
contigs[three].isthree[(j = 1 - orientj)] = 0;
contigs[five].isfive[(i = 1 - orienti)] = 0;
contigs[three].next[j] = five;
contigs[three].orient[j] = i;
contigs[three].node[j] = edge[k];
i = contigs[three].other;
j = contigs[five].other;
contigs[i].other = j;
contigs[j].other = i;
}
}
}
2023-04-12 03:41:11 +08:00
RECONCILE(y, pp, nn) overptr y;
int *pp, *nn;
{
short orienti, orientj; /* orientation of segments */
short orientk, orientd; /* orientation of segments */
int i, j, k, d, f; /* row and column indices */
char *ckalloc(); /* space-allocating function */
2023-04-12 03:39:54 +08:00
char *A, *B;
2023-04-12 03:41:11 +08:00
int M, N;
2023-04-12 03:39:54 +08:00
overptr node1;
k = y->host;
d = y->number;
orientk = y->orienti;
orientd = y->orientj;
2023-04-12 03:41:11 +08:00
if (!contigs[k].isthree[orientk]) {
if (!piece[y->ind].kind) return (0);
if (contigs[d].isthree[orientd]) {
2023-04-12 03:39:54 +08:00
orienti = orientd;
i = d;
orientj = contigs[k].orient[orientk];
j = contigs[k].next[orientk];
}
else
return (0);
}
2023-04-12 03:41:11 +08:00
else if (!contigs[k].isfive[orientk]) {
if (!piece[y->ind].kind) return (0);
if (contigs[d].isfive[orientd]) {
orienti = contigs[k].orient[1 - orientk];
orienti = 1 - orienti;
i = contigs[k].next[1 - orientk];
orientj = orientd;
j = d;
2023-04-12 03:39:54 +08:00
}
else
return (0);
2023-04-12 03:41:11 +08:00
}
else
return (0);
2023-04-12 03:39:54 +08:00
A = orienti ? segment[i].seq : segment[i].rev;
M = segment[i].length;
B = orientj ? segment[j].seq : segment[j].rev;
N = segment[j].length;
SCORE = 0;
2023-04-12 03:41:11 +08:00
big_pass(A, B, M, N, orienti, orientj);
if (SCORE > CUTOFF && ENDI - STARI > over_len && ENDI == M &&
STARJ == 0) {
node1 = (overptr)ckalloc((int)sizeof(over));
2023-04-12 03:39:54 +08:00
node1->kind = 1;
node1->host = i;
node1->number = j;
node1->stari = ++STARI;
node1->endi = ENDI;
node1->orienti = orienti;
node1->starj = ++STARJ;
node1->endj = ENDJ;
node1->orientj = orientj;
node1->score = SCORE;
piece[*pp].kind = 1;
2023-04-12 03:41:11 +08:00
if (i == d) {
2023-04-12 03:39:54 +08:00
node1->ind = *pp;
node1->next = piece[y->ind].list;
piece[y->ind].list = node1;
piece[*pp].id = j;
piece[*pp].list = 0;
}
2023-04-12 03:41:11 +08:00
else {
2023-04-12 03:39:54 +08:00
node1->ind = y->ind;
piece[*pp].list = node1;
node1->next = 0;
piece[*pp].id = i;
}
(*nn)++;
(*pp)++;
f = contigs[k].other;
2023-04-12 03:41:11 +08:00
if (!contigs[k].isthree[orientk]) {
2023-04-12 03:39:54 +08:00
contigs[j].isfive[orientj] = 1;
contigs[j].isthree[1 - orientj] = 1;
contigs[k].isthree[orientk] = 1;
contigs[k].isfive[1 - orientk] = 1;
contigs[f].other = j;
contigs[j].other = f;
}
2023-04-12 03:41:11 +08:00
else {
2023-04-12 03:39:54 +08:00
contigs[i].isthree[orienti] = 1;
contigs[i].isfive[1 - orienti] = 1;
contigs[k].isfive[orientk] = 1;
contigs[k].isthree[1 - orientk] = 1;
contigs[f].other = i;
contigs[i].other = f;
}
contigs[k].other = k;
return (1);
}
return (0);
}
/* Construct a tree of overlapping-containment segments */
FORM_TREE()
2023-04-12 03:41:11 +08:00
{
register int i, j, k; /* index variables */
char *ckalloc(); /* space-allocating function */
overptr node1; /* temporary pointer */
short orient; /* orientation of segment */
int group; /* serial number of contigs */
char *A, *B; /* pointers to segment sequences */
int stari, endi, starj, endj; /* positions where alignment begins */
int M, N; /* lengths of segment sequences */
int count; /* temporary variables */
mtree = (struct TTREE *)ckalloc(seg_num * sizeof(struct TTREE));
for (i = 0; i < seg_num; i++) {
2023-04-12 03:39:54 +08:00
mtree[i].head = 0;
mtree[i].next = mtree[i].child = mtree[i].brother = -1;
}
2023-04-12 03:41:11 +08:00
for (group = 0, i = 0; i < seg_num; i++)
if (segment[i].kind && contigs[i].group < 0 &&
(contigs[i].isfive[1] || contigs[i].isfive[0])) {
2023-04-12 03:39:54 +08:00
orient = contigs[i].isfive[1] ? 1 : 0;
mtree[i].head = 1;
2023-04-12 03:41:11 +08:00
for (j = i;;) {
2023-04-12 03:39:54 +08:00
contigs[j].group = group;
mtree[j].orient = orient;
SORT(j, orient);
2023-04-12 03:41:11 +08:00
if (contigs[j].isthree[orient])
2023-04-12 03:39:54 +08:00
break;
2023-04-12 03:41:11 +08:00
else {
2023-04-12 03:39:54 +08:00
k = contigs[j].next[orient];
node1 = contigs[j].node[orient];
2023-04-12 03:41:11 +08:00
if (j == node1->host) {
2023-04-12 03:39:54 +08:00
stari = node1->stari;
2023-04-12 03:41:11 +08:00
endi = node1->endi;
2023-04-12 03:39:54 +08:00
starj = node1->starj;
2023-04-12 03:41:11 +08:00
endj = node1->endj;
A = node1->orienti
? segment[j].seq
: segment[j].rev;
B = node1->orientj
? segment[k].seq
: segment[k].rev;
2023-04-12 03:39:54 +08:00
}
2023-04-12 03:41:11 +08:00
else {
2023-04-12 03:39:54 +08:00
M = segment[j].length;
stari = M + 1 - node1->endj;
endi = M + 1 - node1->starj;
N = segment[k].length;
starj = N + 1 - node1->endi;
endj = N + 1 - node1->stari;
2023-04-12 03:41:11 +08:00
A = node1->orientj
? segment[j].rev
: segment[j].seq;
B = node1->orienti
? segment[k].rev
: segment[k].seq;
2023-04-12 03:39:54 +08:00
}
M = endi - stari + 1;
N = endj - starj + 1;
sapp = S;
last = 0;
al_len = no_mat = no_mis = 0;
2023-04-12 03:41:11 +08:00
(void)diff(&A[stari] - 1, &B[starj] - 1,
M, N, q, q);
count =
((N = sapp - S) + 1) * sizeof(int);
mtree[k].script = (int *)ckalloc(count);
for (M = 0; M < N; M++)
2023-04-12 03:39:54 +08:00
mtree[k].script[M] = S[M];
mtree[k].size = N;
mtree[k].begin = stari;
mtree[j].next = k;
orient = contigs[j].orient[orient];
j = k;
}
}
group++;
}
}
/* Sort the children of each node by the `begin' field */
2023-04-12 03:41:11 +08:00
SORT(seg, ort) int seg;
2023-04-12 03:39:54 +08:00
short ort;
2023-04-12 03:41:11 +08:00
{
register int i, j, k; /* index variables */
char *ckalloc(); /* space-allocating function */
overptr node1; /* temporary pointer */
short orient; /* orientation of segment */
char *A, *B; /* pointers to segment sequences */
int stari, endi, starj, endj; /* positions where alignment begins */
int M, N; /* lengths of segment sequences */
int count; /* temporary variables */
for (j = contigs[seg].child; j != -1; j = contigs[j].brother) {
2023-04-12 03:39:54 +08:00
node1 = contigs[j].node[1];
2023-04-12 03:41:11 +08:00
if (ort == node1->orientj) {
2023-04-12 03:39:54 +08:00
stari = node1->starj;
2023-04-12 03:41:11 +08:00
endi = node1->endj;
2023-04-12 03:39:54 +08:00
starj = node1->stari;
2023-04-12 03:41:11 +08:00
endj = node1->endi;
A = node1->orientj ? segment[seg].seq
: segment[seg].rev;
2023-04-12 03:39:54 +08:00
B = node1->orienti ? segment[j].seq : segment[j].rev;
orient = node1->orienti;
}
2023-04-12 03:41:11 +08:00
else {
2023-04-12 03:39:54 +08:00
M = segment[seg].length;
stari = M + 1 - node1->endj;
endi = M + 1 - node1->starj;
N = segment[j].length;
starj = N + 1 - node1->endi;
endj = N + 1 - node1->stari;
2023-04-12 03:41:11 +08:00
A = node1->orientj ? segment[seg].rev
: segment[seg].seq;
2023-04-12 03:39:54 +08:00
B = node1->orienti ? segment[j].rev : segment[j].seq;
2023-04-12 03:41:11 +08:00
orient = 1 - node1->orienti;
2023-04-12 03:39:54 +08:00
}
M = endi - stari + 1;
N = endj - starj + 1;
sapp = S;
last = 0;
al_len = no_mat = no_mis = 0;
2023-04-12 03:41:11 +08:00
(void)diff(&A[stari] - 1, &B[starj] - 1, M, N, q, q);
count = ((M = sapp - S) + 1) * sizeof(int);
mtree[j].script = (int *)ckalloc(count);
for (k = 0; k < M; k++) mtree[j].script[k] = S[k];
2023-04-12 03:39:54 +08:00
mtree[j].size = M;
mtree[j].begin = stari;
mtree[j].orient = orient;
2023-04-12 03:41:11 +08:00
if (mtree[seg].child == -1)
2023-04-12 03:39:54 +08:00
mtree[seg].child = j;
2023-04-12 03:41:11 +08:00
else {
2023-04-12 03:39:54 +08:00
i = mtree[seg].child;
2023-04-12 03:41:11 +08:00
if (mtree[i].begin >= stari) {
2023-04-12 03:39:54 +08:00
mtree[j].brother = i;
mtree[seg].child = j;
}
2023-04-12 03:41:11 +08:00
else {
2023-04-12 03:39:54 +08:00
M = mtree[i].brother;
2023-04-12 03:41:11 +08:00
for (; M != -1; i = M, M = mtree[M].brother)
if (mtree[M].begin >= stari) break;
2023-04-12 03:39:54 +08:00
mtree[j].brother = M;
mtree[i].brother = j;
}
}
SORT(j, orient);
}
}
/* Display the alignments of segments */
SHOW()
2023-04-12 03:41:11 +08:00
{
register int i, j, k; /* index variables */
char *ckalloc(); /* space-allocating function */
int n; /* number of working segments */
int limit; /* number of slots in work */
int col; /* number of output columns prepared */
short done; /* tells if current group is done */
rowptr root; /* pointer to root of op tree */
int sym[6]; /* occurrance counts for six chars */
char c; /* temp variable */
rowptr t, w, yy; /* temp pointer */
int x; /* temp variables */
int group; /* Contigs number */
char conlit[20], *a; /* String form of contig number */
char *spt; /* pointer to the start of consensus */
work = (rowptr *)ckalloc(seg_num * sizeof(rowptr));
2023-04-12 03:39:54 +08:00
group = 0;
yy = 0;
2023-04-12 03:41:11 +08:00
for (j = 0; j < 6; j++) sym[j] = 0;
2023-04-12 03:39:54 +08:00
n = limit = col = 0;
2023-04-12 03:41:11 +08:00
for (i = 0; i < seg_num; i++)
if (mtree[i].head) {
(void)sprintf(conlit, ">Contig %d\n", group);
for (a = conlit; *a;) *allconpt++ = *a++;
2023-04-12 03:39:54 +08:00
/* Mod by S.S.
(void) printf("\n#Contig %d\n\n", group++);
*/
group++;
done = 0;
ENTER(&limit, &n, i, col, yy);
root = work[0];
spt = allconpt;
2023-04-12 03:41:11 +08:00
while (!done) {
for (j = 0; j < n;
j++) /* get segments into work */
{
2023-04-12 03:39:54 +08:00
t = work[j];
k = t->id;
2023-04-12 03:41:11 +08:00
if ((x = mtree[k].next) != -1 &&
mtree[x].begin == t->loc) {
2023-04-12 03:39:54 +08:00
ENTER(&limit, &n, x, col, t);
mtree[k].next = -1;
}
2023-04-12 03:41:11 +08:00
for (x = mtree[k].child; x != -1;
x = mtree[x].brother)
if (mtree[x].begin == t->loc) {
ENTER(&limit, &n, x,
col, t);
mtree[k].child =
mtree[x].brother;
2023-04-12 03:39:54 +08:00
}
else
break;
}
2023-04-12 03:41:11 +08:00
COLUMN(root); /* determine next column */
2023-04-12 03:39:54 +08:00
root->c = root->kind;
2023-04-12 03:41:11 +08:00
for (t = head; t != 0; t = t->link)
2023-04-12 03:39:54 +08:00
t->c = t->kind;
2023-04-12 03:41:11 +08:00
for (j = 0; j < n; j++) {
2023-04-12 03:39:54 +08:00
t = work[j];
2023-04-12 03:41:11 +08:00
if (t->done)
2023-04-12 03:39:54 +08:00
*t->a++ = ' ';
2023-04-12 03:41:11 +08:00
else {
if (t->c == 'L') {
if (t->loc == 1)
t->offset =
allconpt -
spt;
c = *t->a++ =
t->seq[t->loc++];
2023-04-12 03:39:54 +08:00
}
2023-04-12 03:41:11 +08:00
else if (t->loc > 1)
c = *t->a++ = '-';
2023-04-12 03:39:54 +08:00
else
2023-04-12 03:41:11 +08:00
c = *t->a++ = ' ';
if (c != ' ')
if (c == '-')
2023-04-12 03:39:54 +08:00
sym[5] += 1;
else
2023-04-12 03:41:11 +08:00
sym[vert[c]] +=
1;
2023-04-12 03:39:54 +08:00
t->c = ' ';
}
}
/* determine consensus char */
k = sym[0] + sym[1] + sym[2] + sym[3] + sym[4];
2023-04-12 03:41:11 +08:00
if (k < sym[5])
2023-04-12 03:39:54 +08:00
*allconpt++ = '-';
2023-04-12 03:41:11 +08:00
else if (sym[0] == sym[1] && sym[1] == sym[2] &&
sym[2] == sym[3])
*allconpt++ = 'N';
else {
k = sym[0];
c = 'A';
if (k < sym[1]) {
k = sym[1];
c = 'C';
2023-04-12 03:39:54 +08:00
}
2023-04-12 03:41:11 +08:00
if (k < sym[2]) {
k = sym[2];
c = 'G';
}
if (k < sym[3]) c = 'T';
*allconpt++ = c;
}
for (j = 0; j < 6; j++) sym[j] = 0;
for (t = head; t != 0; t = t->link) {
2023-04-12 03:39:54 +08:00
NEXTOP(t);
2023-04-12 03:41:11 +08:00
if (t->done) /* delete it from op tree
*/
{
2023-04-12 03:39:54 +08:00
w = t->father;
2023-04-12 03:41:11 +08:00
if (w->child->id == t->id)
2023-04-12 03:39:54 +08:00
w->child = t->brother;
2023-04-12 03:41:11 +08:00
else {
2023-04-12 03:39:54 +08:00
w = w->child;
2023-04-12 03:41:11 +08:00
for (; w->brother->id !=
t->id;
w = w->brother)
2023-04-12 03:39:54 +08:00
;
w->brother = t->brother;
}
}
}
2023-04-12 03:41:11 +08:00
if (root->loc >
root->length) /* check root node */
{
2023-04-12 03:39:54 +08:00
root->done = 1;
2023-04-12 03:41:11 +08:00
if ((w = root->child) != 0) {
2023-04-12 03:39:54 +08:00
w->father = 0;
root = w;
}
else
done = 1;
}
col++;
2023-04-12 03:41:11 +08:00
if (col == LINELEN || done) /* output */
{
2023-04-12 03:39:54 +08:00
col = 0;
2023-04-12 03:41:11 +08:00
for (j = 0; j < n; j++) {
2023-04-12 03:39:54 +08:00
t = work[j];
2023-04-12 03:41:11 +08:00
if (t->done)
2023-04-12 03:39:54 +08:00
/*
Mod by S.S.
{ (void) printf("#");
for ( a = t->name; *a; a++ )
(void) printf("%c", *a);
*/
{
int jj;
2023-04-12 03:41:11 +08:00
(void)printf(
"{\nname ");
for (jj = 0;
jj <
strlen(t->name) -
1;
jj++)
(void)printf(
"%c",
t->name
[jj]);
printf(
"\nstrandedness"
" %c\n",
t->name[strlen(
t->name)] == '+'
? '1'
: '2');
printf(
"offset "
"%d\ntype "
"DNA\ngroup-"
"ID %d\nsequence \"\n",
t->offset, group);
for (k = 0, a = t->line;
a != t->a; a++)
if (*a != ' ') {
2023-04-12 03:39:54 +08:00
k++;
2023-04-12 03:41:11 +08:00
(void)printf(
"%"
"c",
*a);
if (k ==
LINELEN) {
(void)printf(
"\n");
2023-04-12 03:39:54 +08:00
k = 0;
}
}
2023-04-12 03:41:11 +08:00
/*
if ( k )
*/
(void)printf("\"\n}\n");
2023-04-12 03:39:54 +08:00
}
2023-04-12 03:41:11 +08:00
if (t->linesize -
(t->a - t->line) <
LINELEN + 3)
2023-04-12 03:39:54 +08:00
ALOC_SEQ(t);
}
2023-04-12 03:41:11 +08:00
if (!done) {
for (k = j = n - 1; j >= 0; j--)
if (work[j]->done) {
2023-04-12 03:39:54 +08:00
t = work[j];
2023-04-12 03:41:11 +08:00
for (x = j;
x < k; x++)
work[x] = work
[x +
1];
2023-04-12 03:39:54 +08:00
work[k--] = t;
}
n = k + 1;
}
else
n = 0;
}
}
}
}
/* allocate more space for output fragment */
ALOC_SEQ(t) rowptr t;
2023-04-12 03:41:11 +08:00
{
char *start, *end, *p;
2023-04-12 03:39:54 +08:00
t->linesize *= 2;
start = t->line;
end = t->a;
2023-04-12 03:41:11 +08:00
t->line = ckalloc(t->linesize * sizeof(char));
for (t->a = t->line, p = start; p != end;) *t->a++ = *p++;
2023-04-12 03:39:54 +08:00
free(start);
}
/* enter a segment into working set */
2023-04-12 03:41:11 +08:00
ENTER(b, d, id, pos, par) int *b, *d, id, pos;
2023-04-12 03:39:54 +08:00
rowptr par;
2023-04-12 03:41:11 +08:00
{
int i;
char *ckalloc(); /* space-allocating function */
rowptr t;
if (*b <= *d) {
work[*b] = (rowptr)ckalloc((int)sizeof(row));
work[*b]->line = (char *)ckalloc(SEQLEN * sizeof(char));
2023-04-12 03:39:54 +08:00
work[*b]->linesize = SEQLEN;
*b += 1;
}
t = work[*d];
*d += 1;
t->a = t->line;
2023-04-12 03:41:11 +08:00
for (i = 0; i < pos; i++) *t->a++ = ' ';
2023-04-12 03:39:54 +08:00
t->c = ' ';
t->seq = mtree[id].orient ? segment[id].seq : segment[id].rev;
t->length = segment[id].length;
t->id = id;
2023-04-12 03:41:11 +08:00
if (par != 0) {
2023-04-12 03:39:54 +08:00
t->s = mtree[id].script;
t->size = mtree[id].size;
}
t->op = 0;
2023-04-12 03:41:11 +08:00
for (i = 1; i <= segment[id].len && i <= NAMELEN; i++)
t->name[i - 1] = segment[id].name[i];
if (mtree[id].orient)
t->name[i - 1] = '+';
2023-04-12 03:39:54 +08:00
else
2023-04-12 03:41:11 +08:00
t->name[i - 1] = '-';
2023-04-12 03:39:54 +08:00
t->name[i] = '\0';
t->done = 0;
t->loc = 1;
t->child = 0;
t->father = par;
2023-04-12 03:41:11 +08:00
if (par != 0) {
2023-04-12 03:39:54 +08:00
t->brother = par->child;
par->child = t;
NEXTOP(t);
}
}
/* get the next operation */
2023-04-12 03:41:11 +08:00
NEXTOP(t) rowptr t;
{
if (t->size || t->op)
if (t->op == 0 && *t->s == 0) {
2023-04-12 03:39:54 +08:00
t->op = *t->s++;
t->size--;
t->up = 'L';
t->dw = 'L';
}
2023-04-12 03:41:11 +08:00
else {
if (t->op == 0) {
2023-04-12 03:39:54 +08:00
t->op = *t->s++;
t->size--;
}
2023-04-12 03:41:11 +08:00
if (t->op > 0) {
2023-04-12 03:39:54 +08:00
t->up = '-';
t->dw = 'L';
t->op--;
}
2023-04-12 03:41:11 +08:00
else {
2023-04-12 03:39:54 +08:00
t->up = 'L';
t->dw = '-';
t->op++;
}
}
2023-04-12 03:41:11 +08:00
else if (t->loc > t->length)
t->done = 1;
2023-04-12 03:39:54 +08:00
}
COLUMN(x) rowptr x;
2023-04-12 03:41:11 +08:00
{
rowptr y;
rowptr start, end; /* first and last nodes for subtree */
2023-04-12 03:39:54 +08:00
2023-04-12 03:41:11 +08:00
if (x->child == 0) {
2023-04-12 03:39:54 +08:00
head = tail = 0;
x->kind = 'L';
}
2023-04-12 03:41:11 +08:00
else {
2023-04-12 03:39:54 +08:00
start = end = 0;
x->kind = 'L';
2023-04-12 03:41:11 +08:00
for (y = x->child; y != 0; y = y->brother) {
2023-04-12 03:39:54 +08:00
COLUMN(y);
2023-04-12 03:41:11 +08:00
if (x->kind == y->up)
if (y->kind == y->dw) {
if (head == 0) {
2023-04-12 03:39:54 +08:00
y->link = 0;
head = tail = y;
}
2023-04-12 03:41:11 +08:00
else {
2023-04-12 03:39:54 +08:00
y->link = head;
head = y;
}
2023-04-12 03:41:11 +08:00
if (end == 0)
2023-04-12 03:39:54 +08:00
start = head;
else
end->link = head;
end = tail;
}
2023-04-12 03:41:11 +08:00
else if (y->kind == '-') {
start = head;
end = tail;
x->kind = '-';
}
else {
y->link = 0;
y->kind = '-';
if (end == 0)
start = end = y;
else {
end->link = y;
end = y;
2023-04-12 03:39:54 +08:00
}
2023-04-12 03:41:11 +08:00
}
else if (y->kind == y->dw)
if (x->kind == '-')
;
else {
if (head == 0) {
2023-04-12 03:39:54 +08:00
y->link = 0;
2023-04-12 03:41:11 +08:00
head = tail = y;
2023-04-12 03:39:54 +08:00
}
2023-04-12 03:41:11 +08:00
else {
y->link = head;
head = y;
}
start = head;
end = tail;
x->kind = '-';
}
else if (x->kind == '-')
if (y->kind == '-') {
if (end == 0) {
2023-04-12 03:39:54 +08:00
start = head;
end = tail;
}
2023-04-12 03:41:11 +08:00
else if (head == 0)
/* code folded from here */
;
/* unfolding */
else {
/* code folded from here */
end->link = head;
2023-04-12 03:39:54 +08:00
end = tail;
2023-04-12 03:41:11 +08:00
/* unfolding */
2023-04-12 03:39:54 +08:00
}
2023-04-12 03:41:11 +08:00
}
else
;
else {
start = head;
end = tail;
x->kind = '-';
}
2023-04-12 03:39:54 +08:00
}
head = start;
tail = end;
}
}
/* Display a summary of contigs */
GRAPH()
2023-04-12 03:41:11 +08:00
{
int i, j, k; /* index variables */
int group; /* serial number of contigs */
char name[NAMELEN + 2]; /* name of segment */
char *t; /* temp var */
int length; /* length of name */
(void)printf("\nOVERLAPS CONTAINMENTS\n\n");
2023-04-12 03:39:54 +08:00
group = 1;
2023-04-12 03:41:11 +08:00
for (i = 0; i < seg_num; i++)
if (mtree[i].head) {
(void)printf(
"******************* Contig %d "
"********************\n",
group++);
for (j = i; j != -1; j = mtree[j].next) {
2023-04-12 03:39:54 +08:00
length = segment[j].len;
t = segment[j].name + 1;
2023-04-12 03:41:11 +08:00
for (k = 0; k < length && k < NAMELEN; k++)
2023-04-12 03:39:54 +08:00
name[k] = *t++;
2023-04-12 03:41:11 +08:00
if (mtree[j].orient)
2023-04-12 03:39:54 +08:00
name[k] = '+';
else
name[k] = '-';
2023-04-12 03:41:11 +08:00
name[k + 1] = '\0';
(void)printf("%s\n", name);
2023-04-12 03:39:54 +08:00
CONTAIN(mtree[j].child, name);
}
}
}
2023-04-12 03:41:11 +08:00
CONTAIN(id, f) int id;
2023-04-12 03:39:54 +08:00
char *f;
2023-04-12 03:41:11 +08:00
{
int k; /* index variable */
char name[NAMELEN + 2]; /* name of segment */
char *t; /* temp var */
int length; /* length of name */
if (id != -1) {
2023-04-12 03:39:54 +08:00
length = segment[id].len;
t = segment[id].name + 1;
2023-04-12 03:41:11 +08:00
for (k = 0; k < length && k < NAMELEN; k++) name[k] = *t++;
if (mtree[id].orient)
2023-04-12 03:39:54 +08:00
name[k] = '+';
else
name[k] = '-';
2023-04-12 03:41:11 +08:00
name[k + 1] = '\0';
(void)printf(" %s is in %s\n", name, f);
2023-04-12 03:39:54 +08:00
CONTAIN(mtree[id].child, name);
CONTAIN(mtree[id].brother, f);
}
}
2023-04-12 03:41:11 +08:00
big_pass(A, B, M, N, orienti, orientj) char A[], B[];
int M, N;
2023-04-12 03:39:54 +08:00
short orienti, orientj;
2023-04-12 03:41:11 +08:00
{
register int i, j; /* row and column indices */
register int c; /* best score at current point */
register int f; /* best score ending with insertion */
register int d; /* best score ending with deletion */
register int p; /* best score at (i-1, j-1) */
register int ci; /* end-point associated with c */
register int di; /* end-point associated with d */
register int fi; /* end-point associated with f */
register int pi; /* end-point associated with p */
int *va; /* pointer to v(A[i], B[j]) */
int x1,
x2; /* regions of A before x1 or after x2 are lightly penalized */
int y1,
y2; /* regions of B before y1 or after y2 are lightly penalized */
short heavy; /* 1 = heavy penalty */
int ex, gx; /* current gap penalty scores */
2023-04-12 03:39:54 +08:00
/* determine x1, x2, y1, y2 */
2023-04-12 03:41:11 +08:00
if (POS5 >= POS3)
fatal(
"The value for POS5 must be less than the value for POS3");
if (orienti) {
2023-04-12 03:39:54 +08:00
x1 = POS5 >= M ? 1 : POS5;
x2 = POS3 >= M ? M : POS3;
}
2023-04-12 03:41:11 +08:00
else {
2023-04-12 03:39:54 +08:00
x1 = POS3 >= M ? 1 : M - POS3 + 1;
x2 = POS5 >= M ? M : M - POS5 + 1;
}
2023-04-12 03:41:11 +08:00
if (orientj) {
2023-04-12 03:39:54 +08:00
y1 = POS5 >= N ? 1 : POS5;
y2 = POS3 >= N ? N : POS3;
}
2023-04-12 03:41:11 +08:00
else {
2023-04-12 03:39:54 +08:00
y1 = POS3 >= N ? 1 : N - POS3 + 1;
y2 = POS5 >= N ? N : N - POS5 + 1;
}
2023-04-12 03:41:11 +08:00
if (x1 + 1 <= x2) x1++;
if (y1 + 1 <= y2) y1++;
2023-04-12 03:39:54 +08:00
heavy = 0;
/* Compute the matrix.
CC : the scores of the current row
RR : the starting point that leads to score CC
DD : the scores of the current row, ending with deletion
SS : the starting point that leads to score DD */
/* Initialize the 0 th row */
2023-04-12 03:41:11 +08:00
for (j = 1; j <= N; j++) {
2023-04-12 03:39:54 +08:00
CC[j] = 0;
2023-04-12 03:41:11 +08:00
DD[j] = -(q);
2023-04-12 03:39:54 +08:00
RR[j] = SS[j] = -j;
}
2023-04-12 03:41:11 +08:00
for (i = 1; i <= M; i++) {
if (i == x1) heavy = 1 - heavy;
if (i == x2) heavy = 1 - heavy;
2023-04-12 03:39:54 +08:00
ex = r1;
gx = qr1;
va = v1[A[i]];
2023-04-12 03:41:11 +08:00
c = 0; /* Initialize column 0 */
f = -(q);
2023-04-12 03:39:54 +08:00
ci = fi = i;
p = 0;
pi = i - 1;
2023-04-12 03:41:11 +08:00
for (j = 1; j <= N; j++) {
if (j == y1) {
if (heavy) {
2023-04-12 03:39:54 +08:00
ex = r;
gx = qr;
/*
S.S.
2023-04-12 03:41:11 +08:00
va = v[A[i]];
2023-04-12 03:39:54 +08:00
*/
va = v[A[i]];
}
}
2023-04-12 03:41:11 +08:00
if (j == y2) {
if (heavy) {
2023-04-12 03:39:54 +08:00
ex = r1;
gx = qr1;
va = v1[A[i]];
}
}
2023-04-12 03:41:11 +08:00
if ((f = f - ex) < (c = c - gx)) {
f = c;
fi = ci;
2023-04-12 03:39:54 +08:00
}
di = SS[j];
2023-04-12 03:41:11 +08:00
if ((d = DD[j] - ex) < (c = CC[j] - gx)) {
d = c;
di = RR[j];
2023-04-12 03:39:54 +08:00
}
2023-04-12 03:41:11 +08:00
c = p + va[B[j]]; /* diagonal */
2023-04-12 03:39:54 +08:00
ci = pi;
2023-04-12 03:41:11 +08:00
if (c < d) {
c = d;
ci = di;
2023-04-12 03:39:54 +08:00
}
2023-04-12 03:41:11 +08:00
if (c < f) {
c = f;
ci = fi;
2023-04-12 03:39:54 +08:00
}
p = CC[j];
CC[j] = c;
pi = RR[j];
RR[j] = ci;
DD[j] = d;
SS[j] = di;
2023-04-12 03:41:11 +08:00
if ((j == N || i == M) && c > SCORE) {
2023-04-12 03:39:54 +08:00
SCORE = c;
ENDI = i;
ENDJ = j;
STARI = ci;
}
}
}
2023-04-12 03:41:11 +08:00
if (SCORE)
if (STARI < 0) {
STARJ = -STARI;
2023-04-12 03:39:54 +08:00
STARI = 0;
}
else
STARJ = 0;
}
/* diff(A,B,M,N,tb,te) returns the score of an optimum conversion between
A[1..M] and B[1..N] that begins(ends) with a delete if tb(te) is zero
and appends such a conversion to the current script. */
2023-04-12 03:41:11 +08:00
int diff(A, B, M, N, tb, te)
char *A, *B;
int M, N;
int tb, te;
2023-04-12 03:39:54 +08:00
2023-04-12 03:41:11 +08:00
{
int midi, midj, type; /* Midpoint, type, and cost */
int midc;
2023-04-12 03:39:54 +08:00
2023-04-12 03:41:11 +08:00
{
register int i, j;
register int c, e, d, s;
int t, *va;
char *ckalloc();
2023-04-12 03:39:54 +08:00
/* Boundary cases: M <= 1 or N == 0 */
2023-04-12 03:41:11 +08:00
if (N <= 0) {
2023-04-12 03:39:54 +08:00
if (M > 0) DEL(M)
2023-04-12 03:41:11 +08:00
return -gap(M);
2023-04-12 03:39:54 +08:00
}
2023-04-12 03:41:11 +08:00
if (M <= 1) {
if (M <= 0) {
2023-04-12 03:39:54 +08:00
INS(N);
2023-04-12 03:41:11 +08:00
return -gap(N);
2023-04-12 03:39:54 +08:00
}
if (tb > te) tb = te;
2023-04-12 03:41:11 +08:00
midc = -(tb + r + gap(N));
2023-04-12 03:39:54 +08:00
midj = 0;
va = v[A[1]];
2023-04-12 03:41:11 +08:00
for (j = 1; j <= N; j++) {
c = va[B[j]] - (gap(j - 1) + gap(N - j));
if (c > midc) {
2023-04-12 03:39:54 +08:00
midc = c;
midj = j;
}
}
2023-04-12 03:41:11 +08:00
if (midj == 0) {
INS(N) DEL(1)
}
else {
if (midj > 1) INS(midj - 1)
REP if ((A[1] | 32) == (B[midj] | 32)) no_mat +=
1;
else no_mis += 1;
if (midj < N) INS(N - midj)
2023-04-12 03:39:54 +08:00
}
return midc;
}
/* Divide: Find optimum midpoint (midi,midj) of cost midc */
2023-04-12 03:41:11 +08:00
midi = M / 2; /* Forward phase: */
CC[0] = 0; /* Compute C(M/2,k) & D(M/2,k) for all k */
2023-04-12 03:39:54 +08:00
t = -q;
2023-04-12 03:41:11 +08:00
for (j = 1; j <= N; j++) {
CC[j] = t = t - r;
DD[j] = t - q;
2023-04-12 03:39:54 +08:00
}
t = -tb;
2023-04-12 03:41:11 +08:00
for (i = 1; i <= midi; i++) {
2023-04-12 03:39:54 +08:00
s = CC[0];
2023-04-12 03:41:11 +08:00
CC[0] = c = t = t - r;
e = t - q;
2023-04-12 03:39:54 +08:00
va = v[A[i]];
2023-04-12 03:41:11 +08:00
for (j = 1; j <= N; j++) {
2023-04-12 03:39:54 +08:00
if ((c = c - qr) > (e = e - r)) e = c;
if ((c = CC[j] - qr) > (d = DD[j] - r)) d = c;
2023-04-12 03:41:11 +08:00
c = s + va[B[j]];
2023-04-12 03:39:54 +08:00
if (c < d) c = d;
if (c < e) c = e;
s = CC[j];
CC[j] = c;
DD[j] = d;
}
}
DD[0] = CC[0];
2023-04-12 03:41:11 +08:00
RR[N] = 0; /* Reverse phase: */
t = -q; /* Compute R(M/2,k) & S(M/2,k) for all k */
for (j = N - 1; j >= 0; j--) {
RR[j] = t = t - r;
SS[j] = t - q;
2023-04-12 03:39:54 +08:00
}
t = -te;
2023-04-12 03:41:11 +08:00
for (i = M - 1; i >= midi; i--) {
2023-04-12 03:39:54 +08:00
s = RR[N];
2023-04-12 03:41:11 +08:00
RR[N] = c = t = t - r;
e = t - q;
va = v[A[i + 1]];
for (j = N - 1; j >= 0; j--) {
2023-04-12 03:39:54 +08:00
if ((c = c - qr) > (e = e - r)) e = c;
if ((c = RR[j] - qr) > (d = SS[j] - r)) d = c;
2023-04-12 03:41:11 +08:00
c = s + va[B[j + 1]];
2023-04-12 03:39:54 +08:00
if (c < d) c = d;
if (c < e) c = e;
s = RR[j];
RR[j] = c;
SS[j] = d;
}
}
SS[N] = RR[N];
2023-04-12 03:41:11 +08:00
midc = CC[0] + RR[0]; /* Find optimal midpoint */
2023-04-12 03:39:54 +08:00
midj = 0;
type = 1;
for (j = 0; j <= N; j++)
if ((c = CC[j] + RR[j]) >= midc)
2023-04-12 03:41:11 +08:00
if (c > midc ||
CC[j] != DD[j] && RR[j] == SS[j]) {
2023-04-12 03:39:54 +08:00
midc = c;
midj = j;
}
for (j = N; j >= 0; j--)
2023-04-12 03:41:11 +08:00
if ((c = DD[j] + SS[j] + q) > midc) {
2023-04-12 03:39:54 +08:00
midc = c;
midj = j;
type = 2;
}
}
/* Conquer: recursively around midpoint */
2023-04-12 03:41:11 +08:00
if (type == 1) {
(void)diff(A, B, midi, midj, tb, q);
(void)diff(A + midi, B + midj, M - midi, N - midj, q, te);
2023-04-12 03:39:54 +08:00
}
2023-04-12 03:41:11 +08:00
else {
(void)diff(A, B, midi - 1, midj, tb, zero);
2023-04-12 03:39:54 +08:00
DEL(2);
2023-04-12 03:41:11 +08:00
(void)diff(A + midi + 1, B + midj, M - midi - 1, N - midj, zero,
te);
2023-04-12 03:39:54 +08:00
}
return midc;
}
/* lib.c - library of C procedures. */
/* fatal - print message and die */
2023-04-12 03:41:11 +08:00
fatal(msg) char *msg;
2023-04-12 03:39:54 +08:00
{
2023-04-12 03:41:11 +08:00
(void)fprintf(stderr, "%s\n", msg);
2023-04-12 03:39:54 +08:00
exit(1);
}
/* fatalf - format message, print it, and die */
2023-04-12 03:41:11 +08:00
fatalf(msg, val) char *msg, *val;
2023-04-12 03:39:54 +08:00
{
2023-04-12 03:41:11 +08:00
(void)fprintf(stderr, msg, val);
(void)putc('\n', stderr);
2023-04-12 03:39:54 +08:00
exit(1);
}
/* ckopen - open file; check for success */
FILE *ckopen(name, mode)
char *name, *mode;
{
FILE *fopen(), *fp;
2023-04-12 03:41:11 +08:00
if ((fp = fopen(name, mode)) == NULL) fatalf("Cannot open %s.", name);
return (fp);
2023-04-12 03:39:54 +08:00
}
/* ckalloc - allocate space; check for success */
char *ckalloc(amount)
2023-04-12 03:41:11 +08:00
size_t amount;
2023-04-12 03:39:54 +08:00
{
2023-04-12 03:41:11 +08:00
void *malloc(), *p;
2023-04-12 03:39:54 +08:00
2023-04-12 03:41:11 +08:00
if ((p = malloc((unsigned)amount)) == NULL) fatal("Ran out of memory.");
return (p);
2023-04-12 03:39:54 +08:00
}