File: common_functions.cpp

package info (click to toggle)
ray 2.3.1-9
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 6,008 kB
  • sloc: cpp: 49,973; sh: 339; makefile: 281; python: 168
file content (325 lines) | stat: -rw-r--r-- 6,795 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
/*
    Ray -- Parallel genome assemblies for parallel DNA sequencing
    Copyright (C) 2010, 2011, 2012, 2013 Sébastien Boisvert

	http://DeNovoAssembler.SourceForge.Net/

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, version 3 of the License.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You have received a copy of the GNU General Public License
    along with this program (gpl-3.0.txt).  
	see <http://www.gnu.org/licenses/>

*/

#include "common_functions.h"
#include "constants.h"

#include <RayPlatform/core/OperatingSystem.h>
#include <RayPlatform/cryptography/crypto.h>

#include <assert.h>
#include <stdio.h>
#include <time.h>
#include <vector>
#include <fstream>
#include <stdlib.h>
#include <iostream>
#include <string>
#include <cstring>
#include <sstream>

using namespace std;

bool isValidDNA(char*x){
	int len=strlen(x);
	for(int i=0;i<len;i++){
		char a=x[i];
		if(!(a==SYMBOL_A||a==SYMBOL_T||a==SYMBOL_C||a==SYMBOL_G))
			return false;
	}
	return true;
}

string addLineBreaks(string dna,int columns){
	ostringstream output;
	int j=0;
	while(j<(int)dna.length()){
		output<<dna.substr(j,columns)<<endl;
		j+=columns;
	}
	return output.str();
}

string convertToString(GraphPath*b,int m_wordSize,bool color){
	ostringstream a;
	#ifdef USE_DISTANT_SEGMENTS_GRAPH
	//
	//TTAATT
	// TTAATT
	//  TTAATT
	//  the first vertex can not fill in the first delta letter alone, it needs help.
	for(int p=0;p<m_wordSize;p++){
		a<<codeToChar(b->at(p).getFirstSegmentFirstCode(m_wordSize));
	}
	#else
	Kmer object;
	b->at(0,&object);
	a<<object.idToWord(m_wordSize,color);
	#endif

	for(int j=1;j<(int)(*b).size();j++){
		Kmer object;
		b->at(j,&object);
		a<<object.getLastSymbol(m_wordSize,color);
	}
	string contig=a.str();

	return contig;
}

Kmer kmerAtPosition(const char*m_sequence,int pos,int w,char strand,bool color){
	#ifdef CONFIG_ASSERT
	assert(w<=CONFIG_MAXKMERLENGTH);
	#endif
	int length=strlen(m_sequence);
	if(pos>length-w){
		cout<<"Fatal: offset is too large: position= "<<pos<<" Length= "<<length<<" WordSize=" <<w<<endl;
		exit(0);
	}
	if(pos<0){
		cout<<"Fatal: negative offset. "<<pos<<endl;
		exit(0);
	}
	if(strand=='F'){
		char sequence[CONFIG_MAXKMERLENGTH];
		memcpy(sequence,m_sequence+pos,w);
		sequence[w]='\0';
		Kmer v=wordId(sequence);
		return v;
	}else if(strand=='R'){
		char sequence[CONFIG_MAXKMERLENGTH];
		memcpy(sequence,m_sequence+length-pos-w,w);
		sequence[w]='\0';
		Kmer v=wordId(sequence);
		return v.complementVertex(w,color);
	}
	Kmer error;
	return error;
}

PathHandle getPathUniqueId(Rank rank,int id){
	uint64_t a=id;
	a=a*MAX_NUMBER_OF_MPI_PROCESSES+rank;
	return a;
}

int getIdFromPathUniqueId(PathHandle a){
	return a/MAX_NUMBER_OF_MPI_PROCESSES;
}

Rank getRankFromPathUniqueId(PathHandle a){
	Rank rank=a%MAX_NUMBER_OF_MPI_PROCESSES;
	return rank;
}

void print64(uint64_t a){
	for(int k=63;k>=0;k-=2){
		int bit=a<<(k-1)>>63;
		printf("%i",bit);
		bit=a<<(k)>>63;
		printf("%i ",bit);
	}
	printf("\n");
}

void print8(uint8_t a){
	for(int i=7;i>=0;i--){
		int bit=((((uint64_t)a)<<((sizeof(uint64_t)*8-1)-i))>>(sizeof(uint64_t)*8-1));
		printf("%i ",bit);
	}
	printf("\n");
}

uint8_t charToCode(char a){
	switch (a){
		case SYMBOL_A:
			return RAY_NUCLEOTIDE_A;
		case SYMBOL_T:
			return RAY_NUCLEOTIDE_T;
		case SYMBOL_C:
			return RAY_NUCLEOTIDE_C;
		case SYMBOL_G:
			return RAY_NUCLEOTIDE_G;
		default:
			return RAY_NUCLEOTIDE_A;
	}
}

char complementNucleotide(char c){
	switch(c){
		case SYMBOL_A:
			return SYMBOL_T;
		case SYMBOL_T:
			return SYMBOL_A;
		case SYMBOL_G:
			return SYMBOL_C;
		case SYMBOL_C:
			return SYMBOL_G;
		default:
			return c;
	}
}

string reverseComplement(string*a){
	ostringstream b;
	for(int i=a->length()-1;i>=0;i--){
		b<<complementNucleotide((*a)[i]);
	}
	return b.str();
}

Kmer wordId(const char*a){
	Kmer i;
	int theLen=strlen(a);
	for(int j=0;j<(int)theLen;j++){
		uint64_t k=charToCode(a[j]);
		int bitPosition=2*j;
		int chunk=bitPosition/64;
		int bitPositionInChunk=bitPosition%64;
		#ifdef CONFIG_ASSERT
		if(!(chunk<i.getNumberOfU64())){
			cout<<"Chunk="<<chunk<<" positionInKmer="<<j<<" KmerLength="<<strlen(a)<<" bitPosition=" <<bitPosition<<" Chunks="<<i.getNumberOfU64()<<endl;
		}
		assert(chunk<i.getNumberOfU64());
		#endif
		uint64_t filter=(k<<bitPositionInChunk);
		i.setU64(chunk,i.getU64(chunk)|filter);
	}
	return i;
}

/** pack the pointer in a uint64_t */
uint64_t pack_pointer(void**pointer){
	if( NUMBER_OF_BITS == 64){

		uint64_t*placeHolder=(uint64_t*)pointer;
		uint64_t integerValue=*placeHolder;

		return integerValue;
	
	}else if(NUMBER_OF_BITS == 32){

		uint32_t*placeHolder=(uint32_t*)pointer;
		uint32_t integerValue=*placeHolder;

		return integerValue;
	}

	return 0; // not supported
}

void unpack_pointer(void**pointer,uint64_t integerValue){

	if( NUMBER_OF_BITS == 64){

		uint64_t*placeHolder=(uint64_t*)pointer;

		*placeHolder=integerValue;

	}else if(NUMBER_OF_BITS == 32){

		uint32_t*placeHolder=(uint32_t*)pointer;

		*placeHolder=integerValue;
	}

}

#ifdef CONFIG_MPI_IO

bool flushFileOperationBuffer_MPI_IO(bool force,ostringstream*buffer,MPI_File file,int bufferSize){

	int available=buffer->tellp();

	if(available==0)
		return false;

	if(force || available>=bufferSize){

/*
 * The const_cast is not required by MPI 3.0. MPI <= 2.2 has stupid semantic
 * (i.e. MPI_File_write takes a non-const buffer (???))
 */

		string copy=buffer->str();
		const char*constantCopy=copy.c_str();

		char*data=const_cast<char*> ( constantCopy );

		int bytes=copy.length();

		MPI_Status writeStatus;
		int returnValue=MPI_File_write(file,data,bytes,MPI_BYTE,&writeStatus);

		if(returnValue!=MPI_SUCCESS){
			cout<<"Error: could not write to file with MPI I/O."<<endl;
		}

		buffer->str("");

		return true;
	}

	return false;
}

#endif

bool flushFileOperationBuffer(bool force,ostringstream*buffer,ostream*file,int bufferSize){

	int available=buffer->tellp();

	if(available==0)
		return false;

	if(force || available>=bufferSize){

		string copy=buffer->str();
		file->write(copy.c_str(),copy.length());

		buffer->str("");

		return true;
	}

	return false;
}

bool flushFileOperationBuffer_FILE(bool force,ostringstream*buffer,FILE*file,int bufferSize){

	int available=buffer->tellp();

	if(available==0)
		return false;

	if(force || available>=bufferSize){

		string copy=buffer->str();

		fprintf(file,"%s",copy.c_str());
		buffer->str("");

		return true;
	}

	return false;
}