File: seqerror.dy

package info (click to toggle)
wise 2.4.1-21
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, sid
  • size: 27,140 kB
  • sloc: ansic: 276,365; makefile: 1,003; perl: 886; lex: 93; yacc: 81; sh: 24
file content (223 lines) | stat: -rw-r--r-- 4,412 bytes parent folder | download
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

%{
#include "wisebase.h"
#include "probability.h"
#include "aln.h"
#include "sequence.h"

enum SeqErrorType {
  SeqErrorInsertion,
  SeqErrorDeletion,
  SeqErrorSubstitution
};

#define SequenceErrorSetLISTLENGTH 128

%}


struct SequenceError
int type
int start
int end
char * replaced_bases
Probability probability
char * inserted_bases
char * suspected_deletion
%info
This holds information about
a particular insertion or
deletion for one sequence
%%

struct SequenceErrorSet
SequenceError ** error !list
%info
This holds a set of insertion
or deletions for a sequence
%%

struct ErrorSequence
Sequence * seq
SequenceErrorSet * ses
%info
This holds a sequence and what
errors have occurred in it.
%%


%{
#include "seqerror.h"


%func
Makes a sequence error set from standard genewise labels
%%
SequenceErrorSet * genewise_SequenceErrorSet(AlnSequence * als )
{
  SequenceErrorSet * out;
  SequenceError * se;
  AlnUnit * alu;

  assert(als);
  out = SequenceErrorSet_alloc_std();
  
  for(alu=als->start;alu;alu = alu->next ) {
    if( strcmp(alu->text_label,"SEQUENCE_INSERTION") == 0 ) {
      se = SequenceError_alloc();
      add_SequenceErrorSet(out,se);
      se->start = alu->start+1;
      se->end   = alu->end;
      
      se->type = SeqErrorInsertion;
      se->inserted_bases = stringalloc("?");
      se->replaced_bases = stringalloc("NNN");
    } else if ( strcmp(alu->text_label,"SEQUENCE_DELETION") == 0 ) {
      se = SequenceError_alloc();
      se->start = alu->start+1;
      se->end   = alu->end;
      se->type = SeqErrorDeletion;
      se->replaced_bases = stringalloc("NNN");
      add_SequenceErrorSet(out,se);
    }
  }

  return out;
}



%func
Displays a set of sequence errors in space deliminted format
%%
void show_SequenceErrorSet(SequenceErrorSet * ses,FILE * ofp)
{
  int i;

  for(i=0;i<ses->len;i++) {
    show_SequenceError(ses->error[i],ofp);
  }
}

    

%func
Displays sequence error in space deliminted format
%%
void show_SequenceError(SequenceError * se,FILE * ofp)
{
  fprintf(ofp,"%c %5d %5d %10s (%10s)\n",
	  (se->type == SeqErrorInsertion ? 'I' : 
	   (se->type == SeqErrorDeletion ? 'D' : 'S')),
	  se->start,se->end,
	  se->replaced_bases == NULL ? "-none-" : se->replaced_bases,
	  se->type == SeqErrorInsertion ? se->inserted_bases : "-");
}

%func
Makes an error sequence (DNA) with set substitution
and insertion/deletion rates.
%%
ErrorSequence * make_ErrorSequence(Sequence * seq,Probability subs,Probability insertion,Probability deletion)
{
  ErrorSequence * out;
  
  char * seq_buffer;
  int buf_len;
  int i,j;
  double rnd;
  char c[2];
  SequenceError * se;
  
  i=j=0;

  out = ErrorSequence_alloc();
  out->ses = SequenceErrorSet_alloc_std();
  c[1] = '\0';

  buf_len = seq->len *3;
  seq_buffer = (char*)calloc(buf_len,sizeof(char));
  
  for(i=0;i<seq->len;i++) {
    if( j >= buf_len ) {
      warn("run out of temporary buffer in error sequence");
      break;
    }

    rnd = random_0_to_1();

    if( rnd > deletion ) {
      /* yes this base is here */
      rnd = random_0_to_1();
      if( rnd < subs ) {
	rnd = random_0_to_1();
	if( rnd < 0.25 ) {
	  c[0] = 'A';
	} else if ( rnd < 0.5 ) {
	  c[0] = 'T';
	} else if ( rnd < 0.75 ) {
	  c[0] = 'G';
	} else {
	  c[0] = 'C';
	}

	se = SequenceError_alloc();
	se->type = SeqErrorSubstitution;
	se->start = j+1;
	se->end = j+1;
	se->replaced_bases = stringalloc(c);
	add_SequenceErrorSet(out->ses,se);
	seq_buffer[j++] = c[0];
      } else {
	seq_buffer[j++] = seq->seq[i];
      }
    } else {
      /* there has been a deletion */
      se = SequenceError_alloc();
      se->type = SeqErrorDeletion;
      se->start = j+1;
      se->end = j+1;
      c[0] = seq_buffer[i];
      se->suspected_deletion = stringalloc(c);
      add_SequenceErrorSet(out->ses,se);
    }

    rnd = random_0_to_1();
    
    if( rnd < insertion ) {
   
      rnd = random_0_to_1();
      if( rnd < 0.25 ) {
	c[0] = 'A';
      } else if ( rnd < 0.5 ) {
	c[0] = 'T';
      } else if ( rnd < 0.75 ) {
	c[0] = 'G';
      } else {
	c[0] = 'C';
      }

      se = SequenceError_alloc();
      se->type = SeqErrorInsertion;
      se->start = j+1;
      se->end = j+1;
      se->inserted_bases = stringalloc(c);
      add_SequenceErrorSet(out->ses,se);
      seq_buffer[j++] = c[0];
    }
  }

  seq_buffer[j] = '\0';

  out->seq = new_Sequence_from_strings(seq->name,seq_buffer);

  return out;
}