Search code examples
lazarus

Message pops up unexpectedly


I have the following function that is supposed to check each letter in a very long DNA sequence, a string ~250 million characters long, and change the letter to another specific letter. There are only five possible letters (A, C, G and T, which need to be changed to T, G, C and A, respectively). There is also a possible letter, N (standing for "unknown"). This needs to be retained as N. Finally the string needs to be reversed. So if the original string is AACGTA, the converted string needs to be TACGTT.

The problem is that as the function is running, I keep getting a pop-up message with only an "OK" button (see pop-up message). I don't know why I am getting this pop-up message. This would mean that every time it pops up I need to click on the OK button, which is not feasible, especially given the length of the string. Here is the code:

function TForm1.FindReverseComplement(const Motif: ansistring; Len: integer): ansistring;
var
  I, J: integer;
  Rev_Str: ansistring;
  Chr, Rev_Chr: char;
begin
  Rev_Str := '';
  for I := 1 to Len do
  begin
    Chr := Motif[I];
    Rev_Chr := #0; //null character
    if (Chr = 'A') or (Chr = 'a')  then Rev_Chr := 'T'
    else if (Chr = 'C') or (Chr = 'c') then Rev_Chr := 'G'
    else if (Chr = 'G') or (Chr = 'g') then Rev_Chr := 'C'
    else if (Chr = 'T') or (Chr = 't')  then Rev_Chr := 'A'
    else if (Chr = 'N') or (Chr = 'n')  then Rev_Chr := 'N'
    else ShowMessage('Unknown base in ' + Motif + '!');

    Rev_Str := Rev_Str + Rev_Chr;
  end;
  Result := ansireversestring(Rev_Str);
end;                 

Thanks!


Solution

  • If there is an unknown base in the Motif string then you would expect the message dialog to appear. This is probably not what you want to happen inside of a for loop. Also, ShowMessage is not intended to show very long strings (imagine how big it would be if it displayed a string of ~250 million characters). It looks like the Motif string that you are passing into ShowMessage is too long to be displayed so there is no text in the message dialog, just an OK button. If you really want to call ShowMessage inside of the for loop, try just passing in Motif[I] to ShowMessage. You could also pass in IntToStr(I) so you will know where in the Motif string the offending character is.