minor changes
[jalview.git] / forester / ruby / evoruby / lib / evo / tool / multi_sequence_extractor.rb
index e5bb182..2bf5c65 100644 (file)
@@ -1,10 +1,9 @@
 #
 # = lib/evo/apps/multi_sequence_extractor.rb - MultiSequenceExtractor class
 #
-# Copyright::  Copyright (C) 2006-2008 Christian M. Zmasek
+# Copyright::  Copyright (C) 2014 Christian M. Zmasek
 # License::    GNU Lesser General Public License (LGPL)
 #
-# $Id: multi_sequence_extractor.rb,v 1.10 2010/12/13 19:00:11 cmzmasek Exp $
 
 
 require 'lib/evo/util/constants'
@@ -16,18 +15,16 @@ require 'lib/evo/io/parser/fasta_parser'
 require 'lib/evo/io/writer/fasta_writer'
 require 'lib/evo/util/command_line_arguments'
 
-
-
 module Evoruby
 
   class MultiSequenceExtractor
 
     PRG_NAME                           = "mse"
-    PRG_VERSION                        = "1.03"
+    PRG_VERSION                        = "1.04"
     PRG_DESC                           = "extraction of sequences by name from multiple multi-sequence ('fasta') files"
-    PRG_DATE                           = "131127"
-    COPYRIGHT                          = "2008-2013 Christian M Zmasek"
-    CONTACT                            = "phylosoft@gmail.com"
+    PRG_DATE                           = "140318"
+    COPYRIGHT                          = "2014 Christian M Zmasek"
+    CONTACT                            = "phyloxml@gmail.com"
     WWW                                = "https://sites.google.com/site/cmzmasek/home/software/forester"
     HELP_OPTION_1                      = 'help'
     HELP_OPTION_2                      = 'h'
@@ -43,6 +40,7 @@ module Evoruby
 
     def initialize()
       @file_to_msa = Hash.new
+      @seqs = 0
     end
 
     def run()
@@ -70,7 +68,7 @@ module Evoruby
         exit( 0 )
       end
 
-      if ( cla.get_number_of_files != 4 && cla.get_number_of_files != 5 )
+      if ( cla.get_number_of_files != 5 )
         print_help
         exit( -1 )
       end
@@ -90,15 +88,12 @@ module Evoruby
       input_dir              = cla.get_file_name( 1 )
       out_dir                = cla.get_file_name( 2 )
       out_dir_doms           = cla.get_file_name( 3 )
-      mapping_file            = nil
+      mapping_file           = cla.get_file_name( 4 )
 
-      if ( cla.get_number_of_files == 5 )
-        mapping_file = cla.get_file_name( 4 )
-        begin
-          Util.check_file_for_readability( mapping_file )
-        rescue ArgumentError => e
-          Util.fatal_error( PRG_NAME, "error: " + e.to_s )
-        end
+      begin
+        Util.check_file_for_readability( mapping_file )
+      rescue ArgumentError => e
+        Util.fatal_error( PRG_NAME, "error: " + e.to_s )
       end
 
       extension = 0
@@ -114,26 +109,32 @@ module Evoruby
         extract_linkers = true
       end
 
-      if  !File.exist?( input_dir )
+      unless File.exist?( out_dir )
+        Dir.mkdir( out_dir )
+      end
+      unless File.exist?( out_dir_doms )
+        Dir.mkdir( out_dir_doms )
+      end
+
+      unless File.exist?( input_dir )
         Util.fatal_error( PRG_NAME, "error: input directory [#{input_dir}] does not exist" )
       end
-      if  !File.exist?( out_dir )
+      unless File.exist?( out_dir )
         Util.fatal_error( PRG_NAME, "error: output directory [#{out_dir}] does not exist" )
       end
-      if  !File.exist?( out_dir_doms )
+      unless File.exist?( out_dir_doms )
         Util.fatal_error( PRG_NAME, "error: output directory [#{out_dir_doms}] does not exist" )
       end
-      if !File.directory?( input_dir )
+      unless File.directory?( input_dir )
         Util.fatal_error( PRG_NAME, "error: [#{input_dir}] is not a directory" )
       end
-      if !File.directory?( out_dir )
+      unless File.directory?( out_dir )
         Util.fatal_error( PRG_NAME, "error:  [#{out_dir}] is not a directory" )
       end
-      if !File.directory?( out_dir_doms )
+      unless File.directory?( out_dir_doms )
         Util.fatal_error( PRG_NAME, "error:  [#{out_dir_doms}] is not a directory" )
       end
 
-
       log = String.new
 
       log << "Program            : " + PRG_NAME + ld
@@ -149,10 +150,8 @@ module Evoruby
       log << "Output dir                 : " + out_dir + ld
       puts( "Output dir domains         : " + out_dir_doms )
       log << "Output dir domains         : " + out_dir_doms + ld
-      if ( mapping_file != nil )
-        puts( "Mapping file               : " + mapping_file )
-        log << "Mapping file               : " + mapping_file + ld
-      end
+      puts( "Mapping file               : " + mapping_file )
+      log << "Mapping file               : " + mapping_file + ld
       if extension > 0
         puts( "Extension                  : " + extension.to_s )
         log << "Extension                  : " + extension.to_s + ld
@@ -164,9 +163,7 @@ module Evoruby
       log << "Date                       : " + Time.now.to_s + ld
       puts
 
-      if ( mapping_file != nil )
-        species_codes_to_paths = extract_mappings( mapping_file )
-      end
+      species_codes_to_paths = extract_mappings( mapping_file )
 
       input_files = obtain_inputfiles( input_dir, seq_names_files_suffix )
 
@@ -248,6 +245,7 @@ module Evoruby
       puts basename
 
       File.open( input_file ) do | file |
+        species_counter = 1
         while line = file.gets
           line.strip!
           if !Util.is_string_empty?( line ) && !(line =~ /\s*#/ )
@@ -289,17 +287,17 @@ module Evoruby
                 print_counts( per_species_counter, log, Constants::LINE_DELIMITER )
                 per_species_counter = 0
               end
-              puts " " + current_species + " [" + my_readlink + "]"
-              log << current_species + " [" + my_readlink + "]" + Constants::LINE_DELIMITER
+              puts " " + species_counter.to_s +  ":" + current_species + " [" + my_readlink + "]"
+              log << species_counter.to_s <<  ": " << current_species << " [" + my_readlink + "]" << Constants::LINE_DELIMITER
+              species_counter += 1
             end
-            puts "   " + seq_name
-            log << "   " + seq_name + Constants::LINE_DELIMITER
+            log << "   " << seq_name << Constants::LINE_DELIMITER
             per_species_counter = per_species_counter + 1
             seq = nil
 
             indices = current_msa.find_by_name_start( seq_name, true )
             if indices.size == 1
-              seq =  current_msa.get_sequence( indices[ 0 ] )
+              seq = current_msa.get_sequence( indices[ 0 ] )
             elsif indices.size == 0
               # Not found, try finding by partial match.
               begin
@@ -311,21 +309,6 @@ module Evoruby
               Util.fatal_error( PRG_NAME, "error: seq name \"" + seq_name + "\" not unique"  )
             end
 
-            # if current_msa.find_by_name_start( seq_name, true ).size > 0
-            #   begin
-            #     seq = current_msa.get_by_name_start( seq_name, true ).copy
-            #   rescue ArgumentError => e
-            #     Util.fatal_error( PRG_NAME, "error: " + e.to_s )
-            #   end
-            # else
-            #   # Not found, try finding by partial match.
-            #   begin
-            #     seq = current_msa.get_by_name( seq_name, true, true )
-            #   rescue ArgumentError => e
-            #     Util.fatal_error( PRG_NAME, "error: " + e.to_s )
-            #   end
-            # end
-
             normalized_id = per_species_counter.to_s( 16 ).upcase +
              "_" + current_species
 
@@ -508,19 +491,19 @@ module Evoruby
               if ( species_code_to_path.has_value?( path ) )
                 Util.fatal_error( PRG_NAME, "error: path [#{path}] is not unique" )
               end
-              if ( !File.exist?( path ) )
+              unless ( File.exist?( path ) )
                 Util.fatal_error( PRG_NAME, "error: file [#{path}] does not exist" )
               end
-              if ( !File.file?( path ) )
+              unless ( File.file?( path ) )
                 Util.fatal_error( PRG_NAME, "error: [#{path}] is not a regular file" )
               end
-              if ( !File.readable?( path ) )
+              unless ( File.readable?( path ) )
                 Util.fatal_error( PRG_NAME, "error: file [#{path}] is not readable" )
               end
-              if ( File.size( path ) < 10000 )
+              if ( File.size( path ) < 1000 )
                 Util.fatal_error( PRG_NAME, "error: file [#{path}] appears too small" )
               end
-              if ( !Util.looks_like_fasta?( path ) )
+              unless ( Util.looks_like_fasta?( path ) )
                 Util.fatal_error( PRG_NAME, "error: file [#{path}] does not appear to be a fasta file" )
               end
               species_code_to_path[ species ] = path
@@ -533,8 +516,8 @@ module Evoruby
     end
 
     def print_counts( per_species_counter, log, ld )
-      puts "   [sum: " + per_species_counter.to_s + "]"
-      log << "   [sum: " + per_species_counter.to_s + "]" + ld
+      puts "   sum: " + per_species_counter.to_s
+      log << "   sum: " + per_species_counter.to_s + ld
     end
 
     def read_fasta_file( input )
@@ -549,8 +532,10 @@ module Evoruby
       rescue Exception => e
         Util.fatal_error( PRG_NAME, "error: " + e.to_s )
       end
-      if @file_to_msa.size < 500
+      if @seqs <= 400000
         @file_to_msa[ input ] = msa
+        @seqs += msa.get_number_of_seqs
+        puts "   total seqs in memory: " + @seqs.to_s
       end
       msa
     end
@@ -558,9 +543,8 @@ module Evoruby
     def print_help()
       puts( "Usage:" )
       puts()
-      puts( "  " + PRG_NAME + ".rb <sequence names file suffix> <input dir containing sequence names files " +
-         "and possibly genome multiple-sequence ('fasta') files> <output directory for sequences> <output directory for domains> [mapping file for " +
-         "genome multiple-sequence ('fasta') files not in input dir]" )
+      puts( "  " + PRG_NAME + ".rb <sequence id ('prot') files suffix> <dir containing sequence id ('prot') files>" +
+         " <output directory for protein sequences> <output directory for domain sequences> <genome locations file>" )
       puts()
       puts( "  option: -" + EXT_OPTION  + "=<int>: to extend extracted domains" )
       puts( "          -" + EXTRACT_LINKERS_OPTION  + "      : to extract linkers" )