#!/usr/bin/perl -w
# Convert_GenBank
#    VERSION: 1 (11 June 2004)
#    PURPOSE:  Convert sequence file from GenBank format
#                 to EditBase format
#              Makes sequence upper case
#    INPUT FILES: File in GenBank format (may contain html tags)
#              LOCUS   accession number ...
#              ...
#              ORIGIN
#                      1 gatcgct...
#                     61 agaacgg...
#              ...
#              //
#    OUTPUT FILES: File in EditBase format
#              accession number ...
#              ..
#              GATCGCT..

############## LIBRARIES AND PRAGMAS ################

   use strict;

#################### CONSTANTS #######################

  my $LF = "\n";
  my $tab = "\t";

#################### VARIABLES ######################

   my $line;                    # Line just read from input file
   my $header;                  # Line following LOCUS tag
   my $line_of_sequence;        # $line processed to exclude numbers and spaces
   my $nucleotides_printed = 0; # Running count of nucleotides saved to output file

###################### FILES ########################

   my $genbank_file = 'dmtB.gen';
   open INPUT, "<$genbank_file" or die "Can't open $genbank_file: $!\n";

   my $editbase_file = 'dmtB.seq';
   open OUTPUT, ">$editbase_file" or die "Can't open $editbase_file: $!\n";

################### MAIN PROGRAM ####################

   $header = Read_file_until ("LOCUS");
   Write_to_file ($header);
   Write_to_file ("..");

   $line = Read_file_until ("ORIGIN");
   $line = <INPUT>;
   while ($line) {
      $line_of_sequence = Process ($line);
      if (not $line_of_sequence eq "//") {
         Write_to_file ($line_of_sequence);
         $nucleotides_printed = $nucleotides_printed + length($line_of_sequence);
      } else {
         last;
      }
      $line = <INPUT>;
   }

#################### SUBROUTINES ####################

#### READ_FILE_UNTIL (target)
#    Reads input file until the target is encountered
#    Returns line with target (excluding target and subsequent blanks)

sub Read_file_until {
   my ($target) = @_;                    # String to look for in input
   my $line;                             # Contains one line of input

   $line = <INPUT>;                      # Read header line
   chomp $line;                          # Remove line break

   while ($line) {
      if ($line =~ /$target *(.*)/) {
        return $1
      }
      $line = <INPUT>;
   }
}

#### WRITE_TO_FILE (line)
#    Writes single line to output file

sub Write_to_file {
    my ($line) = @_;             # line to output

    print OUTPUT $line, $LF;
}

#### PROCESS (line)
#    Rids line of sequence of numbers and spaces
#    Makes sequence upper case
#    Returns processed line

sub Process {
     my ($line) = @_;             # unprocessed line of sequence
     chomp $line;
     $line =~ s/[0-9 ]//g;
     return uc ($line);
}
