-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathParseGenBankW.pas
More file actions
369 lines (267 loc) · 12.1 KB
/
Copy pathParseGenBankW.pas
File metadata and controls
369 lines (267 loc) · 12.1 KB
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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
UNIT ParseGenBankW;
{=============================================================================================================
Gabriel Moraru
2016.07
==============================================================================================================
GBK to Fasta exporter
===============================================================================}
INTERFACE
USES
Winapi.Windows, System.SysUtils, System.Classes,
ccCore, ccINIFile, clRamLog, ccRichLog, ReadBasicSample, ParseGenBankR;
TYPE
TGbkObjExp = class(TGbkObj)
private
function wrapOrigin: string;
protected
public
Wrap: Boolean; { If true, the bases are wrapped to 80 chars when saving to disk }
NameUse_Strain : Boolean; { which fields should be included in file's name }
NameUse_Clone : Boolean;
NameUse_Isolate : Boolean;
NameUse_Gene : Boolean;
NameUse_Product : Boolean;
NameUse_Organism: Boolean;
NameUse_Version : Boolean;
NameUse_Definit : Boolean;
IgnoreProduct : Boolean; { When generating filename, ignore the 'PRODUCT' field but only if 'GENE' field is not empty }
constructor Create(aLog: TRamLog);
destructor Destroy; override;
procedure Clear; override;
{Export}
function AsText: string; { Get the content of this GBK object as text so I can save it to disk }
procedure SaveAsFasta (CONST FullFileName: string);
procedure SaveAsFasta_AutoName (CONST Folder: string);
procedure GenerateComment; override;
function GenerateNameFromField: string;
function GenerateNameFromFile (CONST Nume: string): string;
end;
IMPLEMENTATION
USES
ReadFasta, ccIO;
{--------------------------------------------------------------------------------------------------
CREATE / DESTROY
--------------------------------------------------------------------------------------------------}
constructor TGbkObjExp.Create;
begin
inherited Create(aLog); { Should I call "inherited" in the constructor of a class derived from TObject or TPersistent? Yes. It does nothing, true, but it's harmless. I think there is value in being consistent about always calling the inherited constructor, without checking to see if there is, in fact, an implementation. Some will say that it's worth calling inherited Create because Embarcadero might add an implementation for TObject.Create in the future, but I doubt this is true; it would break existing code which does not call inherited Create. Still, I think it is a good idea to call it for the reason of consistency alNone. }
NameUse_Strain := TRUE; { which fields should be included in file's name }
NameUse_Clone := TRUE;
NameUse_Isolate := TRUE;
NameUse_Gene := TRUE;
NameUse_Product := TRUE;
NameUse_Organism:= TRUE;
NameUse_Version := TRUE;
NameUse_Definit := TRUE;
IgnoreProduct := TRUE; { When generating FileNameParent, ignore the 'PRODUCT' field but only if 'GENE' field is not empty }
Wrap:= TRUE;
end;
destructor TGbkObjExp.Destroy;
begin
inherited Destroy;
end;
procedure TGbkObjExp.Clear;
begin
inherited;
NameUse_Strain := TRUE; { which fields should be included in file's name }
NameUse_Clone := TRUE;
NameUse_Isolate := TRUE;
NameUse_Gene := TRUE;
NameUse_Product := TRUE;
NameUse_Organism:= TRUE;
NameUse_Version := TRUE;
NameUse_Definit := TRUE;
IgnoreProduct := TRUE; { When generating FileNameParent, ignore the 'PRODUCT' field but only if 'GENE' field is not empty }
end;
{--------------------------------------------------------------------------------------------------
EXPORT AS GBK
--------------------------------------------------------------------------------------------------}
function TGbkObjExp.AsText: string; { Get the content of this GBK object as text so I can save it to disk }
VAR
i: Integer;
procedure AddRecord(RecordName, Data: string; Force: Boolean = FALSE); { Force = force title to be written to disk even if Line is empty }
VAR
i: Integer;
TSL: TStringList;
begin
RecordName:= MakeStringLongRight(RecordName, ' ', HeaderLength);
if Length(Data) > 68 then
begin
Data:= WrapText(Data, 68);
TSL:= SplitText(Data, CRLF);
for i:= 1 to tsl.Count-1 DO
tsl[i]:= ' '+ tsl[i]; // 12 spaces
Data:= TSL.Text;
FreeAndNil(TSL);
end;
Data:= RemoveLastEnter(Data);
if Force
OR (Data > '')
then Result:= Result+ RecordName+ Data+ CRLF;
end;
procedure AddFeatField(FeatName, Data: string); { Force = force title to be written to disk even if Line is empty }
begin
if Data > '' then
begin
//FeatName:= '/'+ FeatName;
FeatName:= StringOfChar(' ', DataStartFeatures) + '/'+ FeatName;
Result:= Result+ FeatName+ '="'+ Data+ '"' + CRLF;
end;
end;
begin
Result:= '';
AddRecord('LOCUS' , Records.LOCUS);
AddRecord('DEFINITION' , Records.DEFINITION);
AddRecord('ACCESSION' , Records.ACCESSION);
AddRecord('VERSION' , Records.VERSION);
AddRecord('KEYWORDS' , Records.KEYWORDS+ '.');
AddRecord('SOURCE' , Records.Source.default, Records.Source.organism.default > '');
AddRecord(' ORGANISM' , Records.Source.organism.default);
AddRecord(' ' , Records.Source.organism.Data+ '.');
AddRecord('REFERENCE', Records.REFERENCE.Default);
for i:= 0 to High(Records.REFERENCE.RefList) DO
begin
AddRecord(' AUTHORS' , Records.REFERENCE.RefList[i].authors);
AddRecord(' TITLE' , Records.REFERENCE.RefList[i].TITLE);
AddRecord(' JOURNAL' , Records.REFERENCE.RefList[i].JOURNAL);
AddRecord(' PUBMED' , Records.REFERENCE.RefList[i].PUBMED);
end;
AddRecord('COMMENT' , Records.COMMENT);
AddRecord('FEATURES' , Records.FEATURES.Default);
AddFeatField('organism' , Records.FEATURES.organism);
AddFeatField('isolate' , Records.FEATURES.isolate);
AddFeatField('clone' , Records.FEATURES.clone);
AddFeatField('mol_type' , Records.FEATURES.mol_type);
AddFeatField('strain' , Records.FEATURES.strain);
AddFeatField('note' , Records.FEATURES.note);
AddFeatField('gene' , Records.FEATURES.gene);
AddFeatField('codon_start' , Records.FEATURES.codon_start);
AddFeatField('transl_table', Records.FEATURES.transl_table);
AddFeatField('product' , Records.FEATURES.product);
AddFeatField('protein_id' , Records.FEATURES.protein_id);
AddFeatField('db_xref' , Records.FEATURES.db_xref);
AddFeatField('translation' , Records.FEATURES.translation);
AddRecord('ORIGIN' , '', TRUE); { This is the sequence }
Result:= Result+ WrapOrigin; //string(Records.ORIGIN);
Result:= Result+ ctGbkEndOfSample;
end;
function TGbkObjExp.WrapOrigin: string;
VAR
Line, s: string;
i: Integer;
procedure AddLine;
VAR LineHdr: string;
begin
if Length(Line) = 60
then LineHdr:= IntToStr(i- 60 +1)+ ' '
else LineHdr:= IntToStr(i- Length(Line))+ ' ';
LineHdr:= MakeStringLongLeft(LineHdr, ' ', 10);
Line:= InsertCharEvery(' ', Line, 10);
Result:= Result+ LineHdr+ Line+ CRLF; { cand textul are fix 'spnSplit.Value' caractere pe rand, imi adauga un rand gol. }
Line:= '';
end;
Begin
Result:= '';
Line:= '';
s:= String(Records.ORIGIN);
for i:= 1 TO Length(s) DO
begin
Line:= Line+ s[i]; {TODO 5: THIS IS SLOW. I need some kind of string builder here }
{
if i mod 10 = 0
then Line:= Line+ ' '; }
if i mod 60 = 0 { trunchez liniile la 65 caractere }
then AddLine;
end;
if FirstChar(Line) <> ' '
then AddLine;
End;
{--------------------------------------------------------------------------------------------------
EXPORT AS FASTA
--------------------------------------------------------------------------------------------------}
procedure TGbkObjExp.SaveAsFasta (CONST FullFileName: string);
VAR ObjFasta: TFastaObj;
begin
ObjFasta:= TFastaObj.Create(RamLog);
TRY
ObjFasta.Bases:= Bases;
ObjFasta.Comment:= Comment;
ObjFasta.SaveAsFasta(FullFileName, Wrap);
FINALLY
FreeAndNil(ObjFasta);
END;
end;
procedure TGbkObjExp.SaveAsFasta_AutoName(CONST Folder: string);
VAR ObjFasta: TFastaObj;
s: String;
begin
ObjFasta:= TFastaObj.Create(RamLog);
TRY
s:= GenerateNameFromField;
FileName:= Trail(Folder)+ s+ '.FASTA';
ObjFasta.Bases:= Bases;
ObjFasta.Comment:= s;
{ Check name lenght }
if Length(FileName) < Max_Path
then ObjFasta.SaveAsFasta(FileName, Wrap) { TRY TO SAVE AS FASTA }
else RamLog.AddWarn(' File name is too long: '+ FileName)
FINALLY
FreeAndNil(ObjFasta);
END;
end;
{--------------------------------------------------------------------------------------------------
GENEREAZA NUMELE
--------------------------------------------------------------------------------------------------}
function TGbkObjExp.GenerateNameFromField: string; { cand am multi GenBank }
VAR
strain, definit, isolate,
clone, gene, product,
organism, version: string;
begin
if (Records.FEATURES.organism <> '') AND NameUse_Organism { which Records should be included in file's name }
then organism:= Records.FEATURES.organism;
if (Records.DEFINITION <> '')
AND NameUse_Definit
then definit := ' ' + Records.DEFINITION;
if (Records.FEATURES.strain <> '')
AND NameUse_Strain
then strain := ' strain ' + Records.FEATURES.strain;
if (Records.FEATURES.clone <> '')
AND NameUse_Clone
then clone := ' clone ' + Records.FEATURES.clone;
if (Records.FEATURES.isolate <> '')
AND NameUse_Isolate
then isolate := ' isolate '+ Records.FEATURES.isolate;
if (Records.FEATURES.gene <> '')
AND NameUse_Gene
then gene:= ' '+ Records.FEATURES.gene;
if (Records.FEATURES.product <> '')
AND NameUse_Product
then
if (Records.FEATURES.gene <> '')
AND IgnoreProduct AND NameUse_Gene { When generating FileName, ignore the 'PRODUCT' field but only if 'GENE' field is not empty }
then product := ''
else product := ' ' + Records.FEATURES.product;
if (Records.VERSION <> '')
AND NameUse_Version
then version := ' ' + Records.VERSION;
Result:= organism+ definit+ strain+ clone+ isolate+ gene+ product+ version;
Result:= CorrectFilename(Result, ' ');
end;
function TGbkObjExp.GenerateNameFromFile(CONST Nume: string): string; { mai imi trebuie asta? poate pt Cris }
VAR
strain, isolate, clone: string;
begin
if Records.FEATURES.strain <> '' then strain := ' strain ' + Records.FEATURES.strain;
if Records.FEATURES.clone <> '' then clone := ' clone ' + Records.FEATURES.clone;
if Records.FEATURES.isolate<> '' then isolate:= ' isolate '+ Records.FEATURES.isolate;
Result:= ExtractOnlyName(Nume);
Result:= CopyTo(Result,1,Pos(' ',Result));
Result:= Records.FEATURES.organism+ strain+ clone+ isolate+ ' [] '+ Result + Records.VERSION;
Result:= CorrectFilename(Result, ' ');
end;
procedure TGbkObjExp.GenerateComment; { GENERATE COMMENTS }
begin
Comment:= '> '+ GenerateNameFromField;
end;
end.