Class: Cytogenetics::Karyotype

Inherits:
Object
  • Object
show all
Defined in:
lib/cytogenetics/karyotype.rb

Constant Summary collapse

@@haploid =
23

Class Attribute Summary collapse

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(karyotype_str) ⇒ Karyotype

Returns a new instance of Karyotype.

Raises:

  • (ArgumentError)


17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/cytogenetics/karyotype.rb', line 17

def initialize(karyotype_str)
  config_logging()
  raise ArgumentError, "#{karyotype_str} is not a karyotype." unless (karyotype_str.is_a? String and karyotype_str.length > 1)
  @log.info("Reading karyotype #{karyotype_str}")

  @karyotype = karyotype_str.gsub(/\s/, "")
  @normal_chr = {}; @abnormal_chr = {}; @aberrations = {}; @unclear_aberrations = [];
  setup_abberation_objs()
  prep_karyotype()
  handle_ploidy_diff()
  analyze()
end

Class Attribute Details

.aberration_objsObject

Returns the value of attribute aberration_objs.



14
15
16
# File 'lib/cytogenetics/karyotype.rb', line 14

def aberration_objs
  @aberration_objs
end

.logObject

Returns the value of attribute log.



14
15
16
# File 'lib/cytogenetics/karyotype.rb', line 14

def log
  @log
end

.unclear_aberrationsObject

Returns the value of attribute unclear_aberrations.



14
15
16
# File 'lib/cytogenetics/karyotype.rb', line 14

def unclear_aberrations
  @unclear_aberrations
end

Instance Attribute Details

#aberrationsObject (readonly)

Returns the value of attribute aberrations.



11
12
13
# File 'lib/cytogenetics/karyotype.rb', line 11

def aberrations
  @aberrations
end

#abnormal_chrObject (readonly)

Returns the value of attribute abnormal_chr.



11
12
13
# File 'lib/cytogenetics/karyotype.rb', line 11

def abnormal_chr
  @abnormal_chr
end

#karyotypeObject (readonly)

Returns the value of attribute karyotype.



11
12
13
# File 'lib/cytogenetics/karyotype.rb', line 11

def karyotype
  @karyotype
end

#normal_chrObject (readonly)

Returns the value of attribute normal_chr.



11
12
13
# File 'lib/cytogenetics/karyotype.rb', line 11

def normal_chr
  @normal_chr
end

#ploidyObject (readonly)

Returns the value of attribute ploidy.



11
12
13
# File 'lib/cytogenetics/karyotype.rb', line 11

def ploidy
  @ploidy
end

#sexObject (readonly)

Returns the value of attribute sex.



11
12
13
# File 'lib/cytogenetics/karyotype.rb', line 11

def sex
  @sex
end

Instance Method Details

#analyzeObject



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/cytogenetics/karyotype.rb', line 30

def analyze
  Aberration.aberration_type.each do |abr_type|
    next unless @aberrations.has_key? abr_type
    regex = @aberration_obj[abr_type].regex

    @aberrations[abr_type].each do |abr|
#        if abr_type
      abr.match(regex)
      @log.warn("Aberration has two chromosomes #{abr} but only the first one is handled.") unless ($2.nil? or $1.eql? $2)

      ## TODO deal with the case of 2 chromosomes defined in the aberration
      chr = Chromosome.new($1, true)
      chr.aberration(@aberration_obj[abr_type].new(abr))

      @abnormal_chr[chr.name] = [] unless @abnormal_chr.has_key? chr.name
      @abnormal_chr[chr.name] << chr
    end
  end
end

#config_loggingObject



101
102
103
104
# File 'lib/cytogenetics/karyotype.rb', line 101

def config_logging
  @log = Cytogenetics.logger
  #@log.progname = self.class.name
end

#handle_ploidy_diffObject



112
113
114
115
# File 'lib/cytogenetics/karyotype.rb', line 112

def handle_ploidy_diff
  @aberrations[:loss].each { |c| @normal_chr[c] -= 1 } if @aberrations[:loss]
  @aberrations[:gain].each { |c| @normal_chr[c] += 1 } if @aberrations[:gain]
end

#prep_karyotypeObject

determine ploidy & gender, clean up each aberration and drop any “unknown”



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
# File 'lib/cytogenetics/karyotype.rb', line 118

def prep_karyotype
  @karyotype.gsub!(/\s/, "")
  clones = @karyotype.scan(/(\[\d+\])/).collect { |a| a[0] }
  @log.warn("Karyotype is a collection of clones, analysis may be inaccurate.") if clones.length > 3

  @karyotype.gsub!(/\[\d+\]/, "") # don't care about numbers of cells: [5]

  (pl, sc) = @karyotype.split(",")[0..1]
  if (pl and sc)
    @ploidy = KaryotypeReader.calculate_ploidy(pl, @@haploid)
    sex_chr = KaryotypeReader.determine_sex(sc)
  else
    raise KaryotypeError, "'#{@karyotype}' is not a valid karyotype. Ploidy and sex defnitions are absent"
  end

  st = sex_chr.values.inject { |sum, v| sum+v }
  @sex = nil
  karyotype_index = 1 # sometimes the sex is not indicated and there's no case information to figure it out
  if st > 0
    @sex = sex_chr.keys.join("")
    karyotype_index = 2
  end

  (Array(1..23)).each { |c| @normal_chr[c.to_s] = @ploidy.to_i }

  sex_chr.each_pair { |c, p| @normal_chr[c] = p.to_i }

  # deal with the most common karyotype string inconsistencies
  cleaned_karyotype = []

  @karyotype.split(",")[karyotype_index..-1].each do |abr|
    cleaned_karyotype |= [cleaned_karyotype, KaryotypeReader.cleanup(abr)].flatten
  end
  @karyotype = cleaned_karyotype

  # classify each type of aberration in the karyotype
  @karyotype.each do |k|
    abrclass = Aberration.classify_aberration(k)
    @aberrations[abrclass] = [] unless @aberrations.has_key? abrclass
    @aberrations[abrclass] << k.sub(/^(\+|-)?/, "")
  end

  @aberrations.each_pair do |abrclass, abrlist|
    next if (abrclass.eql? ChromosomeAberrations::ChromosomeGain.type or abrclass.eql? ChromosomeAberrations::ChromosomeLoss.type)
    # aberrations other than chromosome gains/losses should be uniquely represented

    counts = abrlist.inject(Hash.new(0)) { |h, i| h[i] += 1; h }
    counts.each_pair { |k, v| @log.warn("#{k} was seen multiple times. Analyzed only once.") if v > 1 }

    @aberrations[abrclass] = abrlist.uniq
  end

end

#report_breakpointsObject

get breakpoints for the karyotype



51
52
53
54
55
56
57
58
59
60
61
# File 'lib/cytogenetics/karyotype.rb', line 51

def report_breakpoints
  bps = Array.new
  @abnormal_chr.each_pair do |c, chr_list|
    chr_list.each do |chr|
      bps << chr.breakpoints
    end
  end
  bps.delete_if { |c| c.empty? }
  bps.flatten!
  return bps
end

#report_fragmentsObject



63
64
65
66
67
68
69
70
71
72
73
# File 'lib/cytogenetics/karyotype.rb', line 63

def report_fragments
  frags = []
  @abnormal_chr.each_pair do |c, chr_list|
    chr_list.each do |chr|
      frags << chr.fragments
    end
  end
  frags.delete_if { |c| c.empty? }
  frags.flatten!
  return frags
end

#report_ploidy_changeObject



75
76
77
78
79
80
81
# File 'lib/cytogenetics/karyotype.rb', line 75

def report_ploidy_change
  pd = []
  pd << @aberrations[:loss].map { |e| "-#{e}" } if @aberrations[:loss]
  pd << @aberrations[:gain].map { |e| "+#{e}" } if @aberrations[:gain]
  pd.flatten!
  return pd
end

#setup_abberation_objsObject



107
108
109
# File 'lib/cytogenetics/karyotype.rb', line 107

def setup_abberation_objs
  @aberration_obj = Aberration.aberration_objs
end

#summarizeObject



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/cytogenetics/karyotype.rb', line 83

def summarize
  summary = "NORMAL CHROMOSOMES\n"
  @normal_chr.each_pair do |chr, count|
    summary = "#{summary} #{chr}: #{count}\n"
  end

  summary = "#{summary}\nABNORMAL:"
  @abnormal_chr.each_pair do |chr, list|
    summary = "#{summary}\n#{chr}"
    list.each do |c|
      summary = "#{summary}\n#{c.aberrations}\n"
      summary = "#{summary}\n#{c.breakpoints}\n"
    end
  end
end