htmlsplit.html
#include <fstream>
#include<Rcpp.h>
#include<string.h>
#include <sstream>
#include<cctype>
#include <algorithm>
using namespace Rcpp;
inline CharacterVector read_fasta(std::string file ){
CharacterVector records;
std::ifstream in(file.c_str());
in.get();std::string rec ;
while(getline(in,rec,'>')){
int newLineLoc=rec.find('\n');
std::string header =rec.substr(0,newLineLoc);
std::string sequence= rec.substr(newLineLoc+1,rec.length()-newLineLoc-2);
sequence.erase(std::remove(sequence.begin(),sequence.end(),'\n'),sequence.end());
std::transform(sequence.begin(),sequence.end(),sequence.begin(),tolower);
records[header]=sequence;
}
return records;
}
inline CharacterVector SPLIT(const std::string strings,const int n){
int lim=strings.length()-n+1 ; CharacterVector resultat(lim) ;
for ( int j=0; j<lim ;j++) resultat[j] = strings.substr(j,n) ;
return resultat ;
}
inline double natoi( const std::string A,const int n ){
int np=n-1;
IntegerVector temp(n,4);
char toc;
for( int i=0;i<n;i++){
toc=A[i];
if(toc=='a') temp[i]= 0;
if(toc=='t') temp[i]= 3;
if(toc=='c') temp[i]= 1;
if(toc=='g') temp[i]= 2;
}
double result=0;
for( int i=0;i<n;i++) result+= pow(5,(np-i))*temp[i];
return result;
}
inline NumericVector numeric_split(const std::string A,const int n)
{
const int np=n-1;
const int Lim=A.length()-np; NumericVector res(Lim); double p=0; res[0]=p=natoi(A,n); const double N=pow(5,np); unsigned short int x,y; char fo,ba; for( int i=1;i<Lim;i++)
{
fo=A[i-1];ba=A[i+np];
x=y=4; if(fo=='a')x=0; if(fo=='t')x=3; if(fo=='g')x=2; if(fo=='c')x=1;
if(ba=='a')y=0;
if(ba=='t')y=3;
if(ba=='g')y=2;
if(ba=='c')y=1;
res[i]=p= 5*p-5*N*x+y; }
return res;
}