Module: Vardetect::Vc

Defined in:
lib/vardetect-vc.rb,
lib/vardetect-vc/lib.rb,
lib/vardetect-vc/version.rb,
lib/vardetect-vc/call_snv.rb,
lib/vardetect-vc/call_combine.rb,
lib/vardetect-vc/call_prepare.rb

Constant Summary collapse

VERSION =
"0.0.5"

Class Method Summary collapse

Class Method Details

.analyse_combine(params) ⇒ Object



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
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
265
266
267
268
269
270
271
272
273
274
275
276
# File 'lib/vardetect-vc/call_combine.rb', line 169

def self.analyse_combine params
  

  # index columns
  idx = {}
  %w{snp vc st pos ref vc_a1 vc_a vc_het vc_dep vc_alt vc_r snp_ann snp_a snp_het snp_gc snp_clus st_ref st_a2 st_qua st_dep st_alt st_pos}.each_with_index do |i,index|
    idx[i.to_sym] = index
  end

  # constant
  snp_filter = 0.75
  alt_ratio = 0.1


  gcid = idx[:snp_gc]
  hid = idx[:snp_het] 
  depid= idx[:vc_dep]


  combine_file =params['com_out']
  analyse_file =params['out']
  f_list = File.open(combine_file,'r')
  out  = File.open(analyse_file,'w')

  all_list = []
  list = []

  het_list = []
  hom_list = []


  # filter all by gc score snp array 
  while str = f_list.gets
    # puts str
    v = str.strip.split(',')
    all_list<<v
    if v[gcid].to_f>snp_filter     
      list<<v
    end
  end

  # filter set of snp array 's het hom
  for i in list
    if i[hid]=='het'  
      het_list<<i
    else
      hom_list<<i
    end
  end



  out.puts "SNP Array overlap data "
  out.puts "Filter GC score at,#{snp_filter}"
  out.puts "Total snv,#{all_list.size}"
  out.puts "Total snv not pass filter,#{all_list.size-list.size}"
  out.puts "Total hetero snv,#{het_list.size}"
  out.puts "Total homo snv,#{hom_list.size}"



  vc_het_het = []
  vc_het_hom = []
  vc_hom_hom = []
  vc_hom_het = []

  vchid = getidx :vc_het
  vcrid = getidx :vc_r
  [vchid]=='het' or
  for i in list

    if i[hid]=='het'
      if  i[vcrid].to_f >= alt_ratio
        vc_het_het<<i
      else
        vc_het_hom<<i
      end  
    else
      if  i[vcrid].to_f >= alt_ratio
        vc_hom_het<<i
      else
        vc_hom_hom<<i
      end  
    end
  end


  # matrix=[[vc_het_het.size,vc_het_hom.size],[vc_hom_hom.size,vc_hom_het.size]]



  require 'erb'
  outtmp = File.open(analyse_file+".html",'w')

  template = ERB.new(File.open('index.html.erb','r').read)


  outtmp.puts template.result(binding)

  outtmp.close

  `open #{analyse_file+".html"}`



  out.close
  f_list.close
end

.call_combine_all(params) ⇒ Object



5
6
7
8
9
10
11
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
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
161
162
163
164
165
166
# File 'lib/vardetect-vc/call_combine.rb', line 5

def self.call_combine_all params
  # ====================================================================================================
  # 4. All comparison
  # ====================================================================================================
  
   map_vc = []
   map_st = {}
   map_snp = {}
   

   vc_file =params['vc_out']
   st_file =params['st_out']
   snp_file =params['snp_out']
   
   com_out =params['com_out']
   
   f_st = File.open(st_file,'r')
   f_vc = File.open(vc_file,'r')
   f_snp = File.open(snp_file,'r')
   
   com_output = File.open(com_out,'w')
   
   puts "Indexing SNP Array SNV"
   while str = f_snp.gets
     v = str.strip.split(',')
     map_snp[v[0]] = v
   end
   
   puts map_snp.keys[0..10]
   
   puts "Indexing SamTools SNV"
      while str = f_st.gets
        v = str.strip.split(',')
        map_st[v[0]] = v
      end
   
    puts map_st.keys[0..10]
      
      puts "Indexing VC SNV"
         while str = f_vc.gets
           v = str.split(',')
      
         
      
           # if (v[2]==v[3] and alt.to_f/dep > 0.1) or v[2]!=v[3]   
           #              map_vc<<v
           #            end
           #            
           # if (v[2]==v[3] and alt.to_f/dep > 0.1) or v[2]!=v[3]   
              map_vc<<v
            # end
           
           
         end
         
         puts map_vc.size
         
         # puts map_vc[0..10]
      
        
        
        
        
        # map_vc.sort!{|a,b| a[1].to_i<=>b[1].to_i }
            
            all=0
            vc_count=0  
            st_count=0
            vc_het_rate=0
            st_het_rate=0
            final = []
            
            
            for i in  map_vc
               pos = i[0]
               # puts pos
               if snp = map_snp[pos]
                 
            
                 vc_alt = i[5].to_i
                 vc_dep = i[4].to_i+vc_alt
                 
                 vc_alt_a = '-'
                 vc_het = 0
                 
                     
                 snp_rs = snp[1]
                 snp_a1 = snp[2]
                 snp_a2 = snp[3]
                 
                 snp_a = "#{snp_a1}/#{snp_a2}"
                 snp_het = 0 
                 snp_het = 1 if snp_a1!=snp_a2 
                 snp_gc = snp[4]
                 snp_clust = snp[5]
                 snp_cmp = 1
                 
                 snp_check = false
                 if snp_a.strip !='-/-' 
                 all+=1 
                 snp_check = true
                 end
                 st_check = false
                 
                 vc_het_t = 'hom'
                 snp_het_t = 'hom'
                 snp_het_t = 'het' if snp_a1!=snp_a2 
                 
                 vc_het_ratio = vc_alt.to_f/vc_dep
                 if vc_het_ratio >= 0.15
                   vc_alt_a = i[3] 
                   vc_het = 1
                   vc_het_t = 'het'
                 end
                      
                 str = "#{i[0]},#{i[1]},#{i[2]},#{i[2]}/#{vc_alt_a},#{vc_het_t},#{vc_dep},#{vc_alt},#{vc_het_ratio},"
                 str += "#{snp_rs},#{snp_a},#{snp_het_t},#{snp_gc},#{snp_clust},"
                 
                 
                 
                 
                 if st = map_st[pos]
                   st_het = 0 
                   st_het = 1 if st[5] =='het'
                   str += st[4..-1].join(",")
                    if st_het==snp_het and snp_check
                    st_het_rate+=1
                    st_check =true
                    end
                    st_count+=1
                 end
                 vc_check = false
                 vc_check = true if vc_het==snp_het and snp_check
               
                 if snp_check and vc_check
                   vc_het_rate+=1 
                 end 
                 vc_count+=1
                
                 cmp = [snp_check,vc_check,st_check,str]
                 
                 # final << cmp
                 com_output.puts cmp.join(',')
                 
                 
               end
            end
            
        
        puts "All #{all} is not -/-"
        
        puts "VC Hetero Rate : #{vc_het_rate}"
        
        puts "ST Hetero Rate : #{st_het_rate}"
   
   f_st.close
   f_vc.close
   f_snp.close
   com_output.close
   
  
end

.call_prepare_snp(params) ⇒ Object



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
# File 'lib/vardetect-vc/call_prepare.rb', line 35

def self.call_prepare_snp params
  # ====================================================================================================
  # 3. SNP preparing filtered chromosome
  # ====================================================================================================

  snp_array_file =params['snp']
  snp_file =params['snp_out']

  count = 0
  list = []
  start = false
  run = false

  file = File.open(snp_array_file,'r')
  output = File.open snp_file,'w'
  map = {}
  while str = file.gets #and count<100
    str.strip!
    if str=='[Data]' and !start
      start = true
    elsif start
      s = str.split
      if s[0] =='Sample' and !run
        run = true
      else

          key = "#{s[1]}-#{s[2]}"
           map[key] = s

      end

    end
    count+=1
  end
    # 
    map.keys.sort.each do |k|
        v = map[k]
        output.puts "#{k},"+v[3..-1].join(",")
    end

  output.close   



end

.call_prepare_st(params) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/vardetect-vc/call_prepare.rb', line 6

def self.call_prepare_st params
  # ====================================================================================================
  # 2. ST preparing filtered chromosome
  # ====================================================================================================
  count = 0
  start = false
  sam_tool_file =params['st']
  snp_file =params['st_out']


  st_output = File.open(snp_file,'w')
  CSV.foreach(sam_tool_file) do |row|
   if start 

      i = row[0][3..-1]
      id = i.to_i
      id = i if id == 0
      key = "#{id}-#{row[1]}"
      st_output.puts key+","+row.join(",")

     count +=1
   else
     start=true
   end
  end
  st_output.close

end

.call_prepare_vc(params) ⇒ Object



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
# File 'lib/vardetect-vc/call_prepare.rb', line 81

def self.call_prepare_vc params

  vc_raw_file =params['vc_raw']
  vc_file =params['vc_out']

  count = 0
  list = []

  file = File.open(vc_raw_file,'r')
  output = File.open vc_file,'w'
  map = {}
  while str = file.gets #and count<100
    str.strip!
    s = str.split
    # puts str
    begin
    if str

        alt = s[6].to_i  
        dep = s[5].to_i + alt
        if ( alt.to_f/dep > 0.1) or s[2]!=s[3] 
          i = s[0][3..-1]
          key = "#{i}-#{s[1]}"
            map[key] = s
        end
      end
    rescue
      puts str
    end
  end
    # 
    map.keys.sort.each do |k|
        v = map[k]
        output.puts "#{k},"+v[2..-1].join(",")
    end
  output.close 
end

.call_sam_snv(sam, chr, chr_start, chr_stop, debug = nil) ⇒ Object



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
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
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
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
# File 'lib/vardetect-vc/call_snv.rb', line 42

def self.call_sam_snv sam,chr,chr_start,chr_stop, debug=nil
  
  unless debug
    show = false
    indel_show = false
    var_show = false
  else
      s = debug.split('|')
      show = s.index('show')
      indel_show =  s.index('indel')
      var_show =  s.index('var')
  end
 
  
  l = nil
  snps = []
  indels = []
  indels_map = {}

  puts "#{chr} #{chr_start} - #{chr_stop}"
  seq = sam.fetch_reference(chr,chr_start,chr_stop+1000)   
  sam.load_index
  
  upseq = seq.upcase
      
  
  
  als = []

  
  als = sam.fetch(chr, chr_start, chr_stop)  
  
  profile = []
  list = []
  start = nil
  alleles = {'A'=>0,'C'=>1,'G'=>2,'T'=>3}
  inv_alleles = {0=>'A',1=>'C',2=>'G',3=>'T'}
  
         
  als.each{|i|
    
    # puts "#{i.pos}"
           
               unless start
               start = i.pos  
               end
             
               if i.pos!=start
               
                 while start<i.pos 
                   del = []
                 
                   s = ''
                   ref = seq[start-chr_start]
                   print "#{start}\t#{ref}\t#{list.size}\t" if show
                   ref = ref.upcase
                 
                 
                  p = [0,0,0,0]
                  q = [0,0,0,0]
                  
                   list.each_with_index do|l,index|
                
                   
                     c = l.seq[start-l.pos]
                     cq = l.qual[start-l.pos]
                           
                    # a = seq[start-1..start-1-5]
                    # b =seq[start-l.pos..start-l.pos-5]
                    # puts "#{a} - #{b}"
                  
                     if c 
                      
                       case c 
                       when 'A'
                         p[0]+=1
                         q[0]+=cq-33 if cq 
                       when 'C'
                         p[1]+=1
                         q[1]+=cq-33 if cq 
                       when 'G'
                         p[2]+=1
                         q[2]+=cq-33 if cq 
                       when 'T'
                         p[3]+=1
                         q[3]+=cq-33 if cq 
                       end  
                      
                       s+=c                
                       
                       print "#{c}" if show
                   
                     else
                       del<<l
                     end
                   
                   
                   end
                 
                 
                  
                   pmax = p.index(p.max)
                   max = p[pmax]
                   p[pmax] = 0 if pmax
                   palt = p.index(p.max)
                   alt = p[palt]
                   dep = max+alt
                    max_q = q[pmax].to_f/max
                    alt_q = '0'
                    alt_q = q[palt].to_f/alt if alt > 0
                   
                     
                   if dep > 2 and ((indel = indels_map[start] and indel[:count]>1 )  or  (alt>0 and alt/dep.to_f > 0.1 and alt_q >10) or alleles[ref]!=pmax )
                     
                     
                         
                     text = "#{start}\t#{max}/#{alt.to_i}\t#{ref}\t#{s}"  
                     puts text  if var_show
                     sum = (max+alt).to_f
                     aalt = '-' 
                     
                     aalt = inv_alleles[palt] if alt!=0
                     if (alt>0 and alt/dep.to_f > 0.1)
                       c = 'a'
                     else
                       c = 'A'
                     end
                     
                     
                     if indel 
                      s = indel[:alleles].split('/')
                      ref = s[0]
                      aalt = s[1]
                      alt = indel[:count]
                      if indel[:type]=='ins'
                        c='+'
                      else
                        c='-'
                      end
                     end
                     snps << [chr,start,ref,inv_alleles[pmax],aalt,max,alt,format('%.2f',max_q),format('%.2f',alt_q),c]
                     
                     # snps << [chr,start,ref,inv_alleles[pmax],inv_alleles[palt],max,alt,max/sum,alt/sum]
                       
                   end
                   list-=del
                   if list.size==0
                   start = i.pos
                   else
                   start+=1
                   end

                 
                   puts if show
                 end
               
                 start = i.pos
               end
            
         
               
               seq_size = i.seq.size
               
               tseq = form(i.seq,i.cigar)
               tqual = form(i.qual,i.cigar)
               
               
               if (i.cigar.index('I') or i.cigar.index('D') ) 

                  
                  if indel_show  
                  puts "-------------------------INDEL " 
                  puts i.pos
                  puts i.cigar
                  puts i.seq
                  puts i.qual
                  # puts i.inspect

                  # :qname, :flag, :rname,:pos,:mapq,:cigar, :mrnm, :mpos, :isize, :seq, :qual, :tags, :al, :samstr
                  
                  puts seq[i.pos-chr_start..i.pos-chr_start+i.seq.size]
                  puts tseq[0]
                  puts tqual[0]
                  puts tseq[1]
                  end
                  
                            if tseq[1].size>0
                              for y in tseq[1]
                                if y[:type]=='ins'
                                  iref = '-'
                                  iseq = i.seq[y[:pos]..y[:pos]+y[:size]-1]
                                  y[:alleles] =  "#{iref}/#{iseq}"
                                else
                                  iref = seq[i.pos-chr_start+y[:pos]..i.pos-chr_start+y[:pos]+y[:size]-1]
                                  iseq = '-'
                                  y[:alleles] =  "#{iref}/#{iseq}"
                                end
                                y[:pos]+=i.pos
                                indels<<y
                                unless indels_map[y[:pos]]
                                    y[:count]=1
                                    y[:sm]=y[:alleles]
                                    indels_map[y[:pos]] = y
                                else
                                    indels_map[y[:pos]][:sm]+=",#{y[:alleles]}"
                                  
                                    indels_map[y[:pos]][:count] +=1
                                end

                              end
                            end
                  
                  
                end  
                  
                  
        
                    
              
              i.seq = tseq[0]
              i.qual = tqual[0].bytes.to_a
              
              
               if i.seq.size > seq_size/2
              
                   si = 0 
               
                   i.seq.size.times do |k|
                     # puts "#{i.seq[k]} #{seq[start-chr_start+k]}"
                     si+=1 if i.seq[k]==upseq[start-chr_start+k]
                   end
                   # puts "#{si} #{i.seq.size}"
                   if (si.to_f/i.seq.size)>=0.95
                     list << i 
                   else
                     if indel_show
                     puts "Drop with low similarity #{si.to_f/i.seq.size} #{i.cigar}"
                     puts seq[start-chr_start..start-chr_start+100]
                     puts i.seq
                     end
                   end
               end
                                   
               l = i 
         
    
           }
         
          
            if indel_show  
 
                for i in indels_map.keys.sort
                        puts indels_map[i].inspect
                end
           end
           # puts l.cigar
           # puts l.seq
           # puts snps.size
           return snps
  
end

.call_snv(params) ⇒ Object



455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
# File 'lib/vardetect-vc/call_snv.rb', line 455

def self.call_snv params
  # ====================================================================================================
  # 1. VC calling 
  # ====================================================================================================
  core = 4
  core = params['core'].to_i if params['core'] # number of simultaneous processes
  sam_file =params['sam']
  ref_file =params['ref']
  vc_file =params['vc_out']
  p = {:sam=>sam_file,:ref=>ref_file,:output=>vc_file}

  # params[:start] = 95_658_000
  # params[:stop] = 95_659_000
  # call_variance params
  
  process_vc_multicore p,core
  # # params = {:sam=>sam_file,:ref=>ref_file,:output=>vc_all_file}
  # # process_vc_multicore params,core
end

.call_snv_range(params) ⇒ Object



476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
# File 'lib/vardetect-vc/call_snv.rb', line 476

def self.call_snv_range params
  # ====================================================================================================
  # 1. VC calling 
  # ====================================================================================================
  core = 4
  core = params['core'].to_i if params['core'] # number of simultaneous processes
  sam_file =params['sam']
  ref_file =params['ref']
  vc_file =params['vc_out']
  p = {:sam=>sam_file,:ref=>ref_file,:output=>vc_file,:chr=>"chr#{params['chr']}",:start=>params['start'].to_i,:stop=>params['stop'].to_i,:debug=>'indel'}
  puts p.inspect
  
  call_variance p
  
end

.call_variance(p) ⇒ Object



304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
# File 'lib/vardetect-vc/call_snv.rb', line 304

def self.call_variance p

  
  chr = p[:chr] 
  
  output = p[:output]
  testBAMFile = p[:sam]
  testReference = p[:ref]
  sam = Bio::DB::Sam.new({:bam=>testBAMFile, :fasta=>testReference})
  sam.open

  chr_start = p[:start]
  chr_stop  = p[:stop]
  snps = call_sam_snv sam,chr,chr_start,chr_stop, p[:debug]  
  
  sam.close
  f = File.open(output,'a')
  f.puts snps.collect{|j| j.join("\t")}
  f.close
end

.exec(args) ⇒ Object

command params options variance call



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
# File 'lib/vardetect-vc.rb', line 26

def self.exec args
  params = parse_params args.join(' ')
  puts params.inspect
  
  cmd = params[:cmd]

  case cmd
    
  when 'snv'

    call_snv params
  
  when 'snv-batch'  
    
    path = '/Users/soup/Desktop/chula/data'
    path = params['path'] if params['path']
   
    working = params['exp']
    
    for i in params['samples'].split(',').compact
    
    sample_id = i
    
    
    ref_file = File.join(path,'ref','hg19.fa')
    sam_file = File.join(path,working,'bam',"#{sample_id}.remdup.uniqMap.TS.bam")
    vc_raw_file = File.join(path,working,'snv-vc',"#{sample_id}_vc_raw_snv.tsv")
    
    p = "snv -ref #{ref_file} -sam #{sam_file} -vc_out #{vc_raw_file} -core 16"
    
    p = parse_params(p)
    puts p.inspect
      call_snv p
    
    end
    
    
  when 'snv-range'

    call_snv_range params

  when 'prepare-st'

    call_prepare_st params

  when 'prepare-snp'

    call_prepare_snp params

  when 'prepare-vc'

    call_prepare_vc params

  when 'combine-all'

    call_combine_all params

  when 'analyse-combine'

    call_analyse_combine params
    
  end
  
end

.form(seq, cigar) ⇒ Object



6
7
8
9
10
11
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
# File 'lib/vardetect-vc/call_snv.rb', line 6

def self.form seq, cigar
  ix = cigar.split(/S|M|I|D/) 
  index = 0
  id = 0 
  str = ''
  st = 0
  indel = []
  # puts seq
  for i in ix
    index+=i.size
    cmd = cigar[index]
    # puts "#{cmd} #{i.to_i}"
    index+=1
    i = i.to_i
    case cmd
    when 'S'
      st = i 
      id += i 
    when 'I'
      indel<<{:pos=>id-st,:type=>'ins',:size=>i,:cigar=>cigar}
      id+=i
    when 'D'
      str+='N'*i
      indel<<{:pos=>id-st,:type=>'del',:size=>i,:cigar=>cigar}
    when 'M'
      str += seq[id..id+i-1] if seq[id..id+i-1]
      id+=i
    end
    # puts "Ref:#{seq}"
    # puts "Seq:#{str}"
    # puts "ID #{id}"
    # puts
  end
  return str,indel
end

.getidx(id) ⇒ Object



279
280
281
282
283
284
285
286
287
# File 'lib/vardetect-vc/call_combine.rb', line 279

def self.getidx id
  
  idx = {}
  %w{snp vc st pos ref vc_a1 vc_a vc_het vc_dep vc_alt vc_r snp_ann snp_a snp_het snp_gc snp_clus st_ref st_a2 st_qua st_dep st_alt st_pos}.each_with_index do |i,index|
    idx[i.to_sym] = index
  end
  return idx[id]
  
end

.hiObject



20
21
22
# File 'lib/vardetect-vc.rb', line 20

def self.hi
  puts 'hello inside '
end

.inspect_reference(testReference) ⇒ Object



327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
# File 'lib/vardetect-vc/call_snv.rb', line 327

def self.inspect_reference testReference
  
  # index reference 

  file = File.open(testReference,'r')
  map = {}
  size=0
  chr = nil
  while str=file.gets
    if str[0..0]=='>'
      if chr  
        puts "#{chr}\t#{size}"
        map[chr]=size  
      end 
      chr = str.strip.split[0][1..-1]
      size=0
    else
      size+=str.strip.size
    end
  end
  map[chr]=size
  puts map.inspect
  file.close
  genome = map
  # genome = {"chr1"=>249250621, "chr10"=>135534747, "chr11"=>135006516, "chr12"=>133851895, "chr13"=>115169878, "chr14"=>107349540, "chr15"=>102531392, "chr16"=>90354753, "chr17"=>81195210, "chr18"=>78077248, "chr19"=>59128983, "chr2"=>243199373, "chr20"=>63025520, "chr21"=>48129895, "chr22"=>51304566, "chr3"=>198022430, "chr4"=>191154276, "chr5"=>180915260, "chr6"=>171115067, "chr7"=>159138663, "chr8"=>146364022, "chr9"=>141213431, "chrX"=>155270560, "chrY"=>59373566}

  return genome
end

.inspect_reference_human(testReference) ⇒ Object



356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
# File 'lib/vardetect-vc/call_snv.rb', line 356

def self.inspect_reference_human testReference
  
  # index reference 
  # 
  # file = File.open(testReference,'r')
  # map = {}
  # size=0
  # chr = nil
  # while str=file.gets
  #   if str[0..0]=='>'
  #     if chr  
  #       puts "#{chr}\t#{size}"
  #       map[chr]=size  
  #     end 
  #     chr = str[1..-1].strip
  #     size=0
  #   else
  #     size+=str.strip.size
  #   end
  # end
  # map[chr]=size
  # puts map.inspect
  # file.close

  genome = {"chr1"=>249250621, "chr10"=>135534747, "chr11"=>135006516, "chr12"=>133851895, "chr13"=>115169878, "chr14"=>107349540, "chr15"=>102531392, "chr16"=>90354753, "chr17"=>81195210, "chr18"=>78077248, "chr19"=>59128983, "chr2"=>243199373, "chr20"=>63025520, "chr21"=>48129895, "chr22"=>51304566, "chr3"=>198022430, "chr4"=>191154276, "chr5"=>180915260, "chr6"=>171115067, "chr7"=>159138663, "chr8"=>146364022, "chr9"=>141213431, "chrX"=>155270560, "chrY"=>59373566}

  return genome
end

.parse_params(params) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/vardetect-vc/lib.rb', line 9

def self.parse_params params

  puts params 
  params = params.split
  cmd = params[0]
  tmp = {}
  ((params.size-1)/2).times do |i|
    key = params[1+i*2][1..-1]
    value = params[1+i*2+1]
    tmp[key]=value
  end
  tmp[:cmd]=cmd
  params = tmp 
end

.process_vc_multicore(params, core = 1, chr_filter = nil) ⇒ Object



386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
# File 'lib/vardetect-vc/call_snv.rb', line 386

def self.process_vc_multicore  params, core=1,chr_filter=nil
  
  sim = core
   
  path = '/Users/soup/Desktop/vcqa'
  output = params[:output]
  
  testReference = params[:ref]

  genome = inspect_reference_human(testReference)

  if chr_filter # 1 as text
    
      tmp = {}
      chr = "chr#{chr_filter}"
      tmp[chr]=genome[chr]
      genome = tmp
  end

  puts genome.inspect


  # reset output
  f = File.open(output,'w')
  f.close

  a = []

  # job generator
  genome.each_pair do |chr,size|
    puts "#{chr}\t#{size}"
    m = 1000000
    n = size/m

    n.times do |i|
      params[:chr] = chr
      params[:start] = 1 + i * m
      params[:stop] = (i+1) * m
      a<<params.clone 
    end

    if size%m !=0
      params[:chr] = chr
      params[:start] = size - size% m
      params[:stop] = size
      a<<params.clone 
    end

  end

  sim = a.size if sim>a.size

  # starting first N processes
  sim.times do
    i = a.pop
    Process.fork{call_variance(i)}
  end
  # start one by one as the previous finish
  a.each do |i|
    Process.wait(0)
    Process.fork{call_variance(i)}
  end
  # wait for all to finish
  Process.waitall
  
end

.sampling(list) ⇒ Object



290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
# File 'lib/vardetect-vc/call_combine.rb', line 290

def self.sampling list
  
  gcid = getidx :snp_gc
  depid = getidx :vc_dep
  vcrid = getidx :vc_r

  
  # =====================================================================
  # sampling gc score value from all data set
     gc_bin = []
     gc_range = 100
     gc_max = 1
     for i in list
       j = i[gcid].to_f
       k = (j * gc_range / gc_max).to_i 
       gc_bin[k]=0 unless gc_bin[k]
       gc_bin[k]+=1 
     end
     
   # =====================================================================
   # sampling dep score value from all data set
      d_bin = []
      d_range = 100
      d_max = 500
    
      for i in list
        j = i[depid].to_i
        k = (j * d_range / d_max).to_i 
        d_bin[k]=0 unless d_bin[k]
        d_bin[k]+=1 
      end

      # =====================================================================
      # sampling gc score value from all data set
        
      alt_bin = []
      alt_range = 100
      alt_max = 0.5
      dep_range = 100
      dep_max = 500
      
    
      for i in list
        
        j = i[vcrid].to_f
        d = i[depid].to_f
  
        k = (j * alt_range / alt_max).to_i 
        alt_bin[k]=0 unless alt_bin[k]
        alt_bin[k]+=1 
  
      end
      
      
      dot = []
      
      for i in list
        
        j = i[vcrid].to_f
        d = i[depid].to_f
        k = (j * alt_range / alt_max).to_i 
        k2 = (d * dep_range / dep_max).to_i 
        
        dot << "[#{d},#{j}]"
        
        
      end
      
      
      
      
      return {:gc=>gc_bin,:dep=>d_bin,:alt=>alt_bin,:dot=>dot}
     
end