Module: Exodb::Utils

Defined in:
lib/exodb/utils.rb,
lib/exodb/rositza.rb,
lib/exodb/rositza/load.rb,
lib/exodb/utils/upload_var.rb,
lib/exodb/utils/upload_generef.rb

Class Method Summary collapse

Class Method Details

.guess_miriam(str) ⇒ String

Guess the type of id

Parameters:

Returns:

  • (String)

    a miriam registry or the string itself incase cannot guess



30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/exodb/utils.rb', line 30

def guess_miriam(str)
  case str
  when /\A((AC|AP|NC|NG|NM|NP|NR|NT|NW|XM|XP|XR|YP|ZP)_\d+|(NZ\_[A-Z]{4}\d+))(\.\d+)?\z/
    return "urn:miriam:refseq:#{str}"
  when /\A((ENS[A-Z]*[FPTG]\d{11}(\.\d+)?)|(FB\w{2}\d{7})|(Y[A-Z]{2}\d{3}[a-zA-Z](\-[A-Z])?)|([A-Z_a-z0-9]+(\.)?(t)?(\d+)?([a-z])?))\z/
    return "urn:miriam:ensembl:#{str}"
  when /\A((HGNC|hgnc):)?\d{1,5}\z/
    return "urn:miriam:hgnc:#{str}"
  when /\ACCDS\d+\.\d+\z/
    return "urn:miriam:ccds:#{str}"
  else
    return str
  end
end

.load_indel_from_merge(mergefile) ⇒ Object



65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/exodb/rositza/load.rb', line 65

def load_indel_from_merge(mergefile)
  
  File.open(mergefile).each do |line|
    record = line.chomp.split("\t")
    p record[0..21]
    #others = record[22..-1]
    #until others.empty?
    # p others.shift(11)
    #end
  end
  
end

.load_sample_from_csv(csvfile) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/exodb/rositza/load.rb', line 38

def load_sample_from_csv(csvfile)
  CSV.read(csvfile, col_sep: "\t", headers: true).each do |record|
    
    sample = Tumor.new({oid: record["SampleFinal"],
            type: record["Type"].downcase,
            typeid: "urn:miriam:bioportal.meddra:#{record["Type"] =~ /^spitz /i ? '10041632' : record["Type"] =~ /^spitzoid /i ? '10072450' : '10028679'}",
            patient: record["SampleFinal"].split('T')[0],
            preferred: record["Preferred"] == 'Y' ? true : false,
            paired: record["merge41final"] =~ /\Apaired\z/i ? true : false},
            labels: {})
    
    sample.add_to_dataset('internal.ds:000001')
    
    p sample.save!
    
  end
end

.load_variant_from_ann(annfile) ⇒ Object



56
57
58
59
60
61
62
63
# File 'lib/exodb/rositza/load.rb', line 56

def load_variant_from_ann(annfile)
  
  File.open(annfile).each do |line|
    record = line.chomp.split("\t")
    p record
  end
  
end

.load_variant_from_csv(csvfile) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/exodb/rositza/load.rb', line 20

def load_variant_from_csv(csvfile)
  
  CSV.read(csvfile, col_sep: "\t", headers: true).each do |record|
    
    var = SNV.new()
    var.parse_location("#{record["chromosome"]}:#{record["start position"]}")
    var.reference = record["ref nucleotide"].split('/')[0]
    var.alternate = record["var nucleotide"].split('/').uniq
    var.somaticStatus = record["Somatic Status"]
    var.reads = record["Reads"]
    var.predicted_damage = record["PolyPhen"] =~ /probably_damaging/ || record["SIFT"] =~ /deleterious/i || record["PROVEAN"] =~ /deleterious/i ? true : false
    var.aachange = record["AA Change"]
    var.add_to_sample(record["cell"])
    
    p var.save!
  end
end

.load_variant_from_merge(mergefile) ⇒ Object



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/exodb/rositza/load.rb', line 78

def load_variant_from_merge(mergefile)
  
  File.open(mergefile).each do |line|
    record = line.chomp.split("\t")
    
    header = record[0..15]
    
    list = record[16..-1]
    
    until list.empty?
      
      sampledata = list.shift(7)
      snvq = Exodb::Variant.where(oid: "#{header[2]}:#{header[3]}:#{sampledata[0]}")
      if snvq.exists?
        snv = snvq.first
        snv.temp.push(header[0..6].join("\t"))
        p snv.save!
      else
        snv = Exodb::Variant.new()
        snv.parse_location("#{header[2]}:#{header[3]}..#{header[3]}")
        snv.reference = header[10]
        snv.pileupt = sampledata[6]
        snv.temp = [] if snv.temp == nil
        snv.temp.push(header[0..6].join("\t"))
        snv.add_to_sample(sampledata[0])
        p snv.save!
      end
    end
  end
  
end

.upload_generef_from_gff3(filename, assembly = nil) ⇒ Object

Upload gene information to database using gff3 and genome sequence fasta file

Parameters:

  • gff3 (String)

    file

  • assembly (String) (defaults to: nil)

    name [default: gff file name]



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
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
# File 'lib/exodb/utils/upload_generef.rb', line 23

def upload_generef_from_gff3(filename, assembly = nil)
  
  gff = Bio::GFF::GFF3.new(File.open(filename).read)
  
  processDbxref = lambda do |str|
    case str
    when /^GeneID/
      return "urn:miriam:ncbigene:#{str.split(/:/)[1]}"
    when /^HGNC/
      return "urn:miriam:hgnc:#{str}"
    when /^HPRD/
      return "urn:miriam:hprd:#{str.split(/:/)[1]}"
    when /^miRBase/
      return "urn:miriam:mirbase:#{str.split(/:/)[1]}"
    when /^Genbank/
      return "urn:miriam:refseq:#{str.split(/:/)[1]}"
    when /^CCDS/
      return "urn:miriam:ccds:#{str.split(/:/)[1]}"
    when /^MIM/
      return "urn:miriam:omim:#{str.split(/:/)[1]}"
    else
      return str
    end
  end
  
  assembly = assembly ? assembly : File.basename(filename, '.gff3')
  
  regions = {}
  genes = {}
  seq = {}
  
  
  gff.records.each do |e|
    
    case e.feature
    when 'region'
      e.attributes.each do |attr|
        case attr[0]
        when 'chromosome'
          regions[e.seqname] = attr[1] == 'X' ? 23 : attr[1] == 'Y' ? 24 : attr[1].to_i
        end
      end
      
      if File.exist?("./genome/#{e.seqname}.fa")
        seq = {}
        Bio::FlatFile.open(Bio::FastaFormat, "./genome/#{e.seqname}.fa").each {|fasta| seq[fasta.acc_version] = fasta.to_seq}
      end
      
    when 'gene', 'tRNA'
      
      gene = {type: 'gene', xrefs: [], strand: e.strand, chrrefseq: "#{guess_miriam(e.seqname)}", location: "#{e.seqname =~ /\ANC_/ ? regions[e.seqname] : e.seqname}:#{e.start}..#{e.end}", childs: [], exon: [], cds: []}
      
      e.attributes.each do |attr|
        case attr[0]
        when 'Dbxref'
          gene[:xrefs].push(processDbxref.call(attr[1]))
        when 'Name'
          gene[:xrefs].push("urn:miriam:hgnc.symbol:#{attr[1]}") if attr[1] !~ /^LOC\d+$/
        when 'pseudo'
          gene[:psuedo] = attr[1] == 'true' ? true : false
        when 'ID'
          gene[:id] = attr[1]
        end
      end
      
      gene[:sequence] = seq[e.seqname].subseq(e.start.to_i, e.end.to_i).to_s if seq.has_key?(e.seqname)
      gene[:oid] = gene[:location]
      genes[gene[:id]] = gene
      
    when /\A(transcript|[^t]*RNA)/
      rna = {type: 'rna', xrefs: [], strand: e.strand, chr: regions[e.seqname], location: "#{regions[e.seqname]}:#{e.start}..#{e.end}", exon: [], cds: []}
      
      e.attributes.each do |attr|
        case attr[0]
        when 'Dbxref'
          rna[:xrefs].push(processDbxref.call(attr[1]))
        when 'pseudo'
          rna[:psuedo] = attr[1] == 'true' ? true : false
        when 'ID'
          rna[:id] = attr[1]
        when 'Parent'
          rna[:parent] = attr[1]
        end
      end
      
      genes[rna[:id]] = rna
      genes[rna[:parent]][:childs].push(rna[:id]) if rna[:parent]
      
    when 'exon'
      e.attributes.each do |attr|
        case attr[0]
        when 'Parent'
          genes[attr[1]][:exon].push([e.start, e.end].sort)
        end
      end
    when 'CDS'
      e.attributes.each do |attr|
        case attr[0]
        when 'Parent'
          genes[attr[1]][:cds].push([e.start, e.end].sort)
        end
      end
    end
  end
  
  genes.each_pair do |k, v|
    if v[:type] == 'gene'
      
      gene = Generef.new()
      gene.oid = v[:oid] if v.has_key?(:oid)
      gene.xrefs = v[:xrefs]
      gene.parse_location(v[:location])
      gene.chrrefseq = v[:chrrefseq]
      gene.strand = v[:strand]
      gene.psuedo = v[:psuedo] if v[:psuedo]
      gene.genomeref = assembly
      gene.sequence = v[:sequence] if v.has_key?(:sequence)
      
      v[:childs].each do |child|
        
        rna = Isoform.new()
        data = genes[child]
        rna.xrefs = data[:xrefs]
        rna.exon = data[:exon].sort
        rna.cds = data[:cds].sort
        
        gene.isoforms.push(rna)
        
      end
      
      puts "STATUS: #{gene.save! ? "SUCCESS" : "FAIL"}: Deposit Gene reference #{gene.xrefs[0]}"
      
    end
    
  end
  
  
end