in progress
[jalview.git] / forester / ruby / evoruby / lib / evo / apps / multi_sequence_extractor.rb
1 #
2 # = lib/evo/apps/multi_sequence_extractor.rb - MultiSequenceExtractor class
3 #
4 # Copyright::  Copyright (C) 2006-2008 Christian M. Zmasek
5 # License::    GNU Lesser General Public License (LGPL)
6 #
7 # $Id: multi_sequence_extractor.rb,v 1.10 2010/12/13 19:00:11 cmzmasek Exp $
8
9
10 require 'lib/evo/util/constants'
11 require 'lib/evo/util/util'
12 require 'lib/evo/msa/msa'
13 require 'lib/evo/msa/msa_factory'
14 require 'lib/evo/io/msa_io'
15 require 'lib/evo/io/parser/fasta_parser'
16 require 'lib/evo/io/writer/fasta_writer'
17 require 'lib/evo/util/command_line_arguments'
18
19
20
21 module Evoruby
22
23   class MultiSequenceExtractor
24
25     PRG_NAME                           = "mse"
26     PRG_VERSION                        = "1.0.0"
27     PRG_DESC                           = "extraction of sequences by name from multiple multi-sequence ('fasta') files"
28     PRG_DATE                           = "2008.08.13"
29     COPYRIGHT                          = "2008-2009 Christian M Zmasek"
30     CONTACT                            = "phylosoft@gmail.com"
31     WWW                                = "www.phylosoft.org"
32     HELP_OPTION_1                      = 'help'
33     HELP_OPTION_2                      = 'h'
34
35     LOG_SUFFIX                          = ".mse_log"
36     FASTA_SUFFIX                        = ".fasta"
37     FASTA_WITH_NORMALIZED_IDS_SUFFIX    = ".ni.fasta"
38     NORMALIZED_IDS_MAP_SUFFIX           = ".nim"
39     PROTEINS_LIST_FILE_SEPARATOR        = "\t"
40
41     def run()
42
43       Util.print_program_information( PRG_NAME,
44         PRG_VERSION,
45         PRG_DESC ,
46         PRG_DATE,
47         COPYRIGHT,
48         CONTACT,
49         WWW,
50         STDOUT )
51
52       ld = Constants::LINE_DELIMITER
53
54       begin
55         cla = CommandLineArguments.new( ARGV )
56       rescue ArgumentError => e
57         Util.fatal_error( PRG_NAME, "error: " + e.to_s )
58       end
59
60       if ( cla.is_option_set?( HELP_OPTION_1 ) ||
61            cla.is_option_set?( HELP_OPTION_2 ) )
62         print_help
63         exit( 0 )
64       end
65
66       if ( cla.get_number_of_files != 4 && cla.get_number_of_files != 5 )
67         print_help
68         exit( -1 )
69       end
70
71       allowed_opts = Array.new
72
73       disallowed = cla.validate_allowed_options_as_str( allowed_opts )
74       if ( disallowed.length > 0 )
75         Util.fatal_error( PRG_NAME,
76           "unknown option(s): " + disallowed,
77           STDOUT )
78       end
79
80       seq_names_files_suffix = cla.get_file_name( 0 )
81       input_dir              = cla.get_file_name( 1 )
82       out_dir                = cla.get_file_name( 2 )
83       out_dir_doms           = cla.get_file_name( 3 )
84       mapping_file            = nil
85
86       if ( cla.get_number_of_files == 5 )
87         mapping_file = cla.get_file_name( 4 )
88         begin
89           Util.check_file_for_readability( mapping_file )
90         rescue ArgumentError => e
91           Util.fatal_error( PRG_NAME, "error: " + e.to_s )
92         end
93       end
94
95       if  !File.exist?( input_dir )
96         Util.fatal_error( PRG_NAME, "error: input directory [#{input_dir}] does not exist" )
97       end
98       if  !File.exist?( out_dir )
99         Util.fatal_error( PRG_NAME, "error: output directory [#{out_dir}] does not exist" )
100       end
101       if  !File.exist?( out_dir_doms )
102         Util.fatal_error( PRG_NAME, "error: output directory [#{out_dir_doms}] does not exist" )
103       end
104       if !File.directory?( input_dir )
105         Util.fatal_error( PRG_NAME, "error: [#{input_dir}] is not a directory" )
106       end
107       if !File.directory?( out_dir )
108         Util.fatal_error( PRG_NAME, "error:  [#{out_dir}] is not a directory" )
109       end
110       if !File.directory?( out_dir_doms )
111         Util.fatal_error( PRG_NAME, "error:  [#{out_dir_doms}] is not a directory" )
112       end
113
114
115       log = String.new
116
117       log << "Program            : " + PRG_NAME + ld
118       log << "Version            : " + PRG_VERSION + ld
119       log << "Program date       : " + PRG_DATE + ld
120
121       puts()
122       puts( "Sequence names files suffix: " + seq_names_files_suffix )
123       log << "Sequence names files suffix: " + seq_names_files_suffix + ld
124       puts( "Input dir                  : " + input_dir )
125       log << "Input dir                  : " + input_dir + ld
126       puts( "Output dir                 : " + out_dir )
127       log << "Output dir                 : " + out_dir + ld
128       puts( "Output dir domains         : " + out_dir_doms )
129       log << "Output dir domains         : " + out_dir_doms + ld
130       if ( mapping_file != nil )
131         puts( "Mapping file               : " + mapping_file )
132         log << "Mapping file               : " + mapping_file + ld
133       end
134       log << "Date                       : " + Time.now.to_s + ld
135       puts
136
137       if ( mapping_file != nil )
138         species_codes_to_paths = extract_mappings( mapping_file )
139       end
140
141       input_files = obtain_inputfiles( input_dir, seq_names_files_suffix )
142
143       counter = 0
144
145       input_files.each { |input_file|
146         counter += 1
147         puts
148         puts
149         puts counter.to_s + "/" + input_files.size.to_s
150         read_seq_family_file( input_file,
151           seq_names_files_suffix,
152           input_dir,
153           species_codes_to_paths,
154           log,
155           out_dir,
156           out_dir_doms,
157           mapping_file )
158       }
159       puts
160       Util.print_message( PRG_NAME, "OK" )
161       puts
162
163     end
164
165
166     def read_seq_family_file( input_file,
167         seq_names_files_suffix,
168         input_dir,
169         species_codes_to_paths,
170         log,
171         out_dir,
172         out_dir_doms,
173         mapping_file )
174
175       begin
176         Util.check_file_for_readability( input_file )
177       rescue ArgumentError => e
178         Util.fatal_error( PRG_NAME, "error: " + e.to_s )
179       end
180       basename = File.basename( input_file, seq_names_files_suffix )
181       out_file_path_fasta_file                = out_dir + Constants::FILE_SEPARATOR + basename + FASTA_SUFFIX
182       out_file_path_normalized_ids_fasta_file = out_dir + Constants::FILE_SEPARATOR + basename + FASTA_WITH_NORMALIZED_IDS_SUFFIX
183       out_file_path_ids_map                   = out_dir + Constants::FILE_SEPARATOR + basename + NORMALIZED_IDS_MAP_SUFFIX
184       doms_out_file_path_fasta_file           = out_dir_doms + Constants::FILE_SEPARATOR + basename + "_domains" + FASTA_SUFFIX
185       begin
186         Util.check_file_for_writability( out_file_path_fasta_file )
187         Util.check_file_for_writability( out_file_path_normalized_ids_fasta_file )
188         Util.check_file_for_writability( out_file_path_ids_map  )
189         Util.check_file_for_writability( doms_out_file_path_fasta_file  )
190       rescue ArgumentError => e
191         Util.fatal_error( PRG_NAME, "error: " + e.to_s )
192       end
193
194       ids_map_writer = nil
195       begin
196         ids_map_writer = File.open( out_file_path_ids_map, 'a' )
197       rescue Exception => e
198         Util.fatal_error( PRG_NAME, "error: " + e.to_s )
199       end
200
201       current_species         = ""
202       current_msa            = nil
203       new_msa                = Msa.new
204       new_msa_normalized_ids = Msa.new
205       new_msa_domains = Msa.new
206       per_species_counter = 0
207
208       puts basename
209
210       File.open( input_file ) do | file |
211         while line = file.gets
212           if ( !Util.is_string_empty?( line ) && !(line =~ /\s*#/ ) )
213             values = line.split( PROTEINS_LIST_FILE_SEPARATOR )
214
215             if ( values.length < 2 )
216               Util.fatal_error( PRG_NAME, "unexpected format: " + line )
217             end
218             species = values[ 0 ]
219             if species == "BRADI" || species == "ASPNG" || species == "SCLSC" || species == "PTEVA"  || species == "EIMTE"
220               next
221             end
222             seq_name = values[ 1 ]
223             domain_ranges = nil
224             if ( values.length > 3 )
225               domain_ranges_block = values[ 3 ]
226               domain_ranges = domain_ranges_block.split( "/" )
227             end
228             if ( species != current_species )
229               current_species = species
230               my_file = input_dir + Constants::FILE_SEPARATOR + current_species
231
232               if ( !File.exist?( my_file ) )
233                 if species_codes_to_paths == nil
234                   Util.fatal_error( PRG_NAME, "error: [#{my_file}] not found and no mapping file provided" )
235                 elsif ( !species_codes_to_paths.has_key?( current_species ) )
236                   Util.fatal_error( PRG_NAME, "error: species [#{current_species}] not found in mapping file [#{mapping_file}]" )
237                 end
238                 my_file = species_codes_to_paths[ current_species ]
239               end
240               my_path = File.expand_path( my_file )
241               my_readlink = my_path
242               if ( File.symlink?( my_path ) )
243                 my_readlink = File.readlink( my_path )
244               end
245               current_msa = read_fasta_file( my_file )
246
247               if ( per_species_counter > 0 )
248                 print_counts( per_species_counter, log, Constants::LINE_DELIMITER )
249                 per_species_counter = 0
250               end
251               puts " " + current_species + " [" + my_readlink + "]"
252               log << current_species + " [" + my_readlink + "]" + Constants::LINE_DELIMITER
253             end
254             puts "   " + seq_name
255             log << "   " + seq_name + Constants::LINE_DELIMITER
256             per_species_counter = per_species_counter + 1
257             seq = nil
258
259             if current_msa.find_by_name_start( seq_name, true ).size > 0
260               begin
261                 seq = current_msa.get_by_name_start( seq_name, true ).copy
262               rescue ArgumentError => e
263                 Util.fatal_error( PRG_NAME, "error: " + e.to_s )
264               end
265             else
266               # Not found, try finding by partial match.
267               begin
268                 seq = current_msa.get_by_name( seq_name, true, true )
269               rescue ArgumentError => e
270                 Util.fatal_error( PRG_NAME, "error: " + e.to_s )
271               end
272             end
273
274             normalized_id = per_species_counter.to_s( 16 ).upcase +
275              "_" + current_species
276
277             per_species_counter.to_i
278
279             ids_map_writer.write( normalized_id + ": " + seq.get_name + Constants::LINE_DELIMITER )
280
281             orig_name = nil
282             if ( seq != nil )
283               orig_name = seq.get_name
284               seq.set_name( seq.get_name + " [" + current_species + "]" )
285               new_msa.add_sequence( seq )
286             else
287               Util.fatal_error( PRG_NAME, "unexected error: seq is nil" )
288             end
289
290             if  domain_ranges != nil
291               domain_ranges.each { |range|
292                 if range != nil && range.length > 0
293                   s= range.split("-")
294                   from = s[ 0 ]
295                   to = s[ 1 ]
296                   new_msa_domains.add_sequence( Sequence.new( orig_name + "/" + from + "-" + to + " [" + basename + "] [" + current_species + "]", seq.get_sequence_as_string[from.to_i..to.to_i] ) )
297                 end
298               }
299             end
300
301             new_msa_normalized_ids.add_sequence( Sequence.new( normalized_id, seq.get_sequence_as_string ) )
302
303           end
304         end
305
306       end
307
308       ids_map_writer.close
309
310       if ( per_species_counter > 0 )
311         print_counts( per_species_counter, log, Constants::LINE_DELIMITER )
312       end
313
314       io = MsaIO.new()
315
316       fasta_writer = FastaWriter.new()
317       fasta_writer.remove_gap_chars
318       fasta_writer.clean
319
320       begin
321         io.write_to_file( new_msa, out_file_path_fasta_file, fasta_writer )
322       rescue Exception => e
323         Util.fatal_error( PRG_NAME, "error: " + e.to_s )
324       end
325
326       if  new_msa_domains != nil
327         begin
328           io.write_to_file( new_msa_domains, doms_out_file_path_fasta_file, fasta_writer )
329         rescue Exception => e
330           Util.fatal_error( PRG_NAME, "error: " + e.to_s )
331         end
332       end
333
334       begin
335         io.write_to_file( new_msa_normalized_ids, out_file_path_normalized_ids_fasta_file, fasta_writer )
336       rescue Exception => e
337         Util.fatal_error( PRG_NAME, "error: " + e.to_s )
338       end
339
340       begin
341         f = File.open( out_dir + Constants::FILE_SEPARATOR + basename +  LOG_SUFFIX , 'a' )
342         f.print( log )
343         f.close
344       rescue Exception => e
345         Util.fatal_error( PRG_NAME, "error: " + e.to_s )
346       end
347
348     end
349
350     def obtain_inputfiles( input_dir, seq_names_files_suffix )
351       input_files = Array.new()
352       Dir.foreach( input_dir ) { |file_name|
353         if file_name.index( seq_names_files_suffix ) == ( file_name.size - seq_names_files_suffix.size )
354           input_files.push( input_dir + Constants::FILE_SEPARATOR + file_name )
355         end
356       }
357       input_files
358     end
359
360     def extract_mappings( mapping_file )
361       species_code_to_path = Hash.new()
362       File.open( mapping_file ) do | file |
363         while line = file.gets
364           if ( !Util.is_string_empty?( line ) && !(line =~ /\s*#/ ) )
365             if ( line =~ /(\S+)\s+(\S+)/ )
366               species = $1
367               path = $2
368               if ( species_code_to_path.has_key?( species ) )
369                 Util.fatal_error( PRG_NAME, "error: species code [#{species}] is not unique" )
370               end
371               if ( species_code_to_path.has_value?( path ) )
372                 Util.fatal_error( PRG_NAME, "error: path [#{path}] is not unique" )
373               end
374               if ( !File.exist?( path ) )
375                 Util.fatal_error( PRG_NAME, "error: file [#{path}] does not exist" )
376               end
377               if ( !File.file?( path ) )
378                 Util.fatal_error( PRG_NAME, "error: [#{path}] is not a regular file" )
379               end
380               if ( !File.readable?( path ) )
381                 Util.fatal_error( PRG_NAME, "error: file [#{path}] is not readable" )
382               end
383               if ( File.size( path ) < 10000 )
384                 Util.fatal_error( PRG_NAME, "error: file [#{path}] appears too small" )
385               end
386               if ( !Util.looks_like_fasta?( path ) )
387                 Util.fatal_error( PRG_NAME, "error: file [#{path}] does not appear to be a fasta file" )
388               end
389               species_code_to_path[ species ] = path
390               puts species + " -> " + path
391             end
392           end
393         end
394       end
395       species_code_to_path
396     end
397
398     def print_counts( per_species_counter, log, ld )
399       puts "   [sum: " + per_species_counter.to_s + "]"
400       log << "   [sum: " + per_species_counter.to_s + "]" + ld
401     end
402
403     def read_fasta_file( input )
404       f = MsaFactory.new()
405       msa = nil
406       begin
407         msa = f.create_msa_from_file( input, FastaParser.new() )
408       rescue Exception => e
409         Util.fatal_error( PRG_NAME, "error: " + e.to_s )
410       end
411       msa
412     end
413
414     def print_help()
415       puts( "Usage:" )
416       puts()
417       puts( "  " + PRG_NAME + ".rb <sequence names file suffix> <input dir containing sequence names files " +
418          "and possibly genome multiple-sequence ('fasta') files> <output directory for sequences> <output directory for domains> [mapping file for " +
419          "genome multiple-sequence ('fasta') files not in input dir]" )
420       puts()
421       puts( "  " + "Example: \"mse.rb .prot . seqs doms ../genome_locations.txt\"" )
422       puts()
423     end
424
425   end # class MultiSequenceExtractor
426 end