Module: GEO::SOFT

Defined in:
lib/MARQ/GEO.rb

Overview

Parse information in .soft files

Constant Summary collapse

GEO_SOFT =
"http://www.ncbi.nlm.nih.gov/geo/query/acc.cgi?targ=self&view=full&form=text&acc="
ID_FIX =
{
  :mgi_unigene => proc{|gene| if gene then gene.match(/^Mm./) ? gene : "Mm." + gene end},
  :human_unigene => proc{|gene| if gene then gene.match(/^Hs./) ? gene : "Hs." + gene end},
}
@@nice =
1
@@formats =

{{{ Guess the format of the IDS

{}

Class Method Summary collapse

Class Method Details

.consecutive?(ids) ⇒ Boolean

Id list is in sequence

Returns:

  • (Boolean)


82
83
84
# File 'lib/MARQ/GEO.rb', line 82

def self.consecutive?(ids)
  ids.collect{|id| id.to_i}.sort[0..19] == (1..20).to_a
end

.dna_sequence?(ids) ⇒ Boolean

ID are DNA bases

Returns:

  • (Boolean)


92
93
94
# File 'lib/MARQ/GEO.rb', line 92

def self.dna_sequence?(ids)
  ids.compact.select{|id| ! id.strip.match(/^[ATCG]+$/i)}.empty?
end

.get_soft(item) ⇒ Object

Download a soft file. Uses cache



57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/MARQ/GEO.rb', line 57

def self.get_soft(item)
  item = item.strip
  cache_file = File.join(CACHE_DIR, item + '.soft')
  if File.exist?( cache_file )
    File.open(cache_file).read
  else
    content = Open.read(GEO_SOFT + item, :nocache => true, :nice => @@nice)
    raise "SOFT file error" if content !~ /!/
    fout = File.open(cache_file,'w')
    fout.write content
    fout.close
    content
  end
end

.GPL(platform) ⇒ Object



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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
# File 'lib/MARQ/GEO.rb', line 182

def self.GPL(platform)
  if !File.exist?(File.join(DATA_DIR, 'platforms',"#{platform}.yaml"))  &&
     !File.exist?(File.join(DATA_DIR, 'platforms',"#{platform}.skip")) 
    begin
      raise "Platform Blacklisted" if PLATFORM_BLACKLIST.include? platform

      if platform =~ /_/
        organism =  GPL(platform.match(/(.*?)_/)[1])[:organism]

        info =  {
          :organism => organism,
          :title => "Merged platforms #{ platform }",
        }
        return info
      end
      soft = get_soft(platform)


      raise "SOFT file error" if soft !~ /!/

        organisms = soft.scan(/!Platform_organism\s*=\s*(.*)/).collect{|v| v.first.strip}

      if organisms.empty?
        raise "No Organism information" 
      else
        # This might happen actually GPL2529
        organisms.delete('Schizosaccharomyces pombe') if  organisms.include?('Saccharomyces cerevisiae')
        org_name = organisms.first
      end


      title = ""
      if soft.match(/!Platform_title\s*=\s*(.*)/)
        title = $1
      end

      org = Organism.name2org(org_name)
      raise "Organism not identified: #{org_name}" if org.nil?

      if soft.match(/!platform_table_begin/)
        data = soft.split(/!platform_table_begin/s)[1].collect{|l| l.chomp.split(/\t/)}
        data.shift
        names = data.shift
        total = data.first.length
        genes = data.sort_by{ rand }[1..1000].collect{|v| v.first}

        id = guessIds(genes,org, names.first)
        other = nil
        other_pos = 0
        other_count = 0
        other_name = 0
        if id.nil? 
          (1..total - 1).to_a.each{|num|
            genes = data.collect{|v| v[num]}
            other = guessIds(genes,org, name = names[num])

            if other && other[1] > other_count
              other_pos = num
              other_count = other[1]
              other_name = names[num]
            end
          }
        end
      else
        raise "Soft file incomplete"
      end

      info = {:organism => org, :BioMart_ID => id ? id.first : nil, :title => title }
      info[:other_ID_field] = [other_pos + 1, other_name] if other_pos > 0


      Open.write(File.join(DATA_DIR, 'platforms',"#{platform}.yaml"), info.to_yaml)
    rescue Exception
      puts $!.message
      puts $!.backtrace
      Open.write(File.join(DATA_DIR, 'platforms',"#{platform}.skip"), $!.message)
    end
  end

  raise "Platform info for #{ platform } is not available and could not be automatically produced." if File.exist?(File.join(MARQ.datadir, 'GEO', 'platforms',"#{platform}.skip"))

  YAML::load(File.open(File.join(DATA_DIR, 'platforms',"#{platform}.yaml")))
end

.GSE(series) ⇒ Object

{{{ Parse soft files for several GEO entities



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
# File 'lib/MARQ/GEO.rb', line 116

def self.GSE(series)
  soft = get_soft(series)

  # Find platform
  if match = soft.scan(/!Series_platform_id\s*=?\s*(.*)/)
    platform = match.flatten.collect{|p| p.strip}.join("_")
  else
    raise "No Platform information" 
  end

  # Find title
  if soft.match(/!Series_title \s*=?\s*(.*)/)
    title = $1
  else
    raise "No Title information" 
  end

  # Find summary
  if soft.match(/!Series_summary \s*=?\s*(.*)/)
    matches = soft.scan(/!Series_summary \s*=?\s*(.*)/).to_a
    description = matches.collect{|m| m.to_s.strip.sub(/!Series_summary \s*=?\s*/,'')}.join("\n")
  else
    raise "No Summary information" 
  end

  # Find samples
  if soft.match(/!Series_sample_id \s*=?\s*(.*)/)
    matches = soft.scan(/!Series_sample_id \s*=?\s*(.*)/).to_a
    samples = matches.collect{|m| m.to_s.strip.sub(/!Series_sample_id \s*=?\s*/,'')}
  else
    raise "No Summary information" 
  end

  {
    :platform => platform,
    :description =>description.strip,
    :title => title.strip,
    :samples => samples,
  }
end

.GSM(array) ⇒ Object



157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
# File 'lib/MARQ/GEO.rb', line 157

def self.GSM(array)
  soft = get_soft(array)

  # Find title
  if soft.match(/!Sample_title\s*=?\s*(.*)/)
    title = $1
  else
    raise "No Title information" 
  end


  # Find description
  if soft.match(/!Sample_description \s*=?\s*(.*)/)
    description = $1
  else
    raise "No Description information" 
  end

  {

    :description =>description.strip,
    :title => title.strip,
  }
end

.guessIds(genes, org, name = nil) ⇒ Object

Guess the format of the id in the list. The name parameter can be used to identify some exceptions



98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/MARQ/GEO.rb', line 98

def self.guessIds(genes,org, name = nil)
  @@formats[org] ||= Organism.id_formats(org)
  if consecutive?(genes) || dna_sequence?(genes) || (numerical?(genes) && (name.nil? || !name.match(/entrez/i)))
    id = nil
  else
    fix = ID_FIX[(org + "_" + name.downcase).to_sym] if name
    if fix
      genes = genes.collect{|gene| fix.call(gene)}
    end
    id = Organism.guessIdFormat(@@formats[org], genes)
  end

  id 
end

.numerical?(ids) ⇒ Boolean

Id list is numerical

Returns:

  • (Boolean)


87
88
89
# File 'lib/MARQ/GEO.rb', line 87

def self.numerical?(ids)
  ids.compact.select{|id| ! id.match(/^\d+$/)}.uniq.length < ids.length.to_f / 10
end