File: find_cpg.c

package info (click to toggle)
python-bx 0.13.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 5,000 kB
  • sloc: python: 17,136; ansic: 2,326; makefile: 24; sh: 8
file content (66 lines) | stat: -rw-r--r-- 1,694 bytes parent folder | download | duplicates (4)
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
#include <stdlib.h>
/*
  Author: Ian N Schenck
  Version: 7/21/2006

  Most of this was ripped out of James Taylor's never-released code,
  and plugged in here for use in Python.  Slight modifications were
  made where I saw fit.

  It looks as if CpG's are typically not next to gaps.
*/

static inline int is_cpg( char * sp1, char * sp2, int pos)
{
  if ( pos < 1 ) return 0;
  if ( sp1[pos + 1] == '\0' ) return 0;
  if ( sp1[pos - 1] != 'C' && sp2[pos - 1] != 'C' &&
       sp1[pos + 1] == 'G' && sp2[pos + 1] == 'G' &&
       (sp1[pos] == 'C' || sp2[pos] == 'C') ) return 1;
  if ( sp1[pos + 1] != 'G' && sp2[pos + 1] != 'G' &&
       sp1[pos - 1] == 'C' && sp2[pos - 1] == 'C' &&
       (sp1[pos] == 'G' || sp2[pos] == 'G') ) return 1;
  return 0;
}

static inline int is_non_cpg( char * sp1, char * sp2, int pos)
{
  // first one can't assuredly be cpg
  if ( pos < 1 ) return 1;
  if ( sp1[pos + 1] == '\0' ) return 0;
  return
    ( sp1[pos - 1] != 'C' && sp2[pos - 1] != 'C' &&
      sp1[pos + 1] != 'G' && sp2[pos + 1] != 'G' );
}

static inline int is_cpg_restricted( char * sp1, char * sp2, int pos )
{
  return !is_non_cpg( sp1, sp2, pos );
}

int next( char * sp1, char * sp2, int start, int (*func)(char*,char*,int))
{
  while( sp1[start+1] != '\0')
    {
      if( func(sp1, sp2, start) )
	return start;
      start++;
    }
  // nothing found
  return -1;
}

int next_cpg( char * sp1, char * sp2, int start)
{
  return next( sp1, sp2, start, &is_cpg);
}

int next_cpg_restricted( char * sp1, char *sp2, int start)
{
  return next( sp1, sp2, start, &is_cpg_restricted );
}

int next_non_cpg( char * sp1, char * sp2, int start)
{
  return next( sp1, sp2, start, &is_non_cpg);
}