v 1.001
[jalview.git] / forester / ruby / evoruby / lib / evo / tool / multi_domain_seq_extractor.rb
1 #
2 # = lib/evo/tool/multi_domain_seq_extractor - MultiDomainSeqExtractor class
3 #
4 # Copyright::    Copyright (C) 2017 Christian M. Zmasek
5 # License::      GNU Lesser General Public License (LGPL)
6
7 require 'lib/evo/util/constants'
8 require 'lib/evo/util/util'
9 require 'lib/evo/util/command_line_arguments'
10 require 'lib/evo/io/parser/hmmscan_multi_domain_extractor'
11
12 module Evoruby
13   class MultiDomainSeqExtractor
14
15     PRG_NAME       = "mdsx"
16     PRG_VERSION    = "1.001"
17     PRG_DESC       = "Extraction of multi domain sequences from hmmscan output"
18     PRG_DATE       = "2017/03/08"
19     WWW            = "https://sites.google.com/site/cmzmasek/home/software/forester"
20
21     HELP_OPTION_1                      = 'help'
22     HELP_OPTION_2                      = 'h'
23     def run()
24
25       Util.print_program_information( PRG_NAME,
26       PRG_VERSION,
27       PRG_DESC ,
28       PRG_DATE,
29       WWW,
30       STDOUT )
31
32       if ( ARGV == nil || ( ARGV.length < 1 )  )
33         print_help
34         exit( -1 )
35       end
36
37       begin
38         cla = CommandLineArguments.new( ARGV )
39       rescue ArgumentError
40         Util.fatal_error( PRG_NAME, "error: " + $!, STDOUT )
41       end
42
43       if ( cla.is_option_set?( HELP_OPTION_1 ) ||
44       cla.is_option_set?( HELP_OPTION_2 ) )
45         print_help
46         exit( 0 )
47       end
48
49       unless ( cla.get_number_of_files == 2 || cla.get_number_of_files == 3 )
50         print_help
51         exit( -1 )
52       end
53
54       allowed_opts = Array.new
55
56       disallowed = cla.validate_allowed_options_as_str( allowed_opts )
57       if ( disallowed.length > 0 )
58         Util.fatal_error( PRG_NAME,
59         "unknown option(s): " + disallowed,
60         STDOUT )
61       end
62
63       domain_id           = cla.get_file_name( 0 )
64       hmmscan_output      = cla.get_file_name( 1 )
65       fasta_sequence_file = ""
66       outfile_base        = ""
67
68       if cla.get_number_of_files == 3
69         fasta_sequence_file = cla.get_file_name( 2 )
70       else
71         hmmscan_index = hmmscan_output.index(Constants::HMMSCAN)
72         if ( hmmscan_index != nil )
73           prefix = hmmscan_output[0 .. hmmscan_index-1 ]
74           suffix = Constants::ID_NORMALIZED_FASTA_FILE_SUFFIX
75           files = Dir.entries( "." )
76           matching_files = Util.get_matching_files( files, prefix, suffix)
77           if matching_files.length < 1
78             Util.fatal_error( PRG_NAME, 'no file matching [' + prefix +
79             '...' + suffix + '] present in current directory: need to indicate <file containing complete sequences in fasta format> as second argument' )
80           end
81           if matching_files.length > 1
82             Util.fatal_error( PRG_NAME, 'more than one file matching [' +
83             prefix  + '...' + suffix + '] present in current directory: need to indicate <file containing complete sequences in fasta format> as second argument' )
84           end
85           fasta_sequence_file = matching_files[ 0 ]
86         else
87           Util.fatal_error( PRG_NAME, 'input files do not seem in format for standard analysis pipeline, need to explicitly indicate all' )
88         end
89       end
90       hmmscan_index = hmmscan_output.index(Constants::HMMSCAN)
91       if hmmscan_index != nil
92         outfile_base = hmmscan_output.sub(Constants::HMMSCAN, '_')
93       else
94         Util.fatal_error( PRG_NAME, 'input files do not seem in format for standard analysis pipeline, need to explicitly indicate all' )
95       end
96
97       log_str = ''
98
99       log_str << PRG_NAME << Constants::LINE_DELIMITER
100       log_str <<  PRG_VERSION << Constants::LINE_DELIMITER
101       log_str << PRG_DESC << Constants::LINE_DELIMITER
102       log_str << PRG_DATE << Constants::LINE_DELIMITER
103       log_str << Constants::LINE_DELIMITER
104
105       begin
106         parser = HmmscanMultiDomainExtractor.new()
107         parser.parse( domain_id,
108         hmmscan_output,
109         fasta_sequence_file,
110         outfile_base,
111         log_str)
112       rescue ArgumentError, IOError => e
113         Util.fatal_error( PRG_NAME, "error: " + e.to_s, STDOUT )
114
115       rescue Exception => e
116         puts e.backtrace
117         Util.fatal_error( PRG_NAME, "unexpected exception: " + e.to_s, STDOUT )
118       end
119
120       puts
121       Util.print_message( PRG_NAME, "OK" )
122       puts
123
124     end
125
126     def print_help()
127       puts
128       puts "Usage:"
129       puts
130       puts "  " + PRG_NAME + ".rb <target dom architecture> <hmmscan outputfile> [file containing complete sequences in fasta format]"
131       puts
132       puts "Format for target dom architecture:"
133       puts
134       puts "  domainA=iE-cutoff=abs-len-cutoff=rel-len-cutoff--domainB=..."
135       puts
136       puts "Examples:"
137       puts
138       puts "  " + PRG_NAME + ".rb BH4=1e-6=0=0.5--Bcl-2=1e-6=0=0.5 Bcl2_hmmscan_#{Constants::PFAM_V_FOR_EX}_10"
139       puts
140       puts "  " + PRG_NAME + ".rb BH4=0.1=20=0--Bcl-2=0.1=50=0 Bcl2_hmmscan_#{Constants::PFAM_V_FOR_EX}_10 Bcl2_ni.fasta"
141       puts
142       puts
143     end
144
145   end # class MultiDomainSeqExtractor
146
147 end # module Evoruby