Class: Bio::FinishM::Explorer

Inherits:
Object
  • Object
show all
Includes:
Logging
Defined in:
lib/finishm/explore.rb

Defined Under Namespace

Classes: InterestingPlace

Instance Method Summary collapse

Methods included from Logging

#log

Instance Method Details

#add_options(optparse_object, options) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/finishm/explore.rb', line 12

def add_options(optparse_object, options)
  optparse_object.banner = "\nUsage: finishm explore --contigs <contig_file> --interesting-ends <contig1:end,contig2:start,..> --fastq-gz <reads..> --output-explored-paths <output.fa>

  Given a contig end, explore the assembly graph to determine what is out there. Does assembly
  fail because of lack of coverage? Is there more sequence out there that has yet to be explored?
  \n\n"

  options.merge!({
    :contig_end_length => 200,
    :graph_search_leash_length => 20000,
  })

  optparse_object.separator "\nRequired arguments:\n\n"
  optparse_object.on("--contigs FILE", "Fasta file containing contigs to find the fluff on [required]") do |arg|
    options[:contigs_file] = arg
  end
  optparse_object.on("--interesting-ends INTERESTING_PLACES", Array, "Comma-separated list of places to explore from e.g. 'contig1:end,MyContig2:start' to explore from the end of contig1 and the start of MyContig2. Names of contigs are as they are in the given --contigs file. Or use 'all' to mean all contig ends in the fasta file [required]") do |arg|
    if arg == ['all']
      options[:interesting_places] = :all
    else
      arg.each do |tuple|
        options[:interesting_places] ||= []
        splits = tuple.split(':')
        if splits.length != 2
          log.error "Unable to parse this --interesting-ends argument: #{tuple}"
          exit 1
        end
        place = InterestingPlace.new
        place.contig_name = splits[0]
        if %(start end).include?(splits[1])
          place.start_or_end = splits[1]
        else
          log.error "Unable to parse this --interesting-ends argument, second half must be 'start' or 'end': #{tuple}"
          exit 1
        end
        options[:interesting_places].push place
      end
    end
  end
  optparse_object.on("--output-explored-paths PATH", "Output found paths to this file in fasta format [required]") do |arg|
    options[:output_trails_file] = arg
  end
  optparse_object.separator "\nThere must be some definition of reads too:\n\n" #TODO improve this help
  Bio::FinishM::ReadInput.new.add_options(optparse_object, options)

  optparse_object.separator "\nOptional arguments:\n\n"
  optparse_object.on("--overhang NUM", Integer, "Start assembling this far from the ends of the contigs [default: #{options[:contig_end_length]}]") do |arg|
    options[:contig_end_length] = arg.to_i
  end
  optparse_object.on("--unscaffold-first", "Break the scaffolds in the contigs file apart, and then wander between the resultant contigs[default: #{options[:graph_search_leash_length]}]") do |arg|
    options[:unscaffold_first] = true
  end
  optparse_object.on("--leash-length NUM", Integer, "Don't explore too far in the graph, only this far and not much more [default: #{options[:graph_search_leash_length]}]") do |arg|
    options[:graph_search_leash_length] = arg
  end

  Bio::FinishM::GraphGenerator.new.add_options optparse_object, options
end

#run(options, argv) ⇒ Object



102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
# File 'lib/finishm/explore.rb', line 102

def run(options, argv)
  # Read in all the contigs sequences, removing those that are too short
  probe_sequences = []
  sequence_names = []
  interesting_place_probe_indices = []
  process_sequence = lambda do |name,seq|
    if seq.length < 2*options[:contig_end_length]
      log.warn "Not attempting to make connections from this contig, as it is overly short: #{name}"
      next
    end
    if sequence_names.include?(name)
      log.error "Found duplicate sequence names, being conservative and not continuuing #{name}"
      exit 1
    end
    sequence_names.push name

    sequence = seq.seq
    fwd2 = Bio::Sequence::NA.new(sequence[0...options[:contig_end_length]])
    probe_sequences.push fwd2.reverse_complement.to_s

    probe_sequences.push sequence[(sequence.length-options[:contig_end_length])...sequence.length]
  end

  scaffolds = nil
  if options[:unscaffold_first]
    log.info "Unscaffolding scaffolds (before trying to connect them together again)"
    scaffolds = Bio::FinishM::ScaffoldBreaker.new.break_scaffolds options[:contigs_file]
    scaffolds.each do |scaffold|
      scaffold.contigs.each do |contig|
        process_sequence.call contig.name, contig.sequence
      end
    end
  else
    Bio::FlatFile.foreach(options[:contigs_file]) do |s|
      process_sequence.call s.definition, s.seq
    end
  end

  # Collect the node IDs that I'm interested in before generating the graph so don't have to do a whole assembly before getting an argument error
  interesting_probe_ids_to_place = {}
  if options[:interesting_places] == :all
    options[:interesting_places] = []
    base = 0
    sequence_names.each_with_index do |name, i|
      %w(start end).each do |side|
        place = InterestingPlace.new
        place.start_or_end = side
        place.contig_name = name
        options[:interesting_places].push place

        interesting_probe_ids_to_place[base] = place
        base += 1
      end
    end
  else
    options[:interesting_places].each do |place|
      seq_index = sequence_names.find_index place.contig_name
      if seq_index.nil?
        log.error "Unable to find interesting contig #{place.contig_name}, cannot continue"
        exit 1
      else
        base = seq_index*2
        if place.start_or_end == 'start'
          #
        elsif place.start_or_end == 'end'
          base += 1
        end
        interesting_probe_ids_to_place[base] = place
      end
    end
  end


  # Generate the graph with the probe sequences in it.
  read_input = Bio::FinishM::ReadInput.new
  read_input.parse_options options
  finishm_graph = Bio::FinishM::GraphGenerator.new.generate_graph(probe_sequences, read_input, options)

  # Explore from the interesting nodes

  output = nil
  if options[:output_trails_file] == '-'
    log.info "When trails are found, writing them to stdout"
    output = $stdout
  else
    log.info "When trails are found, writing them to #{options[:output_trails_file]}"
    output = File.open(options[:output_trails_file],'w')
  end

  explorer = Bio::AssemblyGraphAlgorithms::GraphExplorer.new
  interesting_probe_ids_to_place.each do |probe_id, place|
    log.info "Exploring from #{place}"
    if finishm_graph.probe_nodes[probe_id].nil?
      log.warn "Unable to find anchor node for #{place}, skipping exploration from there"
    else
      # Do exploration
      onode = finishm_graph.initial_path_from_probe probe_id
      paths = explorer.explore_from_node(finishm_graph.graph, onode, options[:graph_search_leash_length])
      max_length = paths.collect{|path| path.path.length_in_bp}.max
      log.info "Found #{paths.length} paths from #{place}, maximal length #{max_length}"

      # Print explorations that come back
      paths.each do |explore_path|
        begin
          seq = explore_path.path.sequence
          output.puts ">#{place.contig_name}:#{place.start_or_end} #{explore_path.termination_type} nodes:#{explore_path}"
          output.puts seq
        rescue Bio::Velvet::Graph::OrientedNodeTrail::InsufficientLengthException
          log.warn "Unable to retrieve sequence from '#{place.contig_name}:#{place.start_or_end} #{explore_path.termination_type} nodes:#{explore_path}' due to insufficient length of path, ignoring"
        end
      end
    end
  end
  output.close unless output == $stdout
end

#validate_options(options, argv) ⇒ Object



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/finishm/explore.rb', line 71

def validate_options(options, argv)
  #TODO: give a better description of the error that has occurred
  #TODO: require reads options
  if argv.length != 0
    return "Dangling argument(s) found e.g. #{argv[0]}"
  else
    [
      :contigs_file,
      :interesting_places,
      :output_trails_file,
    ].each do |sym|
      if options[sym].nil?
        return "No option found to specify #{sym}."
      end
    end

    # Need reads unless there is already an assembly
    unless options[:previous_assembly] or options[:previously_serialized_parsed_graph_file]
      error = Bio::FinishM::ReadInput.new.validate_options(options, [])
      return error unless error.nil?
      if options[:contig_end_length] < options[:velvet_kmer_size]
        return "The overhang must be greater than the size of the assembly kmer"
      else
        return nil
      end
    else
      return nil
    end
  end
end