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.19"
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 FASTA_SUFFIX = ".fasta"
39 FASTA_WITH_NORMALIZED_IDS_SUFFIX = ".ni.fasta"
40 NORMALIZED_IDS_MAP_SUFFIX = ".nim"
41 PROTEINS_LIST_FILE_SEPARATOR = "\t"
46 Util.print_program_information( PRG_NAME,
55 ld = Constants::LINE_DELIMITER
58 cla = CommandLineArguments.new( ARGV )
59 rescue ArgumentError => e
60 Util.fatal_error( PRG_NAME, "error: " + e.to_s )
63 if ( cla.is_option_set?( HELP_OPTION_1 ) ||
64 cla.is_option_set?( HELP_OPTION_2 ) )
69 if ( cla.get_number_of_files != 4 && cla.get_number_of_files != 5 )
74 allowed_opts = Array.new
75 allowed_opts.push(EXT_OPTION)
76 allowed_opts.push(EXTRACT_LINKERS_OPTION)
78 disallowed = cla.validate_allowed_options_as_str( allowed_opts )
79 if ( disallowed.length > 0 )
80 Util.fatal_error( PRG_NAME,
81 "unknown option(s): " + disallowed,
85 seq_names_files_suffix = cla.get_file_name( 0 )
86 input_dir = cla.get_file_name( 1 )
87 out_dir = cla.get_file_name( 2 )
88 out_dir_doms = cla.get_file_name( 3 )
91 if ( cla.get_number_of_files == 5 )
92 mapping_file = cla.get_file_name( 4 )
94 Util.check_file_for_readability( mapping_file )
95 rescue ArgumentError => e
96 Util.fatal_error( PRG_NAME, "error: " + e.to_s )
101 if cla.is_option_set?(EXT_OPTION)
102 extension = cla.get_option_value_as_int(EXT_OPTION)
108 extract_linkers = false
109 if cla.is_option_set?(EXTRACT_LINKERS_OPTIO)
110 extract_linkers = true
113 if !File.exist?( input_dir )
114 Util.fatal_error( PRG_NAME, "error: input directory [#{input_dir}] does not exist" )
116 if !File.exist?( out_dir )
117 Util.fatal_error( PRG_NAME, "error: output directory [#{out_dir}] does not exist" )
119 if !File.exist?( out_dir_doms )
120 Util.fatal_error( PRG_NAME, "error: output directory [#{out_dir_doms}] does not exist" )
122 if !File.directory?( input_dir )
123 Util.fatal_error( PRG_NAME, "error: [#{input_dir}] is not a directory" )
125 if !File.directory?( out_dir )
126 Util.fatal_error( PRG_NAME, "error: [#{out_dir}] is not a directory" )
128 if !File.directory?( out_dir_doms )
129 Util.fatal_error( PRG_NAME, "error: [#{out_dir_doms}] is not a directory" )
135 log << "Program : " + PRG_NAME + ld
136 log << "Version : " + PRG_VERSION + ld
137 log << "Program date : " + PRG_DATE + ld
140 puts( "Sequence names files suffix: " + seq_names_files_suffix )
141 log << "Sequence names files suffix: " + seq_names_files_suffix + ld
142 puts( "Input dir : " + input_dir )
143 log << "Input dir : " + input_dir + ld
144 puts( "Output dir : " + out_dir )
145 log << "Output dir : " + out_dir + ld
146 puts( "Output dir domains : " + out_dir_doms )
147 log << "Output dir domains : " + out_dir_doms + ld
148 if ( mapping_file != nil )
149 puts( "Mapping file : " + mapping_file )
150 log << "Mapping file : " + mapping_file + ld
153 puts( "Extension : " + extension.to_s )
154 log << "Extension : " + extension.to_s + ld
156 log << "Date : " + Time.now.to_s + ld
159 if ( mapping_file != nil )
160 species_codes_to_paths = extract_mappings( mapping_file )
163 input_files = obtain_inputfiles( input_dir, seq_names_files_suffix )
167 input_files.each { |input_file|
171 puts counter.to_s + "/" + input_files.size.to_s
172 read_seq_family_file( input_file,
173 seq_names_files_suffix,
175 species_codes_to_paths,
184 Util.print_message( PRG_NAME, "OK" )
190 def read_seq_family_file( input_file,
191 seq_names_files_suffix,
193 species_codes_to_paths,
202 Util.check_file_for_readability( input_file )
203 rescue ArgumentError => e
204 Util.fatal_error( PRG_NAME, "error: " + e.to_s )
206 basename = File.basename( input_file, seq_names_files_suffix )
207 out_file_path_fasta_file = out_dir + Constants::FILE_SEPARATOR + basename + FASTA_SUFFIX
208 out_file_path_normalized_ids_fasta_file = out_dir + Constants::FILE_SEPARATOR + basename + FASTA_WITH_NORMALIZED_IDS_SUFFIX
209 out_file_path_ids_map = out_dir + Constants::FILE_SEPARATOR + basename + NORMALIZED_IDS_MAP_SUFFIX
210 doms_out_file_path_fasta_file = out_dir_doms + Constants::FILE_SEPARATOR + basename + "_domains" + FASTA_SUFFIX
211 doms_ext_out_file_path_fasta_file = nil
213 doms_ext_out_file_path_fasta_file = out_dir_doms + Constants::FILE_SEPARATOR + basename + "_domains_ext_" + extension.to_s + FASTA_SUFFIX
216 Util.check_file_for_writability( out_file_path_fasta_file )
217 Util.check_file_for_writability( out_file_path_normalized_ids_fasta_file )
218 Util.check_file_for_writability( out_file_path_ids_map )
219 Util.check_file_for_writability( doms_out_file_path_fasta_file )
220 rescue ArgumentError => e
221 Util.fatal_error( PRG_NAME, "error: " + e.to_s )
226 ids_map_writer = File.open( out_file_path_ids_map, 'a' )
227 rescue Exception => e
228 Util.fatal_error( PRG_NAME, "error: " + e.to_s )
234 new_msa_normalized_ids = Msa.new
235 new_msa_domains = Msa.new
236 new_msa_domains_extended = Msa.new
237 per_species_counter = 0
241 File.open( input_file ) do | file |
242 while line = file.gets
244 if !Util.is_string_empty?( line ) && !(line =~ /\s*#/ )
245 values = line.split( PROTEINS_LIST_FILE_SEPARATOR )
247 if ( values.length < 2 )
248 Util.fatal_error( PRG_NAME, "unexpected format: " + line )
250 species = values[ 0 ]
251 if species == "BRADI" || species == "ASPNG" || species == "SCLSC" || species == "PTEVA" || species == "EIMTE"
254 seq_name = values[ 1 ]
256 if ( values.length > 3 )
257 domain_ranges_block = values[ 3 ]
258 domain_ranges = domain_ranges_block.split( "/" )
260 if ( species != current_species )
261 current_species = species
262 my_file = input_dir + Constants::FILE_SEPARATOR + current_species
264 if ( !File.exist?( my_file ) )
265 if species_codes_to_paths == nil
266 Util.fatal_error( PRG_NAME, "error: [#{my_file}] not found and no mapping file provided" )
267 elsif ( !species_codes_to_paths.has_key?( current_species ) )
268 Util.fatal_error( PRG_NAME, "error: species [#{current_species}] not found in mapping file [#{mapping_file}]" )
270 my_file = species_codes_to_paths[ current_species ]
272 my_path = File.expand_path( my_file )
273 my_readlink = my_path
274 if ( File.symlink?( my_path ) )
275 my_readlink = File.readlink( my_path )
277 current_msa = read_fasta_file( my_file )
279 if ( per_species_counter > 0 )
280 print_counts( per_species_counter, log, Constants::LINE_DELIMITER )
281 per_species_counter = 0
283 puts " " + current_species + " [" + my_readlink + "]"
284 log << current_species + " [" + my_readlink + "]" + Constants::LINE_DELIMITER
287 log << " " + seq_name + Constants::LINE_DELIMITER
288 per_species_counter = per_species_counter + 1
291 if current_msa.find_by_name_start( seq_name, true ).size > 0
293 seq = current_msa.get_by_name_start( seq_name, true ).copy
294 rescue ArgumentError => e
295 Util.fatal_error( PRG_NAME, "error: " + e.to_s )
298 # Not found, try finding by partial match.
300 seq = current_msa.get_by_name( seq_name, true, true )
301 rescue ArgumentError => e
302 Util.fatal_error( PRG_NAME, "error: " + e.to_s )
306 normalized_id = per_species_counter.to_s( 16 ).upcase +
307 "_" + current_species
309 per_species_counter.to_i
311 ids_map_writer.write( normalized_id + ": " + seq.get_name + Constants::LINE_DELIMITER )
315 orig_name = seq.get_name
316 seq.set_name( seq.get_name + " [" + current_species + "]" )
317 new_msa.add_sequence( seq )
319 Util.fatal_error( PRG_NAME, "unexected error: seq is nil" )
322 if domain_ranges != nil
326 domain_ranges.each { |range|
327 if range != nil && range.length > 0
331 new_msa_domains.add_sequence( Sequence.new( orig_name +
334 "] [" + basename + "] [" +
335 current_species + "]",
336 seq.get_sequence_as_string[from..to] ) )
338 from_e = from - extension
342 to_e = to + extension
343 if to_e > seq.get_sequence_as_string.length - 1
344 to_e = seq.get_sequence_as_string.length - 1
346 new_msa_domains_extended.add_sequence( Sequence.new( orig_name +
351 "] [" + basename + "] [" +
352 current_species + "]",
353 seq.get_sequence_as_string[ from_e..to_e ] ) )
363 mod_line = line + "\t[" + get_linker_sequence( f, t, seq ) + "|"
365 mod_line += get_linker_sequence( prev_to + 1, from - 1, seq ) + "|"
369 end # range != nil && range.length > 0
371 if extract_linkers && prev_to > 0
373 t = seq.get_sequence_as_string.length - 1
377 mod_line += get_linker_sequence( f, t, seq ) + "]"
381 new_msa_normalized_ids.add_sequence( Sequence.new( normalized_id, seq.get_sequence_as_string ) )
385 end # !Util.is_string_empty?( line ) && !(line =~ /\s*#/ )
386 end # while line = file.gets
392 if ( per_species_counter > 0 )
393 print_counts( per_species_counter, log, Constants::LINE_DELIMITER )
398 fasta_writer = FastaWriter.new()
399 fasta_writer.remove_gap_chars
403 io.write_to_file( new_msa, out_file_path_fasta_file, fasta_writer )
404 rescue Exception => e
405 Util.fatal_error( PRG_NAME, "error: " + e.to_s )
408 if new_msa_domains != nil
410 io.write_to_file( new_msa_domains, doms_out_file_path_fasta_file, fasta_writer )
411 rescue Exception => e
412 Util.fatal_error( PRG_NAME, "error: " + e.to_s )
416 if extension > 0 && new_msa_domains_extended != nil
418 io.write_to_file( new_msa_domains_extended, doms_ext_out_file_path_fasta_file, fasta_writer )
419 rescue Exception => e
420 Util.fatal_error( PRG_NAME, "error: " + e.to_s )
425 io.write_to_file( new_msa_normalized_ids, out_file_path_normalized_ids_fasta_file, fasta_writer )
426 rescue Exception => e
427 Util.fatal_error( PRG_NAME, "error: " + e.to_s )
431 f = File.open( out_dir + Constants::FILE_SEPARATOR + basename + LOG_SUFFIX , 'a' )
434 rescue Exception => e
435 Util.fatal_error( PRG_NAME, "error: " + e.to_s )
441 def get_linker_sequence( from, to, seq )
445 if to > seq.get_sequence_as_string.length - 1
446 to = seq.get_sequence_as_string.length - 1
451 return from.to_s + "-" + to.to_s + ":" + seq.get_sequence_as_string[ from..to ]
455 def obtain_inputfiles( input_dir, seq_names_files_suffix )
456 input_files = Array.new()
457 Dir.foreach( input_dir ) { |file_name|
458 if file_name.index( seq_names_files_suffix ) == ( file_name.size - seq_names_files_suffix.size )
459 input_files.push( input_dir + Constants::FILE_SEPARATOR + file_name )
465 def extract_mappings( mapping_file )
466 species_code_to_path = Hash.new()
467 File.open( mapping_file ) do | file |
468 while line = file.gets
469 if ( !Util.is_string_empty?( line ) && !(line =~ /\s*#/ ) )
470 if ( line =~ /(\S+)\s+(\S+)/ )
473 if ( species_code_to_path.has_key?( species ) )
474 Util.fatal_error( PRG_NAME, "error: species code [#{species}] is not unique" )
476 if ( species_code_to_path.has_value?( path ) )
477 Util.fatal_error( PRG_NAME, "error: path [#{path}] is not unique" )
479 if ( !File.exist?( path ) )
480 Util.fatal_error( PRG_NAME, "error: file [#{path}] does not exist" )
482 if ( !File.file?( path ) )
483 Util.fatal_error( PRG_NAME, "error: [#{path}] is not a regular file" )
485 if ( !File.readable?( path ) )
486 Util.fatal_error( PRG_NAME, "error: file [#{path}] is not readable" )
488 if ( File.size( path ) < 10000 )
489 Util.fatal_error( PRG_NAME, "error: file [#{path}] appears too small" )
491 if ( !Util.looks_like_fasta?( path ) )
492 Util.fatal_error( PRG_NAME, "error: file [#{path}] does not appear to be a fasta file" )
494 species_code_to_path[ species ] = path
495 puts species + " -> " + path
503 def print_counts( per_species_counter, log, ld )
504 puts " [sum: " + per_species_counter.to_s + "]"
505 log << " [sum: " + per_species_counter.to_s + "]" + ld
508 def read_fasta_file( input )
512 msa = f.create_msa_from_file( input, FastaParser.new() )
513 rescue Exception => e
514 Util.fatal_error( PRG_NAME, "error: " + e.to_s )
522 puts( " " + PRG_NAME + ".rb <sequence names file suffix> <input dir containing sequence names files " +
523 "and possibly genome multiple-sequence ('fasta') files> <output directory for sequences> <output directory for domains> [mapping file for " +
524 "genome multiple-sequence ('fasta') files not in input dir]" )
526 puts( " option: -" + EXT_OPTION + "=<int>: to extend extracted domains" )
527 puts( " -" + EXTRACT_LINKERS_OPTION + " : to extract linkers" )
529 puts( " " + "Example: \"mse.rb .prot . seqs doms ../genome_locations.txt\"" )
533 end # class MultiSequenceExtractor