Population label key: The code labels used in the analysis below correspond to the following manuscript labels: AUT = AUTO, MAN = NON-AUTO, NEW = NON-AUTO-FIELD.

# Load tidyverse FIRST to avoid rlang version conflict
library(tidyverse)
library(here)
library(scales)
library(ggrepel)

# Then add LDna v.2.15 library path and load
.libPaths(c("/workspace/lib/ldna_v215", .libPaths()))
library(LDna)
library(reshape2)
library(writexl)

6. Combine the chromosomal data

Clean env and memory

# Remove all objects from the environment
rm(list = ls())

# Run the garbage collector to free up memory
gc()
##           used (Mb) gc trigger  (Mb) max used  (Mb)
## Ncells 1176646 62.9    2232931 119.3  2232931 119.3
## Vcells 1972919 15.1    8388608  64.0  3453300  26.4

Read the plotting data for NEW and AUT

# NEW
NEW1 <- readRDS(here("output", "ldna", "pop", "chr1", "NEW_plot2.rds")) |> mutate(Population = "NEW")
NEW2 <- readRDS(here("output", "ldna", "pop", "chr2", "NEW_plot2.rds")) |> mutate(Population = "NEW")
NEW3 <- readRDS(here("output", "ldna", "pop", "chr3", "NEW_plot2.rds")) |> mutate(Population = "NEW")

# AUT
AUT1 <- readRDS(here("output", "ldna", "pop", "chr1", "AUT_plot2.rds")) |> mutate(Population = "AUT")
AUT2 <- readRDS(here("output", "ldna", "pop", "chr2", "AUT_plot2.rds")) |> mutate(Population = "AUT")
AUT3 <- readRDS(here("output", "ldna", "pop", "chr3", "AUT_plot2.rds")) |> mutate(Population = "AUT")

Bind the objects

albo <- rbind(NEW1, NEW2, NEW3, AUT1, AUT2, AUT3)

head(albo)
##   Chromosome   Cluster   r2    Start      End nSegments nSNPs Population
## 1          1 2098_0.81 0.81  1671072 16303506       225  3339        NEW
## 2          1 2098_0.81 0.81 16308219 16458676       225  3339        NEW
## 3          1 2098_0.81 0.81 16560013 16688706       225  3339        NEW
## 4          1 2098_0.81 0.81 16723593 17338376       225  3339        NEW
## 5          1 2098_0.81 0.81 17376443 17614762       225  3339        NEW
## 6          1 2098_0.81 0.81 17763447 19491473       225  3339        NEW

We can create a table

table1 <- albo |>
  mutate(Size = End - Start) |>
  dplyr::select(
    Population, Chromosome, Cluster, r2, nSegments, nSNPs, Start, End, Size
  ) |>
  arrange(Population, Chromosome, Start)
head(table1)
##   Population Chromosome   Cluster   r2 nSegments nSNPs    Start      End
## 1        AUT          1 1927_0.68 0.68        18    43  1619834  2269308
## 2        AUT          1 1877_0.71 0.71        21    46  3766945  4238554
## 3        AUT          1 1946_0.66 0.66        23    30  4981859  5217622
## 4        AUT          1 1877_0.71 0.71        21    46  5443736 10957198
## 5        AUT          1 1980_0.63 0.63        20    31 11125830 11276238
## 6        AUT          1 1559_0.84 0.84        33    42 11638599 12192074
##      Size
## 1  649474
## 2  471609
## 3  235763
## 4 5513462
## 5  150408
## 6  553475

We can focus on LD blocks equal or bigger than 1Mb

table1 <- table1 %>%
  dplyr::filter(Size >= 1000000) %>%
  dplyr::mutate(`Size (Mb)` = round(Size / 1000000, 2)) # %>%
  # dplyr::select(Size)
head(table1)
##   Population Chromosome   Cluster   r2 nSegments nSNPs     Start       End
## 1        AUT          1 1877_0.71 0.71        21    46   5443736  10957198
## 2        AUT          1 1763_0.77 0.77        30    61  87157392  88199259
## 3        AUT          1 1763_0.77 0.77        30    61  88919855  90254445
## 4        AUT          1 1628_0.82 0.82        26    50 119130691 120305920
## 5        AUT          1 1971_0.64 0.64        29    90 138373561 140570398
## 6        AUT          1 1651_0.81 0.81       519   679 151544898 152551833
##      Size Size (Mb)
## 1 5513462      5.51
## 2 1041867      1.04
## 3 1334590      1.33
## 4 1175229      1.18
## 5 2196837      2.20
## 6 1006935      1.01

Create table

knitr::kable(table1)
Population Chromosome Cluster r2 nSegments nSNPs Start End Size Size (Mb)
AUT 1 1877_0.71 0.71 21 46 5443736 10957198 5513462 5.51
AUT 1 1763_0.77 0.77 30 61 87157392 88199259 1041867 1.04
AUT 1 1763_0.77 0.77 30 61 88919855 90254445 1334590 1.33
AUT 1 1628_0.82 0.82 26 50 119130691 120305920 1175229 1.18
AUT 1 1971_0.64 0.64 29 90 138373561 140570398 2196837 2.20
AUT 1 1651_0.81 0.81 519 679 151544898 152551833 1006935 1.01
AUT 1 1673_0.8 0.8 395 486 179031717 180129204 1097487 1.10
AUT 1 1656_0.81 0.81 536 691 242081344 243905804 1824460 1.82
AUT 1 1656_0.81 0.81 536 691 247353243 249207733 1854490 1.85
AUT 1 1673_0.8 0.8 395 486 260581858 262016624 1434766 1.43
AUT 1 1976_0.63 0.63 18 90 266160984 270116064 3955080 3.96
AUT 1 1934_0.67 0.67 19 40 282087582 284546412 2458830 2.46
AUT 2 3065_0.81 0.81 1933 4652 49798927 52237467 2438540 2.44
AUT 2 3065_0.81 0.81 1933 4652 101467688 102825816 1358128 1.36
AUT 2 3065_0.81 0.81 1933 4652 103572137 106204056 2631919 2.63
AUT 2 3065_0.81 0.81 1933 4652 124168259 126538488 2370229 2.37
AUT 2 3065_0.81 0.81 1933 4652 130361623 131660157 1298534 1.30
AUT 2 3065_0.81 0.81 1933 4652 137777373 138981409 1204036 1.20
AUT 2 3065_0.81 0.81 1933 4652 141440521 142703746 1263225 1.26
AUT 2 3065_0.81 0.81 1933 4652 149078628 153093408 4014780 4.01
AUT 2 3065_0.81 0.81 1933 4652 153767456 155771880 2004424 2.00
AUT 2 3066_0.81 0.81 797 989 156275046 158102348 1827302 1.83
AUT 2 3533_0.6 0.6 33 72 328800255 329979871 1179616 1.18
AUT 2 3531_0.61 0.61 21 31 333084727 334205193 1120466 1.12
AUT 2 3215_0.78 0.78 97 324 390518548 391668996 1150448 1.15
AUT 2 3215_0.78 0.78 97 324 391711273 392777580 1066307 1.07
AUT 2 3215_0.78 0.78 97 324 397107735 401367529 4259794 4.26
AUT 2 3215_0.78 0.78 97 324 405893739 407224834 1331095 1.33
AUT 2 3138_0.8 0.8 26 61 407321663 411100836 3779173 3.78
AUT 2 3138_0.8 0.8 26 61 411409101 412997990 1588889 1.59
AUT 2 3065_0.81 0.81 1933 4652 445201256 447678524 2477268 2.48
AUT 2 3065_0.81 0.81 1933 4652 447843218 448906645 1063427 1.06
AUT 2 3065_0.81 0.81 1933 4652 466090283 467282916 1192633 1.19
AUT 2 3065_0.81 0.81 1933 4652 495634845 497303862 1669017 1.67
AUT 2 3065_0.81 0.81 1933 4652 502247398 503278166 1030768 1.03
AUT 2 3065_0.81 0.81 1933 4652 504551083 505702718 1151635 1.15
AUT 2 3065_0.81 0.81 1933 4652 520825136 523110071 2284935 2.28
AUT 3 2950_0.81 0.81 1634 6373 15260009 16679033 1419024 1.42
AUT 3 2950_0.81 0.81 1634 6373 22221783 24876231 2654448 2.65
AUT 3 2950_0.81 0.81 1634 6373 57597040 60996345 3399305 3.40
AUT 3 2950_0.81 0.81 1634 6373 64323643 66464901 2141258 2.14
AUT 3 2950_0.81 0.81 1634 6373 67025564 68305791 1280227 1.28
AUT 3 2950_0.81 0.81 1634 6373 69348616 70416221 1067605 1.07
AUT 3 3292_0.56 0.56 10 37 75010838 76064737 1053899 1.05
AUT 3 2950_0.81 0.81 1634 6373 110079720 112277286 2197566 2.20
AUT 3 2950_0.81 0.81 1634 6373 112497013 113712759 1215746 1.22
AUT 3 2950_0.81 0.81 1634 6373 116172157 117763299 1591142 1.59
AUT 3 3129_0.76 0.76 105 311 197419987 198725960 1305973 1.31
AUT 3 3129_0.76 0.76 105 311 199372953 200641223 1268270 1.27
AUT 3 3195_0.73 0.73 47 94 213789681 215749074 1959393 1.96
AUT 3 2809_0.84 0.84 17 30 217392863 218946788 1553925 1.55
AUT 3 2950_0.81 0.81 1634 6373 223089294 224371815 1282521 1.28
AUT 3 2950_0.81 0.81 1634 6373 252429715 257705872 5276157 5.28
AUT 3 2950_0.81 0.81 1634 6373 261367232 263434740 2067508 2.07
AUT 3 2950_0.81 0.81 1634 6373 263696140 264901249 1205109 1.21
AUT 3 2950_0.81 0.81 1634 6373 267045795 268729206 1683411 1.68
AUT 3 2950_0.81 0.81 1634 6373 269707578 272924524 3216946 3.22
AUT 3 2950_0.81 0.81 1634 6373 284309274 285864116 1554842 1.55
AUT 3 2950_0.81 0.81 1634 6373 296354064 297430287 1076223 1.08
AUT 3 2950_0.81 0.81 1634 6373 314111993 315235784 1123791 1.12
AUT 3 2950_0.81 0.81 1634 6373 340649572 342828725 2179153 2.18
AUT 3 2950_0.81 0.81 1634 6373 345522786 349025972 3503186 3.50
AUT 3 2950_0.81 0.81 1634 6373 378164815 379392861 1228046 1.23
AUT 3 2950_0.81 0.81 1634 6373 380732751 381773127 1040376 1.04
AUT 3 2950_0.81 0.81 1634 6373 437388093 438768367 1380274 1.38
NEW 1 2098_0.81 0.81 225 3339 1671072 16303506 14632434 14.63
NEW 1 2098_0.81 0.81 225 3339 17763447 19491473 1728026 1.73
NEW 1 2098_0.81 0.81 225 3339 31462771 33110064 1647293 1.65
NEW 1 2098_0.81 0.81 225 3339 33812253 39535721 5723468 5.72
NEW 1 2098_0.81 0.81 225 3339 42912079 45617370 2705291 2.71
NEW 1 2098_0.81 0.81 225 3339 45813378 46852266 1038888 1.04
NEW 1 2098_0.81 0.81 225 3339 49058652 52982032 3923380 3.92
NEW 1 2098_0.81 0.81 225 3339 53104692 57458477 4353785 4.35
NEW 1 2098_0.81 0.81 225 3339 57603304 81674467 24071163 24.07
NEW 1 2098_0.81 0.81 225 3339 84125006 85286331 1161325 1.16
NEW 1 2257_0.78 0.78 21 49 88409052 90437664 2028612 2.03
NEW 1 2257_0.78 0.78 21 49 92128407 93161109 1032702 1.03
NEW 1 2098_0.81 0.81 225 3339 96662783 99757810 3095027 3.10
NEW 1 2098_0.81 0.81 225 3339 109454635 110939058 1484423 1.48
NEW 1 2098_0.81 0.81 225 3339 111088239 115093306 4005067 4.01
NEW 1 2098_0.81 0.81 225 3339 116539985 117786853 1246868 1.25
NEW 1 2098_0.81 0.81 225 3339 118382563 133808459 15425896 15.43
NEW 1 2098_0.81 0.81 225 3339 134311499 152641345 18329846 18.33
NEW 1 2098_0.81 0.81 225 3339 155511543 168919506 13407963 13.41
NEW 1 2098_0.81 0.81 225 3339 169035292 173049491 4014199 4.01
NEW 1 2098_0.81 0.81 225 3339 175562655 184285228 8722573 8.72
NEW 1 2098_0.81 0.81 225 3339 184822512 191340592 6518080 6.52
NEW 1 2098_0.81 0.81 225 3339 191491743 192497058 1005315 1.01
NEW 1 2098_0.81 0.81 225 3339 193238959 195536041 2297082 2.30
NEW 1 2098_0.81 0.81 225 3339 196644737 199686032 3041295 3.04
NEW 1 2098_0.81 0.81 225 3339 199758742 207076243 7317501 7.32
NEW 1 2098_0.81 0.81 225 3339 210193969 212041495 1847526 1.85
NEW 1 2098_0.81 0.81 225 3339 217126621 230413950 13287329 13.29
NEW 1 2098_0.81 0.81 225 3339 230429247 241512774 11083527 11.08
NEW 1 2098_0.81 0.81 225 3339 241581708 245964256 4382548 4.38
NEW 1 2098_0.81 0.81 225 3339 246194066 250559827 4365761 4.37
NEW 1 2098_0.81 0.81 225 3339 250638063 252000239 1362176 1.36
NEW 1 2098_0.81 0.81 225 3339 252144386 255284772 3140386 3.14
NEW 1 2098_0.81 0.81 225 3339 255471960 260032529 4560569 4.56
NEW 1 2098_0.81 0.81 225 3339 262169514 263823952 1654438 1.65
NEW 1 2098_0.81 0.81 225 3339 263937222 271417059 7479837 7.48
NEW 1 2098_0.81 0.81 225 3339 274018327 291005909 16987582 16.99
NEW 1 2098_0.81 0.81 225 3339 291128858 294274102 3145244 3.15
NEW 1 2098_0.81 0.81 225 3339 294683970 297498464 2814494 2.81
NEW 1 2098_0.81 0.81 225 3339 299222427 302129356 2906929 2.91
NEW 1 2098_0.81 0.81 225 3339 302199736 305506598 3306862 3.31
NEW 1 2098_0.81 0.81 225 3339 305564825 308828617 3263792 3.26
NEW 1 2098_0.81 0.81 225 3339 308845316 310046723 1201407 1.20
NEW 1 2098_0.81 0.81 225 3339 310436955 312893712 2456757 2.46
NEW 1 2098_0.81 0.81 225 3339 313386794 321651633 8264839 8.26
NEW 1 2098_0.81 0.81 225 3339 329288148 330792056 1503908 1.50
NEW 1 2098_0.81 0.81 225 3339 331903843 334134626 2230783 2.23
NEW 1 2098_0.81 0.81 225 3339 349747385 350752606 1005221 1.01
NEW 1 2098_0.81 0.81 225 3339 350759454 352036196 1276742 1.28
NEW 1 2098_0.81 0.81 225 3339 352250603 353457242 1206639 1.21
NEW 1 2098_0.81 0.81 225 3339 355814525 356832851 1018326 1.02
NEW 1 2098_0.81 0.81 225 3339 357649351 360432526 2783175 2.78
NEW 1 2098_0.81 0.81 225 3339 361376691 362625358 1248667 1.25
NEW 1 2098_0.81 0.81 225 3339 363346050 365490174 2144124 2.14
NEW 1 2098_0.81 0.81 225 3339 365516884 367009263 1492379 1.49
NEW 1 2098_0.81 0.81 225 3339 367076124 368115714 1039590 1.04
NEW 1 2098_0.81 0.81 225 3339 368209240 370940654 2731414 2.73
NEW 2 3393_0.8 0.8 1027 3235 224747 2320220 2095473 2.10
NEW 2 3393_0.8 0.8 1027 3235 3767940 5516504 1748564 1.75
NEW 2 3404_0.8 0.8 905 1502 20865308 23384980 2519672 2.52
NEW 2 3393_0.8 0.8 1027 3235 32333986 33684664 1350678 1.35
NEW 2 3393_0.8 0.8 1027 3235 37484773 39477392 1992619 1.99
NEW 2 3404_0.8 0.8 905 1502 47841211 49223392 1382181 1.38
NEW 2 3393_0.8 0.8 1027 3235 60198806 63385698 3186892 3.19
NEW 2 3404_0.8 0.8 905 1502 63957749 65028705 1070956 1.07
NEW 2 3393_0.8 0.8 1027 3235 71334985 75185080 3850095 3.85
NEW 2 3404_0.8 0.8 905 1502 75680372 77722092 2041720 2.04
NEW 2 3404_0.8 0.8 905 1502 79048197 81335266 2287069 2.29
NEW 2 3393_0.8 0.8 1027 3235 86262808 87274287 1011479 1.01
NEW 2 3393_0.8 0.8 1027 3235 89422203 90648941 1226738 1.23
NEW 2 3393_0.8 0.8 1027 3235 92717918 93748781 1030863 1.03
NEW 2 3393_0.8 0.8 1027 3235 100600773 102052192 1451419 1.45
NEW 2 3393_0.8 0.8 1027 3235 107385063 111397305 4012242 4.01
NEW 2 3393_0.8 0.8 1027 3235 112239445 113398794 1159349 1.16
NEW 2 3404_0.8 0.8 905 1502 115171624 117422202 2250578 2.25
NEW 2 3393_0.8 0.8 1027 3235 120642568 121789169 1146601 1.15
NEW 2 3404_0.8 0.8 905 1502 129752494 132084935 2332441 2.33
NEW 2 3404_0.8 0.8 905 1502 134691453 136254657 1563204 1.56
NEW 2 3393_0.8 0.8 1027 3235 137286537 138732635 1446098 1.45
NEW 2 3404_0.8 0.8 905 1502 141517826 144428065 2910239 2.91
NEW 2 3404_0.8 0.8 905 1502 145519669 147610899 2091230 2.09
NEW 2 3393_0.8 0.8 1027 3235 147686588 148800994 1114406 1.11
NEW 2 3404_0.8 0.8 905 1502 149738780 153093408 3354628 3.35
NEW 2 3404_0.8 0.8 905 1502 159957622 162105222 2147600 2.15
NEW 2 3404_0.8 0.8 905 1502 164942040 166163633 1221593 1.22
NEW 2 3404_0.8 0.8 905 1502 176121303 178566001 2444698 2.44
NEW 2 3393_0.8 0.8 1027 3235 184599662 185697190 1097528 1.10
NEW 2 3404_0.8 0.8 905 1502 187872447 189338359 1465912 1.47
NEW 2 3393_0.8 0.8 1027 3235 196919065 198241184 1322119 1.32
NEW 2 3393_0.8 0.8 1027 3235 198399859 199866764 1466905 1.47
NEW 2 3393_0.8 0.8 1027 3235 200640262 201766366 1126104 1.13
NEW 2 3393_0.8 0.8 1027 3235 211336217 214704214 3367997 3.37
NEW 2 3393_0.8 0.8 1027 3235 221458739 227293746 5835007 5.84
NEW 2 3404_0.8 0.8 905 1502 231449434 232560068 1110634 1.11
NEW 2 3404_0.8 0.8 905 1502 234706206 236580351 1874145 1.87
NEW 2 3393_0.8 0.8 1027 3235 250661099 251754487 1093388 1.09
NEW 2 3393_0.8 0.8 1027 3235 269725161 271001058 1275897 1.28
NEW 2 3393_0.8 0.8 1027 3235 272699658 274730588 2030930 2.03
NEW 2 3393_0.8 0.8 1027 3235 281488259 282754807 1266548 1.27
NEW 2 3393_0.8 0.8 1027 3235 298229333 300182152 1952819 1.95
NEW 2 3404_0.8 0.8 905 1502 301661105 304313204 2652099 2.65
NEW 2 3404_0.8 0.8 905 1502 304515783 306218388 1702605 1.70
NEW 2 3393_0.8 0.8 1027 3235 309917339 318159364 8242025 8.24
NEW 2 3393_0.8 0.8 1027 3235 322231858 323875574 1643716 1.64
NEW 2 3404_0.8 0.8 905 1502 331446801 335080127 3633326 3.63
NEW 2 3393_0.8 0.8 1027 3235 338547271 341098241 2550970 2.55
NEW 2 3404_0.8 0.8 905 1502 343930528 345470965 1540437 1.54
NEW 2 3404_0.8 0.8 905 1502 345633769 354870721 9236952 9.24
NEW 2 3404_0.8 0.8 905 1502 363676430 365753357 2076927 2.08
NEW 2 3393_0.8 0.8 1027 3235 371505037 373052338 1547301 1.55
NEW 2 3393_0.8 0.8 1027 3235 373133669 376347397 3213728 3.21
NEW 2 3393_0.8 0.8 1027 3235 379361111 381543575 2182464 2.18
NEW 2 3393_0.8 0.8 1027 3235 382522637 383782785 1260148 1.26
NEW 2 3393_0.8 0.8 1027 3235 394747517 395926654 1179137 1.18
NEW 2 3404_0.8 0.8 905 1502 407185045 409429909 2244864 2.24
NEW 2 3393_0.8 0.8 1027 3235 423413849 425459834 2045985 2.05
NEW 2 3393_0.8 0.8 1027 3235 426632333 428968851 2336518 2.34
NEW 2 3393_0.8 0.8 1027 3235 434926662 436108135 1181473 1.18
NEW 2 3404_0.8 0.8 905 1502 445321096 447678524 2357428 2.36
NEW 2 3393_0.8 0.8 1027 3235 450997412 452946043 1948631 1.95
NEW 2 3393_0.8 0.8 1027 3235 489711160 491614193 1903033 1.90
NEW 2 3393_0.8 0.8 1027 3235 505908537 507012127 1103590 1.10
NEW 2 3393_0.8 0.8 1027 3235 518565796 519621236 1055440 1.06
NEW 2 3393_0.8 0.8 1027 3235 522038684 523113152 1074468 1.07
NEW 2 3393_0.8 0.8 1027 3235 533383712 534904257 1520545 1.52
NEW 2 3393_0.8 0.8 1027 3235 546386096 547694728 1308632 1.31
NEW 2 3393_0.8 0.8 1027 3235 559442893 561613146 2170253 2.17
NEW 2 3393_0.8 0.8 1027 3235 584590398 585804603 1214205 1.21
NEW 3 2836_0.81 0.81 567 1122 138261 1143824 1005563 1.01
NEW 3 2836_0.81 0.81 567 1122 6888798 8280336 1391538 1.39
NEW 3 2837_0.81 0.81 602 1668 8741873 16401719 7659846 7.66
NEW 3 2837_0.81 0.81 602 1668 30868364 32084271 1215907 1.22
NEW 3 2837_0.81 0.81 602 1668 33607382 34770651 1163269 1.16
NEW 3 2837_0.81 0.81 602 1668 38220097 39497877 1277780 1.28
NEW 3 2837_0.81 0.81 602 1668 41999164 43351711 1352547 1.35
NEW 3 2837_0.81 0.81 602 1668 45971148 49553360 3582212 3.58
NEW 3 2837_0.81 0.81 602 1668 53278835 54796696 1517861 1.52
NEW 3 2837_0.81 0.81 602 1668 57016980 58476637 1459657 1.46
NEW 3 2844_0.81 0.81 94 142 62431035 64197177 1766142 1.77
NEW 3 2837_0.81 0.81 602 1668 66551211 68843398 2292187 2.29
NEW 3 2837_0.81 0.81 602 1668 70979233 76622043 5642810 5.64
NEW 3 2837_0.81 0.81 602 1668 79033650 80149798 1116148 1.12
NEW 3 2837_0.81 0.81 602 1668 82956837 84368104 1411267 1.41
NEW 3 2836_0.81 0.81 567 1122 87052596 88327934 1275338 1.28
NEW 3 2837_0.81 0.81 602 1668 96649526 97704550 1055024 1.06
NEW 3 2837_0.81 0.81 602 1668 103304460 105510232 2205772 2.21
NEW 3 2837_0.81 0.81 602 1668 106246872 108915149 2668277 2.67
NEW 3 2837_0.81 0.81 602 1668 110248778 112627576 2378798 2.38
NEW 3 2837_0.81 0.81 602 1668 117801583 119196324 1394741 1.39
NEW 3 2837_0.81 0.81 602 1668 122688806 125806099 3117293 3.12
NEW 3 2837_0.81 0.81 602 1668 126000611 132461516 6460905 6.46
NEW 3 2837_0.81 0.81 602 1668 134726992 142770788 8043796 8.04
NEW 3 2837_0.81 0.81 602 1668 143504855 146770743 3265888 3.27
NEW 3 2837_0.81 0.81 602 1668 149536509 156001191 6464682 6.46
NEW 3 2837_0.81 0.81 602 1668 158530473 159797863 1267390 1.27
NEW 3 2837_0.81 0.81 602 1668 161308720 163859867 2551147 2.55
NEW 3 2837_0.81 0.81 602 1668 164412695 165638650 1225955 1.23
NEW 3 2837_0.81 0.81 602 1668 165745584 169750947 4005363 4.01
NEW 3 2837_0.81 0.81 602 1668 173459259 182850273 9391014 9.39
NEW 3 2837_0.81 0.81 602 1668 183591308 191456216 7864908 7.86
NEW 3 2837_0.81 0.81 602 1668 191547834 194909609 3361775 3.36
NEW 3 2837_0.81 0.81 602 1668 197419987 208785670 11365683 11.37
NEW 3 2837_0.81 0.81 602 1668 209544043 212578519 3034476 3.03
NEW 3 2837_0.81 0.81 602 1668 212882391 219711403 6829012 6.83
NEW 3 2837_0.81 0.81 602 1668 220295952 223586999 3291047 3.29
NEW 3 2837_0.81 0.81 602 1668 224412047 226630195 2218148 2.22
NEW 3 2837_0.81 0.81 602 1668 227290780 229701667 2410887 2.41
NEW 3 2837_0.81 0.81 602 1668 238924438 243184338 4259900 4.26
NEW 3 2837_0.81 0.81 602 1668 244529416 246058521 1529105 1.53
NEW 3 4066_0.72 0.72 7 42 248606503 250684517 2078014 2.08
NEW 3 2837_0.81 0.81 602 1668 252765819 258190930 5425111 5.43
NEW 3 2837_0.81 0.81 602 1668 260250588 261292851 1042263 1.04
NEW 3 2836_0.81 0.81 567 1122 266966314 268898469 1932155 1.93
NEW 3 2837_0.81 0.81 602 1668 269155423 274126769 4971346 4.97
NEW 3 2837_0.81 0.81 602 1668 274697733 276432605 1734872 1.73
NEW 3 2836_0.81 0.81 567 1122 276462623 279021064 2558441 2.56
NEW 3 2837_0.81 0.81 602 1668 280594192 282048225 1454033 1.45
NEW 3 2837_0.81 0.81 602 1668 286337276 287471025 1133749 1.13
NEW 3 2836_0.81 0.81 567 1122 288896346 290130685 1234339 1.23
NEW 3 2837_0.81 0.81 602 1668 291357677 293293846 1936169 1.94
NEW 3 2837_0.81 0.81 602 1668 296354064 298088579 1734515 1.73
NEW 3 2837_0.81 0.81 602 1668 299533420 301080582 1547162 1.55
NEW 3 2837_0.81 0.81 602 1668 302091398 303360496 1269098 1.27
NEW 3 2837_0.81 0.81 602 1668 303715842 306464838 2748996 2.75
NEW 3 2837_0.81 0.81 602 1668 311114136 313584547 2470411 2.47
NEW 3 2837_0.81 0.81 602 1668 315936740 317289268 1352528 1.35
NEW 3 2836_0.81 0.81 567 1122 320805786 322094119 1288333 1.29
NEW 3 2837_0.81 0.81 602 1668 330738652 332126201 1387549 1.39
NEW 3 2837_0.81 0.81 602 1668 339919173 342105404 2186231 2.19
NEW 3 2836_0.81 0.81 567 1122 344380481 345522786 1142305 1.14
NEW 3 2837_0.81 0.81 602 1668 370379158 371501078 1121920 1.12
NEW 3 2837_0.81 0.81 602 1668 380236644 381564291 1327647 1.33
NEW 3 2837_0.81 0.81 602 1668 382176532 383384925 1208393 1.21
NEW 3 2837_0.81 0.81 602 1668 404173993 405714900 1540907 1.54
NEW 3 2836_0.81 0.81 567 1122 428481578 430261142 1779564 1.78
NEW 3 2836_0.81 0.81 567 1122 437336558 438485871 1149313 1.15
NEW 3 2837_0.81 0.81 602 1668 438979662 440553449 1573787 1.57
NEW 3 2836_0.81 0.81 567 1122 447077906 448164929 1087023 1.09
NEW 3 2837_0.81 0.81 602 1668 459111122 461437206 2326084 2.33
NEW 3 2836_0.81 0.81 567 1122 463741283 464793077 1051794 1.05
NEW 3 2836_0.81 0.81 567 1122 466877065 470726943 3849878 3.85
NEW 3 2837_0.81 0.81 602 1668 470834558 472278174 1443616 1.44
NEW 3 2837_0.81 0.81 602 1668 477874059 479070412 1196353 1.20
NEW 3 2837_0.81 0.81 602 1668 482358967 483846198 1487231 1.49
NEW 3 2837_0.81 0.81 602 1668 483851021 485203479 1352458 1.35

We can create a facet plot now for all chromosomes

# To plot only clusters equal or bigger than 1Mb
albo2 <- table1 |> dplyr::filter(Size >= 1000000)

# Function to format numbers as Mb
label_mb <- function(x) {
  sprintf("%.0fMb", x / 1e6)
}

# Plot it
ggplot(albo2, aes(xmin = Start, xmax = End, ymin = 0, ymax = Size)) +
  geom_rect(aes(fill = as.factor(Cluster)), color = "black", linewidth = 0.2) +
  scale_x_continuous(labels = label_mb, breaks = pretty_breaks(n = 3)) +
  scale_y_continuous(labels = label_mb, breaks = pretty_breaks(n = 5)) +
  labs(x = "Position", y = "Cluster Size") +
  theme_minimal() +
  theme(
    axis.text.y = element_text(margin = margin(t = 0, r = 5, b = 0, l = 5)),
    axis.ticks.y = element_blank(),
    panel.grid.major.x = element_line(color = "gray", linetype = "dotted"),
    strip.background = element_rect(fill = "#e8e8e8", colour = NA),
    panel.grid.minor.x = element_blank(),
    legend.position = "none",
    panel.spacing.x = unit(1, "lines")  # Adjust the unit and number to increase space as needed
  ) +
  facet_grid(Population ~ Chromosome, scales = "fixed", space = "fixed") +
  guides(fill = "none")

# Use ggsave to save the plot as a PDF
# ggsave(
#   filename = here("output", "ldna", "AUT_NEW_fixed.pdf"),
#   device = "pdf",
#   width = 10,
#   height = 5,
#   units = "in"
# )

Make each plot with different scales

# Plot it
ggplot(albo2, aes(xmin = Start, xmax = End, ymin = 0, ymax = Size)) +
  geom_rect(aes(fill = as.factor(Cluster)), color = "black", linewidth = 0.2) +
  scale_x_continuous(labels = label_mb, breaks = pretty_breaks(n = 3)) +
  scale_y_continuous(labels = label_mb, breaks = pretty_breaks(n = 5)) +
  labs(x = "Position", y = "Cluster Size") +
  theme_minimal() +
  theme(
    axis.text.y = element_text(margin = margin(t = 0, r = 5, b = 0, l = 5)),
    axis.ticks.y = element_blank(),
    panel.grid.major.x = element_line(color = "gray", linetype = "dotted"),
    panel.grid.minor.x = element_blank(),
    # strip.background = element_rect(fill = "#e8e8e8", colour = NA),
    legend.position = "none",
    panel.spacing.x = unit(1, "lines")
  ) +
  facet_wrap(~ Population + Chromosome, scales = "free_y", ncol = 3) +  # Free y scale, and each chromosome gets its own row
  guides(fill = "none")

# Use ggsave to save the plot as a PDF
# ggsave(
#   filename = here("output", "ldna", "AUT_NEW_free.pdf"),
#   device = "pdf",
#   width = 8,
#   height = 5,
#   units = "in"
# )

We can also remove the large clusters and plot with fixed scale to look at the cluster between 1 and 10Mb

# To plot only clusters equal or bigger than 1Mb but less that 10Mb
albo3 <- albo2 |> dplyr::filter(Size >= 1000000, Size <= 10000000)


# Plot it
ggplot(albo3, aes(xmin = Start, xmax = End, ymin = 0, ymax = Size)) +
  geom_rect(aes(fill = as.factor(Cluster)), color = "black", linewidth = 0.2) +
  scale_x_continuous(labels = label_mb, breaks = pretty_breaks(n = 3)) +
  scale_y_continuous(labels = label_mb, breaks = pretty_breaks(n = 5)) +
  labs(x = "Position", y = "Cluster Size") +
  theme_minimal() +
  theme(
    axis.text.y = element_text(margin = margin(t = 0, r = 5, b = 0, l = 5)),
    axis.ticks.y = element_blank(),
    panel.grid.major.x = element_line(color = "gray", linetype = "dotted"),
    strip.background = element_rect(fill = "#e8e8e8", colour = NA),
    panel.grid.minor.x = element_blank(),
    legend.position = "none",
    panel.spacing.x = unit(1, "lines")  # Adjust the unit and number to increase space as needed
  ) +
  facet_grid(Population ~ Chromosome, scales = "fixed", space = "fixed") +
  guides(fill = "none")

# Use ggsave to save the plot as a PDF
# ggsave(
#   filename = here("output", "ldna", "AUT_NEW_fixed_1_to_10Mb.pdf"),
#   device = "pdf",
#   width = 8,
#   height = 5,
#   units = "in"
# )

It seems that the linkage patters are quite different from AUT to the wild population. I am not sure why we are seeing clusters at such long distance the wild population for chromosome 2 and 3

We could do the analysis by sex, but we only have 13 females and 15 males in AUT. Perhaps we could use the same number of samples for each. However, the sample size will be too small still.

7. Plot SNPs on DE genes with LD blocks

snps_scan <-
  read.table(
    # here("output", "pcadapt", "man_aut_common_SNPs_pcadapt_outflank.txt"),
    here("output", "snpeff", "SNPs_79_DE.txt"),
    stringsAsFactors = FALSE
  ) |>
  dplyr::rename(
    SNP = V1
  )

head(snps_scan)
##            SNP
## 1 AX-583054970
## 2 AX-583055828
## 3 AX-583093532
## 4 AX-583142560
## 5 AX-583237406
## 6 AX-583279927

Import bim file

# Import the function
source(
  here(
    "notebooks", "helpers", "import_bim.R")
)

# Import the data
snps <- import_bim(here("output", "ldna", "files", "file1.bim"))

# Check it
head(snps)
## # A tibble: 6 x 6
##   Scaffold SNP             Cm Position Allele1 Allele2
##   <chr>    <chr>        <int>    <dbl> <chr>   <chr>  
## 1 1        AX-581444870     0    97856 C       T      
## 2 1        AX-583035083     0   305518 A       G      
## 3 1        AX-583035102     0   308124 A       G      
## 4 1        AX-583033342     0   315059 C       G      
## 5 1        AX-583035163     0   315386 A       G      
## 6 1        AX-583033356     0   315674 C       T

Merge the objects

merged_snps <- inner_join(snps_scan, snps, by = "SNP") |>
  dplyr::rename(
    Chromosome = Scaffold
  )
head(merged_snps)
##            SNP Chromosome Cm Position Allele1 Allele2
## 1 AX-583054970          1  0  5342242       G       A
## 2 AX-583055828          1  0  6260504       A       G
## 3 AX-583093532          1  0 18373267       C       T
## 4 AX-583142560          1  0 33012482       T       C
## 5 AX-583237406          1  0 66299838       T       C
## 6 AX-583279927          1  0 80133483       A       G

We can add them as lines in the plot

# First, create a new data frame with 1Mb windows and count SNPs in each window
merged_snps$Window <- floor(merged_snps$Position / 20e6) * 20e6 # 20mb
snp_counts <- merged_snps %>%
  group_by(Chromosome, Window) %>%
  summarize(SNP_count = n(), .groups = 'drop')

# Now, add this SNP information to the plot
snp_plot <- ggplot() +
  geom_rect(data = albo2, aes(xmin = Start, xmax = End, ymin = 0, ymax = Size, fill = as.factor(Cluster)),
            color = "black", linewidth = 0.2) +
  geom_vline(data = snp_counts, aes(xintercept = Window + 0.5e6), color = "pink", linetype = "dotted", linewidth = 0.5) +
  geom_text(data = snp_counts, aes(x = Window + 0.5e6, y = 8e6, label = SNP_count), vjust = -0.5, color = "blue", size = 2) +
  scale_x_continuous(labels = label_mb, breaks = pretty_breaks(n = 3)) +
  scale_y_continuous(labels = label_mb, breaks = pretty_breaks(n = 5)) +
  labs(x = "Position (Mb)", y = "Cluster Size (Mb)") +
  theme_minimal() +
  theme(
    axis.text.y = element_text(margin = margin(t = 0, r = 5, b = 0, l = 5)),
    axis.ticks.y = element_blank(),
    panel.grid.major.x = element_line(color = "gray", linetype = "dotdash"),
    strip.background = element_rect(fill = "#e8e8e8", colour = NA),
    panel.grid.minor.x = element_blank(),
    legend.position = "none",
    panel.spacing.x = unit(1, "lines")
  ) +
  facet_grid(Population ~ Chromosome, scales = "fixed", space = "fixed")

# Display the plot
print(snp_plot)
## Warning: Combining variables of class <numeric> and <character> was deprecated in
## ggplot2 3.4.0.
## i Please ensure your variables are compatible before plotting (location:
##   `combine_vars()`)
## This warning is displayed once every 8 hours.
## Call `lifecycle::last_lifecycle_warnings()` to see where this warning was
## generated.

#
# # Use ggsave to save the plot as a PDF
# ggsave(
#   filename = here("output", "ldna", "linkage_snps_genes_de.pdf"),
#   device = "pdf",
#   width = 8,
#   height = 5,
#   units = "in"
# )

Find the non-overlapping clusters

# Split the data into two subsets based on Population
albo_NEW <- subset(albo2, Population == "NEW")
albo_AUT <- subset(albo2, Population == "AUT")

# Function to find non-overlapping clusters
find_non_overlapping_clusters <- function(new_clusters, aut_clusters) {
  non_overlapping <- vector("list", length(new_clusters$Chromosome))
  names(non_overlapping) <- unique(new_clusters$Chromosome)

  for (chr in unique(new_clusters$Chromosome)) {
    new_chrom_clusters <- new_clusters[new_clusters$Chromosome == chr, ]
    aut_chrom_clusters <- aut_clusters[aut_clusters$Chromosome == chr, ]

    non_overlap <- vector("list", nrow(new_chrom_clusters))

    for (i in 1:nrow(new_chrom_clusters)) {
      overlaps <- any(aut_chrom_clusters$Start <= new_chrom_clusters$End[i] & aut_chrom_clusters$End >= new_chrom_clusters$Start[i])
      if (!overlaps) {
        non_overlap[[i]] <- new_chrom_clusters[i, ]
      }
    }

    non_overlapping[[chr]] <- do.call(rbind, non_overlap)
  }

  do.call(rbind, non_overlapping)
}

# Find non-overlapping clusters
non_overlapping_clusters <- find_non_overlapping_clusters(albo_NEW, albo_AUT)

# View the non-overlapping clusters
non_overlapping_clusters
##       Population Chromosome   Cluster   r2 nSegments nSNPs     Start       End
## 1.67         NEW          1 2098_0.81 0.81       225  3339  17763447  19491473
## 1.68         NEW          1 2098_0.81 0.81       225  3339  31462771  33110064
## 1.69         NEW          1 2098_0.81 0.81       225  3339  33812253  39535721
## 1.70         NEW          1 2098_0.81 0.81       225  3339  42912079  45617370
## 1.71         NEW          1 2098_0.81 0.81       225  3339  45813378  46852266
## 1.72         NEW          1 2098_0.81 0.81       225  3339  49058652  52982032
## 1.73         NEW          1 2098_0.81 0.81       225  3339  53104692  57458477
## 1.74         NEW          1 2098_0.81 0.81       225  3339  57603304  81674467
## 1.75         NEW          1 2098_0.81 0.81       225  3339  84125006  85286331
## 1.77         NEW          1 2257_0.78 0.78        21    49  92128407  93161109
## 1.78         NEW          1 2098_0.81 0.81       225  3339  96662783  99757810
## 1.79         NEW          1 2098_0.81 0.81       225  3339 109454635 110939058
## 1.80         NEW          1 2098_0.81 0.81       225  3339 111088239 115093306
## 1.81         NEW          1 2098_0.81 0.81       225  3339 116539985 117786853
## 1.84         NEW          1 2098_0.81 0.81       225  3339 155511543 168919506
## 1.85         NEW          1 2098_0.81 0.81       225  3339 169035292 173049491
## 1.87         NEW          1 2098_0.81 0.81       225  3339 184822512 191340592
## 1.88         NEW          1 2098_0.81 0.81       225  3339 191491743 192497058
## 1.89         NEW          1 2098_0.81 0.81       225  3339 193238959 195536041
## 1.90         NEW          1 2098_0.81 0.81       225  3339 196644737 199686032
## 1.91         NEW          1 2098_0.81 0.81       225  3339 199758742 207076243
## 1.92         NEW          1 2098_0.81 0.81       225  3339 210193969 212041495
## 1.93         NEW          1 2098_0.81 0.81       225  3339 217126621 230413950
## 1.94         NEW          1 2098_0.81 0.81       225  3339 230429247 241512774
## 1.97         NEW          1 2098_0.81 0.81       225  3339 250638063 252000239
## 1.98         NEW          1 2098_0.81 0.81       225  3339 252144386 255284772
## 1.99         NEW          1 2098_0.81 0.81       225  3339 255471960 260032529
## 1.100        NEW          1 2098_0.81 0.81       225  3339 262169514 263823952
## 1.103        NEW          1 2098_0.81 0.81       225  3339 291128858 294274102
## 1.104        NEW          1 2098_0.81 0.81       225  3339 294683970 297498464
## 1.105        NEW          1 2098_0.81 0.81       225  3339 299222427 302129356
## 1.106        NEW          1 2098_0.81 0.81       225  3339 302199736 305506598
## 1.107        NEW          1 2098_0.81 0.81       225  3339 305564825 308828617
## 1.108        NEW          1 2098_0.81 0.81       225  3339 308845316 310046723
## 1.109        NEW          1 2098_0.81 0.81       225  3339 310436955 312893712
## 1.110        NEW          1 2098_0.81 0.81       225  3339 313386794 321651633
## 1.111        NEW          1 2098_0.81 0.81       225  3339 329288148 330792056
## 1.112        NEW          1 2098_0.81 0.81       225  3339 331903843 334134626
## 1.113        NEW          1 2098_0.81 0.81       225  3339 349747385 350752606
## 1.114        NEW          1 2098_0.81 0.81       225  3339 350759454 352036196
## 1.115        NEW          1 2098_0.81 0.81       225  3339 352250603 353457242
## 1.116        NEW          1 2098_0.81 0.81       225  3339 355814525 356832851
## 1.117        NEW          1 2098_0.81 0.81       225  3339 357649351 360432526
## 1.118        NEW          1 2098_0.81 0.81       225  3339 361376691 362625358
## 1.119        NEW          1 2098_0.81 0.81       225  3339 363346050 365490174
## 1.120        NEW          1 2098_0.81 0.81       225  3339 365516884 367009263
## 1.121        NEW          1 2098_0.81 0.81       225  3339 367076124 368115714
## 1.122        NEW          1 2098_0.81 0.81       225  3339 368209240 370940654
## 2.123        NEW          2  3393_0.8  0.8      1027  3235    224747   2320220
## 2.124        NEW          2  3393_0.8  0.8      1027  3235   3767940   5516504
## 2.125        NEW          2  3404_0.8  0.8       905  1502  20865308  23384980
## 2.126        NEW          2  3393_0.8  0.8      1027  3235  32333986  33684664
## 2.127        NEW          2  3393_0.8  0.8      1027  3235  37484773  39477392
## 2.128        NEW          2  3404_0.8  0.8       905  1502  47841211  49223392
## 2.129        NEW          2  3393_0.8  0.8      1027  3235  60198806  63385698
## 2.130        NEW          2  3404_0.8  0.8       905  1502  63957749  65028705
## 2.131        NEW          2  3393_0.8  0.8      1027  3235  71334985  75185080
## 2.132        NEW          2  3404_0.8  0.8       905  1502  75680372  77722092
## 2.133        NEW          2  3404_0.8  0.8       905  1502  79048197  81335266
## 2.134        NEW          2  3393_0.8  0.8      1027  3235  86262808  87274287
## 2.135        NEW          2  3393_0.8  0.8      1027  3235  89422203  90648941
## 2.136        NEW          2  3393_0.8  0.8      1027  3235  92717918  93748781
## 2.138        NEW          2  3393_0.8  0.8      1027  3235 107385063 111397305
## 2.139        NEW          2  3393_0.8  0.8      1027  3235 112239445 113398794
## 2.140        NEW          2  3404_0.8  0.8       905  1502 115171624 117422202
## 2.141        NEW          2  3393_0.8  0.8      1027  3235 120642568 121789169
## 2.143        NEW          2  3404_0.8  0.8       905  1502 134691453 136254657
## 2.146        NEW          2  3404_0.8  0.8       905  1502 145519669 147610899
## 2.147        NEW          2  3393_0.8  0.8      1027  3235 147686588 148800994
## 2.149        NEW          2  3404_0.8  0.8       905  1502 159957622 162105222
## 2.150        NEW          2  3404_0.8  0.8       905  1502 164942040 166163633
## 2.151        NEW          2  3404_0.8  0.8       905  1502 176121303 178566001
## 2.152        NEW          2  3393_0.8  0.8      1027  3235 184599662 185697190
## 2.153        NEW          2  3404_0.8  0.8       905  1502 187872447 189338359
## 2.154        NEW          2  3393_0.8  0.8      1027  3235 196919065 198241184
## 2.155        NEW          2  3393_0.8  0.8      1027  3235 198399859 199866764
## 2.156        NEW          2  3393_0.8  0.8      1027  3235 200640262 201766366
## 2.157        NEW          2  3393_0.8  0.8      1027  3235 211336217 214704214
## 2.158        NEW          2  3393_0.8  0.8      1027  3235 221458739 227293746
## 2.159        NEW          2  3404_0.8  0.8       905  1502 231449434 232560068
## 2.160        NEW          2  3404_0.8  0.8       905  1502 234706206 236580351
## 2.161        NEW          2  3393_0.8  0.8      1027  3235 250661099 251754487
## 2.162        NEW          2  3393_0.8  0.8      1027  3235 269725161 271001058
## 2.163        NEW          2  3393_0.8  0.8      1027  3235 272699658 274730588
## 2.164        NEW          2  3393_0.8  0.8      1027  3235 281488259 282754807
## 2.165        NEW          2  3393_0.8  0.8      1027  3235 298229333 300182152
## 2.166        NEW          2  3404_0.8  0.8       905  1502 301661105 304313204
## 2.167        NEW          2  3404_0.8  0.8       905  1502 304515783 306218388
## 2.168        NEW          2  3393_0.8  0.8      1027  3235 309917339 318159364
## 2.169        NEW          2  3393_0.8  0.8      1027  3235 322231858 323875574
## 2.171        NEW          2  3393_0.8  0.8      1027  3235 338547271 341098241
## 2.172        NEW          2  3404_0.8  0.8       905  1502 343930528 345470965
## 2.173        NEW          2  3404_0.8  0.8       905  1502 345633769 354870721
## 2.174        NEW          2  3404_0.8  0.8       905  1502 363676430 365753357
## 2.175        NEW          2  3393_0.8  0.8      1027  3235 371505037 373052338
## 2.176        NEW          2  3393_0.8  0.8      1027  3235 373133669 376347397
## 2.177        NEW          2  3393_0.8  0.8      1027  3235 379361111 381543575
## 2.178        NEW          2  3393_0.8  0.8      1027  3235 382522637 383782785
## 2.179        NEW          2  3393_0.8  0.8      1027  3235 394747517 395926654
## 2.181        NEW          2  3393_0.8  0.8      1027  3235 423413849 425459834
## 2.182        NEW          2  3393_0.8  0.8      1027  3235 426632333 428968851
## 2.183        NEW          2  3393_0.8  0.8      1027  3235 434926662 436108135
## 2.185        NEW          2  3393_0.8  0.8      1027  3235 450997412 452946043
## 2.186        NEW          2  3393_0.8  0.8      1027  3235 489711160 491614193
## 2.187        NEW          2  3393_0.8  0.8      1027  3235 505908537 507012127
## 2.188        NEW          2  3393_0.8  0.8      1027  3235 518565796 519621236
## 2.190        NEW          2  3393_0.8  0.8      1027  3235 533383712 534904257
## 2.191        NEW          2  3393_0.8  0.8      1027  3235 546386096 547694728
## 2.192        NEW          2  3393_0.8  0.8      1027  3235 559442893 561613146
## 2.193        NEW          2  3393_0.8  0.8      1027  3235 584590398 585804603
## 3.194        NEW          3 2836_0.81 0.81       567  1122    138261   1143824
## 3.195        NEW          3 2836_0.81 0.81       567  1122   6888798   8280336
## 3.197        NEW          3 2837_0.81 0.81       602  1668  30868364  32084271
## 3.198        NEW          3 2837_0.81 0.81       602  1668  33607382  34770651
## 3.199        NEW          3 2837_0.81 0.81       602  1668  38220097  39497877
## 3.200        NEW          3 2837_0.81 0.81       602  1668  41999164  43351711
## 3.201        NEW          3 2837_0.81 0.81       602  1668  45971148  49553360
## 3.202        NEW          3 2837_0.81 0.81       602  1668  53278835  54796696
## 3.204        NEW          3 2844_0.81 0.81        94   142  62431035  64197177
## 3.207        NEW          3 2837_0.81 0.81       602  1668  79033650  80149798
## 3.208        NEW          3 2837_0.81 0.81       602  1668  82956837  84368104
## 3.209        NEW          3 2836_0.81 0.81       567  1122  87052596  88327934
## 3.210        NEW          3 2837_0.81 0.81       602  1668  96649526  97704550
## 3.211        NEW          3 2837_0.81 0.81       602  1668 103304460 105510232
## 3.212        NEW          3 2837_0.81 0.81       602  1668 106246872 108915149
## 3.214        NEW          3 2837_0.81 0.81       602  1668 117801583 119196324
## 3.215        NEW          3 2837_0.81 0.81       602  1668 122688806 125806099
## 3.216        NEW          3 2837_0.81 0.81       602  1668 126000611 132461516
## 3.217        NEW          3 2837_0.81 0.81       602  1668 134726992 142770788
## 3.218        NEW          3 2837_0.81 0.81       602  1668 143504855 146770743
## 3.219        NEW          3 2837_0.81 0.81       602  1668 149536509 156001191
## 3.220        NEW          3 2837_0.81 0.81       602  1668 158530473 159797863
## 3.221        NEW          3 2837_0.81 0.81       602  1668 161308720 163859867
## 3.222        NEW          3 2837_0.81 0.81       602  1668 164412695 165638650
## 3.223        NEW          3 2837_0.81 0.81       602  1668 165745584 169750947
## 3.224        NEW          3 2837_0.81 0.81       602  1668 173459259 182850273
## 3.225        NEW          3 2837_0.81 0.81       602  1668 183591308 191456216
## 3.226        NEW          3 2837_0.81 0.81       602  1668 191547834 194909609
## 3.228        NEW          3 2837_0.81 0.81       602  1668 209544043 212578519
## 3.231        NEW          3 2837_0.81 0.81       602  1668 224412047 226630195
## 3.232        NEW          3 2837_0.81 0.81       602  1668 227290780 229701667
## 3.233        NEW          3 2837_0.81 0.81       602  1668 238924438 243184338
## 3.234        NEW          3 2837_0.81 0.81       602  1668 244529416 246058521
## 3.235        NEW          3 4066_0.72 0.72         7    42 248606503 250684517
## 3.237        NEW          3 2837_0.81 0.81       602  1668 260250588 261292851
## 3.240        NEW          3 2837_0.81 0.81       602  1668 274697733 276432605
## 3.241        NEW          3 2836_0.81 0.81       567  1122 276462623 279021064
## 3.242        NEW          3 2837_0.81 0.81       602  1668 280594192 282048225
## 3.243        NEW          3 2837_0.81 0.81       602  1668 286337276 287471025
## 3.244        NEW          3 2836_0.81 0.81       567  1122 288896346 290130685
## 3.245        NEW          3 2837_0.81 0.81       602  1668 291357677 293293846
## 3.247        NEW          3 2837_0.81 0.81       602  1668 299533420 301080582
## 3.248        NEW          3 2837_0.81 0.81       602  1668 302091398 303360496
## 3.249        NEW          3 2837_0.81 0.81       602  1668 303715842 306464838
## 3.250        NEW          3 2837_0.81 0.81       602  1668 311114136 313584547
## 3.251        NEW          3 2837_0.81 0.81       602  1668 315936740 317289268
## 3.252        NEW          3 2836_0.81 0.81       567  1122 320805786 322094119
## 3.253        NEW          3 2837_0.81 0.81       602  1668 330738652 332126201
## 3.256        NEW          3 2837_0.81 0.81       602  1668 370379158 371501078
## 3.258        NEW          3 2837_0.81 0.81       602  1668 382176532 383384925
## 3.259        NEW          3 2837_0.81 0.81       602  1668 404173993 405714900
## 3.260        NEW          3 2836_0.81 0.81       567  1122 428481578 430261142
## 3.262        NEW          3 2837_0.81 0.81       602  1668 438979662 440553449
## 3.263        NEW          3 2836_0.81 0.81       567  1122 447077906 448164929
## 3.264        NEW          3 2837_0.81 0.81       602  1668 459111122 461437206
## 3.265        NEW          3 2836_0.81 0.81       567  1122 463741283 464793077
## 3.266        NEW          3 2836_0.81 0.81       567  1122 466877065 470726943
## 3.267        NEW          3 2837_0.81 0.81       602  1668 470834558 472278174
## 3.268        NEW          3 2837_0.81 0.81       602  1668 477874059 479070412
## 3.269        NEW          3 2837_0.81 0.81       602  1668 482358967 483846198
## 3.270        NEW          3 2837_0.81 0.81       602  1668 483851021 485203479
##           Size Size (Mb)
## 1.67   1728026      1.73
## 1.68   1647293      1.65
## 1.69   5723468      5.72
## 1.70   2705291      2.71
## 1.71   1038888      1.04
## 1.72   3923380      3.92
## 1.73   4353785      4.35
## 1.74  24071163     24.07
## 1.75   1161325      1.16
## 1.77   1032702      1.03
## 1.78   3095027      3.10
## 1.79   1484423      1.48
## 1.80   4005067      4.01
## 1.81   1246868      1.25
## 1.84  13407963     13.41
## 1.85   4014199      4.01
## 1.87   6518080      6.52
## 1.88   1005315      1.01
## 1.89   2297082      2.30
## 1.90   3041295      3.04
## 1.91   7317501      7.32
## 1.92   1847526      1.85
## 1.93  13287329     13.29
## 1.94  11083527     11.08
## 1.97   1362176      1.36
## 1.98   3140386      3.14
## 1.99   4560569      4.56
## 1.100  1654438      1.65
## 1.103  3145244      3.15
## 1.104  2814494      2.81
## 1.105  2906929      2.91
## 1.106  3306862      3.31
## 1.107  3263792      3.26
## 1.108  1201407      1.20
## 1.109  2456757      2.46
## 1.110  8264839      8.26
## 1.111  1503908      1.50
## 1.112  2230783      2.23
## 1.113  1005221      1.01
## 1.114  1276742      1.28
## 1.115  1206639      1.21
## 1.116  1018326      1.02
## 1.117  2783175      2.78
## 1.118  1248667      1.25
## 1.119  2144124      2.14
## 1.120  1492379      1.49
## 1.121  1039590      1.04
## 1.122  2731414      2.73
## 2.123  2095473      2.10
## 2.124  1748564      1.75
## 2.125  2519672      2.52
## 2.126  1350678      1.35
## 2.127  1992619      1.99
## 2.128  1382181      1.38
## 2.129  3186892      3.19
## 2.130  1070956      1.07
## 2.131  3850095      3.85
## 2.132  2041720      2.04
## 2.133  2287069      2.29
## 2.134  1011479      1.01
## 2.135  1226738      1.23
## 2.136  1030863      1.03
## 2.138  4012242      4.01
## 2.139  1159349      1.16
## 2.140  2250578      2.25
## 2.141  1146601      1.15
## 2.143  1563204      1.56
## 2.146  2091230      2.09
## 2.147  1114406      1.11
## 2.149  2147600      2.15
## 2.150  1221593      1.22
## 2.151  2444698      2.44
## 2.152  1097528      1.10
## 2.153  1465912      1.47
## 2.154  1322119      1.32
## 2.155  1466905      1.47
## 2.156  1126104      1.13
## 2.157  3367997      3.37
## 2.158  5835007      5.84
## 2.159  1110634      1.11
## 2.160  1874145      1.87
## 2.161  1093388      1.09
## 2.162  1275897      1.28
## 2.163  2030930      2.03
## 2.164  1266548      1.27
## 2.165  1952819      1.95
## 2.166  2652099      2.65
## 2.167  1702605      1.70
## 2.168  8242025      8.24
## 2.169  1643716      1.64
## 2.171  2550970      2.55
## 2.172  1540437      1.54
## 2.173  9236952      9.24
## 2.174  2076927      2.08
## 2.175  1547301      1.55
## 2.176  3213728      3.21
## 2.177  2182464      2.18
## 2.178  1260148      1.26
## 2.179  1179137      1.18
## 2.181  2045985      2.05
## 2.182  2336518      2.34
## 2.183  1181473      1.18
## 2.185  1948631      1.95
## 2.186  1903033      1.90
## 2.187  1103590      1.10
## 2.188  1055440      1.06
## 2.190  1520545      1.52
## 2.191  1308632      1.31
## 2.192  2170253      2.17
## 2.193  1214205      1.21
## 3.194  1005563      1.01
## 3.195  1391538      1.39
## 3.197  1215907      1.22
## 3.198  1163269      1.16
## 3.199  1277780      1.28
## 3.200  1352547      1.35
## 3.201  3582212      3.58
## 3.202  1517861      1.52
## 3.204  1766142      1.77
## 3.207  1116148      1.12
## 3.208  1411267      1.41
## 3.209  1275338      1.28
## 3.210  1055024      1.06
## 3.211  2205772      2.21
## 3.212  2668277      2.67
## 3.214  1394741      1.39
## 3.215  3117293      3.12
## 3.216  6460905      6.46
## 3.217  8043796      8.04
## 3.218  3265888      3.27
## 3.219  6464682      6.46
## 3.220  1267390      1.27
## 3.221  2551147      2.55
## 3.222  1225955      1.23
## 3.223  4005363      4.01
## 3.224  9391014      9.39
## 3.225  7864908      7.86
## 3.226  3361775      3.36
## 3.228  3034476      3.03
## 3.231  2218148      2.22
## 3.232  2410887      2.41
## 3.233  4259900      4.26
## 3.234  1529105      1.53
## 3.235  2078014      2.08
## 3.237  1042263      1.04
## 3.240  1734872      1.73
## 3.241  2558441      2.56
## 3.242  1454033      1.45
## 3.243  1133749      1.13
## 3.244  1234339      1.23
## 3.245  1936169      1.94
## 3.247  1547162      1.55
## 3.248  1269098      1.27
## 3.249  2748996      2.75
## 3.250  2470411      2.47
## 3.251  1352528      1.35
## 3.252  1288333      1.29
## 3.253  1387549      1.39
## 3.256  1121920      1.12
## 3.258  1208393      1.21
## 3.259  1540907      1.54
## 3.260  1779564      1.78
## 3.262  1573787      1.57
## 3.263  1087023      1.09
## 3.264  2326084      2.33
## 3.265  1051794      1.05
## 3.266  3849878      3.85
## 3.267  1443616      1.44
## 3.268  1196353      1.20
## 3.269  1487231      1.49
## 3.270  1352458      1.35

Sanity check: Second way to get it

# Function to find non-overlapping segments
find_non_overlapping_segments <- function(new_clusters, aut_clusters) {
  non_overlapping <- list()

  for (chr in unique(new_clusters$Chromosome)) {
    new_chrom_segments <- new_clusters[new_clusters$Chromosome == chr, ]
    aut_chrom_segments <- aut_clusters[aut_clusters$Chromosome == chr, ]

    non_overlap <- list()

    for (i in 1:nrow(new_chrom_segments)) {
      overlaps <- FALSE
      for (j in 1:nrow(aut_chrom_segments)) {
        if (new_chrom_segments$Start[i] <= aut_chrom_segments$End[j] && new_chrom_segments$End[i] >= aut_chrom_segments$Start[j]) {
          overlaps <- TRUE
          break
        }
      }

      if (!overlaps) {
        non_overlap <- c(non_overlap, list(new_chrom_segments[i, ]))
      }
    }

    non_overlapping[[chr]] <- do.call(rbind, non_overlap)
  }

  do.call(rbind, non_overlapping)
}

# Find non-overlapping segments
non_overlapping_segments <- find_non_overlapping_segments(albo_NEW, albo_AUT)

# View the non-overlapping segments
non_overlapping_segments
##     Population Chromosome   Cluster   r2 nSegments nSNPs     Start       End
## 67         NEW          1 2098_0.81 0.81       225  3339  17763447  19491473
## 68         NEW          1 2098_0.81 0.81       225  3339  31462771  33110064
## 69         NEW          1 2098_0.81 0.81       225  3339  33812253  39535721
## 70         NEW          1 2098_0.81 0.81       225  3339  42912079  45617370
## 71         NEW          1 2098_0.81 0.81       225  3339  45813378  46852266
## 72         NEW          1 2098_0.81 0.81       225  3339  49058652  52982032
## 73         NEW          1 2098_0.81 0.81       225  3339  53104692  57458477
## 74         NEW          1 2098_0.81 0.81       225  3339  57603304  81674467
## 75         NEW          1 2098_0.81 0.81       225  3339  84125006  85286331
## 77         NEW          1 2257_0.78 0.78        21    49  92128407  93161109
## 78         NEW          1 2098_0.81 0.81       225  3339  96662783  99757810
## 79         NEW          1 2098_0.81 0.81       225  3339 109454635 110939058
## 80         NEW          1 2098_0.81 0.81       225  3339 111088239 115093306
## 81         NEW          1 2098_0.81 0.81       225  3339 116539985 117786853
## 84         NEW          1 2098_0.81 0.81       225  3339 155511543 168919506
## 85         NEW          1 2098_0.81 0.81       225  3339 169035292 173049491
## 87         NEW          1 2098_0.81 0.81       225  3339 184822512 191340592
## 88         NEW          1 2098_0.81 0.81       225  3339 191491743 192497058
## 89         NEW          1 2098_0.81 0.81       225  3339 193238959 195536041
## 90         NEW          1 2098_0.81 0.81       225  3339 196644737 199686032
## 91         NEW          1 2098_0.81 0.81       225  3339 199758742 207076243
## 92         NEW          1 2098_0.81 0.81       225  3339 210193969 212041495
## 93         NEW          1 2098_0.81 0.81       225  3339 217126621 230413950
## 94         NEW          1 2098_0.81 0.81       225  3339 230429247 241512774
## 97         NEW          1 2098_0.81 0.81       225  3339 250638063 252000239
## 98         NEW          1 2098_0.81 0.81       225  3339 252144386 255284772
## 99         NEW          1 2098_0.81 0.81       225  3339 255471960 260032529
## 100        NEW          1 2098_0.81 0.81       225  3339 262169514 263823952
## 103        NEW          1 2098_0.81 0.81       225  3339 291128858 294274102
## 104        NEW          1 2098_0.81 0.81       225  3339 294683970 297498464
## 105        NEW          1 2098_0.81 0.81       225  3339 299222427 302129356
## 106        NEW          1 2098_0.81 0.81       225  3339 302199736 305506598
## 107        NEW          1 2098_0.81 0.81       225  3339 305564825 308828617
## 108        NEW          1 2098_0.81 0.81       225  3339 308845316 310046723
## 109        NEW          1 2098_0.81 0.81       225  3339 310436955 312893712
## 110        NEW          1 2098_0.81 0.81       225  3339 313386794 321651633
## 111        NEW          1 2098_0.81 0.81       225  3339 329288148 330792056
## 112        NEW          1 2098_0.81 0.81       225  3339 331903843 334134626
## 113        NEW          1 2098_0.81 0.81       225  3339 349747385 350752606
## 114        NEW          1 2098_0.81 0.81       225  3339 350759454 352036196
## 115        NEW          1 2098_0.81 0.81       225  3339 352250603 353457242
## 116        NEW          1 2098_0.81 0.81       225  3339 355814525 356832851
## 117        NEW          1 2098_0.81 0.81       225  3339 357649351 360432526
## 118        NEW          1 2098_0.81 0.81       225  3339 361376691 362625358
## 119        NEW          1 2098_0.81 0.81       225  3339 363346050 365490174
## 120        NEW          1 2098_0.81 0.81       225  3339 365516884 367009263
## 121        NEW          1 2098_0.81 0.81       225  3339 367076124 368115714
## 122        NEW          1 2098_0.81 0.81       225  3339 368209240 370940654
## 123        NEW          2  3393_0.8  0.8      1027  3235    224747   2320220
## 124        NEW          2  3393_0.8  0.8      1027  3235   3767940   5516504
## 125        NEW          2  3404_0.8  0.8       905  1502  20865308  23384980
## 126        NEW          2  3393_0.8  0.8      1027  3235  32333986  33684664
## 127        NEW          2  3393_0.8  0.8      1027  3235  37484773  39477392
## 128        NEW          2  3404_0.8  0.8       905  1502  47841211  49223392
## 129        NEW          2  3393_0.8  0.8      1027  3235  60198806  63385698
## 130        NEW          2  3404_0.8  0.8       905  1502  63957749  65028705
## 131        NEW          2  3393_0.8  0.8      1027  3235  71334985  75185080
## 132        NEW          2  3404_0.8  0.8       905  1502  75680372  77722092
## 133        NEW          2  3404_0.8  0.8       905  1502  79048197  81335266
## 134        NEW          2  3393_0.8  0.8      1027  3235  86262808  87274287
## 135        NEW          2  3393_0.8  0.8      1027  3235  89422203  90648941
## 136        NEW          2  3393_0.8  0.8      1027  3235  92717918  93748781
## 138        NEW          2  3393_0.8  0.8      1027  3235 107385063 111397305
## 139        NEW          2  3393_0.8  0.8      1027  3235 112239445 113398794
## 140        NEW          2  3404_0.8  0.8       905  1502 115171624 117422202
## 141        NEW          2  3393_0.8  0.8      1027  3235 120642568 121789169
## 143        NEW          2  3404_0.8  0.8       905  1502 134691453 136254657
## 146        NEW          2  3404_0.8  0.8       905  1502 145519669 147610899
## 147        NEW          2  3393_0.8  0.8      1027  3235 147686588 148800994
## 149        NEW          2  3404_0.8  0.8       905  1502 159957622 162105222
## 150        NEW          2  3404_0.8  0.8       905  1502 164942040 166163633
## 151        NEW          2  3404_0.8  0.8       905  1502 176121303 178566001
## 152        NEW          2  3393_0.8  0.8      1027  3235 184599662 185697190
## 153        NEW          2  3404_0.8  0.8       905  1502 187872447 189338359
## 154        NEW          2  3393_0.8  0.8      1027  3235 196919065 198241184
## 155        NEW          2  3393_0.8  0.8      1027  3235 198399859 199866764
## 156        NEW          2  3393_0.8  0.8      1027  3235 200640262 201766366
## 157        NEW          2  3393_0.8  0.8      1027  3235 211336217 214704214
## 158        NEW          2  3393_0.8  0.8      1027  3235 221458739 227293746
## 159        NEW          2  3404_0.8  0.8       905  1502 231449434 232560068
## 160        NEW          2  3404_0.8  0.8       905  1502 234706206 236580351
## 161        NEW          2  3393_0.8  0.8      1027  3235 250661099 251754487
## 162        NEW          2  3393_0.8  0.8      1027  3235 269725161 271001058
## 163        NEW          2  3393_0.8  0.8      1027  3235 272699658 274730588
## 164        NEW          2  3393_0.8  0.8      1027  3235 281488259 282754807
## 165        NEW          2  3393_0.8  0.8      1027  3235 298229333 300182152
## 166        NEW          2  3404_0.8  0.8       905  1502 301661105 304313204
## 167        NEW          2  3404_0.8  0.8       905  1502 304515783 306218388
## 168        NEW          2  3393_0.8  0.8      1027  3235 309917339 318159364
## 169        NEW          2  3393_0.8  0.8      1027  3235 322231858 323875574
## 171        NEW          2  3393_0.8  0.8      1027  3235 338547271 341098241
## 172        NEW          2  3404_0.8  0.8       905  1502 343930528 345470965
## 173        NEW          2  3404_0.8  0.8       905  1502 345633769 354870721
## 174        NEW          2  3404_0.8  0.8       905  1502 363676430 365753357
## 175        NEW          2  3393_0.8  0.8      1027  3235 371505037 373052338
## 176        NEW          2  3393_0.8  0.8      1027  3235 373133669 376347397
## 177        NEW          2  3393_0.8  0.8      1027  3235 379361111 381543575
## 178        NEW          2  3393_0.8  0.8      1027  3235 382522637 383782785
## 179        NEW          2  3393_0.8  0.8      1027  3235 394747517 395926654
## 181        NEW          2  3393_0.8  0.8      1027  3235 423413849 425459834
## 182        NEW          2  3393_0.8  0.8      1027  3235 426632333 428968851
## 183        NEW          2  3393_0.8  0.8      1027  3235 434926662 436108135
## 185        NEW          2  3393_0.8  0.8      1027  3235 450997412 452946043
## 186        NEW          2  3393_0.8  0.8      1027  3235 489711160 491614193
## 187        NEW          2  3393_0.8  0.8      1027  3235 505908537 507012127
## 188        NEW          2  3393_0.8  0.8      1027  3235 518565796 519621236
## 190        NEW          2  3393_0.8  0.8      1027  3235 533383712 534904257
## 191        NEW          2  3393_0.8  0.8      1027  3235 546386096 547694728
## 192        NEW          2  3393_0.8  0.8      1027  3235 559442893 561613146
## 193        NEW          2  3393_0.8  0.8      1027  3235 584590398 585804603
## 194        NEW          3 2836_0.81 0.81       567  1122    138261   1143824
## 195        NEW          3 2836_0.81 0.81       567  1122   6888798   8280336
## 197        NEW          3 2837_0.81 0.81       602  1668  30868364  32084271
## 198        NEW          3 2837_0.81 0.81       602  1668  33607382  34770651
## 199        NEW          3 2837_0.81 0.81       602  1668  38220097  39497877
## 200        NEW          3 2837_0.81 0.81       602  1668  41999164  43351711
## 201        NEW          3 2837_0.81 0.81       602  1668  45971148  49553360
## 202        NEW          3 2837_0.81 0.81       602  1668  53278835  54796696
## 204        NEW          3 2844_0.81 0.81        94   142  62431035  64197177
## 207        NEW          3 2837_0.81 0.81       602  1668  79033650  80149798
## 208        NEW          3 2837_0.81 0.81       602  1668  82956837  84368104
## 209        NEW          3 2836_0.81 0.81       567  1122  87052596  88327934
## 210        NEW          3 2837_0.81 0.81       602  1668  96649526  97704550
## 211        NEW          3 2837_0.81 0.81       602  1668 103304460 105510232
## 212        NEW          3 2837_0.81 0.81       602  1668 106246872 108915149
## 214        NEW          3 2837_0.81 0.81       602  1668 117801583 119196324
## 215        NEW          3 2837_0.81 0.81       602  1668 122688806 125806099
## 216        NEW          3 2837_0.81 0.81       602  1668 126000611 132461516
## 217        NEW          3 2837_0.81 0.81       602  1668 134726992 142770788
## 218        NEW          3 2837_0.81 0.81       602  1668 143504855 146770743
## 219        NEW          3 2837_0.81 0.81       602  1668 149536509 156001191
## 220        NEW          3 2837_0.81 0.81       602  1668 158530473 159797863
## 221        NEW          3 2837_0.81 0.81       602  1668 161308720 163859867
## 222        NEW          3 2837_0.81 0.81       602  1668 164412695 165638650
## 223        NEW          3 2837_0.81 0.81       602  1668 165745584 169750947
## 224        NEW          3 2837_0.81 0.81       602  1668 173459259 182850273
## 225        NEW          3 2837_0.81 0.81       602  1668 183591308 191456216
## 226        NEW          3 2837_0.81 0.81       602  1668 191547834 194909609
## 228        NEW          3 2837_0.81 0.81       602  1668 209544043 212578519
## 231        NEW          3 2837_0.81 0.81       602  1668 224412047 226630195
## 232        NEW          3 2837_0.81 0.81       602  1668 227290780 229701667
## 233        NEW          3 2837_0.81 0.81       602  1668 238924438 243184338
## 234        NEW          3 2837_0.81 0.81       602  1668 244529416 246058521
## 235        NEW          3 4066_0.72 0.72         7    42 248606503 250684517
## 237        NEW          3 2837_0.81 0.81       602  1668 260250588 261292851
## 240        NEW          3 2837_0.81 0.81       602  1668 274697733 276432605
## 241        NEW          3 2836_0.81 0.81       567  1122 276462623 279021064
## 242        NEW          3 2837_0.81 0.81       602  1668 280594192 282048225
## 243        NEW          3 2837_0.81 0.81       602  1668 286337276 287471025
## 244        NEW          3 2836_0.81 0.81       567  1122 288896346 290130685
## 245        NEW          3 2837_0.81 0.81       602  1668 291357677 293293846
## 247        NEW          3 2837_0.81 0.81       602  1668 299533420 301080582
## 248        NEW          3 2837_0.81 0.81       602  1668 302091398 303360496
## 249        NEW          3 2837_0.81 0.81       602  1668 303715842 306464838
## 250        NEW          3 2837_0.81 0.81       602  1668 311114136 313584547
## 251        NEW          3 2837_0.81 0.81       602  1668 315936740 317289268
## 252        NEW          3 2836_0.81 0.81       567  1122 320805786 322094119
## 253        NEW          3 2837_0.81 0.81       602  1668 330738652 332126201
## 256        NEW          3 2837_0.81 0.81       602  1668 370379158 371501078
## 258        NEW          3 2837_0.81 0.81       602  1668 382176532 383384925
## 259        NEW          3 2837_0.81 0.81       602  1668 404173993 405714900
## 260        NEW          3 2836_0.81 0.81       567  1122 428481578 430261142
## 262        NEW          3 2837_0.81 0.81       602  1668 438979662 440553449
## 263        NEW          3 2836_0.81 0.81       567  1122 447077906 448164929
## 264        NEW          3 2837_0.81 0.81       602  1668 459111122 461437206
## 265        NEW          3 2836_0.81 0.81       567  1122 463741283 464793077
## 266        NEW          3 2836_0.81 0.81       567  1122 466877065 470726943
## 267        NEW          3 2837_0.81 0.81       602  1668 470834558 472278174
## 268        NEW          3 2837_0.81 0.81       602  1668 477874059 479070412
## 269        NEW          3 2837_0.81 0.81       602  1668 482358967 483846198
## 270        NEW          3 2837_0.81 0.81       602  1668 483851021 485203479
##         Size Size (Mb)
## 67   1728026      1.73
## 68   1647293      1.65
## 69   5723468      5.72
## 70   2705291      2.71
## 71   1038888      1.04
## 72   3923380      3.92
## 73   4353785      4.35
## 74  24071163     24.07
## 75   1161325      1.16
## 77   1032702      1.03
## 78   3095027      3.10
## 79   1484423      1.48
## 80   4005067      4.01
## 81   1246868      1.25
## 84  13407963     13.41
## 85   4014199      4.01
## 87   6518080      6.52
## 88   1005315      1.01
## 89   2297082      2.30
## 90   3041295      3.04
## 91   7317501      7.32
## 92   1847526      1.85
## 93  13287329     13.29
## 94  11083527     11.08
## 97   1362176      1.36
## 98   3140386      3.14
## 99   4560569      4.56
## 100  1654438      1.65
## 103  3145244      3.15
## 104  2814494      2.81
## 105  2906929      2.91
## 106  3306862      3.31
## 107  3263792      3.26
## 108  1201407      1.20
## 109  2456757      2.46
## 110  8264839      8.26
## 111  1503908      1.50
## 112  2230783      2.23
## 113  1005221      1.01
## 114  1276742      1.28
## 115  1206639      1.21
## 116  1018326      1.02
## 117  2783175      2.78
## 118  1248667      1.25
## 119  2144124      2.14
## 120  1492379      1.49
## 121  1039590      1.04
## 122  2731414      2.73
## 123  2095473      2.10
## 124  1748564      1.75
## 125  2519672      2.52
## 126  1350678      1.35
## 127  1992619      1.99
## 128  1382181      1.38
## 129  3186892      3.19
## 130  1070956      1.07
## 131  3850095      3.85
## 132  2041720      2.04
## 133  2287069      2.29
## 134  1011479      1.01
## 135  1226738      1.23
## 136  1030863      1.03
## 138  4012242      4.01
## 139  1159349      1.16
## 140  2250578      2.25
## 141  1146601      1.15
## 143  1563204      1.56
## 146  2091230      2.09
## 147  1114406      1.11
## 149  2147600      2.15
## 150  1221593      1.22
## 151  2444698      2.44
## 152  1097528      1.10
## 153  1465912      1.47
## 154  1322119      1.32
## 155  1466905      1.47
## 156  1126104      1.13
## 157  3367997      3.37
## 158  5835007      5.84
## 159  1110634      1.11
## 160  1874145      1.87
## 161  1093388      1.09
## 162  1275897      1.28
## 163  2030930      2.03
## 164  1266548      1.27
## 165  1952819      1.95
## 166  2652099      2.65
## 167  1702605      1.70
## 168  8242025      8.24
## 169  1643716      1.64
## 171  2550970      2.55
## 172  1540437      1.54
## 173  9236952      9.24
## 174  2076927      2.08
## 175  1547301      1.55
## 176  3213728      3.21
## 177  2182464      2.18
## 178  1260148      1.26
## 179  1179137      1.18
## 181  2045985      2.05
## 182  2336518      2.34
## 183  1181473      1.18
## 185  1948631      1.95
## 186  1903033      1.90
## 187  1103590      1.10
## 188  1055440      1.06
## 190  1520545      1.52
## 191  1308632      1.31
## 192  2170253      2.17
## 193  1214205      1.21
## 194  1005563      1.01
## 195  1391538      1.39
## 197  1215907      1.22
## 198  1163269      1.16
## 199  1277780      1.28
## 200  1352547      1.35
## 201  3582212      3.58
## 202  1517861      1.52
## 204  1766142      1.77
## 207  1116148      1.12
## 208  1411267      1.41
## 209  1275338      1.28
## 210  1055024      1.06
## 211  2205772      2.21
## 212  2668277      2.67
## 214  1394741      1.39
## 215  3117293      3.12
## 216  6460905      6.46
## 217  8043796      8.04
## 218  3265888      3.27
## 219  6464682      6.46
## 220  1267390      1.27
## 221  2551147      2.55
## 222  1225955      1.23
## 223  4005363      4.01
## 224  9391014      9.39
## 225  7864908      7.86
## 226  3361775      3.36
## 228  3034476      3.03
## 231  2218148      2.22
## 232  2410887      2.41
## 233  4259900      4.26
## 234  1529105      1.53
## 235  2078014      2.08
## 237  1042263      1.04
## 240  1734872      1.73
## 241  2558441      2.56
## 242  1454033      1.45
## 243  1133749      1.13
## 244  1234339      1.23
## 245  1936169      1.94
## 247  1547162      1.55
## 248  1269098      1.27
## 249  2748996      2.75
## 250  2470411      2.47
## 251  1352528      1.35
## 252  1288333      1.29
## 253  1387549      1.39
## 256  1121920      1.12
## 258  1208393      1.21
## 259  1540907      1.54
## 260  1779564      1.78
## 262  1573787      1.57
## 263  1087023      1.09
## 264  2326084      2.33
## 265  1051794      1.05
## 266  3849878      3.85
## 267  1443616      1.44
## 268  1196353      1.20
## 269  1487231      1.49
## 270  1352458      1.35
# Function to find unique non-overlapping clusters for each population
find_unique_clusters <- function(population1, population2) {
  unique_clusters <- list()

  for (chr in unique(population1$Chromosome)) {
    pop1_chrom_clusters <- population1[population1$Chromosome == chr, ]
    pop2_chrom_clusters <- population2[population2$Chromosome == chr, ]

    unique_clusters_chr <- list()

    for (i in 1:nrow(pop1_chrom_clusters)) {
      overlaps <- any(
        pop2_chrom_clusters$Start <= pop1_chrom_clusters$End[i] &
        pop2_chrom_clusters$End >= pop1_chrom_clusters$Start[i]
      )

      if (!overlaps) {
        unique_clusters_chr <- c(unique_clusters_chr, list(pop1_chrom_clusters[i, ]))
      }
    }

    if (length(unique_clusters_chr) > 0) {
      unique_clusters[[chr]] <- do.call(rbind, unique_clusters_chr)
    }
  }

  do.call(rbind, unique_clusters)
}

# Find unique non-overlapping clusters for NEW and AUT populations
unique_clusters_NEW <- find_unique_clusters(albo2[albo2$Population == "NEW",], albo2[albo2$Population == "AUT",])
unique_clusters_AUT <- find_unique_clusters(albo2[albo2$Population == "AUT",], albo2[albo2$Population == "NEW",])

# View the unique non-overlapping clusters for each population
unique_clusters_NEW
##     Population Chromosome   Cluster   r2 nSegments nSNPs     Start       End
## 67         NEW          1 2098_0.81 0.81       225  3339  17763447  19491473
## 68         NEW          1 2098_0.81 0.81       225  3339  31462771  33110064
## 69         NEW          1 2098_0.81 0.81       225  3339  33812253  39535721
## 70         NEW          1 2098_0.81 0.81       225  3339  42912079  45617370
## 71         NEW          1 2098_0.81 0.81       225  3339  45813378  46852266
## 72         NEW          1 2098_0.81 0.81       225  3339  49058652  52982032
## 73         NEW          1 2098_0.81 0.81       225  3339  53104692  57458477
## 74         NEW          1 2098_0.81 0.81       225  3339  57603304  81674467
## 75         NEW          1 2098_0.81 0.81       225  3339  84125006  85286331
## 77         NEW          1 2257_0.78 0.78        21    49  92128407  93161109
## 78         NEW          1 2098_0.81 0.81       225  3339  96662783  99757810
## 79         NEW          1 2098_0.81 0.81       225  3339 109454635 110939058
## 80         NEW          1 2098_0.81 0.81       225  3339 111088239 115093306
## 81         NEW          1 2098_0.81 0.81       225  3339 116539985 117786853
## 84         NEW          1 2098_0.81 0.81       225  3339 155511543 168919506
## 85         NEW          1 2098_0.81 0.81       225  3339 169035292 173049491
## 87         NEW          1 2098_0.81 0.81       225  3339 184822512 191340592
## 88         NEW          1 2098_0.81 0.81       225  3339 191491743 192497058
## 89         NEW          1 2098_0.81 0.81       225  3339 193238959 195536041
## 90         NEW          1 2098_0.81 0.81       225  3339 196644737 199686032
## 91         NEW          1 2098_0.81 0.81       225  3339 199758742 207076243
## 92         NEW          1 2098_0.81 0.81       225  3339 210193969 212041495
## 93         NEW          1 2098_0.81 0.81       225  3339 217126621 230413950
## 94         NEW          1 2098_0.81 0.81       225  3339 230429247 241512774
## 97         NEW          1 2098_0.81 0.81       225  3339 250638063 252000239
## 98         NEW          1 2098_0.81 0.81       225  3339 252144386 255284772
## 99         NEW          1 2098_0.81 0.81       225  3339 255471960 260032529
## 100        NEW          1 2098_0.81 0.81       225  3339 262169514 263823952
## 103        NEW          1 2098_0.81 0.81       225  3339 291128858 294274102
## 104        NEW          1 2098_0.81 0.81       225  3339 294683970 297498464
## 105        NEW          1 2098_0.81 0.81       225  3339 299222427 302129356
## 106        NEW          1 2098_0.81 0.81       225  3339 302199736 305506598
## 107        NEW          1 2098_0.81 0.81       225  3339 305564825 308828617
## 108        NEW          1 2098_0.81 0.81       225  3339 308845316 310046723
## 109        NEW          1 2098_0.81 0.81       225  3339 310436955 312893712
## 110        NEW          1 2098_0.81 0.81       225  3339 313386794 321651633
## 111        NEW          1 2098_0.81 0.81       225  3339 329288148 330792056
## 112        NEW          1 2098_0.81 0.81       225  3339 331903843 334134626
## 113        NEW          1 2098_0.81 0.81       225  3339 349747385 350752606
## 114        NEW          1 2098_0.81 0.81       225  3339 350759454 352036196
## 115        NEW          1 2098_0.81 0.81       225  3339 352250603 353457242
## 116        NEW          1 2098_0.81 0.81       225  3339 355814525 356832851
## 117        NEW          1 2098_0.81 0.81       225  3339 357649351 360432526
## 118        NEW          1 2098_0.81 0.81       225  3339 361376691 362625358
## 119        NEW          1 2098_0.81 0.81       225  3339 363346050 365490174
## 120        NEW          1 2098_0.81 0.81       225  3339 365516884 367009263
## 121        NEW          1 2098_0.81 0.81       225  3339 367076124 368115714
## 122        NEW          1 2098_0.81 0.81       225  3339 368209240 370940654
## 123        NEW          2  3393_0.8  0.8      1027  3235    224747   2320220
## 124        NEW          2  3393_0.8  0.8      1027  3235   3767940   5516504
## 125        NEW          2  3404_0.8  0.8       905  1502  20865308  23384980
## 126        NEW          2  3393_0.8  0.8      1027  3235  32333986  33684664
## 127        NEW          2  3393_0.8  0.8      1027  3235  37484773  39477392
## 128        NEW          2  3404_0.8  0.8       905  1502  47841211  49223392
## 129        NEW          2  3393_0.8  0.8      1027  3235  60198806  63385698
## 130        NEW          2  3404_0.8  0.8       905  1502  63957749  65028705
## 131        NEW          2  3393_0.8  0.8      1027  3235  71334985  75185080
## 132        NEW          2  3404_0.8  0.8       905  1502  75680372  77722092
## 133        NEW          2  3404_0.8  0.8       905  1502  79048197  81335266
## 134        NEW          2  3393_0.8  0.8      1027  3235  86262808  87274287
## 135        NEW          2  3393_0.8  0.8      1027  3235  89422203  90648941
## 136        NEW          2  3393_0.8  0.8      1027  3235  92717918  93748781
## 138        NEW          2  3393_0.8  0.8      1027  3235 107385063 111397305
## 139        NEW          2  3393_0.8  0.8      1027  3235 112239445 113398794
## 140        NEW          2  3404_0.8  0.8       905  1502 115171624 117422202
## 141        NEW          2  3393_0.8  0.8      1027  3235 120642568 121789169
## 143        NEW          2  3404_0.8  0.8       905  1502 134691453 136254657
## 146        NEW          2  3404_0.8  0.8       905  1502 145519669 147610899
## 147        NEW          2  3393_0.8  0.8      1027  3235 147686588 148800994
## 149        NEW          2  3404_0.8  0.8       905  1502 159957622 162105222
## 150        NEW          2  3404_0.8  0.8       905  1502 164942040 166163633
## 151        NEW          2  3404_0.8  0.8       905  1502 176121303 178566001
## 152        NEW          2  3393_0.8  0.8      1027  3235 184599662 185697190
## 153        NEW          2  3404_0.8  0.8       905  1502 187872447 189338359
## 154        NEW          2  3393_0.8  0.8      1027  3235 196919065 198241184
## 155        NEW          2  3393_0.8  0.8      1027  3235 198399859 199866764
## 156        NEW          2  3393_0.8  0.8      1027  3235 200640262 201766366
## 157        NEW          2  3393_0.8  0.8      1027  3235 211336217 214704214
## 158        NEW          2  3393_0.8  0.8      1027  3235 221458739 227293746
## 159        NEW          2  3404_0.8  0.8       905  1502 231449434 232560068
## 160        NEW          2  3404_0.8  0.8       905  1502 234706206 236580351
## 161        NEW          2  3393_0.8  0.8      1027  3235 250661099 251754487
## 162        NEW          2  3393_0.8  0.8      1027  3235 269725161 271001058
## 163        NEW          2  3393_0.8  0.8      1027  3235 272699658 274730588
## 164        NEW          2  3393_0.8  0.8      1027  3235 281488259 282754807
## 165        NEW          2  3393_0.8  0.8      1027  3235 298229333 300182152
## 166        NEW          2  3404_0.8  0.8       905  1502 301661105 304313204
## 167        NEW          2  3404_0.8  0.8       905  1502 304515783 306218388
## 168        NEW          2  3393_0.8  0.8      1027  3235 309917339 318159364
## 169        NEW          2  3393_0.8  0.8      1027  3235 322231858 323875574
## 171        NEW          2  3393_0.8  0.8      1027  3235 338547271 341098241
## 172        NEW          2  3404_0.8  0.8       905  1502 343930528 345470965
## 173        NEW          2  3404_0.8  0.8       905  1502 345633769 354870721
## 174        NEW          2  3404_0.8  0.8       905  1502 363676430 365753357
## 175        NEW          2  3393_0.8  0.8      1027  3235 371505037 373052338
## 176        NEW          2  3393_0.8  0.8      1027  3235 373133669 376347397
## 177        NEW          2  3393_0.8  0.8      1027  3235 379361111 381543575
## 178        NEW          2  3393_0.8  0.8      1027  3235 382522637 383782785
## 179        NEW          2  3393_0.8  0.8      1027  3235 394747517 395926654
## 181        NEW          2  3393_0.8  0.8      1027  3235 423413849 425459834
## 182        NEW          2  3393_0.8  0.8      1027  3235 426632333 428968851
## 183        NEW          2  3393_0.8  0.8      1027  3235 434926662 436108135
## 185        NEW          2  3393_0.8  0.8      1027  3235 450997412 452946043
## 186        NEW          2  3393_0.8  0.8      1027  3235 489711160 491614193
## 187        NEW          2  3393_0.8  0.8      1027  3235 505908537 507012127
## 188        NEW          2  3393_0.8  0.8      1027  3235 518565796 519621236
## 190        NEW          2  3393_0.8  0.8      1027  3235 533383712 534904257
## 191        NEW          2  3393_0.8  0.8      1027  3235 546386096 547694728
## 192        NEW          2  3393_0.8  0.8      1027  3235 559442893 561613146
## 193        NEW          2  3393_0.8  0.8      1027  3235 584590398 585804603
## 194        NEW          3 2836_0.81 0.81       567  1122    138261   1143824
## 195        NEW          3 2836_0.81 0.81       567  1122   6888798   8280336
## 197        NEW          3 2837_0.81 0.81       602  1668  30868364  32084271
## 198        NEW          3 2837_0.81 0.81       602  1668  33607382  34770651
## 199        NEW          3 2837_0.81 0.81       602  1668  38220097  39497877
## 200        NEW          3 2837_0.81 0.81       602  1668  41999164  43351711
## 201        NEW          3 2837_0.81 0.81       602  1668  45971148  49553360
## 202        NEW          3 2837_0.81 0.81       602  1668  53278835  54796696
## 204        NEW          3 2844_0.81 0.81        94   142  62431035  64197177
## 207        NEW          3 2837_0.81 0.81       602  1668  79033650  80149798
## 208        NEW          3 2837_0.81 0.81       602  1668  82956837  84368104
## 209        NEW          3 2836_0.81 0.81       567  1122  87052596  88327934
## 210        NEW          3 2837_0.81 0.81       602  1668  96649526  97704550
## 211        NEW          3 2837_0.81 0.81       602  1668 103304460 105510232
## 212        NEW          3 2837_0.81 0.81       602  1668 106246872 108915149
## 214        NEW          3 2837_0.81 0.81       602  1668 117801583 119196324
## 215        NEW          3 2837_0.81 0.81       602  1668 122688806 125806099
## 216        NEW          3 2837_0.81 0.81       602  1668 126000611 132461516
## 217        NEW          3 2837_0.81 0.81       602  1668 134726992 142770788
## 218        NEW          3 2837_0.81 0.81       602  1668 143504855 146770743
## 219        NEW          3 2837_0.81 0.81       602  1668 149536509 156001191
## 220        NEW          3 2837_0.81 0.81       602  1668 158530473 159797863
## 221        NEW          3 2837_0.81 0.81       602  1668 161308720 163859867
## 222        NEW          3 2837_0.81 0.81       602  1668 164412695 165638650
## 223        NEW          3 2837_0.81 0.81       602  1668 165745584 169750947
## 224        NEW          3 2837_0.81 0.81       602  1668 173459259 182850273
## 225        NEW          3 2837_0.81 0.81       602  1668 183591308 191456216
## 226        NEW          3 2837_0.81 0.81       602  1668 191547834 194909609
## 228        NEW          3 2837_0.81 0.81       602  1668 209544043 212578519
## 231        NEW          3 2837_0.81 0.81       602  1668 224412047 226630195
## 232        NEW          3 2837_0.81 0.81       602  1668 227290780 229701667
## 233        NEW          3 2837_0.81 0.81       602  1668 238924438 243184338
## 234        NEW          3 2837_0.81 0.81       602  1668 244529416 246058521
## 235        NEW          3 4066_0.72 0.72         7    42 248606503 250684517
## 237        NEW          3 2837_0.81 0.81       602  1668 260250588 261292851
## 240        NEW          3 2837_0.81 0.81       602  1668 274697733 276432605
## 241        NEW          3 2836_0.81 0.81       567  1122 276462623 279021064
## 242        NEW          3 2837_0.81 0.81       602  1668 280594192 282048225
## 243        NEW          3 2837_0.81 0.81       602  1668 286337276 287471025
## 244        NEW          3 2836_0.81 0.81       567  1122 288896346 290130685
## 245        NEW          3 2837_0.81 0.81       602  1668 291357677 293293846
## 247        NEW          3 2837_0.81 0.81       602  1668 299533420 301080582
## 248        NEW          3 2837_0.81 0.81       602  1668 302091398 303360496
## 249        NEW          3 2837_0.81 0.81       602  1668 303715842 306464838
## 250        NEW          3 2837_0.81 0.81       602  1668 311114136 313584547
## 251        NEW          3 2837_0.81 0.81       602  1668 315936740 317289268
## 252        NEW          3 2836_0.81 0.81       567  1122 320805786 322094119
## 253        NEW          3 2837_0.81 0.81       602  1668 330738652 332126201
## 256        NEW          3 2837_0.81 0.81       602  1668 370379158 371501078
## 258        NEW          3 2837_0.81 0.81       602  1668 382176532 383384925
## 259        NEW          3 2837_0.81 0.81       602  1668 404173993 405714900
## 260        NEW          3 2836_0.81 0.81       567  1122 428481578 430261142
## 262        NEW          3 2837_0.81 0.81       602  1668 438979662 440553449
## 263        NEW          3 2836_0.81 0.81       567  1122 447077906 448164929
## 264        NEW          3 2837_0.81 0.81       602  1668 459111122 461437206
## 265        NEW          3 2836_0.81 0.81       567  1122 463741283 464793077
## 266        NEW          3 2836_0.81 0.81       567  1122 466877065 470726943
## 267        NEW          3 2837_0.81 0.81       602  1668 470834558 472278174
## 268        NEW          3 2837_0.81 0.81       602  1668 477874059 479070412
## 269        NEW          3 2837_0.81 0.81       602  1668 482358967 483846198
## 270        NEW          3 2837_0.81 0.81       602  1668 483851021 485203479
##         Size Size (Mb)
## 67   1728026      1.73
## 68   1647293      1.65
## 69   5723468      5.72
## 70   2705291      2.71
## 71   1038888      1.04
## 72   3923380      3.92
## 73   4353785      4.35
## 74  24071163     24.07
## 75   1161325      1.16
## 77   1032702      1.03
## 78   3095027      3.10
## 79   1484423      1.48
## 80   4005067      4.01
## 81   1246868      1.25
## 84  13407963     13.41
## 85   4014199      4.01
## 87   6518080      6.52
## 88   1005315      1.01
## 89   2297082      2.30
## 90   3041295      3.04
## 91   7317501      7.32
## 92   1847526      1.85
## 93  13287329     13.29
## 94  11083527     11.08
## 97   1362176      1.36
## 98   3140386      3.14
## 99   4560569      4.56
## 100  1654438      1.65
## 103  3145244      3.15
## 104  2814494      2.81
## 105  2906929      2.91
## 106  3306862      3.31
## 107  3263792      3.26
## 108  1201407      1.20
## 109  2456757      2.46
## 110  8264839      8.26
## 111  1503908      1.50
## 112  2230783      2.23
## 113  1005221      1.01
## 114  1276742      1.28
## 115  1206639      1.21
## 116  1018326      1.02
## 117  2783175      2.78
## 118  1248667      1.25
## 119  2144124      2.14
## 120  1492379      1.49
## 121  1039590      1.04
## 122  2731414      2.73
## 123  2095473      2.10
## 124  1748564      1.75
## 125  2519672      2.52
## 126  1350678      1.35
## 127  1992619      1.99
## 128  1382181      1.38
## 129  3186892      3.19
## 130  1070956      1.07
## 131  3850095      3.85
## 132  2041720      2.04
## 133  2287069      2.29
## 134  1011479      1.01
## 135  1226738      1.23
## 136  1030863      1.03
## 138  4012242      4.01
## 139  1159349      1.16
## 140  2250578      2.25
## 141  1146601      1.15
## 143  1563204      1.56
## 146  2091230      2.09
## 147  1114406      1.11
## 149  2147600      2.15
## 150  1221593      1.22
## 151  2444698      2.44
## 152  1097528      1.10
## 153  1465912      1.47
## 154  1322119      1.32
## 155  1466905      1.47
## 156  1126104      1.13
## 157  3367997      3.37
## 158  5835007      5.84
## 159  1110634      1.11
## 160  1874145      1.87
## 161  1093388      1.09
## 162  1275897      1.28
## 163  2030930      2.03
## 164  1266548      1.27
## 165  1952819      1.95
## 166  2652099      2.65
## 167  1702605      1.70
## 168  8242025      8.24
## 169  1643716      1.64
## 171  2550970      2.55
## 172  1540437      1.54
## 173  9236952      9.24
## 174  2076927      2.08
## 175  1547301      1.55
## 176  3213728      3.21
## 177  2182464      2.18
## 178  1260148      1.26
## 179  1179137      1.18
## 181  2045985      2.05
## 182  2336518      2.34
## 183  1181473      1.18
## 185  1948631      1.95
## 186  1903033      1.90
## 187  1103590      1.10
## 188  1055440      1.06
## 190  1520545      1.52
## 191  1308632      1.31
## 192  2170253      2.17
## 193  1214205      1.21
## 194  1005563      1.01
## 195  1391538      1.39
## 197  1215907      1.22
## 198  1163269      1.16
## 199  1277780      1.28
## 200  1352547      1.35
## 201  3582212      3.58
## 202  1517861      1.52
## 204  1766142      1.77
## 207  1116148      1.12
## 208  1411267      1.41
## 209  1275338      1.28
## 210  1055024      1.06
## 211  2205772      2.21
## 212  2668277      2.67
## 214  1394741      1.39
## 215  3117293      3.12
## 216  6460905      6.46
## 217  8043796      8.04
## 218  3265888      3.27
## 219  6464682      6.46
## 220  1267390      1.27
## 221  2551147      2.55
## 222  1225955      1.23
## 223  4005363      4.01
## 224  9391014      9.39
## 225  7864908      7.86
## 226  3361775      3.36
## 228  3034476      3.03
## 231  2218148      2.22
## 232  2410887      2.41
## 233  4259900      4.26
## 234  1529105      1.53
## 235  2078014      2.08
## 237  1042263      1.04
## 240  1734872      1.73
## 241  2558441      2.56
## 242  1454033      1.45
## 243  1133749      1.13
## 244  1234339      1.23
## 245  1936169      1.94
## 247  1547162      1.55
## 248  1269098      1.27
## 249  2748996      2.75
## 250  2470411      2.47
## 251  1352528      1.35
## 252  1288333      1.29
## 253  1387549      1.39
## 256  1121920      1.12
## 258  1208393      1.21
## 259  1540907      1.54
## 260  1779564      1.78
## 262  1573787      1.57
## 263  1087023      1.09
## 264  2326084      2.33
## 265  1051794      1.05
## 266  3849878      3.85
## 267  1443616      1.44
## 268  1196353      1.20
## 269  1487231      1.49
## 270  1352458      1.35
unique_clusters_AUT
##    Population Chromosome   Cluster   r2 nSegments nSNPs     Start       End
## 2         AUT          1 1763_0.77 0.77        30    61  87157392  88199259
## 10        AUT          1  1673_0.8  0.8       395   486 260581858 262016624
## 13        AUT          2 3065_0.81 0.81      1933  4652  49798927  52237467
## 15        AUT          2 3065_0.81 0.81      1933  4652 103572137 106204056
## 16        AUT          2 3065_0.81 0.81      1933  4652 124168259 126538488
## 21        AUT          2 3065_0.81 0.81      1933  4652 153767456 155771880
## 22        AUT          2 3066_0.81 0.81       797   989 156275046 158102348
## 23        AUT          2  3533_0.6  0.6        33    72 328800255 329979871
## 25        AUT          2 3215_0.78 0.78        97   324 390518548 391668996
## 26        AUT          2 3215_0.78 0.78        97   324 391711273 392777580
## 27        AUT          2 3215_0.78 0.78        97   324 397107735 401367529
## 30        AUT          2  3138_0.8  0.8        26    61 411409101 412997990
## 32        AUT          2 3065_0.81 0.81      1933  4652 447843218 448906645
## 33        AUT          2 3065_0.81 0.81      1933  4652 466090283 467282916
## 34        AUT          2 3065_0.81 0.81      1933  4652 495634845 497303862
## 35        AUT          2 3065_0.81 0.81      1933  4652 502247398 503278166
## 36        AUT          2 3065_0.81 0.81      1933  4652 504551083 505702718
## 39        AUT          3 2950_0.81 0.81      1634  6373  22221783  24876231
## 41        AUT          3 2950_0.81 0.81      1634  6373  64323643  66464901
## 43        AUT          3 2950_0.81 0.81      1634  6373  69348616  70416221
## 47        AUT          3 2950_0.81 0.81      1634  6373 116172157 117763299
## 54        AUT          3 2950_0.81 0.81      1634  6373 261367232 263434740
## 55        AUT          3 2950_0.81 0.81      1634  6373 263696140 264901249
## 58        AUT          3 2950_0.81 0.81      1634  6373 284309274 285864116
## 60        AUT          3 2950_0.81 0.81      1634  6373 314111993 315235784
## 63        AUT          3 2950_0.81 0.81      1634  6373 378164815 379392861
##       Size Size (Mb)
## 2  1041867      1.04
## 10 1434766      1.43
## 13 2438540      2.44
## 15 2631919      2.63
## 16 2370229      2.37
## 21 2004424      2.00
## 22 1827302      1.83
## 23 1179616      1.18
## 25 1150448      1.15
## 26 1066307      1.07
## 27 4259794      4.26
## 30 1588889      1.59
## 32 1063427      1.06
## 33 1192633      1.19
## 34 1669017      1.67
## 35 1030768      1.03
## 36 1151635      1.15
## 39 2654448      2.65
## 41 2141258      2.14
## 43 1067605      1.07
## 47 1591142      1.59
## 54 2067508      2.07
## 55 1205109      1.21
## 58 1554842      1.55
## 60 1123791      1.12
## 63 1228046      1.23

Plot it

# Combine the two data frames
combined_clusters <- rbind(unique_clusters_NEW, unique_clusters_AUT)


# Function to format numbers as Mb
label_mb <- function(x) {
  sprintf("%.0fMb", x / 1e6)
}

# Plot it
ggplot(combined_clusters, aes(xmin = Start, xmax = End, ymin = 0, ymax = Size)) +
  geom_rect(aes(fill = as.factor(Cluster)), color = "black", linewidth = 0.2) +
  scale_x_continuous(labels = label_mb, breaks = pretty_breaks(n = 3)) +
  scale_y_continuous(labels = label_mb, breaks = pretty_breaks(n = 5)) +
  labs(x = "Position", y = "Cluster Size") +
  theme_minimal() +
  theme(
    axis.text.y = element_text(margin = margin(t = 0, r = 5, b = 0, l = 5)),
    axis.ticks.y = element_blank(),
    panel.grid.major.x = element_line(color = "gray", linetype = "dotted"),
    strip.background = element_rect(fill = "#e8e8e8", colour = NA),
    panel.grid.minor.x = element_blank(),
    legend.position = "none",
    panel.spacing.x = unit(1, "lines")  # Adjust the unit and number to increase space as needed
  ) +
  facet_grid(Population ~ Chromosome, scales = "fixed", space = "fixed") +
  guides(fill = "none")

# Use ggsave to save the plot as a PDF
# ggsave(
#   filename = here("output", "ldna", "AUT_NEW_fixed_non_overlapping.pdf"),
#   device = "pdf",
#   width = 10,
#   height = 5,
#   units = "in"
# )
snps_DEgenes<- read_delim(here("output", "snpeff", "SNPs_on_DE_genes.txt"), delim = "\t", col_names = TRUE, show_col_types = FALSE)
head(snps_DEgenes)
## # A tibble: 6 x 3
##   SNP          Chromosome Position_chr
##   <chr>             <dbl>        <dbl>
## 1 AX-583050970          1      3777633
## 2 AX-583052853          1      3777864
## 3 AX-583051011          1      3779183
## 4 AX-583052887          1      3793314
## 5 AX-583051025          1      3793653
## 6 AX-583052920          1      3794185

Check if any SNP fall within the clusters

# Initialize an empty data frame to store the results
results <- data.frame(Population = character(), Chromosome = integer(),
                      Cluster = character(), r2 = numeric(),
                      nSegments = integer(), nSNPs = integer(),
                      Start = integer(), End = integer(), Size = integer(),
                      SizeMb = numeric(), SNP = character(), Position_chr = integer(),
                      stringsAsFactors = FALSE)

for (i in 1:nrow(snps_DEgenes)) {
    for (j in 1:nrow(combined_clusters)) {
        if (snps_DEgenes$Chromosome[i] == combined_clusters$Chromosome[j] &&
            snps_DEgenes$Position_chr[i] >= combined_clusters$Start[j] &&
            snps_DEgenes$Position_chr[i] <= combined_clusters$End[j]) {

            # Create a new row as a data frame with the same column names
            new_row <- data.frame(Population = combined_clusters$Population[j],
                                  Chromosome = combined_clusters$Chromosome[j],
                                  Cluster = combined_clusters$Cluster[j],
                                  r2 = combined_clusters$r2[j],
                                  nSegments = combined_clusters$nSegments[j],
                                  nSNPs = combined_clusters$nSNPs[j],
                                  Start = combined_clusters$Start[j],
                                  End = combined_clusters$End[j],
                                  Size = combined_clusters$Size[j],
                                  SizeMb = combined_clusters$Size[j],
                                  SNP = snps_DEgenes$SNP[i],
                                  Position_chr = snps_DEgenes$Position_chr[i],
                                  stringsAsFactors = FALSE)

            # Append the new row to the results data frame
            results <- rbind(results, new_row)
        }
    }
}


head(results)
##   Population Chromosome   Cluster   r2 nSegments nSNPs    Start      End
## 1        NEW          1 2098_0.81 0.81       225  3339 17763447 19491473
## 2        NEW          1 2098_0.81 0.81       225  3339 17763447 19491473
## 3        NEW          1 2098_0.81 0.81       225  3339 17763447 19491473
## 4        NEW          1 2098_0.81 0.81       225  3339 17763447 19491473
## 5        NEW          1 2098_0.81 0.81       225  3339 17763447 19491473
## 6        NEW          1 2098_0.81 0.81       225  3339 17763447 19491473
##      Size  SizeMb          SNP Position_chr
## 1 1728026 1728026 AX-583091405     18352792
## 2 1728026 1728026 AX-583091423     18353091
## 3 1728026 1728026 AX-583093509     18354233
## 4 1728026 1728026 AX-583091460     18354996
## 5 1728026 1728026 AX-583093519     18355386
## 6 1728026 1728026 AX-583093532     18373267

We can count how many SNPs we have per cluster per population

snp_counts <- results %>%
  select(Population, Chromosome, Cluster, nSNPs) %>%
  distinct() %>%
  rename(SNP_Count = nSNPs)


head(snp_counts)
##   Population Chromosome   Cluster SNP_Count
## 1        NEW          1 2098_0.81      3339
## 2        NEW          2  3404_0.8      1502
## 3        NEW          2  3393_0.8      3235
## 4        AUT          2 3215_0.78       324
## 5        NEW          3 2837_0.81      1668
## 6        AUT          3 2950_0.81      6373

Plot the clusters for which we have SNPs on the DE genes

# Merge SNP counts into results
results_with_counts <- merge(results, snp_counts, by = c("Population", "Chromosome", "Cluster"))

ggplot(results_with_counts, aes(xmin = Start, xmax = End, ymin = 0, ymax = Size)) +
  geom_rect(aes(fill = as.factor(Cluster)), color = "black", linewidth = 0.2) +
  geom_text(aes(x = (Start + End) / 2, y = Size + 0.5, label = SNP_Count), size = 3, vjust = -0.2) + # Annotate SNP counts on top
  scale_x_continuous(labels = label_mb, breaks = pretty_breaks(n = 3)) +
  scale_y_continuous(labels = label_mb, breaks = pretty_breaks(n = 5)) +
  labs(x = "Position", y = "Cluster Size") +
  theme_minimal() +
  theme(
    axis.text.y = element_text(margin = margin(t = 0, r = 5, b = 0, l = 5)),
    axis.ticks.y = element_blank(),
    panel.grid.major.x = element_line(color = "gray", linetype = "dotted"),
    strip.background = element_rect(fill = "#e8e8e8", colour = NA),
    panel.grid.minor.x = element_blank(),
    legend.position = "none",
    panel.spacing.x = unit(1, "lines")
  ) +
  facet_grid(Population ~ Chromosome, scales = "fixed", space = "fixed") +
  guides(fill = "none")

# 1. Generate integer IDs for each cluster in plotting order
cluster_ids <- data.frame(
  Cluster     = sort(unique(results$Cluster)),
  Cluster_ID  = seq_along(unique(results$Cluster))
)

# 2. Merge IDs into your results
results_with_ids <- merge(results, cluster_ids, by = "Cluster")

# 3. Plot, labeling by your original 1,2,3,… IDs
ggplot(results_with_ids, aes(xmin = Start, xmax = End, ymin = 0, ymax = Size)) +
  geom_rect(aes(fill = factor(Cluster_ID)), color = "black", linewidth = 0.2) +
  geom_text(aes(x = (Start + End) / 2,
                y = Size + 0.5,
                label = Cluster_ID),
            size = 3, vjust = -0.2) +
  scale_x_continuous(labels = label_mb, breaks = pretty_breaks(n = 3)) +
  scale_y_continuous(labels = label_mb, breaks = pretty_breaks(n = 5)) +
  labs(x = "Position", y = "Cluster Size") +
  theme_minimal() +
  theme(
    axis.text.y        = element_text(margin = margin(t = 0, r = 5)),
    axis.ticks.y       = element_blank(),
    panel.grid.major.x = element_line(color = "gray", linetype = "dotted"),
    strip.background   = element_rect(fill = "#e8e8e8", colour = NA),
    panel.grid.minor.x = element_blank(),
    legend.position    = "none",
    panel.spacing.x    = unit(1, "lines")
  ) +
  facet_grid(Population ~ Chromosome, scales = "fixed", space = "fixed") +
  guides(fill = "none")

# 1. Merge integer IDs into your SNP‐counted data
cluster_ids <- data.frame(
  Cluster    = sort(unique(results_with_counts$Cluster)),
  Cluster_ID = seq_along(unique(results_with_counts$Cluster))
)
results_with_counts <- merge(results_with_counts, cluster_ids, by = "Cluster")

# 2. Build the fill label
results_with_counts$Cluster_SNP <- with(
  results_with_counts,
  paste0(Cluster, " (", SNP_Count, " SNPs)")
)

# 3. Plot with fill by SNP count label and text by cluster ID
ggplot(results_with_counts, aes(xmin = Start, xmax = End, ymin = 0, ymax = Size)) +
  geom_rect(aes(fill = Cluster_SNP), color = "black", linewidth = 0.2) +
  geom_text(
    aes(x = (Start + End) / 2, y = Size + 0.5, label = Cluster_ID),
    size = 3, vjust = -0.2
  ) +
  scale_fill_discrete(name = "Cluster and SNP Count") +
  scale_x_continuous(labels = label_mb, breaks = pretty_breaks(n = 3)) +
  scale_y_continuous(labels = label_mb, breaks = pretty_breaks(n = 5)) +
  labs(x = "Position", y = "Cluster Size") +
  theme_minimal() +
  theme(
    axis.text.y        = element_text(margin = margin(t = 0, r = 5)),
    axis.ticks.y       = element_blank(),
    panel.grid.major.x = element_line(color = "gray", linetype = "dotted"),
    strip.background   = element_rect(fill = "#e8e8e8", colour = NA),
    panel.grid.minor.x = element_blank(),
    legend.position    = "right",
    panel.spacing.x    = unit(1, "lines")
  ) +
  facet_grid(Population ~ Chromosome, scales = "fixed", space = "fixed")

Save

saveRDS(results_with_counts, file = here("output", "ldna", "results_with_counts.rds"))

Read the data

results_with_counts <- readRDS(file = here("output", "ldna", "results_with_counts.rds"))
# Rebuild the fill label: add ID before Cluster and drop “ SNPs”
results_with_counts$Cluster_SNP <- with(
  results_with_counts,
  paste0(Cluster_ID, ": ", Cluster, " (", SNP_Count, ")")
)

# 1. Reorder Cluster_SNP factor by Cluster_ID so the legend follows numeric order
results_with_counts$Cluster_SNP <- factor(
  results_with_counts$Cluster_SNP,
  levels = results_with_counts %>%
    distinct(Cluster_ID, Cluster_SNP) %>%
    arrange(Cluster_ID) %>%
    pull(Cluster_SNP)
)

# 2. Plot with sorted legend
ggplot(results_with_counts, aes(xmin = Start, xmax = End, ymin = 0, ymax = Size)) +
  geom_rect(aes(fill = Cluster_SNP), color = "black", linewidth = 0.2) +
  geom_text(
    aes(x = (Start + End) / 2, y = Size + 0.5, label = Cluster_ID),
    size = 3, vjust = -0.2
  ) +
  scale_fill_discrete(name = "Cluster and SNP Count") +
  scale_x_continuous(labels = label_mb, breaks = pretty_breaks(n = 3)) +
  scale_y_continuous(labels = label_mb, breaks = pretty_breaks(n = 5)) +
  labs(x = "Position", y = "Cluster Size") +
  theme_minimal() +
  theme(
    axis.text.y        = element_text(margin = margin(t = 0, r = 5)),
    axis.ticks.y       = element_blank(),
    panel.grid.major.x = element_line(color = "gray", linetype = "dotted"),
    strip.background   = element_rect(fill = "#e8e8e8", colour = NA),
    panel.grid.minor.x = element_blank(),
    legend.position    = "right",
    panel.spacing.x    = unit(1, "lines")
  ) +
  facet_grid(Population ~ Chromosome, scales = "fixed", space = "fixed")

# Use ggsave to save the plot as a PDF
ggsave(
  filename = here("output", "ldna", "AUT_NEW_non_overlapping_clusters_with_SNPs_on_DE_genes.pdf"),
  device = "pdf",
  width = 10,
  height = 5,
  units = "in"
)

Save it

library(writexl)

# 1. Drop Cluster_ID column(s)
cleaned_results <- results_with_counts %>%
  select(-starts_with("Cluster_ID")) %>%
  mutate(
    Start        = formatC(Start,        format = "f", digits = 0),
    End          = formatC(End,          format = "f", digits = 0),
    Position_chr = formatC(Position_chr, format = "f", digits = 0)
  )


# 2. Export cleaned results_with_counts to Excel
write_xlsx(
  list(Results = cleaned_results),
  here("output/ldna/results_with_counts.xlsx")
)

Now we can get the SNPs on the DE genes

snps79_DE_genes<- read_delim(here("output", "snpeff","SNPs_79_DE.txt"), delim = "\t", col_names = FALSE, show_col_types = FALSE)
head(snps79_DE_genes)
## # A tibble: 6 x 1
##   X1          
##   <chr>       
## 1 AX-583054970
## 2 AX-583055828
## 3 AX-583093532
## 4 AX-583142560
## 5 AX-583237406
## 6 AX-583279927

Or the SNPs from the selection scan

snps_selection <- read_delim(here("output", "pcadapt","outlier_157_SNPs.txt"), delim = "\t", col_names = FALSE, show_col_types = FALSE)
head(snps_selection)
## # A tibble: 6 x 1
##   X1          
##   <chr>       
## 1 AX-583054970
## 2 AX-583095890
## 3 AX-583324654
## 4 AX-583423493
## 5 AX-583426050
## 6 AX-583467551

Filter

# Create a logical vector to filter rows based on SNP IDs
filter_vector <- results_with_counts$SNP %in% snps79_DE_genes$X1
# filter_vector <- results_with_counts$SNP %in% snps_selection$X1  # we do not have any

# Subset results_with_counts to keep only rows where SNP matches
filtered_results <- results_with_counts[filter_vector, ]

head(filtered_results)
##      Cluster Population Chromosome   r2 nSegments nSNPs     Start       End
## 6  2098_0.81        NEW          1 0.81       225  3339  17763447  19491473
## 16 2098_0.81        NEW          1 0.81       225  3339  31462771  33110064
## 19 2098_0.81        NEW          1 0.81       225  3339  57603304  81674467
## 23 2098_0.81        NEW          1 0.81       225  3339  57603304  81674467
## 52 2098_0.81        NEW          1 0.81       225  3339 199758742 207076243
## 57 2098_0.81        NEW          1 0.81       225  3339 199758742 207076243
##        Size   SizeMb          SNP Position_chr SNP_Count Cluster_ID
## 6   1728026  1728026 AX-583093532     18373267      3339          1
## 16  1647293  1647293 AX-583142560     33012482      3339          1
## 19 24071163 24071163 AX-583237406     66299838      3339          1
## 23 24071163 24071163 AX-583279927     80133483      3339          1
## 52  7317501  7317501 AX-583696963    199917250      3339          1
## 57  7317501  7317501 AX-583709904    206309664      3339          1
##            Cluster_SNP
## 6  1: 2098_0.81 (3339)
## 16 1: 2098_0.81 (3339)
## 19 1: 2098_0.81 (3339)
## 23 1: 2098_0.81 (3339)
## 52 1: 2098_0.81 (3339)
## 57 1: 2098_0.81 (3339)
# length(filtered_results)

We have 16 SNPs

# Assign unique numbers to each cluster and create labels
filtered_results <- filtered_results %>%
  mutate(Cluster_Number = as.numeric(as.factor(Cluster))) %>%
  arrange(Cluster_Number) %>%
  mutate(Cluster_Label = paste(Cluster_Number, "_", Cluster, " (", SNP_Count, ")", sep=""))

# Reorder the factor levels of Cluster_Label based on Cluster_Number
filtered_results$Cluster_Label <- factor(filtered_results$Cluster_Label,
                                            levels = unique(filtered_results$Cluster_Label[order(filtered_results$Cluster_Number)]))

# Plotting
ggplot(filtered_results, aes(xmin = Start, xmax = End, ymin = 0, ymax = Size)) +
  geom_rect(aes(fill = Cluster_Label), color = "black", linewidth = 0.2) +
  geom_text(aes(x = (Start + End) / 2, y = Size + 0.5, label = Cluster_Number), size = 3, vjust = -0.2) +
  scale_fill_discrete(name = "Cluster ID") +
  scale_x_continuous(labels = label_mb, breaks = pretty_breaks(n = 3)) +
  scale_y_continuous(labels = label_mb, breaks = pretty_breaks(n = 5)) +
  labs(x = "Position", y = "Cluster Size") +
  theme_minimal() +
  theme(
    axis.text.y = element_text(margin = margin(t = 0, r = 5, b = 0, l = 5)),
    axis.ticks.y = element_blank(),
    panel.grid.major.x = element_line(color = "gray", linetype = "dotted"),
    strip.background = element_rect(fill = "#e8e8e8", colour = NA),
    panel.grid.minor.x = element_blank(),
    legend.position = "right",
    panel.spacing.x = unit(1, "lines")
  ) +
  facet_grid(Population ~ Chromosome, scales = "fixed", space = "fixed")

# Use ggsave to save the plot as a PDF
ggsave(
  filename = here("output", "ldna", "AUT_NEW_overlapping_clusters_with_SNPs_on_DE_genes_intersect.pdf"),
  device = "pdf",
  width = 10,
  height = 5,
  units = "in"
)

This are the clusters for which we found the SNP as outlier and the gene the SNP in on is DE

Get the overlapping clusters

# Function to find overlapping clusters
find_overlapping_clusters <- function(population1, population2) {
  overlapping_clusters <- list()

  for (chr in unique(population1$Chromosome)) {
    pop1_chrom_clusters <- population1[population1$Chromosome == chr, ]
    pop2_chrom_clusters <- population2[population2$Chromosome == chr, ]

    overlapping_clusters_chr <- list()

    for (i in 1:nrow(pop1_chrom_clusters)) {
      for (j in 1:nrow(pop2_chrom_clusters)) {
        if (pop1_chrom_clusters$Start[i] <= pop2_chrom_clusters$End[j] &&
            pop1_chrom_clusters$End[i] >= pop2_chrom_clusters$Start[j]) {
          overlapping_clusters_chr <- c(overlapping_clusters_chr, list(pop1_chrom_clusters[i, ]))
          break
        }
      }
    }

    if (length(overlapping_clusters_chr) > 0) {
      overlapping_clusters[[chr]] <- do.call(rbind, overlapping_clusters_chr)
    }
  }

  do.call(rbind, overlapping_clusters)
}

# Subset data for NEW and AUT populations
albo_NEW <- subset(albo2, Population == "NEW")
albo_AUT <- subset(albo2, Population == "AUT")

# Find overlapping clusters between NEW and AUT
overlapping_clusters <- find_overlapping_clusters(albo_NEW, albo_AUT)

# View the overlapping clusters
overlapping_clusters
##     Population Chromosome   Cluster   r2 nSegments nSNPs     Start       End
## 66         NEW          1 2098_0.81 0.81       225  3339   1671072  16303506
## 76         NEW          1 2257_0.78 0.78        21    49  88409052  90437664
## 82         NEW          1 2098_0.81 0.81       225  3339 118382563 133808459
## 83         NEW          1 2098_0.81 0.81       225  3339 134311499 152641345
## 86         NEW          1 2098_0.81 0.81       225  3339 175562655 184285228
## 95         NEW          1 2098_0.81 0.81       225  3339 241581708 245964256
## 96         NEW          1 2098_0.81 0.81       225  3339 246194066 250559827
## 101        NEW          1 2098_0.81 0.81       225  3339 263937222 271417059
## 102        NEW          1 2098_0.81 0.81       225  3339 274018327 291005909
## 137        NEW          2  3393_0.8  0.8      1027  3235 100600773 102052192
## 142        NEW          2  3404_0.8  0.8       905  1502 129752494 132084935
## 144        NEW          2  3393_0.8  0.8      1027  3235 137286537 138732635
## 145        NEW          2  3404_0.8  0.8       905  1502 141517826 144428065
## 148        NEW          2  3404_0.8  0.8       905  1502 149738780 153093408
## 170        NEW          2  3404_0.8  0.8       905  1502 331446801 335080127
## 180        NEW          2  3404_0.8  0.8       905  1502 407185045 409429909
## 184        NEW          2  3404_0.8  0.8       905  1502 445321096 447678524
## 189        NEW          2  3393_0.8  0.8      1027  3235 522038684 523113152
## 196        NEW          3 2837_0.81 0.81       602  1668   8741873  16401719
## 203        NEW          3 2837_0.81 0.81       602  1668  57016980  58476637
## 205        NEW          3 2837_0.81 0.81       602  1668  66551211  68843398
## 206        NEW          3 2837_0.81 0.81       602  1668  70979233  76622043
## 213        NEW          3 2837_0.81 0.81       602  1668 110248778 112627576
## 227        NEW          3 2837_0.81 0.81       602  1668 197419987 208785670
## 229        NEW          3 2837_0.81 0.81       602  1668 212882391 219711403
## 230        NEW          3 2837_0.81 0.81       602  1668 220295952 223586999
## 236        NEW          3 2837_0.81 0.81       602  1668 252765819 258190930
## 238        NEW          3 2836_0.81 0.81       567  1122 266966314 268898469
## 239        NEW          3 2837_0.81 0.81       602  1668 269155423 274126769
## 246        NEW          3 2837_0.81 0.81       602  1668 296354064 298088579
## 254        NEW          3 2837_0.81 0.81       602  1668 339919173 342105404
## 255        NEW          3 2836_0.81 0.81       567  1122 344380481 345522786
## 257        NEW          3 2837_0.81 0.81       602  1668 380236644 381564291
## 261        NEW          3 2836_0.81 0.81       567  1122 437336558 438485871
##         Size Size (Mb)
## 66  14632434     14.63
## 76   2028612      2.03
## 82  15425896     15.43
## 83  18329846     18.33
## 86   8722573      8.72
## 95   4382548      4.38
## 96   4365761      4.37
## 101  7479837      7.48
## 102 16987582     16.99
## 137  1451419      1.45
## 142  2332441      2.33
## 144  1446098      1.45
## 145  2910239      2.91
## 148  3354628      3.35
## 170  3633326      3.63
## 180  2244864      2.24
## 184  2357428      2.36
## 189  1074468      1.07
## 196  7659846      7.66
## 203  1459657      1.46
## 205  2292187      2.29
## 206  5642810      5.64
## 213  2378798      2.38
## 227 11365683     11.37
## 229  6829012      6.83
## 230  3291047      3.29
## 236  5425111      5.43
## 238  1932155      1.93
## 239  4971346      4.97
## 246  1734515      1.73
## 254  2186231      2.19
## 255  1142305      1.14
## 257  1327647      1.33
## 261  1149313      1.15

Plot it

# Function to format numbers as Mb
label_mb <- function(x) {
  sprintf("%.0fMb", x / 1e6)
}

# Plot it
ggplot(overlapping_clusters, aes(xmin = Start, xmax = End, ymin = 0, ymax = Size)) +
  geom_rect(aes(fill = as.factor(Cluster)), color = "black", linewidth = 0.2) +
  scale_x_continuous(labels = label_mb, breaks = pretty_breaks(n = 3)) +
  scale_y_continuous(labels = label_mb, breaks = pretty_breaks(n = 5)) +
  labs(x = "Position", y = "Cluster Size") +
  theme_minimal() +
  theme(
    axis.text.y = element_text(margin = margin(t = 0, r = 5, b = 0, l = 5)),
    axis.ticks.y = element_blank(),
    panel.grid.major.x = element_line(color = "gray", linetype = "dotted"),
    strip.background = element_rect(fill = "#e8e8e8", colour = NA),
    panel.grid.minor.x = element_blank(),
    legend.position = "none",
    panel.spacing.x = unit(1, "lines")  # Adjust the unit and number to increase space as needed
  ) +
  facet_grid(Population ~ Chromosome, scales = "fixed", space = "fixed") +
  guides(fill = "none")

# Use ggsave to save the plot as a PDF
ggsave(
  filename = here("output", "ldna", "AUT_NEW_fixed_overlapping.pdf"),
  device = "pdf",
  width = 10,
  height = 5,
  units = "in"
)

8. Mapping back SNP clusters to scaffolds

Clean env and memory

# Remove all objects from the environment
rm(list = ls())

# Run the garbage collector to free up memory
gc()
##           used (Mb) gc trigger  (Mb) max used  (Mb)
## Ncells 1540503 82.3    3147227 168.1  3147227 168.1
## Vcells 2771221 21.2    8388608  64.0  5863028  44.8

I created the chromosomal scale for the linkage networks analysis, now we have to map back the SNPs from clusters of interest to the scaffold scale, then we can check the gene annotation file to find what genes are in the linkage group.

Cluster 14 is on chromosome 2 and only in the AUT line. I create files with the SNP ids, we can import it (on the legend is the cluster 14_3215_0.78 (18))

aut_ch2 <- readRDS(here("output", "ldna", "pop", "chr2", "AUT_clusters_snps.rds"))
str(aut_ch2$`3215_0.78`)
##  chr [1:4652] "AX-579450486" "AX-579456609" "AX-579459157" "AX-579459504" ...
# We have 566 SNPs on this cluster

Now we can import the bim file with the scaffolds. We can use the backup file we created when we created the chromosomal scale.

head output/quality_control/file7_backup.bim
## 1.1  AX-581444870    0   97856   C   T
## 1.1  AX-583035083    0   305518  A   G
## 1.1  AX-583035102    0   308124  A   G
## 1.1  AX-583033342    0   315059  C   G
## 1.1  AX-583035163    0   315386  A   G
## 1.1  AX-583033356    0   315674  C   T
## 1.1  AX-583033370    0   330057  G   A
## 1.1  AX-583035194    0   330265  A   G
## 1.1  AX-583035198    0   330908  G   T
## 1.1  AX-583033387    0   331288  C   T

Import it

# Import the function
source(
  here(
    "notebooks", "helpers", "import_bim.R")
)

# Import the data
snps <- import_bim(here("output", "quality_control", "file7_backup.bim"))

# Check it
head(snps)
## # A tibble: 6 x 6
##   Scaffold SNP             Cm Position Allele1 Allele2
##   <chr>    <chr>        <int>    <dbl> <chr>   <chr>  
## 1 1.1      AX-581444870     0    97856 C       T      
## 2 1.1      AX-583035083     0   305518 A       G      
## 3 1.1      AX-583035102     0   308124 A       G      
## 4 1.1      AX-583033342     0   315059 C       G      
## 5 1.1      AX-583035163     0   315386 A       G      
## 6 1.1      AX-583033356     0   315674 C       T

Now we can filter the 566 SNPs that are in the cluster and see what scaffolds there are located

cluster_14 <- snps[snps$SNP %in% aut_ch2$`3215_0.78`, ]
head(cluster_14) # 316 SNPs
## # A tibble: 6 x 6
##   Scaffold SNP             Cm Position Allele1 Allele2
##   <chr>    <chr>        <int>    <dbl> <chr>   <chr>  
## 1 2.22     AX-579450486     0  1538827 G       A      
## 2 2.22     AX-579456609     0  2793553 T       G      
## 3 2.22     AX-579459157     0  3278309 C       T      
## 4 2.22     AX-579459504     0  3384885 C       A      
## 5 2.22     AX-579459790     0  3448373 T       C      
## 6 2.22     AX-579461404     0  3490196 C       T
# Save it
saveRDS(
  cluster_14, here(
    "output", "ldna", "cluster_14.rds"
    )
  )

Create table

knitr::kable(cluster_14)
Scaffold SNP Cm Position Allele1 Allele2
2.22 AX-579450486 0 1538827 G A
2.22 AX-579456609 0 2793553 T G
2.22 AX-579459157 0 3278309 C T
2.22 AX-579459504 0 3384885 C A
2.22 AX-579459790 0 3448373 T C
2.22 AX-579461404 0 3490196 C T
2.22 AX-579461838 0 3576474 G A
2.22 AX-579461056 0 3629081 C T
2.22 AX-579461271 0 3653077 C T
2.22 AX-579462558 0 3681870 G A
2.22 AX-579463030 0 3776496 A G
2.22 AX-579463068 0 3777765 A T
2.22 AX-579463086 0 3791971 A C
2.22 AX-579463269 0 3848058 C T
2.22 AX-579462195 0 3852455 T C
2.22 AX-579463347 0 3852798 T G
2.22 AX-579462236 0 3859113 A G
2.22 AX-579463414 0 3861124 A G
2.22 AX-579463494 0 3881563 T C
2.22 AX-579463498 0 3882589 C T
2.22 AX-579463587 0 3895702 A G
2.22 AX-579462440 0 3895907 T C
2.22 AX-579463874 0 3956685 T C
2.22 AX-579462720 0 3966733 C T
2.22 AX-579463888 0 3975909 C T
2.22 AX-579464091 0 4020869 G T
2.22 AX-579463106 0 4064758 T C
2.22 AX-579463231 0 4094036 T C
2.22 AX-579464583 0 4107741 C G
2.22 AX-579464787 0 4212179 T C
2.22 AX-579464795 0 4226874 C T
2.22 AX-579463642 0 4262471 A G
2.22 AX-579465027 0 4314848 G A
2.22 AX-579463851 0 4326276 A G
2.22 AX-579463996 0 4330431 C T
2.22 AX-579465698 0 4393351 C T
2.22 AX-579465949 0 4467885 C G
2.22 AX-579466055 0 4480968 G C
2.22 AX-579466082 0 4489343 A C
2.22 AX-579465216 0 4574326 T C
2.22 AX-579465235 0 4574813 A G
2.22 AX-579466528 0 4587884 C G
2.22 AX-579466629 0 4598725 T C
2.22 AX-579465413 0 4599659 G T
2.22 AX-579465498 0 4629289 T A
2.22 AX-579465649 0 4656488 T A
2.22 AX-579466030 0 4689200 C T
2.22 AX-579468753 0 5318173 T C
2.22 AX-579469407 0 5510490 A G
2.22 AX-579469600 0 5515347 T C
2.22 AX-579471414 0 5609135 A G
2.22 AX-579471641 0 5644918 T A
2.22 AX-579471677 0 5645846 T C
2.22 AX-579471008 0 5770455 A G
2.22 AX-579471253 0 5803284 C T
2.22 AX-579471334 0 5819884 C G
2.22 AX-579471617 0 5832805 A G
2.22 AX-579471746 0 5843378 A G
2.22 AX-579472640 0 6037441 G T
2.22 AX-579474681 0 6123654 C T
2.22 AX-579474277 0 6162084 T C
2.22 AX-579475628 0 6170338 A G
2.22 AX-579475675 0 6171699 T C
2.22 AX-579474609 0 6173500 T A
2.22 AX-579475863 0 6177554 T C
2.22 AX-579477079 0 6337539 T C
2.22 AX-579477559 0 6419058 C T
2.22 AX-579480488 0 7113284 C T
2.22 AX-579480540 0 7114693 C T
2.22 AX-579481793 0 7166834 T C
2.22 AX-579481954 0 7473323 T C
2.22 AX-579483482 0 7806720 G A
2.22 AX-579486420 0 8074399 C T
2.22 AX-579486555 0 8186532 T C
2.22 AX-579486568 0 8187000 T G
2.22 AX-579490366 0 8853756 C T
2.22 AX-579489177 0 8870912 C G
2.22 AX-579489211 0 8871496 A T
2.22 AX-579489422 0 8894417 A C
2.22 AX-579492297 0 9511379 C T
2.22 AX-579491302 0 9574144 G C
2.22 AX-579493621 0 9747187 A C
2.22 AX-579493195 0 9878211 G A
2.22 AX-579494546 0 9881500 G A
2.22 AX-579497824 0 10502539 T C
2.22 AX-579498357 0 10769457 A C
2.22 AX-579498441 0 10771570 A G
2.22 AX-579499981 0 10805036 T C
2.22 AX-579500777 0 11005339 A G
2.22 AX-579499823 0 11078281 A G
2.22 AX-579501488 0 11165465 A C
2.22 AX-579501509 0 11486922 T C
2.22 AX-579501594 0 11500114 T A
2.22 AX-579501695 0 11518427 C T
2.22 AX-579503052 0 11521815 T C
2.22 AX-579503167 0 11811918 A G
2.22 AX-579504531 0 11812599 A G
2.22 AX-579505825 0 12322037 C T
2.22 AX-579506213 0 12415362 C G
2.27 AX-579514764 0 3143 A T
2.27 AX-579514780 0 3924 A T
2.27 AX-579514832 0 19391 T C
2.27 AX-579513447 0 19620 T C
2.27 AX-579515086 0 93803 G A
2.27 AX-579515106 0 94019 A G
2.27 AX-579516800 0 99017 A G
2.27 AX-579521449 0 1313549 C G
2.27 AX-579522956 0 1325963 C T
2.27 AX-579521736 0 1345366 G A
2.27 AX-579525293 0 1561644 C T
2.27 AX-579524022 0 1563849 T C
2.27 AX-579525501 0 1574245 A G
2.27 AX-579528877 0 2083014 A G
2.27 AX-579530612 0 2656493 A G
2.27 AX-579531606 0 3939052 T C
2.27 AX-579533372 0 4626751 T C
2.27 AX-579533771 0 5190811 T C
2.27 AX-579534229 0 5300874 A T
2.27 AX-579533798 0 5480935 A G
2.27 AX-579536022 0 5626846 T C
2.27 AX-579536107 0 5639527 C T
2.27 AX-579535050 0 5653677 G A
2.27 AX-579535085 0 5654174 G A
2.27 AX-579535400 0 5695991 T G
2.27 AX-579537686 0 6005191 G C
2.27 AX-579536482 0 6021051 C G
2.27 AX-579537999 0 6037155 T C
2.27 AX-579536746 0 6037770 G A
2.27 AX-579538092 0 6266645 G A
2.27 AX-579539919 0 6627914 A C
2.27 AX-579544247 0 7482829 T C
2.27 AX-579545575 0 7483880 T G
2.27 AX-579546394 0 7837393 A G
2.27 AX-579548165 0 7946950 T C
2.27 AX-579548233 0 7958035 A C
2.27 AX-579547370 0 8017758 C T
2.27 AX-579547376 0 8017979 G C
2.32 AX-579553752 0 2293932 T C
2.36 AX-579554924 0 122073 T C
2.36 AX-579555022 0 122978 A G
2.36 AX-579554313 0 177712 G A
2.36 AX-579554700 0 233093 G A
2.36 AX-579555223 0 316293 G C
2.36 AX-579556352 0 966741 A G
2.36 AX-579556686 0 1233251 A G
2.36 AX-579558663 0 1923334 G A
2.36 AX-579560288 0 1988456 G A
2.36 AX-579560662 0 2006981 C T
2.36 AX-579559480 0 2010914 A C
2.36 AX-579560965 0 2023214 A G
2.36 AX-579559703 0 2024629 A G
2.36 AX-579561712 0 2103073 A G
2.36 AX-579560506 0 2117189 G T
2.36 AX-579560776 0 2159466 C T
2.36 AX-579561033 0 2360547 T G
2.36 AX-579561118 0 2375602 A C
2.36 AX-579561629 0 2476040 T C
2.36 AX-579562408 0 2742031 C T
2.36 AX-579564629 0 2977222 C T
2.36 AX-579563611 0 3039586 G A
2.36 AX-579564982 0 3063901 G A
2.36 AX-579565814 0 3225773 A G
2.36 AX-579566111 0 3499011 G A
2.36 AX-579566130 0 3499260 T G
2.36 AX-579566954 0 3775104 A G
2.36 AX-579568657 0 3855224 G T
2.36 AX-579568951 0 3924403 T G
2.36 AX-579571176 0 4429492 C T
2.36 AX-579571698 0 4506577 G A
2.36 AX-579570501 0 4525604 A G
2.36 AX-579571897 0 4525871 A G
2.36 AX-579571606 0 4722263 G T
2.36 AX-579573008 0 4722473 G A
2.36 AX-579571640 0 4722920 T G
2.36 AX-579571741 0 4732500 T C
2.36 AX-579571778 0 4733288 C A
2.36 AX-579571885 0 4735900 T G
2.36 AX-579572182 0 4771453 T C
2.36 AX-579572348 0 4773049 A G
2.36 AX-579575218 0 4954626 T C
2.36 AX-579574417 0 5093568 A C
2.36 AX-579576372 0 5174565 G C
2.36 AX-579575014 0 5182212 G A
2.36 AX-579576494 0 5195710 A G
2.36 AX-579576620 0 5220518 C T
2.36 AX-579575264 0 5221019 T C
2.36 AX-579576700 0 5232144 T A
2.36 AX-579578676 0 5871547 C A
2.36 AX-579578960 0 5905142 A C
2.36 AX-579579230 0 5977765 C T
2.36 AX-579580763 0 6013782 A T
2.36 AX-579579387 0 6044877 A G
2.36 AX-579579808 0 6131092 C T
2.36 AX-579580108 0 6183757 G A
2.36 AX-579580428 0 6264997 T G
2.36 AX-579582210 0 6366093 C T
2.36 AX-579580737 0 6374847 C T
2.36 AX-579580782 0 6399348 G C
2.36 AX-579582325 0 6409036 C T
2.36 AX-579582344 0 6424187 G T
2.36 AX-579582396 0 6451287 A G
2.36 AX-579581051 0 6471713 G A
2.36 AX-579582591 0 6471983 T C
2.36 AX-579581085 0 6472487 G A
2.36 AX-579582777 0 6484066 T A
2.36 AX-579581842 0 6601224 T C
2.36 AX-579582896 0 6815052 T C
2.36 AX-579584795 0 6840596 T G
2.36 AX-579585088 0 6864648 G A
2.36 AX-579585247 0 6884462 A G
2.36 AX-579585413 0 6913172 G A
2.36 AX-579585547 0 6979869 A G
2.36 AX-579584631 0 7310698 G A
2.36 AX-579586817 0 7524611 C T
2.36 AX-579587054 0 7555928 A T
2.36 AX-579587132 0 7558468 A G
2.36 AX-579585816 0 7598150 T C
2.36 AX-579586639 0 7738695 A G
2.36 AX-579588150 0 7739652 G A
2.36 AX-579588280 0 7769335 C T
2.36 AX-579588443 0 7785936 G T
2.36 AX-579588768 0 7842421 C A
2.36 AX-579588851 0 7847327 C T
2.36 AX-579589139 0 7863252 C T
2.36 AX-579589153 0 7863471 A G
2.36 AX-579589060 0 8163406 G A
2.36 AX-579591399 0 8368205 T C
2.38 AX-579592977 0 33173 T C
2.38 AX-579591594 0 105906 T G
2.38 AX-579591718 0 138430 C T
2.38 AX-579593540 0 225032 T G
2.38 AX-579593773 0 307640 A G
2.38 AX-579592307 0 307963 T C
2.38 AX-579593876 0 332935 A C
2.38 AX-579592773 0 808486 T C
2.38 AX-579594255 0 808711 C T
2.38 AX-579593116 0 904947 A G
2.38 AX-579593401 0 1012549 C T
2.38 AX-579595097 0 1068943 A G
2.38 AX-579593808 0 1253358 C T
2.38 AX-579595377 0 1435426 A G
2.38 AX-579595568 0 1546396 C T
2.38 AX-579594123 0 1555916 G A
2.38 AX-579595641 0 1582105 A G
2.38 AX-579595929 0 1745456 T G
2.38 AX-579595131 0 1890450 C G
2.38 AX-579596432 0 2092493 T C
2.38 AX-579596460 0 2093042 A C
2.38 AX-579596587 0 2133235 A G
2.38 AX-579597760 0 2412837 G A
2.38 AX-579600249 0 2643323 G C
2.38 AX-579598791 0 2679893 C A
2.38 AX-579598805 0 2680377 A G
2.38 AX-579600802 0 2768268 A G
2.38 AX-579599348 0 2769292 A G
2.38 AX-579601954 0 3420287 C T
2.38 AX-579603787 0 3855171 T C
2.38 AX-579605417 0 3866591 C T
2.38 AX-579605652 0 3869671 G A
2.38 AX-579604364 0 3875395 A G
2.38 AX-579605090 0 4025293 T C
2.38 AX-579605155 0 4034581 T C
2.38 AX-579605258 0 4059911 G C
2.38 AX-579605997 0 4217138 T C
2.38 AX-579606037 0 4233102 T C
2.38 AX-579606082 0 4261878 A C
2.38 AX-579606324 0 4321442 C T
2.38 AX-579606484 0 4362367 T C
2.38 AX-579607967 0 4365528 A G
2.40 AX-579609322 0 7757 T G
2.40 AX-579607994 0 22781 C T
2.40 AX-579608017 0 23519 A C
2.40 AX-579608372 0 63601 T C
2.40 AX-579609853 0 63812 A G
2.40 AX-579608454 0 66117 A G
2.40 AX-579610235 0 117199 C T
2.40 AX-579610308 0 269848 A G
2.40 AX-579610990 0 314487 T C
2.40 AX-579610992 0 314726 G A
2.40 AX-579609938 0 391207 A G
2.40 AX-579611740 0 476232 G A
2.40 AX-579611969 0 535273 G A
2.40 AX-579610498 0 542557 C A
2.40 AX-579612052 0 555521 C A
2.40 AX-579610585 0 556029 T C
2.40 AX-579612219 0 589415 T C
2.40 AX-579610845 0 611373 T C
2.40 AX-579611070 0 637163 A T
2.40 AX-579612655 0 659602 C A
2.40 AX-579611192 0 668647 T G
2.40 AX-579613074 0 747694 A G
2.40 AX-579613337 0 825033 T G
2.40 AX-579613453 0 836220 G C
2.40 AX-579612387 0 894866 A G
2.40 AX-579612453 0 896032 G A
2.40 AX-579614469 0 1027270 C T
2.40 AX-579612967 0 1027987 A G
2.40 AX-579613089 0 1031879 G A
2.40 AX-579614661 0 1032599 G A
2.40 AX-579614672 0 1032875 A G
2.40 AX-579613249 0 1035703 G A
2.40 AX-579615168 0 1075172 G C
2.40 AX-579615300 0 1086477 A G
2.40 AX-579615327 0 1087851 A G
2.40 AX-579615710 0 1164681 G A
2.40 AX-579616152 0 1273262 C A
2.40 AX-579616931 0 1384904 C T
2.40 AX-579615476 0 1387460 T C
2.40 AX-579615529 0 1407741 T C
2.40 AX-579617096 0 1410210 T A
2.40 AX-579617194 0 1412279 A G
2.40 AX-579615703 0 1412642 A G
2.40 AX-579617599 0 1468946 A C
2.40 AX-579617368 0 1651660 T C
2.40 AX-579619072 0 1665441 T C
2.40 AX-579617534 0 1665651 A C
2.40 AX-579618424 0 1904505 G T
2.40 AX-579619997 0 1910958 T C
2.40 AX-579620126 0 1955336 T C
2.40 AX-579618630 0 1972960 C T
2.40 AX-579618760 0 1995445 A G
2.40 AX-579620866 0 2124643 G A
2.40 AX-579619588 0 2186172 A G
2.40 AX-579619840 0 2225961 T C

Let’s check cluster 6_19_42 on AUT chr1

aut_ch1 <- readRDS(here("output", "ldna", "pop", "chr1", "AUT_clusters_snps.rds"))
str(aut_ch1$`1971_0.64`)
##  chr [1:1579] "AX-583500511" "AX-583500520" "AX-583503153" "AX-583500379" ...
# We have 566 SNPs on this cluster
cluster_6 <- snps[snps$SNP %in% aut_ch1$`1971_0.64`, ]
head(cluster_6) # 316 SNPs
## # A tibble: 6 x 6
##   Scaffold SNP             Cm Position Allele1 Allele2
##   <chr>    <chr>        <int>    <dbl> <chr>   <chr>  
## 1 1.152    AX-583500511     0   339500 G       C      
## 2 1.152    AX-583500520     0   345870 A       G      
## 3 1.152    AX-583503153     0   977944 T       G      
## 4 1.152    AX-583500379     0   978467 G       T      
## 5 1.152    AX-583503258     0   987625 C       T      
## 6 1.152    AX-583504302     0  1149628 A       G
# Save it
saveRDS(
  cluster_6, here(
    "output", "ldna", "cluster_6.rds"
    )
  )

Create table

knitr::kable(cluster_6)
Scaffold SNP Cm Position Allele1 Allele2
1.152 AX-583500511 0 339500 G C
1.152 AX-583500520 0 345870 A G
1.152 AX-583503153 0 977944 T G
1.152 AX-583500379 0 978467 G T
1.152 AX-583503258 0 987625 C T
1.152 AX-583504302 0 1149628 A G
1.152 AX-583503279 0 1491640 G C
1.152 AX-583503440 0 1535179 A G
1.152 AX-583503906 0 1564958 T C
1.152 AX-583506777 0 1565216 C G
1.152 AX-583504295 0 1602992 G A
1.152 AX-583507197 0 1603711 G T
1.152 AX-583507425 0 1627574 G C
1.152 AX-583507639 0 1644594 T C
1.152 AX-583507878 0 1664015 G A
1.152 AX-583505073 0 1664826 T C
1.152 AX-583508045 0 1665840 C A
1.152 AX-583508498 0 1669803 T C
1.152 AX-583508594 0 1670439 G A
1.152 AX-583508771 0 1673676 T C
1.152 AX-583505971 0 1673937 C T
1.152 AX-583506892 0 1803316 G C
1.152 AX-583511182 0 1955809 A G
1.152 AX-583508519 0 1966895 C T
1.152 AX-583508573 0 1971684 T C
1.152 AX-583509292 0 2556372 T C
1.152 AX-583509391 0 2580817 T C
1.152 AX-583509412 0 2581274 A G
1.152 AX-583509733 0 2598090 G T
1.152 AX-583512511 0 2598734 A G
1.152 AX-583510076 0 2619288 C A
1.152 AX-583512897 0 2625567 G C
1.152 AX-583513248 0 2655189 T G
1.152 AX-583513308 0 2666934 T G
1.152 AX-583513359 0 2668625 A G
1.152 AX-583513377 0 2668892 A G
1.152 AX-583510794 0 2701081 A G
1.152 AX-583510903 0 2706078 C T
1.152 AX-583513648 0 2725250 C T
1.152 AX-583511483 0 2848038 G A
1.152 AX-583514235 0 2848259 T G
1.152 AX-583514359 0 2888182 A G
1.152 AX-583514397 0 2900388 A G
1.152 AX-583511792 0 2913109 T C
1.152 AX-583512377 0 3056913 G A
1.152 AX-583512666 0 3117848 C A
1.152 AX-583515819 0 3174781 A G
1.152 AX-583513555 0 3382178 T C
1.152 AX-583513773 0 3464938 G A
1.152 AX-583517814 0 3775044 T C
1.152 AX-583519450 0 4112020 T C
1.152 AX-583516942 0 4210175 A G
1.152 AX-583517794 0 4432697 G T
1.152 AX-583521326 0 5078954 C A
1.152 AX-583521530 0 5098043 T C
1.152 AX-583525764 0 5184643 T C
1.152 AX-583527475 0 5423719 G T
1.152 AX-583527695 0 5449431 G A
1.152 AX-583526378 0 5567513 G A
1.152 AX-583529290 0 5577967 C T
1.152 AX-583530453 0 5927434 T C
1.152 AX-583528261 0 6093153 A G
1.152 AX-583531073 0 6102875 T C
1.152 AX-583528689 0 6128044 T C
1.152 AX-583530679 0 6478224 G A
1.152 AX-583533848 0 6554566 A G
1.152 AX-583532801 0 6856535 T A
1.152 AX-583533673 0 7055034 T C
1.152 AX-583536382 0 7055500 A G
1.152 AX-583536435 0 7055779 C T
1.152 AX-583534984 0 7166546 A C
1.152 AX-583535005 0 7166793 G C
1.152 AX-583537871 0 7168782 T C
1.152 AX-583538179 0 7190150 A G
1.152 AX-583536224 0 7313002 A G
1.152 AX-583538596 0 7562522 C T
1.152 AX-583539329 0 7641844 T C
1.152 AX-583539476 0 7657914 T G
1.152 AX-583542283 0 7660371 C T
1.152 AX-583542500 0 7716798 G A
1.152 AX-583542697 0 7766866 T C
1.154 AX-583542132 0 533422 G A
1.156 AX-583547648 0 11722 C G
1.156 AX-583544949 0 74115 C T
1.170 AX-583556250 0 197780 A C
1.170 AX-583560886 0 919858 C T
1.170 AX-583571044 0 1622696 T C
1.170 AX-583571065 0 1623404 T G
1.170 AX-583571174 0 1631928 A G
1.170 AX-583571507 0 2091806 A C
cluster_6_all <- snps[snps$Scaffold %in% cluster_6$Scaffold, ]
head(cluster_6_all)
## # A tibble: 6 x 6
##   Scaffold SNP             Cm Position Allele1 Allele2
##   <chr>    <chr>        <int>    <dbl> <chr>   <chr>  
## 1 1.152    AX-583496546     0    28493 T       C      
## 2 1.152    AX-583496607     0    43318 A       G      
## 3 1.152    AX-583499574     0    96763 G       A      
## 4 1.152    AX-583499672     0   132155 T       G      
## 5 1.152    AX-583499700     0   136348 A       G      
## 6 1.152    AX-583499977     0   188640 T       A

However we need the other SNPs to know if the blocks are connected or not. Now we know what scaffolds the SNPs are located and we can check it

cluster_14_all <- snps[snps$Scaffold %in% cluster_14$Scaffold, ]
head(cluster_14_all)
## # A tibble: 6 x 6
##   Scaffold SNP             Cm Position Allele1 Allele2
##   <chr>    <chr>        <int>    <dbl> <chr>   <chr>  
## 1 2.22     AX-579445128     0    49751 G       A      
## 2 2.22     AX-579445195     0    64564 A       T      
## 3 2.22     AX-579443938     0   100572 A       G      
## 4 2.22     AX-579445430     0   157266 G       T      
## 5 2.22     AX-579445592     0   202795 T       A      
## 6 2.22     AX-579444301     0   212193 C       T

Now we can create a new column and tag the SNPs that are linked

# Creating the new column 'linked'
cluster_14_all <- cluster_14_all %>%
  mutate(linked = SNP %in% aut_ch2$`3215_0.78`)
head(cluster_14_all)
## # A tibble: 6 x 7
##   Scaffold SNP             Cm Position Allele1 Allele2 linked
##   <chr>    <chr>        <int>    <dbl> <chr>   <chr>   <lgl> 
## 1 2.22     AX-579445128     0    49751 G       A       FALSE 
## 2 2.22     AX-579445195     0    64564 A       T       FALSE 
## 3 2.22     AX-579443938     0   100572 A       G       FALSE 
## 4 2.22     AX-579445430     0   157266 G       T       FALSE 
## 5 2.22     AX-579445592     0   202795 T       A       FALSE 
## 6 2.22     AX-579444301     0   212193 C       T       FALSE
# Creating the new column 'linked'
cluster_6_all <- cluster_6_all %>%
  mutate(linked = SNP %in% aut_ch1$`1971_0.64`)
head(cluster_6_all)
## # A tibble: 6 x 7
##   Scaffold SNP             Cm Position Allele1 Allele2 linked
##   <chr>    <chr>        <int>    <dbl> <chr>   <chr>   <lgl> 
## 1 1.152    AX-583496546     0    28493 T       C       FALSE 
## 2 1.152    AX-583496607     0    43318 A       G       FALSE 
## 3 1.152    AX-583499574     0    96763 G       A       FALSE 
## 4 1.152    AX-583499672     0   132155 T       G       FALSE 
## 5 1.152    AX-583499700     0   136348 A       G       FALSE 
## 6 1.152    AX-583499977     0   188640 T       A       FALSE

Lets check how many linked SNPs we have and how many aren’t

count_linked <- cluster_14_all %>%
  group_by(linked) %>%
  summarise(count = n())
count_linked
## # A tibble: 2 x 2
##   linked count
##   <lgl>  <int>
## 1 FALSE   2649
## 2 TRUE     324
count_linked <- cluster_6_all %>%
  group_by(linked) %>%
  summarise(count = n())
count_linked
## # A tibble: 2 x 2
##   linked count
##   <lgl>  <int>
## 1 FALSE   1268
## 2 TRUE      90

Get the 157 outliers

snps_157 <-
  read.table(
    here("output", "pcadapt", "outlier_157_SNPs.txt"),
    stringsAsFactors = FALSE
    )

# Get the 157 SNPs
snps_157b <- cluster_14_all |>
  filter(SNP %in% snps_157$V1)

head(snps_157b)
## # A tibble: 6 x 7
##   Scaffold SNP             Cm Position Allele1 Allele2 linked
##   <chr>    <chr>        <int>    <dbl> <chr>   <chr>   <lgl> 
## 1 2.22     AX-579474142     0  6075268 T       C       FALSE 
## 2 2.22     AX-579474650     0  6122430 A       G       FALSE 
## 3 2.22     AX-579509845     0 13142038 A       T       FALSE 
## 4 2.36     AX-579556477     0  1062758 T       C       FALSE 
## 5 2.36     AX-579560686     0  2139679 C       T       FALSE 
## 6 2.36     AX-579564292     0  2894537 A       G       FALSE
# Get the 157 SNPs
snps_157c <- cluster_6_all |>
  filter(SNP %in% snps_157$V1)

head(snps_157c)
## # A tibble: 6 x 7
##   Scaffold SNP             Cm Position Allele1 Allele2 linked
##   <chr>    <chr>        <int>    <dbl> <chr>   <chr>   <lgl> 
## 1 1.152    AX-583504302     0  1149628 A       G       TRUE  
## 2 1.152    AX-583514148     0  2847295 T       C       FALSE 
## 3 1.152    AX-583515734     0  3903190 A       G       FALSE 
## 4 1.152    AX-583518586     0  3966380 T       C       FALSE 
## 5 1.152    AX-583518994     0  4024772 A       G       FALSE 
## 6 1.152    AX-583516491     0  4057192 T       C       FALSE

Plot it

# Function to format numbers as Mb
label_mb <- function(x) {
  sprintf("%.0fMb", x / 1e6)
}


# Calculate the start and end positions for each stretch of TRUE or FALSE
rect_data <- cluster_14_all %>%
  arrange(Scaffold, Position) %>%
  mutate(change = linked != lag(linked, default = first(linked))) %>%
  group_by(Scaffold) %>%
  mutate(group_id = cumsum(change)) %>%
  group_by(Scaffold, group_id, linked) %>%
  summarize(start = min(Position), end = max(Position), .groups = 'drop') %>%
  ungroup()

# Plotting using geom_rect
ggplot(rect_data, aes(xmin = start, xmax = end, ymin = as.numeric(Scaffold) - 0.1, ymax = as.numeric(Scaffold) + 0.1)) +
  # Add a background layer for each scaffold with gray color
  geom_rect(data = rect_data %>% group_by(Scaffold) %>%
            summarize(start = min(start), end = max(end), .groups = 'drop'),
            aes(xmin = start, xmax = end, ymin = as.numeric(Scaffold) - 0.1, ymax = as.numeric(Scaffold) + 0.1),
            fill = "gray", inherit.aes = FALSE) +
  # Add the TRUE stretches
  geom_rect(aes(fill = linked), data = rect_data %>% filter(linked == TRUE)) +
  scale_fill_manual(values = c("TRUE" = "green")) +
  facet_wrap(~ Scaffold, scales = "fixed", ncol = 1) +
  geom_vline(data = snps_157b, aes(xintercept = Position, color = SNP), linetype = "solid", color = "red") +
  geom_text(data = snps_157b, aes(x = Position, y = as.numeric(Scaffold) + 0.2, label = SNP), inherit.aes = FALSE, angle = 90, vjust = 0, size = 2, check_overlap = TRUE) +
  theme_minimal() +
  theme(
    axis.title.y = element_blank(),
    axis.text.y = element_blank(),
    axis.ticks.y = element_blank(),
    panel.spacing = unit(0.1, "lines"),
    panel.grid.major = element_blank(),
    panel.grid.minor = element_blank(),
    strip.text = element_text(size = 12, face = "bold", hjust = 0.5, margin = margin(t = 1, b = 5))
  ) +
  scale_x_continuous(labels = label_mb, breaks = scales::pretty_breaks(n = 6)) +
  labs(x = "Position", title = "Continuous Stretches of Linked SNPs by Scaffold - Cluster 14 AUT", fill = "Linked") +
  guides(fill = "none")

# Save Venn diagram to PDF
output_path <- here("output", "ldna", "figures", "cluster_14_aut_scaffolds.pdf")
ggsave(output_path, height = 5, width = 8, units = "in")

To make sure the windows appear even if they are small

# Set a minimum width for the windows
min_width <- 1e4  # for example, 1 million base pairs

# Adjust the rect_data calculation
rect_data <- cluster_14_all %>%
  arrange(Scaffold, Position) %>%
  mutate(change = linked != lag(linked, default = first(linked))) %>%
  group_by(Scaffold) %>%
  mutate(group_id = cumsum(change)) %>%
  group_by(Scaffold, group_id, linked) %>%
  summarize(start = min(Position), end = max(Position), .groups = 'drop') %>%
  ungroup() %>%
  mutate(width = end - start,
         adjusted_end = ifelse(width < min_width, start + min_width, end))

# Plotting using geom_rect with adjusted ends
ggplot(rect_data, aes(xmin = start, xmax = adjusted_end, ymin = as.numeric(Scaffold) - 0.1, ymax = as.numeric(Scaffold) + 0.1)) +
  geom_rect(data = rect_data %>% group_by(Scaffold) %>%
            summarize(start = min(start), end = max(adjusted_end), .groups = 'drop'),
            aes(xmin = start, xmax = end, ymin = as.numeric(Scaffold) - 0.1, ymax = as.numeric(Scaffold) + 0.1),
            fill = "gray", inherit.aes = FALSE) +
  geom_rect(aes(fill = linked), data = rect_data %>% filter(linked == TRUE)) +
  scale_fill_manual(values = c("TRUE" = "green")) +
  facet_wrap(~ Scaffold, scales = "fixed", ncol = 1) +
  theme_minimal() +
  geom_vline(data = snps_157b, aes(xintercept = Position, color = SNP), linetype = "solid", color = "red") +
  geom_text_repel(data = snps_157b, aes(x = Position, y = as.numeric(Scaffold) + 0.2, label = SNP),
                  inherit.aes = FALSE, angle = 45, size = 2,
                  nudge_y = 0.1,   # Adjust this value as needed
                  direction = "y") +
  theme(axis.title.y = element_blank(), axis.text.y = element_blank(), axis.ticks.y = element_blank(), panel.spacing = unit(0.1, "lines"),
        panel.grid.major = element_blank(), panel.grid.minor = element_blank(),
        strip.text = element_text(size = 12, face = "bold", hjust = 0.5)) +
  scale_x_continuous(labels = label_mb, breaks = scales::pretty_breaks(n = 6)) +
  labs(x = "Position", title = "Continuous Stretches of Linked SNPs by Scaffold - Cluster 14 AUT", fill = "Linked") +
  guides(fill = "none")
## Warning: ggrepel: Repulsion works correctly only for rotation angles multiple of 90 degrees
## ggrepel: Repulsion works correctly only for rotation angles multiple of 90 degrees
## ggrepel: Repulsion works correctly only for rotation angles multiple of 90 degrees
## ggrepel: Repulsion works correctly only for rotation angles multiple of 90 degrees

# Save Venn diagram to PDF
output_path <- here("output", "ldna", "figures", "cluster_14_aut_scaffolds_with_small_windows.pdf")
ggsave(output_path, height = 5, width = 8, units = "in")
## Warning: ggrepel: Repulsion works correctly only for rotation angles multiple of 90 degrees
## ggrepel: Repulsion works correctly only for rotation angles multiple of 90 degrees
## ggrepel: Repulsion works correctly only for rotation angles multiple of 90 degrees
## ggrepel: Repulsion works correctly only for rotation angles multiple of 90 degrees

Cluster 6

# Function to format numbers as Mb
label_mb <- function(x) {
  sprintf("%.0fMb", x / 1e6)
}


# Calculate the start and end positions for each stretch of TRUE or FALSE
rect_data <- cluster_6_all %>%
  arrange(Scaffold, Position) %>%
  mutate(change = linked != lag(linked, default = first(linked))) %>%
  group_by(Scaffold) %>%
  mutate(group_id = cumsum(change)) %>%
  group_by(Scaffold, group_id, linked) %>%
  summarize(start = min(Position), end = max(Position), .groups = 'drop') %>%
  ungroup()

# Plotting using geom_rect
ggplot(rect_data, aes(xmin = start, xmax = end, ymin = as.numeric(Scaffold) - 0.1, ymax = as.numeric(Scaffold) + 0.1)) +
  # Add a background layer for each scaffold with gray color
  geom_rect(data = rect_data %>% group_by(Scaffold) %>%
            summarize(start = min(start), end = max(end), .groups = 'drop'),
            aes(xmin = start, xmax = end, ymin = as.numeric(Scaffold) - 0.1, ymax = as.numeric(Scaffold) + 0.1),
            fill = "gray", inherit.aes = FALSE) +
  # Add the TRUE stretches
  geom_rect(aes(fill = linked), data = rect_data %>% filter(linked == TRUE)) +
  scale_fill_manual(values = c("TRUE" = "green")) +
  facet_wrap(~ Scaffold, scales = "fixed", ncol = 1) +
  geom_vline(data = snps_157c, aes(xintercept = Position, color = SNP), linetype = "solid", color = "red") +
  geom_text(data = snps_157c, aes(x = Position, y = as.numeric(Scaffold) + 0.2, label = SNP), inherit.aes = FALSE, angle = 90, vjust = 0, size = 2, check_overlap = TRUE) +
  theme_minimal() +
  theme(
    axis.title.y = element_blank(),
    axis.text.y = element_blank(),
    axis.ticks.y = element_blank(),
    panel.spacing = unit(0.1, "lines"),
    panel.grid.major = element_blank(),
    panel.grid.minor = element_blank(),
    strip.text = element_text(size = 12, face = "bold", hjust = 0.5, margin = margin(t = 1, b = 5))
  ) +
  scale_x_continuous(labels = label_mb, breaks = scales::pretty_breaks(n = 6)) +
  labs(x = "Position", title = "Continuous Stretches of Linked SNPs by Scaffold - Cluster 6 AUT", fill = "Linked") +
  guides(fill = "none")

# Save Venn diagram to PDF
output_path <- here("output", "ldna", "figures", "cluster_6_aut_scaffolds.pdf")
ggsave(output_path, height = 5, width = 8, units = "in")

To make sure the windows appear even if they are small

# Set a minimum width for the windows
min_width <- 1e4  # for example, 1 million base pairs

# Adjust the rect_data calculation
rect_data <- cluster_6_all %>%
  arrange(Scaffold, Position) %>%
  mutate(change = linked != lag(linked, default = first(linked))) %>%
  group_by(Scaffold) %>%
  mutate(group_id = cumsum(change)) %>%
  group_by(Scaffold, group_id, linked) %>%
  summarize(start = min(Position), end = max(Position), .groups = 'drop') %>%
  ungroup() %>%
  mutate(width = end - start,
         adjusted_end = ifelse(width < min_width, start + min_width, end))

# Plotting using geom_rect with adjusted ends
ggplot(rect_data, aes(xmin = start, xmax = adjusted_end, ymin = as.numeric(Scaffold) - 0.1, ymax = as.numeric(Scaffold) + 0.1)) +
  geom_rect(data = rect_data %>% group_by(Scaffold) %>%
            summarize(start = min(start), end = max(adjusted_end), .groups = 'drop'),
            aes(xmin = start, xmax = end, ymin = as.numeric(Scaffold) - 0.1, ymax = as.numeric(Scaffold) + 0.1),
            fill = "gray", inherit.aes = FALSE) +
  geom_rect(aes(fill = linked), data = rect_data %>% filter(linked == TRUE)) +
  scale_fill_manual(values = c("TRUE" = "green")) +
  facet_wrap(~ Scaffold, scales = "fixed", ncol = 1) +
  theme_minimal() +
  geom_vline(data = snps_157c, aes(xintercept = Position, color = SNP), linetype = "solid", color = "red") +
  geom_text_repel(data = snps_157c, aes(x = Position, y = as.numeric(Scaffold) + 0.2, label = SNP),
                  inherit.aes = FALSE, angle = 45, size = 2,
                  nudge_y = 0.1,   # Adjust this value as needed
                  direction = "y") +
  theme(axis.title.y = element_blank(), axis.text.y = element_blank(), axis.ticks.y = element_blank(), panel.spacing = unit(0.1, "lines"),
        panel.grid.major = element_blank(), panel.grid.minor = element_blank(),
        strip.text = element_text(size = 12, face = "bold", hjust = 0.5)) +
  scale_x_continuous(labels = label_mb, breaks = scales::pretty_breaks(n = 6)) +
  labs(x = "Position", title = "Continuous Stretches of Linked SNPs by Scaffold - Cluster 6 AUT", fill = "Linked") +
  guides(fill = "none")
## Warning: ggrepel: Repulsion works correctly only for rotation angles multiple
## of 90 degrees

# Save Venn diagram to PDF
output_path <- here("output", "ldna", "figures", "cluster_6_aut_scaffolds_with_small_windows.pdf")
ggsave(output_path, height = 5, width = 8, units = "in")
## Warning: ggrepel: Repulsion works correctly only for rotation angles multiple
## of 90 degrees

Calculate size

# Calculate the length of each stretch
rect_data <- rect_data %>%
  mutate(length = end - start + 1)

# Calculate average sizes for TRUE and FALSE stretches
average_sizes <- rect_data %>%
  group_by(linked) %>%
  summarize(average_size = mean(length))

# Display the average sizes
print(average_sizes)
## # A tibble: 2 x 2
##   linked average_size
##   <lgl>         <dbl>
## 1 FALSE       132584.
## 2 TRUE          1015.

We can also check what genes are in these scaffolds.

Import the data

snps_genes_chr <- readRDS(here("output", "ldna", "snps_genes_chr.rds"))

The expression data

gene_expression <- read_delim(here("data", "files","MANvsAUTO_sig_mRNAs.csv"), delim = ",", col_names = TRUE, show_col_types = FALSE) |>
  dplyr::select(
    gene,log2FoldChange
  ) |>
  dplyr::rename(
    Gene_ID = gene
  )
head(gene_expression)
## # A tibble: 6 x 2
##   Gene_ID      log2FoldChange
##   <chr>                 <dbl>
## 1 LOC115262812         -10.5 
## 2 LOC109401291          -9.19
## 3 LOC109397830           8.73
## 4 LOC115264022           8.56
## 5 LOC115260314           8.51
## 6 LOC115258723           8.39

For example, what genes are in the cluster 14, so we can check what genes are in these scaffolds or more specifically what SNPs are in the genes

First, what genes are in these scaffolds?

cluster_14_genes <- snps_genes_chr[snps_genes_chr$Scaffold %in% cluster_14_all$Scaffold, ]

# How many genes in the scaffolds
length(unique(cluster_14_genes$Gene_ID))
## [1] 321

Now lets check what genes have the linked SNPs

cluster_14_snps2 <- snps_genes_chr[snps_genes_chr$SNP %in% aut_ch2$`3215_0.78`, ]

# How many genes with SNPs from the ld block
length(unique(cluster_14_snps2$Gene_ID))
## [1] 109

Save as Excel file

# Save the data frame to an Excel file
write_xlsx(cluster_14_snps2, here("output", "ldna", "cluster_14_snps2.xlsx"))

We can count how many linked SNPs per gene

# Count the number of SNPs for each Gene_ID
snp_count_per_gene <- cluster_14_snps2 %>%
  group_by(Gene_ID) %>%
  summarise(SNP_count = n())

# View the first few rows of the result
head(snp_count_per_gene)
## # A tibble: 6 x 2
##   Gene_ID      SNP_count
##   <chr>            <int>
## 1 LOC109397326         1
## 2 LOC109397337         1
## 3 LOC109397339         1
## 4 LOC109397340         1
## 5 LOC109397344         2
## 6 LOC109397369         4

Save as Excel file

# Save the data frame to an Excel file
write_xlsx(snp_count_per_gene, here("output", "ldna", "snp_count_per_gene.xlsx"))

Now we can check how many DE genes are among the 109 genes that have SNPs on the cluster 14

cluster_14_snps3 <- cluster_14_snps2[cluster_14_snps2$Gene_ID %in% gene_expression$Gene_ID, ]

# How many genes in the scaffolds
length(unique(cluster_14_snps3$Gene_ID))
## [1] 3
unique(cluster_14_snps3$Gene_ID)
## [1] "LOC109415738" "LOC109415743" "LOC109414739"

Get the 17 SNPs position

# Get the 17 SNPs
genes_17_snps <- snps_genes_chr |>
  dplyr::filter(SNP %in% snps_157b$SNP)

genes_17_snps <- snps_genes_chr[snps_genes_chr$SNP %in% snps_157b$SNP, ]

head(genes_17_snps)
## # A tibble: 6 x 8
##   SNP          Chromosome Position_chr Scaffold Position Gene_ID    Start    End
##   <chr>        <chr>             <dbl> <chr>       <dbl> <chr>      <dbl>  <dbl>
## 1 AX-579474142 2             368006161 2.22      6075268 LOC10941~ 6.07e6 6.08e6
## 2 AX-579474650 2             368053323 2.22      6122430 LOC10941~ 6.12e6 6.19e6
## 3 AX-579509845 2             375072931 2.22     13142038 LOC10939~ 1.25e7 1.31e7
## 4 AX-579556477 2             390614565 2.36      1062758 LOC10939~ 9.66e5 1.06e6
## 5 AX-579580051 2             395422791 2.36      5870984 LOC10940~ 5.87e6 5.90e6
## 6 AX-579584369 2             396355776 2.36      6803969 LOC10941~ 6.71e6 6.82e6

Save as Excel file

# Save the data frame to an Excel file
write_xlsx(genes_17_snps, here("output", "ldna", "cluster_14_17_snps.xlsx"))

Get the SNPs for which we do not have genes

genes_17_snps2 <- snps_157b |>
  dplyr::filter(!(SNP %in% genes_17_snps$SNP))
head(genes_17_snps2)
## # A tibble: 6 x 7
##   Scaffold SNP             Cm Position Allele1 Allele2 linked
##   <chr>    <chr>        <int>    <dbl> <chr>   <chr>   <lgl> 
## 1 2.36     AX-579560686     0  2139679 C       T       FALSE 
## 2 2.36     AX-579564292     0  2894537 A       G       FALSE 
## 3 2.36     AX-579565469     0  3152089 A       T       FALSE 
## 4 2.38     AX-579596233     0  2030415 A       G       FALSE 
## 5 2.38     AX-579602153     0  3099174 G       A       FALSE 
## 6 2.38     AX-579604213     0  3599115 T       C       FALSE

Save it (17 genes)

write.table(
  snps_157b,
  file      = here(
    "output", "ldna", "snps_157b.txt"
  ),
  sep       = "\t",
  row.names = FALSE,
  col.names = FALSE,
  quote     = FALSE
)

8. Cluster gene intersection with differentially expressed genes

This section identifies genes within the two major LD clusters (cluster 14 on chromosome 2 and cluster 6 on chromosome 1) and checks whether any overlap with differentially expressed genes from the NON-AUTO vs AUTO comparison.

The gene coordinate data (snps_genes_chr.rds) and expression data (snps_expression.rds) were generated in File S5.

8.1 Cluster 14 genes

snps_genes_chr <-
  readRDS(
    file = here(
      "output", "ldna", "snps_genes_chr.rds"
      )
    )
head(snps_genes_chr)
## # A tibble: 6 x 8
##   SNP          Chromosome Position_chr Scaffold Position Gene_ID    Start    End
##   <chr>        <chr>             <dbl> <chr>       <dbl> <chr>      <dbl>  <dbl>
## 1 AX-581444870 1                 97856 1.1         97856 LOC10939~  64508 102530
## 2 AX-583033342 1                315059 1.1        315059 LOC10943~ 310594 316608
## 3 AX-583035163 1                315386 1.1        315386 LOC10943~ 310594 316608
## 4 AX-583033356 1                315674 1.1        315674 LOC10943~ 310594 316608
## 5 AX-583035257 1                442875 1.10        91677 LOC10940~  66918 192829
## 6 AX-583035268 1                462944 1.10       111746 LOC10940~  66918 192829
snps_expression <-
  readRDS(
    here(
      "output", "ldna", "snps_expression.rds"
      )
    )
head(snps_expression)
## # A tibble: 6 x 9
##   SNP          Chromosome Position_chr Scaffold Position Gene_ID    Start    End
##   <chr>        <chr>             <dbl> <chr>       <dbl> <chr>      <dbl>  <dbl>
## 1 AX-583050970 1               3777633 1.10      3426435 LOC10942~ 3.42e6 3.43e6
## 2 AX-583052853 1               3777864 1.10      3426666 LOC10942~ 3.42e6 3.43e6
## 3 AX-583051011 1               3779183 1.10      3427985 LOC10942~ 3.42e6 3.43e6
## 4 AX-583052887 1               3793314 1.10      3442116 LOC10942~ 3.44e6 3.47e6
## 5 AX-583051025 1               3793653 1.10      3442455 LOC10942~ 3.44e6 3.47e6
## 6 AX-583052920 1               3794185 1.10      3442987 LOC10942~ 3.44e6 3.47e6
## # i 1 more variable: log2FoldChange <dbl>
cluster_14_SNPs <-
  cluster_14 |>
  select(
    SNP
  )

head(cluster_14_SNPs)
## # A tibble: 6 x 1
##   SNP         
##   <chr>       
## 1 AX-579450486
## 2 AX-579456609
## 3 AX-579459157
## 4 AX-579459504
## 5 AX-579459790
## 6 AX-579461404
8.1.1 Genes from coding regions
genes_cluster_14 <- inner_join(cluster_14_SNPs, snps_genes_chr, by = "SNP", suffix = c("", ""))

head(genes_cluster_14)
## # A tibble: 6 x 8
##   SNP          Chromosome Position_chr Scaffold Position Gene_ID    Start    End
##   <chr>        <chr>             <dbl> <chr>       <dbl> <chr>      <dbl>  <dbl>
## 1 AX-579450486 2             363469720 2.22      1538827 LOC10941~ 1.54e6 1.55e6
## 2 AX-579459504 2             365315778 2.22      3384885 LOC11525~ 3.37e6 4.60e6
## 3 AX-579459790 2             365379266 2.22      3448373 LOC11525~ 3.37e6 4.60e6
## 4 AX-579459790 2             365379266 2.22      3448373 LOC11526~ 3.45e6 3.45e6
## 5 AX-579461404 2             365421089 2.22      3490196 LOC11525~ 3.37e6 4.60e6
## 6 AX-579461838 2             365507367 2.22      3576474 LOC11525~ 3.37e6 4.60e6
write_xlsx(
  genes_cluster_14,
  here(
    "output", "ldna","genes_cluster_14.xlsx"
    )
  )
8.1.2 Genes with differential expression
DE_genes_cluster_14 <- inner_join(cluster_14_SNPs, snps_expression, by = "SNP", suffix = c("", ""))

head(DE_genes_cluster_14)
## # A tibble: 4 x 9
##   SNP          Chromosome Position_chr Scaffold Position Gene_ID    Start    End
##   <chr>        <chr>             <dbl> <chr>       <dbl> <chr>      <dbl>  <dbl>
## 1 AX-579585547 2             396531676 2.36      6979869 LOC10941~ 6.98e6 7.19e6
## 2 AX-579588768 2             397394228 2.36      7842421 LOC10941~ 7.84e6 7.84e6
## 3 AX-579598791 2             401278130 2.38      2679893 LOC10941~ 2.67e6 2.68e6
## 4 AX-579598805 2             401278614 2.38      2680377 LOC10941~ 2.67e6 2.68e6
## # i 1 more variable: log2FoldChange <dbl>
write_xlsx(
  DE_genes_cluster_14,
  here(
    "output", "ldna","DE_genes_cluster_14.xlsx"
    )
  )

8.2 Cluster 6 genes

cluster_6_SNPs <-
  cluster_6 |>
  select(
    SNP
  )

head(cluster_6_SNPs)
## # A tibble: 6 x 1
##   SNP         
##   <chr>       
## 1 AX-583500511
## 2 AX-583500520
## 3 AX-583503153
## 4 AX-583500379
## 5 AX-583503258
## 6 AX-583504302
8.2.1 Genes from coding regions
genes_cluster_6 <- inner_join(cluster_6_SNPs, snps_genes_chr, by = "SNP", suffix = c("", ""))

head(genes_cluster_6)
## # A tibble: 6 x 8
##   SNP          Chromosome Position_chr Scaffold Position Gene_ID    Start    End
##   <chr>        <chr>             <dbl> <chr>       <dbl> <chr>      <dbl>  <dbl>
## 1 AX-583503153 1             138373561 1.152      977944 LOC11526~ 9.77e5 9.79e5
## 2 AX-583500379 1             138374084 1.152      978467 LOC11526~ 9.77e5 9.79e5
## 3 AX-583503258 1             138383242 1.152      987625 LOC11525~ 9.79e5 9.89e5
## 4 AX-583503279 1             138887257 1.152     1491640 LOC10941~ 1.43e6 1.49e6
## 5 AX-583503440 1             138930796 1.152     1535179 LOC10941~ 1.51e6 1.54e6
## 6 AX-583503906 1             138960575 1.152     1564958 LOC10941~ 1.56e6 1.57e6
write_xlsx(
  genes_cluster_6,
  here(
    "output", "ldna","genes_cluster_6.xlsx"
    )
  )
8.2.2 Genes with differential expression
DE_genes_cluster_6 <- inner_join(cluster_6_SNPs, snps_expression, by = "SNP", suffix = c("", ""))

head(DE_genes_cluster_6)
## # A tibble: 0 x 9
## # i 9 variables: SNP <chr>, Chromosome <chr>, Position_chr <dbl>,
## #   Scaffold <chr>, Position <dbl>, Gene_ID <chr>, Start <dbl>, End <dbl>,
## #   log2FoldChange <dbl>
write_xlsx(
  DE_genes_cluster_6,
  here(
    "output", "ldna","DE_genes_cluster_6.xlsx"
    )
  )