-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdna.pas
163 lines (145 loc) · 3.17 KB
/
dna.pas
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
unit DNA;
{$MODE DELPHI}
interface
uses
Classes, SysUtils, StrUtils;
type
TDNA = class(TObject)
private
seq: string;
{cds, tally: TStringList;}
public
constructor Create(s: string); { Create DNA instance initialized to string s. }
function Sequence: string; { return DNA sequence. }
function Transcribe: string; { Return as rna string. }
function Reverse: string; { Return dna string in reverse order. }
function Complement: string; { Return the complementary dna string. }
function ReverseComplement: string;
{ Return the reverse complement of the dna string. }
function GC: double; { Return the percentage of dna composed of G+C. }
function Codons: TStringList; { Return list of codons for the dna string. }
function Frequency(cds: TStringList): TStringList;
{ Return frequency table of codons for the dna string. }
end;
implementation
constructor TDNA.Create(s: string);
begin
seq := UpperCase(s);
end;
{destructor TDNA.Destroy;
begin
if Assigned(cds) then
FreeAndNil(cds);
if Assigned(tally) then
FreeAndNil(tally);
end;}
function TDNA.Sequence: string;
begin
Result := seq;
end;
function TDNA.Transcribe: string;
begin
Result := StringReplace(seq, 'T', 'U', [rfReplaceAll, rfIgnoreCase]);
end;
function TDNA.Reverse: string;
begin
Result := ReverseString(seq);
end;
function TDNA.Complement: string;
var
c: string;
i: integer;
begin
c := '';
for i := 1 to Length(seq) do
begin
if (seq[i] = 'A') then
c := c + 'T'
else if (seq[i] = 'T') then
c := c + 'A'
else if (seq[i] = 'C') then
c := c + 'G'
else if (seq[i] = 'G') then
c := c + 'C'
else
c := c + seq[i];
end;
Result := c;
end;
function TDNA.ReverseComplement: string;
var
s, c: string;
i: integer;
begin
s := ReverseString(seq);
c := '';
for i := 1 to Length(s) do
begin
if (s[i] = 'A') then
c := c + 'T'
else if (s[i] = 'T') then
c := c + 'A'
else if (s[i] = 'C') then
c := c + 'G'
else if (s[i] = 'G') then
c := c + 'C'
else
c := c + s[i];
end;
Result := c;
end;
function TDNA.GC: double;
var
c: double;
begin
c := seq.CountChar('G') + seq.CountChar('C');
Result := c * 100.0 / Length(seq);
end;
function TDNA.Codons: TStringList;
var
i, c: integer;
codon: string;
cds: TStringList;
begin
c := 0;
codon := '';
cds := TStringList.Create;
for i := 1 to Length(seq) do
begin
codon := codon + seq[i];
Inc(c);
if (c = 3) then
begin
cds.Add(codon);
codon := '';
c := 0;
end;
end;
Result := cds;
end;
function TDNA.Frequency(cds: TStringList): TStringList;
var
tally: TStringList;
i, k: integer;
function search(arr: TStringList; s: string; n: integer): integer;
var
j, counter: integer;
begin
counter := 0;
for j := 0 to n - 1 do
if arr[j] = s then
Inc(counter);
Result := counter;
end;
begin
tally := TStringList.Create;
tally.Sorted := True;
tally.Duplicates := dupIgnore;
for i := 0 to cds.Count - 1 do
begin
k := search(cds, cds[i], cds.Count);
tally.Add(cds[i] + tally.NameValueSeparator + IntToStr(k));
end;
Result := tally;
end;
end.