Class: Transfuse::Consensus

Inherits:
Object
  • Object
show all
Defined in:
lib/transfuse/consensus.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(verbose) ⇒ Consensus

Returns a new instance of Consensus.



11
12
13
# File 'lib/transfuse/consensus.rb', line 11

def initialize verbose
  @verbose = verbose
end

Instance Attribute Details

#contigsObject (readonly)

Returns the value of attribute contigs.



9
10
11
# File 'lib/transfuse/consensus.rb', line 9

def contigs
  @contigs
end

Instance Method Details

#run(msa, scores, output) ⇒ Object



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
70
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
101
# File 'lib/transfuse/consensus.rb', line 15

def run msa, scores, output
  return 1 if File.exist?(output)
  print "writing consensus " if @verbose
  # msa is a hash
  #   key = cluster id
  #   value = list
  #     list of sequences in cluster aligned with gaps
  preoutput = "#{File.basename(output, File.extname(output))}_cons.fa"
  count = 0
  File.open("#{output}.data", "w") do |out2|
    File.open(preoutput, "w") do |out|
      msa.each do |id, list|
        count+=1
        print "." if count%5_000==0 and @verbose
        exons={}
        cons = []
        length = list[0][:seq].length
          list.each_with_index do |hash, index|
            seq = hash[:seq]
            name = hash[:name]
            out2.write "#{id}\t#{scores[name][:score]}\t#{name}\n"
            prev = ""
            gap = 0
            exon = 0
            seq.each_char do |c|
              if c=="-"
                base="-"
              else
                base="*"
              end
              if base!=prev
                if c=="-"
                  gap+=1
                else
                  exon+=1
                end
              end
              if c=="-"
                prev = "-"
              else
                prev = "*"
              end
            end
            exons[index] = exon
          end

        consensus = ""
        0.upto(length-1) do |i|
          base="N"
          counts = {}
          list.each_with_index do |hash, index|
            seq = hash[:seq]
            if seq[i] != "-" and seq[i] != "N"
              counts[seq[i]]||=0
              counts[seq[i]] += 1
              if exons[index]==1
                base = seq[i]
              end
            end
          end
          if counts.size>0
            base = counts.sort.last.first
          end
          consensus << base
        end

        if consensus.count("N") < consensus.length.to_f*0.5
          cons << consensus
        end

        list.each_with_index do |hash, index|
          if exons[index] > 1
            cons << hash[:seq].delete("-")
          end
        end

        cons.each_with_index do |s,index|
          out.write ">contig#{id}.#{index+1}\n"
          out.write "#{s}\n"
        end

      end # msa.each
    end # file
  end # file open
  puts " Done" if @verbose
  return preoutput
end