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
|
/*
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 "Read.h"
#include <code/Mock/common_functions.h>
#include <assert.h>
#include <cstdlib>
#include <iostream>
#include <cstring>
using namespace std;
char*Read::trim(char*buffer,const char*sequence){
//cout<<"In=."<<sequence<<"."<<endl;
int theLen=strlen(sequence);
strcpy(buffer,sequence);
for(int i=0;i<theLen;i++){
if(buffer[i]==SYMBOL_LOWER_A)
buffer[i]=SYMBOL_A;
else if(buffer[i]==SYMBOL_LOWER_T)
buffer[i]=SYMBOL_T;
else if(buffer[i]==SYMBOL_LOWER_C)
buffer[i]=SYMBOL_C;
else if(buffer[i]==SYMBOL_LOWER_G)
buffer[i]=SYMBOL_G;
}
// discard N at the beginning and end of the read.
// find the first symbol that is a A,T,C or G
int first=0;
while(buffer[first]!=SYMBOL_A && buffer[first]!=SYMBOL_T &&buffer[first]!=SYMBOL_C &&buffer[first]!=SYMBOL_G &&first<theLen){
first++;
}
char*corrected=buffer+first;
//cout<<"Trimmed first "<<first<<endl;
// find the last symbol that is a A,T,C, or G
int last=0;
int len=strlen(corrected);
for(int i=0;i<len;i++){
if(corrected[i]==SYMBOL_A || corrected[i]==SYMBOL_T || corrected[i]==SYMBOL_C || corrected[i]==SYMBOL_G){
last=i;
}
}
last++;
// only junk awaits beyond <last>
//cout<<"Trimmed last "<<last<<endl;
corrected[last]='\0';
//cout<<"Out= ."<<corrected<<"."<<endl;
//cout<<endl;
return corrected;
}
void Read::constructorWithRawSequence(const char*seq,uint8_t*raw,bool flag){
m_type=TYPE_SINGLE_END;
m_length=strlen(seq);
m_sequence=raw;
}
void Read::constructor(const char*sequence,MyAllocator*seqMyAllocator,bool trimFlag){
/*
#define DEBUG_GCC_4_7_2
#define __READ_VERBOSITY
*/
#ifdef DEBUG_GCC_4_7_2
cout<<"[Read::constructor] sequence is "<<sequence<<endl;
#endif
m_forwardOffset=0;
m_reverseOffset=0;
m_type=TYPE_SINGLE_END;
char buffer[RAY_MAXIMUM_READ_LENGTH];
if(trimFlag && strlen(sequence)<RAY_MAXIMUM_READ_LENGTH){
sequence=trim(buffer,sequence);
}
#ifdef DEBUG_GCC_4_7_2
cout<<"[DEBUG_GCC_4_7_2] after trim "<<sequence<<endl;
#endif
int length=strlen(sequence);
m_length=length;
int requiredBytes=getRequiredBytes();
uint8_t workingBuffer[RAY_MAXIMUM_READ_LENGTH];
for(int i=0;i<requiredBytes;i++){
workingBuffer[i]=0;
}
#ifdef DEBUG_GCC_4_7_2
cout<<"[DEBUG_GCC_4_7_2] before loop, sequence= "<<sequence<<endl;
#endif
for(int position=0;position<length;position++){
char nucleotide=sequence[position];
if(nucleotide!=SYMBOL_A&&nucleotide!=SYMBOL_T&&nucleotide!=SYMBOL_C&&nucleotide!=SYMBOL_G){
#ifdef DEBUG_GCC_4_7_2
cout<<"[DEBUG_GCC_4_7_2] nucleotide "<<nucleotide<<" is not in {A,T,C,G}, position "<<position<<" in "<<sequence<<", length is "<<length<<endl;
#endif
nucleotide=SYMBOL_A;
}
uint8_t code=charToCode(nucleotide);
#ifdef __READ_VERBOSITY
if(position%4==0){
cout<<"|";
}
cout<<" "<<(int)code<<"("<<nucleotide<<")";
#endif
int positionInWorkingBuffer=position/4;
int codePositionInWord=position%4;
uint8_t wordToUpdate=workingBuffer[positionInWorkingBuffer];
// shift the code and or with the word to update
code=(code<<(codePositionInWord*2));
wordToUpdate=wordToUpdate|code;
workingBuffer[positionInWorkingBuffer]=wordToUpdate;
}
#ifdef __READ_VERBOSITY
cout<<endl;
for(int i=0;i<requiredBytes;i++){
cout<<" "<<(int)workingBuffer[i];
}
cout<<endl;
#endif
if(requiredBytes==0){
m_sequence=NULL;
}else{
m_sequence=(uint8_t*)seqMyAllocator->allocate(requiredBytes*sizeof(uint8_t));
memcpy(m_sequence,workingBuffer,requiredBytes);
}
}
void Read::getSeq(char*workingBuffer,bool color,bool doubleEncoding) const{
for(int position=0;position<m_length;position++){
int positionInWorkingBuffer=position/4;
uint8_t word=m_sequence[positionInWorkingBuffer];
int codePositionInWord=position%4;
uint8_t code=(word<<(6-codePositionInWord*2));//eliminate bits before
code=(code>>6);
if(!doubleEncoding)
color=false;
char nucleotide=codeToChar(code,color);
workingBuffer[position]=nucleotide;
}
workingBuffer[m_length]='\0';
}
int Read::length()const{
return m_length;
}
/*
* -----------------------------------
* -----------------------------------
* p p-1 p-2 0
*/
Kmer Read::getVertex(int pos,int w,char strand,bool color) const {
char buffer[RAY_MAXIMUM_READ_LENGTH];
getSeq(buffer,color,false);
return kmerAtPosition(buffer,pos,w,strand,color);
}
bool Read::hasPairedRead()const{
return m_type!=TYPE_SINGLE_END;
}
PairedRead*Read::getPairedRead(){
if(m_type==TYPE_SINGLE_END){
return NULL;
}
return &m_pairedRead;
}
uint8_t*Read::getRawSequence(){
return m_sequence;
}
int Read::getRequiredBytes(){
int requiredBits=2*m_length;
int modulo=requiredBits%8;
if(modulo!=0){
int bitsToAdd=8-modulo;
requiredBits+=bitsToAdd;
}
#ifdef CONFIG_ASSERT
assert(requiredBits%8==0);
#endif
int requiredBytes=requiredBits/8;
return requiredBytes;
}
void Read::setRawSequence(uint8_t*seq,int length){
m_sequence=seq;
m_length=length;
}
void Read::setLeftType(){
m_type=TYPE_LEFT_END;
}
void Read::setRightType(){
m_type=TYPE_RIGHT_END;
}
int Read::getType(){
return m_type;
}
void Read::setType(uint8_t a){
m_type=a;
}
void Read::setForwardOffset(int a){
m_forwardOffset=a;
}
void Read::setReverseOffset(int a){
m_reverseOffset=a;
}
int Read::getForwardOffset(){
return m_forwardOffset;
}
int Read::getReverseOffset(){
return m_reverseOffset;
}
void Read::writeOffsets(ostream*f){
int forwardOffset=getForwardOffset();
int reverseOffset=getReverseOffset();
f->write((char*)&forwardOffset,sizeof(int));
f->write((char*)&reverseOffset,sizeof(int));
}
void Read::readOffsets(istream*f){
int forwardOffset=0;
int reverseOffset=0;
f->read((char*)&forwardOffset,sizeof(int));
f->read((char*)&reverseOffset,sizeof(int));
setForwardOffset(forwardOffset);
setReverseOffset(reverseOffset);
}
void Read::write(ostream*f){
m_pairedRead.write(f);
f->write((char*)&m_type,sizeof(uint8_t));
f->write((char*)&m_length,sizeof(uint16_t));
if(getRequiredBytes()>0)
f->write((char*)m_sequence,getRequiredBytes());
}
void Read::read(istream*f,MyAllocator*seqMyAllocator){
m_pairedRead.read(f);
f->read((char*)&m_type,sizeof(uint8_t));
f->read((char*)&m_length,sizeof(uint16_t));
m_sequence=NULL;
if(getRequiredBytes()>0){
m_sequence=(uint8_t*)seqMyAllocator->allocate(getRequiredBytes()*sizeof(uint8_t));
f->read((char*)m_sequence,getRequiredBytes());
}
}
|