2 # = lib/evo/apps/multi_sequence_extractor.rb - MultiSequenceExtractor class
4 # Copyright:: Copyright (C) 2006-2008 Christian M. Zmasek
5 # License:: GNU Lesser General Public License (LGPL)
7 # $Id: multi_sequence_extractor.rb,v 1.10 2010/12/13 19:00:11 cmzmasek Exp $
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'
23 class MultiSequenceExtractor
27 PRG_DESC = "extraction of sequences by name from multiple multi-sequence ('fasta') files"
28 PRG_DATE = "2012.07.20"
29 COPYRIGHT = "2008-2012 Christian M Zmasek"
30 CONTACT = "phylosoft@gmail.com"
31 WWW = "www.phylosoft.org"
32 HELP_OPTION_1 = 'help'
36 EXTRACT_LINKERS_OPTION = 'l'
37 LOG_SUFFIX = ".mse_log"
38 LINKERS_SUFFIX = ".linkers"
39 FASTA_SUFFIX = ".fasta"
40 FASTA_WITH_NORMALIZED_IDS_SUFFIX = ".ni.fasta"
41 NORMALIZED_IDS_MAP_SUFFIX = ".nim"
42 PROTEINS_LIST_FILE_SEPARATOR = "\t"
47 Util.print_program_information( PRG_NAME,
56 ld = Constants::LINE_DELIMITER
59 cla = CommandLineArguments.new( ARGV )
60 rescue ArgumentError => e
61 Util.fatal_error( PRG_NAME, "error: " + e.to_s )
64 if ( cla.is_option_set?( HELP_OPTION_1 ) ||
65 cla.is_option_set?( HELP_OPTION_2 ) )
70 if ( cla.get_number_of_files != 4 && cla.get_number_of_files != 5 )
75 allowed_opts = Array.new
76 allowed_opts.push(EXT_OPTION)
77 allowed_opts.push(EXTRACT_LINKERS_OPTION)
79 disallowed = cla.validate_allowed_options_as_str( allowed_opts )
80 if ( disallowed.length > 0 )
81 Util.fatal_error( PRG_NAME,
82 "unknown option(s): " + disallowed,
86 seq_names_files_suffix = cla.get_file_name( 0 )
87 input_dir = cla.get_file_name( 1 )
88 out_dir = cla.get_file_name( 2 )
89 out_dir_doms = cla.get_file_name( 3 )
92 if ( cla.get_number_of_files == 5 )
93 mapping_file = cla.get_file_name( 4 )
95 Util.check_file_for_readability( mapping_file )
96 rescue ArgumentError => e
97 Util.fatal_error( PRG_NAME, "error: " + e.to_s )
102 if cla.is_option_set?(EXT_OPTION)
103 extension = cla.get_option_value_as_int(EXT_OPTION)
109 extract_linkers = false
110 if cla.is_option_set?(EXTRACT_LINKERS_OPTION)
111 extract_linkers = true
114 if !File.exist?( input_dir )
115 Util.fatal_error( PRG_NAME, "error: input directory [#{input_dir}] does not exist" )
117 if !File.exist?( out_dir )
118 Util.fatal_error( PRG_NAME, "error: output directory [#{out_dir}] does not exist" )
120 if !File.exist?( out_dir_doms )
121 Util.fatal_error( PRG_NAME, "error: output directory [#{out_dir_doms}] does not exist" )
123 if !File.directory?( input_dir )
124 Util.fatal_error( PRG_NAME, "error: [#{input_dir}] is not a directory" )
126 if !File.directory?( out_dir )
127 Util.fatal_error( PRG_NAME, "error: [#{out_dir}] is not a directory" )
129 if !File.directory?( out_dir_doms )
130 Util.fatal_error( PRG_NAME, "error: [#{out_dir_doms}] is not a directory" )
136 log << "Program : " + PRG_NAME + ld
137 log << "Version : " + PRG_VERSION + ld
138 log << "Program date : " + PRG_DATE + ld
141 puts( "Sequence names files suffix: " + seq_names_files_suffix )
142 log << "Sequence names files suffix: " + seq_names_files_suffix + ld
143 puts( "Input dir : " + input_dir )
144 log << "Input dir : " + input_dir + ld
145 puts( "Output dir : " + out_dir )
146 log << "Output dir : " + out_dir + ld
147 puts( "Output dir domains : " + out_dir_doms )
148 log << "Output dir domains : " + out_dir_doms + ld
149 if ( mapping_file != nil )
150 puts( "Mapping file : " + mapping_file )
151 log << "Mapping file : " + mapping_file + ld
154 puts( "Extension : " + extension.to_s )
155 log << "Extension : " + extension.to_s + ld
158 puts( "Extract linkers : true" )
159 log << "Extract linkers : true" + ld
161 log << "Date : " + Time.now.to_s + ld
164 if ( mapping_file != nil )
165 species_codes_to_paths = extract_mappings( mapping_file )
168 input_files = obtain_inputfiles( input_dir, seq_names_files_suffix )
172 input_files.each { |input_file|
176 puts counter.to_s + "/" + input_files.size.to_s
177 read_seq_family_file( input_file,
178 seq_names_files_suffix,
180 species_codes_to_paths,
189 Util.print_message( PRG_NAME, "OK" )
195 def read_seq_family_file( input_file,
196 seq_names_files_suffix,
198 species_codes_to_paths,
207 Util.check_file_for_readability( input_file )
208 rescue ArgumentError => e
209 Util.fatal_error( PRG_NAME, "error: " + e.to_s )
211 basename = File.basename( input_file, seq_names_files_suffix )
212 out_file_path_fasta_file = out_dir + Constants::FILE_SEPARATOR + basename + FASTA_SUFFIX
213 out_file_path_normalized_ids_fasta_file = out_dir + Constants::FILE_SEPARATOR + basename + FASTA_WITH_NORMALIZED_IDS_SUFFIX
214 out_file_path_ids_map = out_dir + Constants::FILE_SEPARATOR + basename + NORMALIZED_IDS_MAP_SUFFIX
215 doms_out_file_path_fasta_file = out_dir_doms + Constants::FILE_SEPARATOR + basename + "_domains" + FASTA_SUFFIX
216 doms_ext_out_file_path_fasta_file = nil
218 doms_ext_out_file_path_fasta_file = out_dir_doms + Constants::FILE_SEPARATOR + basename + "_domains_ext_" + extension.to_s + FASTA_SUFFIX
221 Util.check_file_for_writability( out_file_path_fasta_file )
222 Util.check_file_for_writability( out_file_path_normalized_ids_fasta_file )
223 Util.check_file_for_writability( out_file_path_ids_map )
224 Util.check_file_for_writability( doms_out_file_path_fasta_file )
225 rescue ArgumentError => e
226 Util.fatal_error( PRG_NAME, "error: " + e.to_s )
231 ids_map_writer = File.open( out_file_path_ids_map, 'a' )
232 rescue Exception => e
233 Util.fatal_error( PRG_NAME, "error: " + e.to_s )
239 new_msa_normalized_ids = Msa.new
240 new_msa_domains = Msa.new
241 new_msa_domains_extended = Msa.new
242 per_species_counter = 0
247 File.open( input_file ) do | file |
248 while line = file.gets
250 if !Util.is_string_empty?( line ) && !(line =~ /\s*#/ )
251 values = line.split( PROTEINS_LIST_FILE_SEPARATOR )
253 if ( values.length < 2 )
254 Util.fatal_error( PRG_NAME, "unexpected format: " + line )
256 species = values[ 0 ]
257 if species == "BRADI" || species == "ASPNG" || species == "SCLSC" || species == "PTEVA" || species == "EIMTE"
260 seq_name = values[ 1 ]
262 if ( values.length > 3 )
263 domain_ranges_block = values[ 3 ]
264 domain_ranges = domain_ranges_block.split( "/" )
266 if ( species != current_species )
267 current_species = species
268 my_file = input_dir + Constants::FILE_SEPARATOR + current_species
270 if ( !File.exist?( my_file ) )
271 if species_codes_to_paths == nil
272 Util.fatal_error( PRG_NAME, "error: [#{my_file}] not found and no mapping file provided" )
273 elsif ( !species_codes_to_paths.has_key?( current_species ) )
274 Util.fatal_error( PRG_NAME, "error: species [#{current_species}] not found in mapping file [#{mapping_file}]" )
276 my_file = species_codes_to_paths[ current_species ]
278 my_path = File.expand_path( my_file )
279 my_readlink = my_path
280 if ( File.symlink?( my_path ) )
281 my_readlink = File.readlink( my_path )
283 current_msa = read_fasta_file( my_file )
285 if ( per_species_counter > 0 )
286 print_counts( per_species_counter, log, Constants::LINE_DELIMITER )
287 per_species_counter = 0
289 puts " " + current_species + " [" + my_readlink + "]"
290 log << current_species + " [" + my_readlink + "]" + Constants::LINE_DELIMITER
293 log << " " + seq_name + Constants::LINE_DELIMITER
294 per_species_counter = per_species_counter + 1
297 if current_msa.find_by_name_start( seq_name, true ).size > 0
299 seq = current_msa.get_by_name_start( seq_name, true ).copy
300 rescue ArgumentError => e
301 Util.fatal_error( PRG_NAME, "error: " + e.to_s )
304 # Not found, try finding by partial match.
306 seq = current_msa.get_by_name( seq_name, true, true )
307 rescue ArgumentError => e
308 Util.fatal_error( PRG_NAME, "error: " + e.to_s )
312 normalized_id = per_species_counter.to_s( 16 ).upcase +
313 "_" + current_species
315 per_species_counter.to_i
317 ids_map_writer.write( normalized_id + ": " + seq.get_name + Constants::LINE_DELIMITER )
321 orig_name = seq.get_name
322 seq.set_name( seq.get_name + " [" + current_species + "]" )
323 new_msa.add_sequence( seq )
325 Util.fatal_error( PRG_NAME, "unexected error: seq is nil" )
328 if domain_ranges != nil
332 domain_ranges.each { |range|
333 if range != nil && range.length > 0
337 new_msa_domains.add_sequence( Sequence.new( orig_name +
340 "] [" + basename + "] [" +
341 current_species + "]",
342 seq.get_sequence_as_string[from..to] ) )
344 from_e = from - extension
348 to_e = to + extension
349 if to_e > seq.get_sequence_as_string.length - 1
350 to_e = seq.get_sequence_as_string.length - 1
352 new_msa_domains_extended.add_sequence( Sequence.new( orig_name +
357 "] [" + basename + "] [" +
358 current_species + "]",
359 seq.get_sequence_as_string[ from_e..to_e ] ) )
369 mod_line = line + "\t[" + get_linker_sequence( f, t, seq ) + "|"
371 mod_line += get_linker_sequence( prev_to + 1, from - 1, seq ) + "|"
375 end # range != nil && range.length > 0
377 if extract_linkers && prev_to > 0
379 t = seq.get_sequence_as_string.length - 1
381 t = prev_to + extension
383 mod_line += get_linker_sequence( f, t, seq ) + "]"
387 new_msa_normalized_ids.add_sequence( Sequence.new( normalized_id, seq.get_sequence_as_string ) )
389 linkers << mod_line + Constants::LINE_DELIMITER
391 end # !Util.is_string_empty?( line ) && !(line =~ /\s*#/ )
392 end # while line = file.gets
398 if ( per_species_counter > 0 )
399 print_counts( per_species_counter, log, Constants::LINE_DELIMITER )
404 fasta_writer = FastaWriter.new()
405 fasta_writer.remove_gap_chars
409 io.write_to_file( new_msa, out_file_path_fasta_file, fasta_writer )
410 rescue Exception => e
411 Util.fatal_error( PRG_NAME, "error: " + e.to_s )
414 if new_msa_domains != nil
416 io.write_to_file( new_msa_domains, doms_out_file_path_fasta_file, fasta_writer )
417 rescue Exception => e
418 Util.fatal_error( PRG_NAME, "error: " + e.to_s )
422 if extension > 0 && new_msa_domains_extended != nil
424 io.write_to_file( new_msa_domains_extended, doms_ext_out_file_path_fasta_file, fasta_writer )
425 rescue Exception => e
426 Util.fatal_error( PRG_NAME, "error: " + e.to_s )
431 io.write_to_file( new_msa_normalized_ids, out_file_path_normalized_ids_fasta_file, fasta_writer )
432 rescue Exception => e
433 Util.fatal_error( PRG_NAME, "error: " + e.to_s )
437 f = File.open( out_dir + Constants::FILE_SEPARATOR + basename + LOG_SUFFIX , 'a' )
440 rescue Exception => e
441 Util.fatal_error( PRG_NAME, "error: " + e.to_s )
444 if extract_linkers && linkers != nil
446 f = File.open( out_dir + Constants::FILE_SEPARATOR + basename + LINKERS_SUFFIX , 'a' )
449 rescue Exception => e
450 Util.fatal_error( PRG_NAME, "error: " + e.to_s )
458 def get_linker_sequence( from, to, seq )
462 if to > seq.get_sequence_as_string.length - 1
463 to = seq.get_sequence_as_string.length - 1
468 return from.to_s + "-" + to.to_s + ":" + seq.get_sequence_as_string[ from..to ]
472 def obtain_inputfiles( input_dir, seq_names_files_suffix )
473 input_files = Array.new()
474 Dir.foreach( input_dir ) { |file_name|
475 if file_name.index( seq_names_files_suffix ) == ( file_name.size - seq_names_files_suffix.size )
476 input_files.push( input_dir + Constants::FILE_SEPARATOR + file_name )
482 def extract_mappings( mapping_file )
483 species_code_to_path = Hash.new()
484 File.open( mapping_file ) do | file |
485 while line = file.gets
486 if ( !Util.is_string_empty?( line ) && !(line =~ /\s*#/ ) )
487 if ( line =~ /(\S+)\s+(\S+)/ )
490 if ( species_code_to_path.has_key?( species ) )
491 Util.fatal_error( PRG_NAME, "error: species code [#{species}] is not unique" )
493 if ( species_code_to_path.has_value?( path ) )
494 Util.fatal_error( PRG_NAME, "error: path [#{path}] is not unique" )
496 if ( !File.exist?( path ) )
497 Util.fatal_error( PRG_NAME, "error: file [#{path}] does not exist" )
499 if ( !File.file?( path ) )
500 Util.fatal_error( PRG_NAME, "error: [#{path}] is not a regular file" )
502 if ( !File.readable?( path ) )
503 Util.fatal_error( PRG_NAME, "error: file [#{path}] is not readable" )
505 if ( File.size( path ) < 10000 )
506 Util.fatal_error( PRG_NAME, "error: file [#{path}] appears too small" )
508 if ( !Util.looks_like_fasta?( path ) )
509 Util.fatal_error( PRG_NAME, "error: file [#{path}] does not appear to be a fasta file" )
511 species_code_to_path[ species ] = path
512 puts species + " -> " + path
520 def print_counts( per_species_counter, log, ld )
521 puts " [sum: " + per_species_counter.to_s + "]"
522 log << " [sum: " + per_species_counter.to_s + "]" + ld
525 def read_fasta_file( input )
529 msa = f.create_msa_from_file( input, FastaParser.new() )
530 rescue Exception => e
531 Util.fatal_error( PRG_NAME, "error: " + e.to_s )
539 puts( " " + PRG_NAME + ".rb <sequence names file suffix> <input dir containing sequence names files " +
540 "and possibly genome multiple-sequence ('fasta') files> <output directory for sequences> <output directory for domains> [mapping file for " +
541 "genome multiple-sequence ('fasta') files not in input dir]" )
543 puts( " option: -" + EXT_OPTION + "=<int>: to extend extracted domains" )
544 puts( " -" + EXTRACT_LINKERS_OPTION + " : to extract linkers" )
546 puts( " " + "Example: \"mse.rb .prot . protein_seqs domain_seqs ../genome_locations.txt\"" )
550 end # class MultiSequenceExtractor