in progress...
[jalview.git] / forester / ruby / evoruby / lib / evo / tool / taxonomy_processor.rb
index 20c5a53..1d8a25b 100644 (file)
@@ -1,12 +1,10 @@
 #
 # = lib/evo/apps/taxonomy_processor - TaxonomyProcessor class
 #
-# Copyright::  Copyright (C) 2006-2007 Christian M. Zmasek
-# License::    GNU Lesser General Public License (LGPL)
-#
-# $Id: taxonomy_processor.rb,v 1.26 2010/12/13 19:00:11 cmzmasek Exp $
-
+# Copyright::    Copyright (C) 2017 Christian M. Zmasek
+# License::      GNU Lesser General Public License (LGPL)
 
+require 'lib/evo/util/constants'
 require 'lib/evo/util/util'
 require 'lib/evo/msa/msa_factory'
 require 'lib/evo/msa/msa'
@@ -18,41 +16,29 @@ require 'lib/evo/io/writer/phylip_sequential_writer'
 require 'lib/evo/util/command_line_arguments'
 
 module Evoruby
-
   class TaxonomyProcessor
 
     PRG_NAME       = "tap"
-    PRG_DATE       = "2012.09.27"
-    PRG_DESC       = "replacement of species names in multiple sequence files"
-    PRG_VERSION    = "1.02"
-    COPYRIGHT      = "2012 Christian M Zmasek"
-    CONTACT        = "phylosoft@gmail.com"
-    WWW            = "www.phylosoft.org"
-
-    SIMPLE = false
+    PRG_DATE       = "170214"
+    PRG_DESC       = "Replacement of labels in multiple sequence files"
+    PRG_VERSION    = "2.004"
+    WWW            = "https://sites.google.com/site/cmzmasek/home/software/forester"
 
     EXTRACT_TAXONOMY_OPTION = "t"
-
-    def initialize()
-      @taxonomies = Hash.new()
-    end
-
+    ANNOTATION_OPTION       = "a"
+    HELP_OPTION_1           = "help"
+    HELP_OPTION_2           = "h"
     def run()
 
       Util.print_program_information( PRG_NAME,
-        PRG_VERSION,
-        PRG_DESC,
-        PRG_DATE,
-        COPYRIGHT,
-        CONTACT,
-        WWW,
-        STDOUT )
-
-      if ( ARGV == nil || ( ARGV.length != 1 && ARGV.length != 2 && ARGV.length != 3 && ARGV.length != 4 && ARGV.length != 5 && ARGV.length != 6 ) )
-        puts( "Usage: #{PRG_NAME}.rb [options] [input map file] <input sequences> [output sequences] [output id list]" )
-        puts()
-        puts( "  options: -" + EXTRACT_TAXONOMY_OPTION + ": to extract taxonomy information from bracketed expression" )
-        puts()
+      PRG_VERSION,
+      PRG_DESC,
+      PRG_DATE,
+      WWW,
+      STDOUT )
+
+      if ( ARGV == nil || ( ARGV.length < 1 ) )
+        print_help()
         exit( -1 )
       end
 
@@ -62,20 +48,17 @@ module Evoruby
         Util.fatal_error( PRG_NAME, "error: " + e.to_s )
       end
 
+      if ( cla.is_option_set?( HELP_OPTION_1 ) ||
+      cla.is_option_set?( HELP_OPTION_2 ) )
+        print_help
+        exit( 0 )
+      end
 
-      mapfile   = nil
-      input     = nil
-      output    = nil
-      list_file = nil
-
-
+      input      = nil
+      output     = nil
+      list_file  = nil
 
-      if cla.get_number_of_files == 4
-        mapfile   = cla.get_file_name( 0 )
-        input     = cla.get_file_name( 1 )
-        output    = cla.get_file_name( 2 )
-        list_file = cla.get_file_name( 3 )
-      elsif cla.get_number_of_files == 3
+      if cla.get_number_of_files == 3
         input     = cla.get_file_name( 0 )
         output    = cla.get_file_name( 1 )
         list_file = cla.get_file_name( 2 )
@@ -89,13 +72,16 @@ module Evoruby
         else
           i = input
         end
-        output    = i + "_ni.fasta"
-        list_file = i + ".nim"
+        output    = i + Constants::ID_NORMALIZED_FASTA_FILE_SUFFIX
+        list_file = i + Constants::ID_MAP_FILE_SUFFIX
+      else
+        print_help()
+        exit(-1)
       end
 
-
       allowed_opts = Array.new
       allowed_opts.push( EXTRACT_TAXONOMY_OPTION )
+      allowed_opts.push( ANNOTATION_OPTION )
 
       disallowed = cla.validate_allowed_options_as_str( allowed_opts )
       if ( disallowed.length > 0 )
@@ -107,25 +93,24 @@ module Evoruby
         extract_taxonomy = true
       end
 
-      if ( File.exists?( output ) )
+      annotation = nil
+      if ( cla.is_option_set?( ANNOTATION_OPTION ) )
+        annotation = cla.get_option_value( ANNOTATION_OPTION )
+      end
+
+      if ( File.exist?( output ) )
         Util.fatal_error( PRG_NAME, "outfile [" + output + "] already exists" )
       end
-      if ( File.exists?( list_file ) )
+      if ( File.exist?( list_file ) )
         Util.fatal_error( PRG_NAME, "list file [" + list_file + "] already exists" )
       end
-      if ( !File.exists?( input) )
+      if ( !File.exist?( input) )
         Util.fatal_error( PRG_NAME, "infile [" + input + "] does not exist" )
       end
-      if ( mapfile != nil && !File.exists?( mapfile ) )
-        Util.fatal_error( PRG_NAME, "mapfile [" + mapfile + "] does not exist" )
-      end
 
       fasta_like = Util.looks_like_fasta?( input )
 
       puts()
-      if mapfile != nil
-        puts( "Map file        : " + mapfile )
-      end
       puts( "Input alignment : " + input )
       puts( "Output alignment: " + output )
       puts( "Name list       : " + list_file )
@@ -137,19 +122,10 @@ module Evoruby
       if ( extract_taxonomy )
         puts( "Extract taxonomy: true"  )
       end
-      puts()
-
-      species_map = Hash.new
-      if mapfile != nil
-        File.open( mapfile ) do | file |
-          while line = file.gets
-            if ( line =~/(.+)#(.+)/ || line =~/(.+)\s+(.+)/ )
-              species_map[ $1 ] = $2
-              Util.print_message( PRG_NAME, "mapping: " + $1 + ' => ' + $2 )
-            end
-          end
-        end
+      if ( annotation != nil )
+        puts( "Annotation      : " + annotation )
       end
+      puts()
 
       f = MsaFactory.new()
       begin
@@ -171,21 +147,11 @@ module Evoruby
         Util.fatal_error( PRG_NAME, "error: " + e.to_, STDOUT )
       end
 
-      #removed = msa.remove_redundant_sequences!( true )
-      #if removed.size > 0
-      #  Util.print_message( PRG_NAME, "going to ignore the following " + removed.size.to_s + " redundant sequences:" )
-      #  removed.each { | seq_name |
-      #    puts seq_name
-      #  }
-      #  Util.print_message( PRG_NAME, "will process " + msa.get_number_of_seqs.to_s + " non redundant sequences" )
-      #end
-
       lf = File.open( list_file, "a" )
       for i in 0 ... msa.get_number_of_seqs
-        seq  = msa.get_sequence( i )
-        seq.set_name( Util::normalize_seq_name( modify_name( seq.get_name(), i, lf, species_map, extract_taxonomy ), 10 ) )
+        seq = msa.get_sequence( i )
+        seq.set_name( modify_name( seq.get_name(), i, lf, extract_taxonomy, annotation ) )
       end
-
       io = MsaIO.new()
       w = nil
       if ( fasta_like )
@@ -193,7 +159,7 @@ module Evoruby
       else
         w = PhylipSequentialWriter.new()
       end
-      w.set_max_name_length( 10 )
+      w.set_max_name_length( 9 )
       w.clean( true )
       begin
         io.write_to_file( msa, output, w )
@@ -201,118 +167,55 @@ module Evoruby
         Util.fatal_error( PRG_NAME, "failed to write file: " + e.to_s )
       end
       lf.close()
-      if ( @taxonomies.length > 0 )
-        Util.print_message( PRG_NAME, "number of unique taxonomies: " + @taxonomies.length.to_s )
-      end
       Util.print_message( PRG_NAME, "wrote: " + list_file )
       Util.print_message( PRG_NAME, "wrote: " + output )
+      Util.print_message( PRG_NAME, "next steps in standard analysis pipeline: hmmscan followed by hsp.rb")
       Util.print_message( PRG_NAME, "OK" )
     end
 
     private
 
-    def modify_name( desc, counter, file, species_map, extract_taxonomy )
+    def modify_name( desc, counter, file, extract_taxonomy, annotation )
       new_desc = nil
-      my_species = nil
-      # if desc =~ /^>?\s*\S{1,10}_([0-9A-Z]{3,5})/
-      desc.gsub!( /:\s+/, ":" ) #new
-      desc.gsub!( /\s+/, " " ) #new
-      if desc =~ /^>?\s*\S{1,10}_([A-Z]{3,5})/
-        new_desc = counter.to_s( 16 ) + "_" + $1
-      elsif SIMPLE
-        new_desc = counter.to_s( 16 )
-      elsif extract_taxonomy
-
-        species = nil
-        species_map.each_key do | key |
-          if desc =~ /[\b|_]#{key}\b/  # Added boundaries to prevent e.g. RAT matching ARATH.
-            species = species_map[ key ]
-            new_desc = counter.to_s( 16 ) + "_" + species
-            break
-          end
-        end
-        if species == nil
-          #if desc =~/.*\[(\S{3,}?)\]/
-          if desc =~/\[([A-Z0-9]{3,6})\]\s*$/ #new
-            species = $1
-            species.strip!
-            species.upcase!
-            species.gsub!( /\s+/, " " )
-            species.gsub!( /-/, "" )
-            species.gsub!( /\)/, "" )
-            species.gsub!( /\(/, "" )
-            species.gsub!( /\'/, "" )
-            if species =~ /\S+\s\S+/ || species =~ /\S{3,5}/
-              if species =~ /(\S+)\s(\S+)/
-                code = $1[ 0..2 ] + $2[ 0..1 ]
-              elsif  species =~ /\S{3,5}/
-                code = species
-              elsif species.count( " " ) > 2
-                species =~ /(\S+)\s+(\S+)\s+(\S+)$/
-                third_last = $1
-                second_last = $2
-                last = $3
-                code = code[ 0 ] + third_last[ 0 ] + second_last[ 0 ] + last[ 0 ] + last[ last.size - 1 ]
-              elsif species.count( " " ) > 1
-                species =~ /(\S+)\s+(\S+)$/
-                second_last = $1
-                last = $2
-                code = code[ 0..1 ] + second_last[ 0 ] + last[ 0 ] + last[ last.size - 1 ]
-              end
-              new_desc = counter.to_s( 16 ) + "_" + code
-              if @taxonomies.has_key?( code )
-                if ( !@taxonomies.has_value?( species ) )
-                  Util.fatal_error( PRG_NAME, "code [#{code}] is not unique in [#{desc}]" )
-                end
-              else
-                if ( @taxonomies.has_value?( species ) )
-                  Util.fatal_error( PRG_NAME, "genome [#{species}] is not unique in [#{desc}]" )
-                else
-                  @taxonomies[ code ] = species
-                end
-              end
-            else
-              Util.fatal_error( PRG_NAME, "illegal format [#{species}] in: " + desc )
-            end
-          else
-            Util.fatal_error( PRG_NAME, "illegal format in: " + desc )
-          end
-        end
-      else
-        species = nil
-        my_species = nil
-        species_map.each_key do | key |
-          if desc =~ /#{key}/
-            species = species_map[ key ]
-            species = species.gsub( /\s+/, "" )
-            species = species.gsub( /_/, " " )
-            my_species = species
-            if species =~ /(\S+)\s+(\S+)/
-              species = $1[0..2] + $2[0..1]
-            end
-            species = species.gsub( /\s+/, "" )
-            species = species.slice(0, 5)
-            species.upcase!
-            break
-          end
-        end
-        if species == nil
-          Util.fatal_error( PRG_NAME, "species not found in: " + desc  )
+      desc.gsub!( /\s+/, ' ' )
+      if extract_taxonomy
+        if desc =~/\s\[(([A-Z9][A-Z]{2}[A-Z0-9]{2})|RAT|PIG|PEA|CAP)\]/
+          new_desc = counter.to_s( 16 ) + "_" + $1
         else
-          new_desc = counter.to_s( 16 ) + "_" + species
+          Util.fatal_error( PRG_NAME, "could not get taxonomy from: " + desc )
         end
+      else
+        new_desc = counter.to_s( 16 )
       end
-      if new_desc == nil
-        Util.fatal_error( PRG_NAME, "failed to extract species from: " + desc  )
-      end
-      if my_species != nil
-        file.print( new_desc + ": " + desc + " [" + my_species + "]" + "\n" )
+      if (annotation != nil)
+        new_desc = new_desc + annotation
+        file.print( new_desc + "\t" + desc + " " + annotation + "\n" )
       else
-        file.print( new_desc + ": " + desc + "\n" )
+        file.print( new_desc + "\t" + desc + "\n" )
+      end
+      if ( new_desc.length > 9)
+        Util.fatal_error( PRG_NAME, "shortened identifier [" +
+        new_desc + "] is too long (" + new_desc.length.to_s + " characters)" )
       end
       new_desc
     end
 
+    def print_help()
+      puts( "Usage:" )
+      puts()
+      puts( "  " + PRG_NAME + ".rb [options] <input sequences> [output sequences] [output id list]" )
+      puts()
+      puts( "  options: -" + EXTRACT_TAXONOMY_OPTION + "    : to extract taxonomy information from bracketed expressions" )
+      puts( "           -" + ANNOTATION_OPTION + "=<s>: to add an annotation to all entries" )
+      puts()
+      puts( "  [next steps in standard analysis pipeline: hmmscan followed by hsp.rb]")
+      puts()
+      puts( "Example:" )
+      puts()
+      puts( "  " + PRG_NAME + ".rb P53.fasta" )
+      puts()
+    end
+
   end # class TaxonomyProcessor
 
 end # module Evoruby