-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPluginDefinition.cpp
More file actions
577 lines (495 loc) · 14.9 KB
/
PluginDefinition.cpp
File metadata and controls
577 lines (495 loc) · 14.9 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
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
//this file is part of notepad++
//Copyright (C)2022 Don HO <don.h@free.fr>
//
//This program is free software; you can redistribute it and/or
//modify it under the terms of the GNU General Public License
//as published by the Free Software Foundation; either
//version 2 of the License, or (at your option) any later version.
//
//This program is distributed in the hope that it will be useful,
//but WITHOUT ANY WARRANTY; without even the implied warranty of
//MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
//GNU General Public License for more details.
//
//You should have received a copy of the GNU General Public License
//along with this program; if not, write to the Free Software
//Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#include "PluginDefinition.h"
#include "menuCmdID.h"
#include <string>
#include <algorithm>
//
// The plugin data that Notepad++ needs
//
FuncItem funcItem[nbFunc];
//
// The data of Notepad++ that you can use in your plugin commands
//
NppData nppData;
//
// Initialize your plugin data here
// It will be called while plugin loading
void pluginInit(HANDLE /*hModule*/)
{
}
//
// Here you can do the clean up, save the parameters (if any) for the next session
//
void pluginCleanUp()
{
}
//
// Initialization of your plugin commands
// You should fill your plugins commands here
void commandMenuInit()
{
//--------------------------------------------//
//-- STEP 3. CUSTOMIZE YOUR PLUGIN COMMANDS --//
//--------------------------------------------//
// with function :
// setCommand(int index, // zero based number to indicate the order of command
// TCHAR *commandName, // the command name that you want to see in plugin menu
// PFUNCPLUGINCMD functionPointer, // the symbol of function (function pointer) associated with this command. The body should be defined below. See Step 4.
// ShortcutKey *shortcut, // optional. Define a shortcut to trigger this command
// bool check0nInit // optional. Make this menu item be checked visually
// );
setCommand(0, TEXT("Reverse-Complement"), revcomp, NULL, false);
setCommand(1, TEXT("Translate"), translate, NULL, false);
setCommand(2, TEXT("Next ORF"), nextORF, NULL, false);
}
//
// Here you can do the clean up (especially for the shortcut)
//
void commandMenuCleanUp()
{
// Don't forget to deallocate your shortcut here
}
//
// This function help you to initialize your plugin commands
//
bool setCommand(size_t index, TCHAR *cmdName, PFUNCPLUGINCMD pFunc, ShortcutKey *sk, bool check0nInit)
{
if (index >= nbFunc)
return false;
if (!pFunc)
return false;
lstrcpy(funcItem[index]._itemName, cmdName);
funcItem[index]._pFunc = pFunc;
funcItem[index]._init2Check = check0nInit;
funcItem[index]._pShKey = sk;
return true;
}
//----------------------------------------------//
//-- STEP 4. DEFINE YOUR ASSOCIATED FUNCTIONS --//
//----------------------------------------------//
std::string getSelectedText(HWND scintillaHandle)
{
std::string selectedText;
// Determine the buffer size needed for the selected text
LPARAM length = ::SendMessage(scintillaHandle, SCI_GETSELTEXT, 0, (LPARAM)nullptr);
if (length > 0)
{
char* buffer = new char[length + 1];
buffer[0] = '\0'; // Ensure the buffer is null-terminated
// Retrieve the selected text
::SendMessage(scintillaHandle, SCI_GETSELTEXT, 0, (LPARAM)buffer);
selectedText = buffer;
delete[] buffer;
}
else {
selectedText = "";
}
return selectedText;
}
void replaceSelection(std::string text, HWND scintillaHandle)
{
/*std::wstring debugMessage = L"Will replace current selection with: " + std::wstring(text.begin(), text.end());
OutputDebugString(debugMessage.c_str());*/
::SendMessage(scintillaHandle, SCI_REPLACESEL, 0, (LPARAM)text.c_str());
}
bool checkCharacters(std::string seq, std::string charset)
{
if (charset == "DNA")
{
std::transform(seq.begin(), seq.end(), seq.begin(), [](unsigned char c) { return static_cast<char>(std::tolower(c)); });
for (char c: seq)
{
if (c != 'a' && c != 'c' && c != 't' && c != 'g' && c != ' ')
{
/*std::string cs(1, c);
std::wstring debugMessage = L"Incorrect character: " + std::wstring(cs.begin(), cs.end());
OutputDebugString(debugMessage.c_str());*/
return false;
}
}
}
else
{
// undefined charset
/*std::wstring debugMessage2 = L"Undefined charset: " + std::wstring(charset.begin(), charset.end());
OutputDebugString(debugMessage2.c_str());*/
return false;
}
return true;
}
std::string reverseComplement(std::string seq)
{
std::reverse(seq.begin(), seq.end());
std::string comp;
char c;
for (size_t i = 0; i < seq.length(); ++i)
{
switch (seq[i])
{
case 'a':
c = 't';
break;
case 'A':
c = 'T';
break;
case 't':
c = 'a';
break;
case 'T':
c = 'A';
break;
case 'c':
c = 'g';
break;
case 'C':
c = 'G';
break;
case 'g':
c = 'c';
break;
case 'G':
c = 'C';
break;
default:
c = 'N';
}
comp += c;
}
/*std::wstring debugMessage = L"Finished rev-comp: " + std::wstring(comp.begin(), comp.end());
OutputDebugString(debugMessage.c_str());*/
return comp;
}
void revcomp()
{
// Get a handle to the current Scintilla editor
int which = -1;
::SendMessage(nppData._nppHandle, NPPM_GETCURRENTSCINTILLA, 0, (LPARAM)&which);
if (which == -1)
return ;
HWND scintillaHandle = (which == 0) ? nppData._scintillaMainHandle : nppData._scintillaSecondHandle;
std::string selection = getSelectedText(scintillaHandle);
bool check = checkCharacters(selection, "DNA");
if (!check)
{
/*std::wstring debugMessage2 = L"Characters were not all recognized, aborting";
OutputDebugString(debugMessage2.c_str());*/
return;
}
std::string revcomp = reverseComplement(selection);
replaceSelection(revcomp, scintillaHandle);
}
std::string translateDNA(std::string seq)
{
std::transform(seq.begin(), seq.end(), seq.begin(), [](unsigned char c) { return static_cast<char>(std::toupper(c)); });
std::string translation;
std::string codon;
for (size_t i = 0; i < seq.length() / 3; ++i)
{
codon = seq.substr(i*3, 3);
/* Code written with R:
* Biostrings::GENETIC_CODE |>
purrr::imap(~paste0('if(codon == "',.y, '"){
translation += "',.x,'";
}')) |>
paste0(collapse = "\n") |>
clipr::write_clip()
*/
if (codon == "TTT") {
translation += "F";
}
if (codon == "TTC") {
translation += "F";
}
if (codon == "TTA") {
translation += "L";
}
if (codon == "TTG") {
translation += "L";
}
if (codon == "TCT") {
translation += "S";
}
if (codon == "TCC") {
translation += "S";
}
if (codon == "TCA") {
translation += "S";
}
if (codon == "TCG") {
translation += "S";
}
if (codon == "TAT") {
translation += "Y";
}
if (codon == "TAC") {
translation += "Y";
}
if (codon == "TAA") {
translation += "*";
}
if (codon == "TAG") {
translation += "*";
}
if (codon == "TGT") {
translation += "C";
}
if (codon == "TGC") {
translation += "C";
}
if (codon == "TGA") {
translation += "*";
}
if (codon == "TGG") {
translation += "W";
}
if (codon == "CTT") {
translation += "L";
}
if (codon == "CTC") {
translation += "L";
}
if (codon == "CTA") {
translation += "L";
}
if (codon == "CTG") {
translation += "L";
}
if (codon == "CCT") {
translation += "P";
}
if (codon == "CCC") {
translation += "P";
}
if (codon == "CCA") {
translation += "P";
}
if (codon == "CCG") {
translation += "P";
}
if (codon == "CAT") {
translation += "H";
}
if (codon == "CAC") {
translation += "H";
}
if (codon == "CAA") {
translation += "Q";
}
if (codon == "CAG") {
translation += "Q";
}
if (codon == "CGT") {
translation += "R";
}
if (codon == "CGC") {
translation += "R";
}
if (codon == "CGA") {
translation += "R";
}
if (codon == "CGG") {
translation += "R";
}
if (codon == "ATT") {
translation += "I";
}
if (codon == "ATC") {
translation += "I";
}
if (codon == "ATA") {
translation += "I";
}
if (codon == "ATG") {
translation += "M";
}
if (codon == "ACT") {
translation += "T";
}
if (codon == "ACC") {
translation += "T";
}
if (codon == "ACA") {
translation += "T";
}
if (codon == "ACG") {
translation += "T";
}
if (codon == "AAT") {
translation += "N";
}
if (codon == "AAC") {
translation += "N";
}
if (codon == "AAA") {
translation += "K";
}
if (codon == "AAG") {
translation += "K";
}
if (codon == "AGT") {
translation += "S";
}
if (codon == "AGC") {
translation += "S";
}
if (codon == "AGA") {
translation += "R";
}
if (codon == "AGG") {
translation += "R";
}
if (codon == "GTT") {
translation += "V";
}
if (codon == "GTC") {
translation += "V";
}
if (codon == "GTA") {
translation += "V";
}
if (codon == "GTG") {
translation += "V";
}
if (codon == "GCT") {
translation += "A";
}
if (codon == "GCC") {
translation += "A";
}
if (codon == "GCA") {
translation += "A";
}
if (codon == "GCG") {
translation += "A";
}
if (codon == "GAT") {
translation += "D";
}
if (codon == "GAC") {
translation += "D";
}
if (codon == "GAA") {
translation += "E";
}
if (codon == "GAG") {
translation += "E";
}
if (codon == "GGT") {
translation += "G";
}
if (codon == "GGC") {
translation += "G";
}
if (codon == "GGA") {
translation += "G";
}
if (codon == "GGG") {
translation += "G";
}
}
return translation;
}
void translate()
{
// Get a handle to the current Scintilla editor
int which = -1;
::SendMessage(nppData._nppHandle, NPPM_GETCURRENTSCINTILLA, 0, (LPARAM)&which);
if (which == -1)
return;
HWND scintillaHandle = (which == 0) ? nppData._scintillaMainHandle : nppData._scintillaSecondHandle;
std::string selection = getSelectedText(scintillaHandle);
bool check = checkCharacters(selection, "DNA");
if (!check)
{
/*std::wstring debugMessage2 = L"Characters were not all recognized, aborting";
OutputDebugString(debugMessage2.c_str());*/
return;
}
if (selection.length() % 3 != 0)
{
/*std::wstring debugMessage2 = L"Selection not a multiple of 3, aborting";
OutputDebugString(debugMessage2.c_str());*/
return;
}
std::string translation = translateDNA(selection);
replaceSelection(translation, scintillaHandle);
}
char* getText(HWND sciHandle, size_t start_pos, size_t end_pos)
{
Sci_TextRangeFull tr;
tr.chrg.cpMin = start_pos;
tr.chrg.cpMax = end_pos;
tr.lpstrText = new char[end_pos - start_pos + 1];
::SendMessage(sciHandle, SCI_GETTEXTRANGEFULL, 0, (LPARAM)&tr);
return(tr.lpstrText);
}
size_t nextStartCodon(std::string text)
{
// find the position of the first start codon
size_t pos = text.find("ATG");
return(pos);
}
size_t nextStopCodon(std::string text, size_t pos_start)
{
// find position of next stop codon
text = text.substr(pos_start, text.length());
std::string translation = translateDNA(text);
size_t pos = translation.find("*");
return( pos_start + 3*(pos+1) );
}
void nextORF(){
// Get a handle to the current Scintilla editor
int which = -1;
::SendMessage(nppData._nppHandle, NPPM_GETCURRENTSCINTILLA, 0, (LPARAM)&which);
if (which == -1)
return;
HWND scintillaHandle = (which == 0) ? nppData._scintillaMainHandle : nppData._scintillaSecondHandle;
/* Two cases
* if text is selected, look for ORF inside it
* if no text selected, look for ORF between current cursor position and end of document
*/
size_t selStart = (size_t)::SendMessage(scintillaHandle, SCI_GETSELECTIONSTART, 0, 0);
size_t selEnd = (size_t)::SendMessage(scintillaHandle, SCI_GETSELECTIONEND, 0, 0);
if (selEnd == selStart)
{
selEnd = (size_t)::SendMessage(scintillaHandle, SCI_GETTEXTLENGTH, 0, 0);
}
std::string selText = getText(scintillaHandle, selStart, selEnd);
std::transform(selText.begin(), selText.end(), selText.begin(), [](unsigned char c) { return static_cast<char>(std::toupper(c)); });
bool check = checkCharacters(selText, "DNA");
if (!check)
{
std::wstring debugMessage2 = L"Characters were not all recognized, aborting";
OutputDebugString(debugMessage2.c_str());
return;
}
size_t posStart = nextStartCodon(selText);
size_t posStop = nextStopCodon(selText, posStart);
// back to document coords
posStart = posStart + selStart;
posStop = posStop + selStart;
if (posStop == selStart)
{
posStop = selEnd;
}
::SendMessage(scintillaHandle, SCI_SETSEL, posStart, posStop);
/*std::string posstr = " --- start: " + std::to_string(posStart) +
" -- end: "+std::to_string(posStop)+" --- ";
replaceSelection(posstr, scintillaHandle);
*/
}