[Shootout-list] reverse-complement Ada bench

Pascal Obry pascal@obry.net
Sat, 19 Mar 2005 19:13:46 +0100


Here is a first version for this bench.

-- $Id$
-- http://dada.perl.it/shootout/
-- Contributed by Pascal Obry on 2005/03/19

with Ada.Strings.Fixed;       use Ada.Strings.Fixed;
with Ada.Strings.Maps;        use Ada.Strings.Maps;
with Ada.Text_IO;             use Ada.Text_IO;
with Ada.Strings.Unbounded;   use Ada.Strings.Unbounded;

procedure Reverse_Complement is

   Line_Length : constant := 60;

   Comp_Values : constant Character_Mapping :=
     To_Mapping
       (From => "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ",
        To   => "TVGHEFCDIJMLKNOPQYSAUBWXRZTVGHEFCDIJMLKNOPQYSAUBWXRZ");

   procedure Write_Reverse_Fasta
     (Desc : in String; Str : in Unbounded_String)
   is
      S : String  := To_String (Str);
      I : Integer := 1;
      M : Integer := 0;
      J : Integer := S'Last;
   begin
      Translate (S, Comp_Values);
      Put_Line (Desc);

      while I <= S'Last loop
         M := M + Line_length;
         if S'Last < M then
            M := S'Last;
         end if;

         while I <= M loop
            Put (S (J));
            J := J - 1;
            I := I + 1;
         end loop;
         New_Line;
      end loop;
   end Write_Reverse_Fasta;

   Line : String (1 .. Line_Length);
   Last : Natural;

   Desc : Unbounded_String;
   Str  : Unbounded_String;
begin
   while not End_Of_File loop
      Get_Line (Line, Last);

      if Line (1) = '>' then
         if Desc /= Null_Unbounded_String then
            Write_Reverse_Fasta (To_String (Desc), Str);
            Str := Null_Unbounded_String;
         end if;

         Desc := To_Unbounded_String (Line (1 .. Last));

      elsif Line (1) /= ';' then
         Append (Str, Line (1 .. Last));
      end if;
   end loop;

   if Desc /= Null_Unbounded_String then
      Write_Reverse_Fasta (To_String (Desc), Str);
   end if;
end Reverse_Complement;

Pascal.

-- 

--|------------------------------------------------------
--| Pascal Obry                           Team-Ada Member
--| 45, rue Gabriel Peri - 78114 Magny Les Hameaux FRANCE
--|------------------------------------------------------
--|              http://www.obry.org
--| "The best way to travel is by means of imagination"
--|
--| gpg --keyserver wwwkeys.pgp.net --recv-key C1082595