process_hifibr.R 8.25 KB
Newer Older
Rebecca E Batorsky's avatar
Rebecca E Batorsky committed
1
2
3
4
5
6
#!/usr/bin/env Rscript
args = commandArgs(trailingOnly=TRUE)

suppressPackageStartupMessages(library(tidyverse))
suppressPackageStartupMessages(library(Biostrings))

7
8
9
10
# test if there is at least 5 argument: if not, return an error
if (length(args)<5) {
  stop("Usage: Rscript process_hifibr.R input_file.csv outdir search_radius debug", call.=FALSE)
} else if (length(args)==5) {
Rebecca E Batorsky's avatar
Rebecca E Batorsky committed
11
  input_file = args[1]
Rebecca E Batorsky's avatar
Rebecca E Batorsky committed
12
  out_dir = args[2]
13
14
15
  search_radius=as.integer(args[3])
  break_location=as.integer(args[4])
  debug=as.integer(args[5])
Rebecca E Batorsky's avatar
Rebecca E Batorsky committed
16
17
}

18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
print(paste0("Input file: " ,input_file))
print(paste0("Output dir: ", out_dir))
print(paste0("Search radius: ", search_radius))
print(paste0("Break location: ", break_location))
print(paste0("Debug: ", debug))

## functions

## 
# input_file="~/Documents/git/sdmmej/test_data_2/PolyA1Seq_questions.csv"
# out_dir="~/Documents/git/sdmmej/test_data_2/"
# search_radius=30
# break_location=161
# debug=1

Rebecca E Batorsky's avatar
Rebecca E Batorsky committed
33
#input_file="~/Documents/git/sdmmej/test_data/TestData_HiFiBR_Output_mod.csv"
34
35


Rebecca E Batorsky's avatar
Rebecca E Batorsky committed
36
37
38
39
40
hifibr_input = read.csv(input_file)

## add an id col and col for reclassified CLASS
hifibr_input <- tibble::rowid_to_column(hifibr_input, "ID")
hifibr_input$CLASS_final = ifelse(hifibr_input$CLASS %in% c('deletion','insertion','exact') , hifibr_input$CLASS, NA)
41
hifibr_input$CLASS_final = ifelse(hifibr_input$READS < 10 & hifibr_input$CLASS != 'exact', 'filtered',hifibr_input$CLASS)
Rebecca E Batorsky's avatar
Rebecca E Batorsky committed
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57

## names for output
in_string  = basename(input_file)
out_string = gsub(".csv","",in_string)

## check to make sure only one ref, otherwise exit
ref = hifibr_input %>% dplyr::filter(., CLASS == 'exact')
ref_seq = ref$ALIGNED_SEQ

n_ref = nrow(ref)
if ( n_ref != 1){
  print(paste0("ERROR: Found ", n_ref, " reference sequences in Hifibr output."))
  quit(save = "no", status = 1, runLast = FALSE)
}

# separate sequences and filter for length
58
59
60
hifibr_input_filter_del = hifibr_input %>% dplyr::filter(.,READS > 10 & CLASS == 'deletion')
hifibr_input_filter_ins = hifibr_input %>% dplyr::filter(.,READS > 10 & CLASS == 'insertion')
hifibr_input_filter_complex = hifibr_input %>% dplyr::filter(.,READS > 10 & CLASS == 'complex')
Rebecca E Batorsky's avatar
Rebecca E Batorsky committed
61
62
63
64
65
66
67

# process deletions
deletions = hifibr_input_filter_del$ALIGNED_SEQ

aligned_deletions = c()

for (seq in deletions){
68
  sigma <- nucleotideSubstitutionMatrix(match = 2, mismatch = -1, baseOnly = FALSE)
Rebecca E Batorsky's avatar
Rebecca E Batorsky committed
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
  align <- pairwiseAlignment(seq, ref_seq, substitutionMatrix = sigma, gapOpening = -2,
                                       gapExtension = -8, scoreOnly = FALSE)
  align_string <- toString(align@pattern)
  aligned_deletions = c(aligned_deletions,align_string)
}

#print("we have these aligned deletions")
#print(aligned_deletions)

# process insertions
insertions = hifibr_input_filter_ins$ALIGNED_SEQ

#print("we have these insertions")
#print(insertions)

# process complex
comp_unknown = c()

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
# ### debug
# seq = hifibr_input_filter_complex[2, 'ALIGNED_SEQ']
# id = hifibr_input_filter_complex[2,'ID']
# print(seq)
# print(id)
# sigma <- nucleotideSubstitutionMatrix(match = 2, mismatch = -1, baseOnly = FALSE)
# align <- pairwiseAlignment(seq, ref_seq, substitutionMatrix = sigma, gapOpening = -2,
#                            gapExtension = -8, scoreOnly = FALSE)
# 
# ins_info = insertion(align)[[1]]
# del_info = deletion(align)[[1]]
# mis_info = mismatchTable(align)
# 
# ins_n = length(ins_info)
# ins_pos = start(ins_info)
# ins_r = abs(break_location - ins_pos)
# 
# del_n = length(del_info)
# del_pos = start(del_info)
# del_r = abs(break_location - del_pos)
# 
# mis_n = nmismatch(align)
# mis_pos = mis_info$SubjectStart
# mis_r = abs(break_location - mis_pos)
# 
# 
# print(c(ins_n,ins_pos, ins_r))
# print(c(del_n,del_pos, del_r))
# print(c(mis_n,mis_pos, mis_r))
# 
# ## it seems they are always IRanges objects of length 1
# label = ""
# 
# ## check number of deletions
# deletion=deletion(align)[[1]]
# n_del=length(deletion)
# print(n_del)
# # check
# if (n_del >1){
#   label = "complex"  
# }
# 
# # check position of deletions
# pos_del=start(deletion)[1]
# r_del=abs(break_location-pos_del)
# print(r_del)
# if (r_del > search_radius){
#   label = "complex"  
# }
# 
# # check number of insertions
# insertion=insertion(align)[[1]]
# n_ins=length(insertion)
# print(n_ins)
# if (n_ins >1){
#   label="complex"  
# }
# 
# # check position of insertions
# pos_ins=start(insertion)[1]
# print(pos_ins)
# 
# r_ins=abs(break_location-pos_ins)
# print(r_ins)
# # check
# if (r_ins > search_radius){
#   label="complex" 
# }
# 
# # check position of mismatch, assuming it is only one
# pos_mis=mismatchTable(align)$SubjectStart
# r_mis=abs(break_location-pos_mis)
# 
# if (r_mis > search_radius){
#   label="complex" 
# }
# 
# 
# print(label)

### 
Rebecca E Batorsky's avatar
Rebecca E Batorsky committed
168
169
170
for (row in 1:nrow(hifibr_input_filter_complex)){
  seq = hifibr_input_filter_complex[row, 'ALIGNED_SEQ']
  id = hifibr_input_filter_complex[row,'ID']
171
172
173
174
  if(debug == 1){
    print(id)
  }
  sigma <- nucleotideSubstitutionMatrix(match = 2, mismatch = -1, baseOnly = FALSE)
Rebecca E Batorsky's avatar
Rebecca E Batorsky committed
175
176
177
  align <- pairwiseAlignment(seq, ref_seq, substitutionMatrix = sigma, gapOpening = -2,
                             gapExtension = -8, scoreOnly = FALSE)
  
178
179
180
181
182
  if(debug == 1){
    print(insertion(align))
    print(deletion(align))
    print(mismatchTable(align))
  }
Rebecca E Batorsky's avatar
Rebecca E Batorsky committed
183
  
184
185
186
187
188
189
190
  ins_info = insertion(align)[[1]]
  del_info = deletion(align)[[1]]
  mis_info = mismatchTable(align)
  
  ins_n = length(ins_info)
  ins_pos = start(ins_info)
  ins_r = abs(break_location - ins_pos)
Rebecca E Batorsky's avatar
Rebecca E Batorsky committed
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
  del_n = length(del_info)
  del_pos = start(del_info)
  del_r = abs(break_location - del_pos)
  
  mis_n = nmismatch(align)
  mis_pos = mis_info$SubjectStart
  mis_r = abs(break_location - mis_pos)
  
  # check if it's a single deletion within the search radius
  if (ins_n == 0 & mis_n == 0 & del_n ==1 ){
    if (del_r < search_radius){
      align_string <- toString(align@pattern)
      aligned_deletions = c(aligned_deletions, align_string)
      hifibr_input[hifibr_input$ID == id,]$CLASS_final = 'deletion'
      
      if(debug == 1){
        print("found del")
        print("ref")
        print(toString(align@subject))
        print("seq")
        print(toString(align@pattern))
      }
    }
  }else if (mis_n == 0 & del_n ==0 & ins_n == 1 ){
    if (ins_r < search_radius){
      insertions = c(insertions, seq)
      hifibr_input[hifibr_input$ID == id,]$CLASS_final = 'insertion'
      
      if(debug == 1){
        print("found ins")
        print("ref")
        print(toString(align@subject))
        print("seq")
        print(toString(align@pattern))
      }
    }else{
      if(debug == 1){
        print("found complex (del outside search radius)")
        print("ref")
        print(toString(align@subject))
        print("seq")
        print(toString(align@pattern))
      }
      comp_unknown = c(comp_unknown, seq)
      hifibr_input[hifibr_input$ID == id,]$CLASS_final = 'complex'
    }
  }else if (del_n ==0 & ins_n == 0 & mis_n == 1){
    if (mis_r < search_radius){
      insertions = c(insertions, seq)
      hifibr_input[hifibr_input$ID == id,]$CLASS_final = 'insertion'
      
      if(debug == 1){
        print("found single mismatch ins")
        print("ref")
        print(toString(align@subject))
        print("seq")
        print(toString(align@pattern))
      }
    }else{
      if(debug == 1){
        print("found complex (ins outside search radius)")
        print("ref")
        print(toString(align@subject))
        print("seq")
        print(toString(align@pattern))
      }
      comp_unknown = c(comp_unknown, seq)
      hifibr_input[hifibr_input$ID == id,]$CLASS_final = 'complex'
    }
Rebecca E Batorsky's avatar
Rebecca E Batorsky committed
261
  }else{
262
263
264
265
266
267
268
    if(debug == 1){
      print("found complex")
      print("ref")
      print(toString(align@subject))
      print("seq")
      print(toString(align@pattern))
    }
Rebecca E Batorsky's avatar
Rebecca E Batorsky committed
269
270
271
272
273
274
    comp_unknown = c(comp_unknown, seq)
    hifibr_input[hifibr_input$ID == id,]$CLASS_final = 'complex'
  }
}

## write out
Rebecca E Batorsky's avatar
Rebecca E Batorsky committed
275
276
dir.create(out_dir,showWarnings = FALSE)

Rebecca E Batorsky's avatar
Rebecca E Batorsky committed
277
## deletions
Rebecca E Batorsky's avatar
Rebecca E Batorsky committed
278
write(aligned_deletions, file = paste0(out_dir,"/", out_string,"_deletion.txt"),append = FALSE, sep = "\n")
Rebecca E Batorsky's avatar
Rebecca E Batorsky committed
279
280

## insertions
Rebecca E Batorsky's avatar
Rebecca E Batorsky committed
281
282
insertions = c("RECONSTRUCTED_SEQ",insertions)
write(insertions, file = paste0(out_dir,"/",out_string,"_insertion.txt"),append = FALSE, sep = "\n")
Rebecca E Batorsky's avatar
Rebecca E Batorsky committed
283
284

## unknown complex
Rebecca E Batorsky's avatar
Rebecca E Batorsky committed
285
write(comp_unknown, file = paste0(out_dir,"/", out_string,"_complex.txt"),append = FALSE, sep = "\n")
Rebecca E Batorsky's avatar
Rebecca E Batorsky committed
286
287

## output file
Rebecca E Batorsky's avatar
Rebecca E Batorsky committed
288
write.csv(hifibr_input, file = paste0(out_dir,"/", out_string,"_reclassified.csv"), row.names = FALSE)
289