modified: makefile
[GalaxyCodeBases.git] / BGI / SOAPdenovo2 / standardPregraph / loadPath.c
blob38d280a894cd610f5876a44e980a17b197b796d4
1 /*
2 * loadPath.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 /*************************************************
30 Function:
31 add1marker2edge
32 Description:
33 Records the id of read which crosses the edge.
34 Input:
35 1. edgeno: the edge index
36 2. readid: the read id
37 Output:
38 None.
39 Return:
40 None.
41 *************************************************/
42 static void add1marker2edge ( unsigned int edgeno, long long readid )
44 if ( edge_array[edgeno].multi == 255 )
46 return;
49 unsigned int bal_ed = getTwinEdge ( edgeno );
50 unsigned char counter = edge_array[edgeno].multi++;
51 edge_array[edgeno].markers[counter] = readid;
52 counter = edge_array[bal_ed].multi++;
53 edge_array[bal_ed].markers[counter] = -readid;
56 /*************************************************
57 Function:
58 loadPath
59 Description:
60 1. Loads the path info.
61 2. Records the ids of reads crossing edges.
62 Input:
63 1. graphfile: the input prefix
64 Output:
65 None.
66 Return:
67 None.
68 *************************************************/
69 boolean loadPath ( char * graphfile )
71 FILE * fp;
72 char name[256], line[1024];
73 unsigned int i, bal_ed, num1, edgeno, num2;
74 long long markCounter = 0, readid = 0;
75 char * seg;
76 sprintf ( name, "%s.markOnEdge", graphfile );
77 fp = fopen ( name, "r" );
79 if ( !fp )
81 return 0;
84 for ( i = 1; i <= num_ed; i++ )
86 edge_array[i].multi = 0;
89 for ( i = 1; i <= num_ed; i++ )
91 fscanf ( fp, "%d", &num1 );
93 if ( EdSmallerThanTwin ( i ) )
95 fscanf ( fp, "%d", &num2 );
96 bal_ed = getTwinEdge ( i );
98 if ( num1 + num2 >= 255 )
100 edge_array[i].multi = 255;
101 edge_array[bal_ed].multi = 255;
103 else
105 edge_array[i].multi = num1 + num2;
106 edge_array[bal_ed].multi = num1 + num2;
107 markCounter += 2 * ( num1 + num2 );
110 i++;
112 else
114 if ( 2 * num1 >= 255 )
116 edge_array[i].multi = 255;
118 else
120 edge_array[i].multi = 2 * num1;
121 markCounter += 2 * num1;
126 fclose ( fp );
127 fprintf ( stderr, "%lld markers overall.\n", markCounter );
128 markersArray = ( long long * ) ckalloc ( markCounter * sizeof ( long long ) );
129 markCounter = 0;
131 for ( i = 1; i <= num_ed; i++ )
133 if ( edge_array[i].multi == 255 )
135 continue;
138 edge_array[i].markers = markersArray + markCounter;
139 markCounter += edge_array[i].multi;
140 edge_array[i].multi = 0;
143 sprintf ( name, "%s.path", graphfile );
144 fp = fopen ( name, "r" );
146 if ( !fp )
148 return 0;
151 while ( fgets ( line, sizeof ( line ), fp ) != NULL )
153 //printf("%s",line);
154 readid++;
155 seg = strtok ( line, " " );
157 while ( seg )
159 edgeno = atoi ( seg );
160 //printf("%s, %d\n",seg,edgeno);
161 add1marker2edge ( edgeno, readid );
162 seg = strtok ( NULL, " " );
166 fclose ( fp );
167 markCounter = 0;
169 for ( i = 1; i <= num_ed; i++ )
171 if ( edge_array[i].multi == 255 )
173 continue;
176 markCounter += edge_array[i].multi;
179 fprintf ( stderr, "%lld marks loaded.\n", markCounter );
180 return 1;
183 static int comp ( const void * a , const void * b )
185 long long m , n ;
186 m = * ( long long * ) a > 0 ? * ( long long * ) a : -* ( long long * ) a;
187 n = * ( long long * ) b > 0 ? * ( long long * ) b : -* ( long long * ) b;
189 // return (int)(m-n);
190 if ( m > n )
191 { return 1; }
192 else if ( m < n )
193 { return -1; }
194 else
195 { return 0; }
198 /*************************************************
199 Function:
200 loadPathBin
201 Description:
202 1. Loads the path info.
203 2. Records the ids of reads crossing edges.
204 Input:
205 1. graphfile: the input prefix
206 Output:
207 None.
208 Return:
209 0 if it's fail to load the path.
210 *************************************************/
212 boolean loadPathBin ( char * graphfile )
214 FILE * fp;
215 char name[256];
216 unsigned int i, bal_ed, num1, num2;
217 long long markCounter = 0, readid = 0;
218 unsigned char seg, ch;
219 unsigned int * freadBuf;
220 sprintf ( name, "%s.markOnEdge", graphfile );
221 fp = fopen ( name, "r" );
223 if ( !fp )
225 return 0;
228 for ( i = 1; i <= num_ed; i++ )
230 edge_array[i].multi = 0;
231 edge_array[i].markers = NULL;
234 for ( i = 1; i <= num_ed; i++ )
236 fscanf ( fp, "%d", &num1 );
238 if ( EdSmallerThanTwin ( i ) )
240 fscanf ( fp, "%d", &num2 );
241 bal_ed = getTwinEdge ( i );
243 if ( num1 + num2 >= 255 )
245 edge_array[i].multi = 255;
246 edge_array[bal_ed].multi = 255;
248 else
250 edge_array[i].multi = num1 + num2;
251 edge_array[bal_ed].multi = num1 + num2;
252 markCounter += 2 * ( num1 + num2 );
255 i++;
257 else
259 if ( 2 * num1 >= 255 )
261 edge_array[i].multi = 255;
263 else
265 edge_array[i].multi = 2 * num1;
266 markCounter += 2 * num1;
271 fclose ( fp );
272 fprintf ( stderr, "%lld markers overall.\n", markCounter );
273 markersArray = ( long long * ) ckalloc ( markCounter * sizeof ( long long ) );
274 markCounter = 0;
276 for ( i = 1; i <= num_ed; i++ )
278 if ( edge_array[i].multi == 255 )
280 continue;
283 edge_array[i].markers = markersArray + markCounter;
284 markCounter += edge_array[i].multi;
285 edge_array[i].multi = 0;
288 sprintf ( name, "%s.path", graphfile );
289 fp = fopen ( name, "rb" );
291 if ( !fp )
293 return 0;
296 freadBuf = ( unsigned int * ) ckalloc ( ( maxReadLen - overlaplen + 1 ) * sizeof ( unsigned int ) );
298 while ( fread ( &ch, sizeof ( char ), 1, fp ) == 1 )
300 //printf("%s",line);
301 if ( fread ( freadBuf, sizeof ( unsigned int ), ch, fp ) != ch )
303 break;
306 readid++;
308 for ( seg = 0; seg < ch; seg++ )
310 add1marker2edge ( freadBuf[seg], readid );
314 fclose ( fp );
315 markCounter = 0;
317 for ( i = 1; i <= num_ed; i++ )
319 if ( edge_array[i].multi == 255 )
321 continue;
324 markCounter += edge_array[i].multi;
327 for ( i = 0; i <= num_ed; i++ )
329 if ( edge_array[i].multi >= 2 && edge_array[i].multi != 255 )
330 { qsort ( edge_array[i].markers, ( int ) edge_array[i].multi, sizeof ( long long ), comp ); }
333 fprintf ( stderr, "%lld markers loaded.\n", markCounter );
334 free ( ( void * ) freadBuf );
335 return 1;