modified: makefile
[GalaxyCodeBases.git] / BGI / SOAPdenovo2 / standardPregraph / connect.c
blob1f10a8c92197c4a2a0608af0d7e77aed3c21dcee
1 /*
2 * connect.c
4 * Copyright (c) 2008-2012 BGI-Shenzhen <soap at genomics dot org dot cn>.
6 * This file is part of SOAPdenovo.
8 * SOAPdenovo is free software: you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation, either version 3 of the License, or
11 * (at your option) any later version.
13 * SOAPdenovo is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with SOAPdenovo. If not, see <http://www.gnu.org/licenses/>.
23 #include "stdinc.h"
24 #include "newhash.h"
25 #include "kmerhash.h"
26 #include "extfunc.h"
27 #include "extvab.h"
29 #define CNBLOCKSIZE 100000
31 void createCntMemManager ()
33 if ( !cn_mem_manager )
35 cn_mem_manager = createMem_manager ( CNBLOCKSIZE, sizeof ( CONNECT ) );
37 else
39 fprintf ( stderr, "The cn_mem_manger created.\n" );
43 void destroyConnectMem ()
45 freeMem_manager ( cn_mem_manager );
46 cn_mem_manager = NULL;
49 CONNECT * allocateCN ( unsigned int contigId, int gap )
51 CONNECT * newCN;
52 newCN = ( CONNECT * ) getItem ( cn_mem_manager );
53 newCN->contigID = contigId;
54 newCN->gapLen = gap;
55 newCN->minGap = 0;
56 newCN->maxGap = 0;
57 newCN->bySmall = 0;
58 newCN->smallIns = 0;
59 newCN->weakPoint = 0;
60 newCN->weight = 1;
61 newCN->weightNotInherit = 0;
62 newCN->mask = 0;
63 newCN->used = 0;
64 newCN->checking = 0;
65 newCN->deleted = 0;
66 newCN->prevInScaf = 0;
67 newCN->inherit = 0;
68 newCN->singleInScaf = 0;
69 newCN->nextInScaf = NULL;
70 return newCN;
73 void output_cntGVZ ( char * outfile )
75 char name[256];
76 FILE * fp;
77 unsigned int i;
78 CONNECT * connect;
79 boolean flag;
80 sprintf ( name, "%s.scaffold.gvz", outfile );
81 fp = ckopen ( name, "w" );
82 fprintf ( fp, "digraph G{\n" );
83 fprintf ( fp, "\tsize=\"512,512\";\n" );
85 for ( i = num_ctg; i > 0; i-- )
87 if ( !contig_array[i].downwardConnect )
89 continue;
92 connect = contig_array[i].downwardConnect;
94 while ( connect )
96 if ( connect->deleted )
98 connect = connect->next;
99 continue;
102 if ( connect->prevInScaf || connect->nextInScaf )
104 flag = 1;
106 else
108 flag = 0;
111 if ( !connect->mask )
112 fprintf ( fp, "\tC%d_%d -> C%d_%d [label = \"%d(%d_%d)\"];\n", i, contig_array[i].length, connect->contigID, contig_array[connect->contigID].length,
113 connect->gapLen, flag, connect->weight );
114 else
115 fprintf ( fp, "\tC%d_%d -> C%d_%d [label = \"%d(%d_%d)\", color = red];\n", i, contig_array[i].length, connect->contigID, contig_array[connect->contigID].length,
116 connect->gapLen, flag, connect->weight );
118 connect = connect->next;
122 fprintf ( fp, "}\n" );
123 fclose ( fp );
126 /***************** below this line all codes are about lookup table *****************/
128 void createCntLookupTable ()
130 if ( !cntLookupTable )
132 cntLookupTable = ( CONNECT ** ) ckalloc ( ( 3 * num_ctg + 1 ) * sizeof ( CONNECT * ) );
136 void deleteCntLookupTable ()
138 if ( cntLookupTable )
140 free ( ( void * ) cntLookupTable );
141 cntLookupTable = NULL;
145 void putCnt2LookupTable ( unsigned int from_c, CONNECT * cnt )
147 if ( !cnt || !cntLookupTable )
149 return;
152 unsigned int index = 2 * from_c + cnt->contigID;
153 cnt->nextInLookupTable = cntLookupTable[index];
154 cntLookupTable[index] = cnt;
157 static CONNECT * getCntInLookupTable ( unsigned int from_c, unsigned int to_c )
159 unsigned int index = 2 * from_c + to_c;
160 CONNECT * ite_cnt = cntLookupTable[index];
162 while ( ite_cnt )
164 if ( ite_cnt->contigID == to_c )
166 return ite_cnt;
169 ite_cnt = ite_cnt->nextInLookupTable;
172 return NULL;
175 CONNECT * getCntBetween ( unsigned int from_c, unsigned int to_c )
177 CONNECT * pcnt;
179 if ( cntLookupTable )
181 pcnt = getCntInLookupTable ( from_c, to_c );
182 return pcnt;
185 pcnt = contig_array[from_c].downwardConnect;
187 while ( pcnt )
189 if ( pcnt->contigID == to_c )
191 return pcnt;
194 pcnt = pcnt->next;
197 return pcnt;
201 void removeCntInLookupTable(unsigned int from_c,unsigned int to_c)
203 unsigned int index = 2*from_c + to_c;
204 CONNECT *ite_cnt = cntLookupTable[index];
205 CONNECT *cnt;
207 if(!ite_cnt){
208 printf("removeCntInLookupTable: not found A\n");
209 return;
211 if(ite_cnt->contigID==to_c){
212 cntLookupTable[index] = ite_cnt->nextInLookupTable;
213 return;
216 while(ite_cnt->nextInLookupTable&&ite_cnt->nextInLookupTable->contigID!=to_c)
217 ite_cnt = ite_cnt->nextInLookupTable;
219 if(ite_cnt->nextInLookupTable){
220 cnt = ite_cnt->nextInLookupTable;
221 ite_cnt->nextInLookupTable = cnt->nextInLookupTable;
222 return;
224 printf("removeCntInLookupTable: not found B\n");
225 return;