-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLab 2.Rmd
421 lines (235 loc) · 10.4 KB
/
Lab 2.Rmd
1
2
3
4
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
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
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
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
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
---
title: "HW2"
author: "Group 8"
date: "28 May 2024"
output:
html_document:
code_folding: show
editor_options:
markdown:
wrap: sentence
---
<br>
Group :
Lisa Bensousan - 346462534 - [email protected] <br>
Dan Levy - 346453202 - [email protected] <br>
Emmanuelle Fareau - 342687233 - [email protected] <br>
<br>
### Libraries used :
<br>
```{r setup-packages}
if (!require('data.table')){
install.packages('data.table')
library('data.table')
}
library('tictoc')
if (!require('manipulate')){
install.packages('manipulate')
library('manipulate')
}
if (!require("fitdistrplus")) install.packages("fitdistrplus")
library(data.table)
library(ggplot2)
library(fitdistrplus)
```
<br>
### Paths and Data :
<br>
We are loading the data in our R file.
<br>
```{r paths}
reads_file <- "/Users/lisabensoussan/Desktop/Lab2/TCGA-13-0723-01A_lib1_all_chr1.forward"
```
<br>
## Introduction :
<br>
In this exercise we are interested in the frequency of appearance of the number of reads in the positions on chromosome 1 . We try to see if there is something that repeats according to their distribution. The goal will also be to agree to this distribution the normal law that is most approaching.
<br>
## Part 1:
<br>
We use `fread` to read data. Then, we use `head` to get an overview of our data and see if it downloaded correctly.
<br>
```{r , include=TRUE}
chr1_reads = fread(reads_file)
head(chr1_reads)
```
<br>
We rename columns of our data with `colnames` to make it easier to use and understand.
<br>
```{r , include=TRUE}
colnames(chr1_reads) = c("Chrom","Loc","FragLen")
head(chr1_reads)
```
<br>
<br>
```{r , include=TRUE}
chr1_reads[sample(nrow(chr1_reads),10)] #Just consider a simple of size 10
```
<br>
Our goal in this part is to create a function that takes two parameters: a start point and an end point. It calculates for each base between the two points the number of fragments beginning with this point.
<br>
For this, we create function to count the start of fragments. In this function, we filter the data for the specific chromosome and range, we create a vector for counts initialized to zero, then we tabulate starts within the range with function `table`. After this, we place counter in the corresponding positions.
<br>
```{r , include=TRUE}
count_fragment_starts <- function(data, chrom_number, start_range, end_range) {
relevant_data <- data[Chrom == chrom_number & Loc >= start_range & Loc <= end_range,]
start_counts <- integer(end_range - start_range + 1)
names(start_counts) <- as.character(start_range:end_range)
starts <- table(relevant_data$Loc)
start_positions <- as.character(names(starts))
start_counts[start_positions] <- as.integer(starts)
return(start_counts)
}
```
<br>
Thanks to this function, we do the sum of fragments and printing the results.
<br>
```{r , include=TRUE}
tic()
fragment_counts <- count_fragment_starts(chr1_reads, 1, 0, 20000000)
toc()
total_fragments <- sum(fragment_counts)
total_fragments
# Example of using the function on a smaller range for demonstration
small_range_counts <- count_fragment_starts(chr1_reads, 1, 0, 1000)
print(small_range_counts)
# Measure the execution time for a smaller area
tic()
small_range_counts <- count_fragment_starts(chr1_reads, 1, 0, 1000)
toc()
```
<br>
We successfully loaded the data and renamed the columns for better clarity. By examining a sample of the data, we ensured its integrity and correctness. We then developed a function, count_fragment_starts, which efficiently counts the number of fragments starting at each base within a specified range. Most of them are 0, but there are some 1 and 3 and very fews 3.
<br>
## Part 2 :
<br>
In this part, our aim is to create an histogram which represents the frequencies of the number of reads linked to bases .
<br>
```{r , include=TRUE, warning=FALSE}
last_read <- max(chr1_reads$Loc)
total_reads <- nrow(chr1_reads)
coverage_estimate <- total_reads / last_read
beg_region <- 1
end_region <- 10000000
N <- end_region - beg_region + 1 # Total number of positions in the region of interest
read_starts <- rep(0, N)
tic()
filtered_reads <- chr1_reads[Loc >= beg_region & Loc <= end_region, .(Loc)]
for (r in filtered_reads$Loc) {
read_starts[r - beg_region + 1] <- read_starts[r - beg_region + 1] + 1
}
toc()
coverage_data <- data.frame(Position = beg_region:end_region, ReadStarts = read_starts)
ggplot(coverage_data, aes(x = ReadStarts)) +
geom_histogram(binwidth = 1, fill = "blue", color = "black") +
labs(title = "Histogram of Read Start Frequencies",
x = "Read Start Frequency",
y = "Count of Positions") +
theme_minimal() +
geom_text(stat='count', aes(label=..count..), vjust=-0.5, check_overlap = TRUE)
```
<br>
We calculated the read start frequencies across the first 10 million bases of chromosome 1. Using these frequencies, we generated a histogram to visualize the distribution. The histogram revealed that the majority of positions have a low frequency of read starts, with a few positions having significantly higher counts. This indicates a non-uniform distribution of reads across the analyzed region.
<br>
# Part 3 :
<br>
In this section, we want to create a function that calculates the sums of fragment lengths per cell. <br>
So to do this we create a variable cell size and we will divide our data according to this variable. Then we calculate the sum of the fragments.
We create a new column `TotalFragLen` which contains the sum of the fragment lengths (FragLen) for each cell.
<br>
```{r , include=TRUE}
summarize_fragments <- function(chr1_reads, cell_size) {
chr1_reads[, Cell := floor(Loc / cell_size)]
result <- chr1_reads[, .(TotalFragLen = sum(FragLen)), by = Cell]
return(result)
}
```
<br>
We test our function on our data and with cell size of 50 000.
<br>
```{r, warning=FALSE}
result <- summarize_fragments(chr1_reads, 50000)
print(result)
```
<br>
So, we obtain a new data frame with the number of fragments for each cell (with cell size equal to 50 000).
<br>
# Question a :
<br>
We want to create a graph describing the distribution on the chromosome with the number of fragments in relation to the location of the cells. <br>
So we use the function that we have create in the previous question and apply `ggplot` function.
<br>
```{r , include=TRUE}
ggplot(result, aes(x = Cell, y = TotalFragLen)) +
geom_bar(stat = "identity", fill = "blue") +
labs(title = "Distribution of fragments on chromosome 1",
x = "Cell",
y = "Sum of fragment lengths") +
theme_minimal()
```
<br>
We obtain a plot with the number of fragments for each cell. We can see that there are some cells with a large number of fragments and others with almost no fragments, but the majority of cells have a number of fragments that are all in the same interval.
<br>
# Question b :
<br>
We would like to present a second graph representing the distribution in a window of 20 million bases (a zoom in on the first), which will allow us to better understand the distribution of the number of fragments.
<br>
We define the window star, window end and the cell size. We calculate the cell number corresponding to the end of the window with $\frac{window \: end}{cell size}$. Then, we filter data for the 20 million base window.
After this, we can create our plot with `ggplot`.
<br>
```{r}
cell_size <- 50000
window_start <- 0
window_end <- 20000000
cell_end <- window_end / cell_size
filtered_result <- result[Cell >= window_start & Cell <= cell_end]
ggplot(filtered_result, aes(x = Cell, y = TotalFragLen)) +
geom_bar(stat = "identity", fill = "green") +
labs(title = "Distribution of fragments on chromosome 1 (Zoomed to 20 million bases)",
x = "Cell",
y = "Sum of fragment lengths") +
theme_minimal()
```
<br>
Thanks to this zoom, we can to see with more precision the plot and to better understand the distribution of the number of fragments.
We can notice a spike in the number of fragments towards cells 330 to 340.
And around cells 60 and 260 we can notice a total absence of peak.
<br>
# Part 4 :
<br>
We want to study the marginal distribution of the number of reads in the cell. For this we create a plot with our data `results` that we had create in a previous question.
<br>
```{r , include=TRUE, warning=FALSE}
ggplot(coverage_data, aes(x = ReadStarts)) +
geom_histogram(aes(y = ..density..), bins = 30, fill = "skyblue", color = "black") +
labs(title = "Marginal distribution of the number of reads per cell",
x = "Number of reads per cell",
y = "Density") +
theme_minimal()
```
<br>
We want to fit a normal distribution to the data. For this we use the `fitdist` function in R.
Then we add the fitted normal distribution curve to the graph created previously.
<br>
```{r , include=TRUE, warning=FALSE, message=FALSE}
fit <- fitdist(coverage_data$ReadStarts, "norm")
ggplot(coverage_data, aes(x = ReadStarts)) +
geom_histogram(aes(y = ..density..), bins = 30, fill = "skyblue", color = "black") +
stat_function(fun = dnorm, args = list(mean = fit$estimate["mean"], sd = fit$estimate["sd"]), color = "magenta3", size = 1) +
labs(title = "Fitting the normal distribution to the number of reads per cell",
x = "Number of reads per cell",
y = "Density") +
theme_minimal()
print(fit)
```
<br>
The density curve follows the shape of the histogram well, this indicates that the normal distribution is not a good fit for the data.
<br>
## Short summary :
<br>
In this exercise, we aimed to analyze the distribution of read fragments on chromosome 1 to identify any recurring patterns. Our analysis was divided into several key parts, each contributing to a comprehensive understanding of the data.
Overall, our analysis highlighted the non-uniform distribution of read fragments along chromosome 1. The variability in fragment counts across cells and the fit of a normal distribution to the number of reads per cell provide insights into the underlying patterns of our genomic data. This study showcases the importance of thorough data exploration and visualization in understanding complex biological datasets.
By following the structured approach outlined in this document, we were able to effectively analyze and interpret the distribution of read fragments, providing a solid foundation for further genomic studies.
<br>
<br>
<br>