Skip to content

Commit

Permalink
Merge pull request #21 from jlab/restructure_rna
Browse files Browse the repository at this point in the history
restructure rna.hh and rnalib.*
sjanssen2 authored Jan 17, 2020
2 parents 4699222 + 44f240a commit ad7e4be
Showing 5 changed files with 46 additions and 73 deletions.
18 changes: 2 additions & 16 deletions librna/rnalib.c
Original file line number Diff line number Diff line change
@@ -70,32 +70,27 @@ void librna_read_param_file(const char *filename)
x = 5' base of the basepair
y = 3' base of the basepair
*/
static int bp_index(char x, char y)
int bp_index(char x, char y)
{
switch (x) {
case A_BASE : switch (y) {
case U_BASE : return AU_BP;
case GAP_BASE : return NO_BP;
}
break;
case C_BASE : switch (y) {
case G_BASE : return CG_BP;
case GAP_BASE : return NO_BP;
}
break;
case G_BASE : switch (y) {
case C_BASE : return GC_BP;
case U_BASE : return GU_BP;
case GAP_BASE : return NO_BP;
}
break;
case U_BASE : switch (y) {
case G_BASE : return UG_BP;
case A_BASE : return UA_BP;
case GAP_BASE : return NO_BP;
}
break;
case GAP_BASE : return NO_BP;
}
return NO_BP;
}
@@ -199,16 +194,7 @@ static void decode(char *s, const char *x, const int len)
{
unsigned int i;
for (i = 0; i < len; ++i) {
switch (x[i]) {
case 0 : s[i] = 'N'; break;
case 1 : s[i] = 'A'; break;
case 2 : s[i] = 'C'; break;
case 3 : s[i] = 'G'; break;
case 4 : s[i] = 'U'; break;
case 5 : s[i] = '_'; break;
case 6 : s[i] = '+'; break;
default: abort();
};
s[i] = BASE_CHARS[x[i]];
}
}

7 changes: 6 additions & 1 deletion librna/rnalib.h
Original file line number Diff line number Diff line change
@@ -7,7 +7,11 @@
#include <stdbool.h>
#endif

enum base_t { N_BASE, A_BASE, C_BASE, G_BASE, U_BASE, GAP_BASE, SEPARATOR_BASE };
enum base_t {
N_BASE, A_BASE, C_BASE, G_BASE, U_BASE, GAP_BASE, SEPARATOR_BASE };
static char BASE_CHARS[SEPARATOR_BASE+1] = {
'N', 'A', 'C', 'G', 'U', '_', '+'};

enum iupac_t { N_IUPAC = 0,
B_IUPAC = 7,
D_IUPAC = 8,
@@ -57,5 +61,6 @@ double scale(int x);


bool iupac_match(char base, char iupac_base);
int bp_index(char x, char y);

#endif
72 changes: 21 additions & 51 deletions rtlib/rna.hh
Original file line number Diff line number Diff line change
@@ -40,34 +40,13 @@ inline bool basepairing(const alphabet *seq,
{
if (j<=i+1)
return false;
char a = seq[i];
char b = seq[j-1];

switch (a) {
case A_BASE :
switch (b) {
case U_BASE : return true;
}
break;
case U_BASE :
switch (b) {
case A_BASE : return true;
case G_BASE : return true;
}
break;
case G_BASE :
switch (b) {
case C_BASE : return true;
case U_BASE : return true;
}
break;
case C_BASE :
switch (b) {
case G_BASE : return true;
}
break;
int basepair = bp_index(seq[i], seq[j-1]);
if ((basepair == N_BP) || (basepair == NO_BP)) {
return false;
} else {
return true;
}
return false;
}

template<typename alphabet, typename pos_type, typename T>
@@ -145,34 +124,25 @@ class BaseException : public std::exception {
}
};

inline char char_to_base(char a)
inline int char_to_base(char a)
{
char c = lower_case(a);
switch (c) {
case 'n' : return N_BASE;
case 'a' : return A_BASE;
case 'c' : return C_BASE;
case 'g' : return G_BASE;
case 'u' : return U_BASE;
case '_' : return GAP_BASE;
case '+' : return SEPARATOR_BASE;
default: throw BaseException(a);
};
char c = upper_case(a);
for (int i = 0; i <= SEPARATOR_BASE; ++i) {
if (c == BASE_CHARS[i]) {
return i;
}
}
throw BaseException(a);
return 0;
}

inline char base_to_char(char a)
inline char base_to_char(int a)
{
switch (a) {
case N_BASE : return 'n';
case A_BASE : return 'a';
case C_BASE : return 'c';
case G_BASE : return 'g';
case U_BASE : return 'u';
case GAP_BASE : return '_';
case SEPARATOR_BASE : return '+';
default: throw BaseException(a);
};
if (a < SEPARATOR_BASE+1) {
return BASE_CHARS[a];
} else {
throw BaseException(a);
}
return 0;
}

@@ -226,7 +196,7 @@ inline void append_deep_rna(rope::Ref<X> &str, const Basic_Subsequence<alphabet,
{
for (typename Basic_Subsequence<alphabet, pos_type>::const_iterator i = sub.begin();
i != sub.end(); ++i)
str.append(base_to_char(*i));
str.append((char) base_to_char(*i));
}

// ======== energy wrapper function ========
@@ -275,7 +245,7 @@ template<typename alphabet, typename pos_type>
inline int hl_energy_stem(const Basic_Subsequence<alphabet, pos_type> &a)
{
int energy = 0;

for (unsigned k = 0; k < a.seq->rows(); k++)
energy += hl_energy_stem(a.seq->row(k), a.i-1, a.j);

16 changes: 14 additions & 2 deletions rtlib/sequence.hh
Original file line number Diff line number Diff line change
@@ -287,7 +287,7 @@ class Basic_Sequence {
return *this;
}

alphabet &operator[](pos_type x)
alphabet &operator[](pos_type x)
{
assert(x < n);
return seq[x];
@@ -328,13 +328,25 @@ inline char lower_case(char c)
if (c == '_')
return c;
if (c == '+')
return c;
return c;
if (c < 'a')
return c+('a'-'A');
else
return c;
}

inline char upper_case(char c)
{
if (c == '_')
return c;
if (c == '+')
return c;
if (c >= 'a')
return c-('a'-'A');
else
return c;
}

//template<typename alphabet, typename pos_type>
inline const char &column(const M_Char /*<alphabet, pos_type>*/ &c, /*pos_type*/ unsigned l)
{
6 changes: 3 additions & 3 deletions testdata/unittest/rna.cc
Original file line number Diff line number Diff line change
@@ -46,7 +46,7 @@ BOOST_AUTO_TEST_CASE ( app_subseq_rna )
Subsequence x(s, 3, 6);
append_deep_rna(r, x);
Rope q;
append(q, "uag");
append(q, "UAG");
CHECK_EQ(r, q);
}

@@ -88,15 +88,15 @@ BOOST_AUTO_TEST_CASE ( iupac )
CHECK(!filter.query(6, 14));
CHECK(filter.query(6, 22));
CHECK(filter.query(16, 21));

char sequence2[14] = "ccacuccucccgg";
Sequence seq2(sequence2);
char_to_rna(seq2);
iupac_filter<char, unsigned> filter2;
const char pattern2[] = "ccuccuccc";
filter2.init(seq2,pattern2);
CHECK(!filter2.query(2,11));

iupac_filter<char, unsigned> filter3;
const char pattern3[] = "acuccuccc";
filter3.init(seq2,pattern3);

0 comments on commit ad7e4be

Please sign in to comment.