2 BL1 and BL2

Setup

suppressPackageStartupMessages(library(copykit))
library(patchwork)
library(rstatix)
## 
## Attaching package: 'rstatix'
## The following object is masked from 'package:IRanges':
## 
##     desc
## The following object is masked from 'package:stats':
## 
##     filter
suppressPackageStartupMessages(library(tidyverse))
library(ggrepel)
library(ggnewscale)
library(janitor)
## 
## Attaching package: 'janitor'
## The following object is masked from 'package:rstatix':
## 
##     make_clean_names
## The following objects are masked from 'package:stats':
## 
##     chisq.test, fisher.test

Functions to Calculate Shannon Diversity Indexes

shan <- function(data, indices) {
  data_ind <- data[indices]
  prop <- janitor::tabyl(data_ind) %>% pull(percent)
  div <- -sum(prop*log(prop))
  return(div)
}

shan_sample <- function(meta, group) {
  meta_group <- meta %>%
    filter(sectors == !!group)
  group_perc <- janitor::tabyl(as.character(meta_group$subclones)) %>% pull(percent)
  group_div <- -sum(group_perc*log(group_perc))

  # Check for sectors composed of a unique subclone
  if (group_div == 0) {
    df <- data.frame(foci = group,
                     div = 0,
                     lci = 0,
                     uci = 0)
    return(df)
  }

  shan_group_boot <-
    boot::boot(
      as.character(meta_group$subclones),
      statistic = shan,
      R = 2000
    )
  shan_group_boot_ci <- suppressWarnings(boot::boot.ci(shan_group_boot))
  df <- data.frame(foci = group,
                   div = group_div,
                   lci = shan_group_boot_ci$normal[2],
                   uci = shan_group_boot_ci$normal[3])

}

2.1 BL1

2.1.1 Filtering

bl1 <- runVarbin("/mnt/lab/users/dminussi/projects/CopyKit_Manuscript_Code/datasets/BL1/marked_bams/", remove_Y = T)
## Counting reads for genome hg38 and resolution: 220kb
## 34 bam files had less than 10 mean bincounts and were removed.
## Performing GC correction.
## iteration: 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502
## Smoothing bin counts.
## iteration: 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502
## Running segmentation algorithm: CBS for genome hg38
## iteration: 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502
## Merging levels.
## iteration: 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502
## Done.
# Finding diploid cells and sub-setting the copykit object
bl1 <- findAneuploidCells(bl1)
## number of iterations= 28
## Copykit detected 607 that are possibly diploid cells using a resolution of: 0.069
## Added information to colData(CopyKit).
plotHeatmap(bl1, label = c('is_aneuploid'),
            row_split = 'is_aneuploid',
            n_threads = 50)
## Ordering by consensus requires cluster information.
## Switching to hclust.
## No distance matrix detected in the scCNA object.
## Calculating distance matrix with metric: euclidean
## Using 50 cores.
## Access distance matrix with copykit::distMat()
## Done.
## Plotting Heatmap.

bl1 <- bl1[,colData(bl1)$is_aneuploid == TRUE]

# Finding low-quality cells
bl1 <- findOutliers(bl1)
## Calculating correlation matrix.
## Marked 102 cells as outliers.
## Adding information to metadata. Access with colData(scCNA).
## Done.
plotHeatmap(bl1, label = c('outlier'),
            row_split = 'outlier',
            n_threads = 50)
## Ordering by consensus requires cluster information.
## Switching to hclust.
## No distance matrix detected in the scCNA object.
## Calculating distance matrix with metric: euclidean
## Using 50 cores.
## Access distance matrix with copykit::distMat()
## Done.
## Plotting Heatmap.

# obtaining metrics and plotting
bl1 <- runMetrics(bl1)
## Calculating overdispersion.
## iteration: 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895
## Counting breakpoints.
## Done.
bl1_p_metrics <- plotMetrics(
  bl1,
  metric = c(
    "reads_total",
    "percentage_duplicates",
    "overdispersion",
    "breakpoint_count"
  ), label = 'outlier',
  dodge.width = 0.8,
  ncol = 2
) + scale_fill_manual(values = c("TRUE" = "#DA614D",
                                 "FALSE" = "#5F917A"))
## Coloring by: outlier
## Scale for 'fill' is already present. Adding another scale for 'fill', which will replace the existing
## scale.
bl1_p_metrics

# calculating averages from the colData information and statistics
bl1_metrics <- as.data.frame(colData(bl1))
bl1_metrics %>%
  summarise(reads_total_mean = mean(reads_total),
            reads_total_sd = sd(reads_total),
            percent_dup_mean = mean(percentage_duplicates),
            percent_dup_sd = sd(percentage_duplicates)
            )
##   reads_total_mean reads_total_sd percent_dup_mean percent_dup_sd
## 1          1035783         246883       0.08147598    0.009734118
bl1_metrics %>% as.data.frame() %>%
  rstatix::kruskal_test(overdispersion ~ outlier)
## # A tibble: 1 × 6
##   .y.                n statistic    df     p method        
## * <chr>          <int>     <dbl> <int> <dbl> <chr>         
## 1 overdispersion   895      3.95     1 0.047 Kruskal-Wallis
bl1_metrics %>% as.data.frame() %>%
  rstatix::kruskal_test(breakpoint_count ~ outlier)
## # A tibble: 1 × 6
##   .y.                  n statistic    df        p method        
## * <chr>            <int>     <dbl> <int>    <dbl> <chr>         
## 1 breakpoint_count   895      63.2     1 1.87e-15 Kruskal-Wallis
# removing low-quality cells
bl1 <- bl1[,colData(bl1)$outlier == FALSE]

2.1.2 UMAP and clustering

# Running UMAP and storing into the copykit object
bl1 <- runUmap(bl1)
## Using assay: logr
## Embedding data with UMAP. Using seed 17
## Access reduced dimensions slot with: reducedDim(scCNA, 'umap').
## Done.
# Grid Search of Jaccard Similarity (cluster stability)
bl1 <- findSuggestedK(bl1)
## Calculating jaccard similarity for k range: 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
## 
## iteration: 12345678910111213141516171819
## Suggested k = 10 with median jaccard similarity of: 1
bl1_primary_suggestedK_p <- plotSuggestedK(bl1)
bl1_primary_suggestedK_p

# Finding clusters using the suggested k value as per default settings, 
# can be changed with the argument k_subclones
bl1 <- findClusters(bl1)
## Using suggested k_subclones = 10
## Finding clusters, using method: hdbscan
## Found 4 subclones.
## Done.
# Plotting UMAP colored by subclones
bl1_umap_p <- plotUmap(bl1, label = 'subclones')
## Plotting Umap.
## Coloring by: subclones.
bl1_umap_p

# Calculating consensus matrix and consensus phylo, 
# this will be used by copykit to order the subclones on the heatmap
bl1 <- calcConsensus(bl1)
## iteration: 1234
bl1 <- runConsensusPhylo(bl1)

2.1.3 Subclones Heatmap

# Plotting copy number heatmap and annotating with subclones
plotHeatmap(bl1, label = c('subclones'))
## Plotting Heatmap.

# Adding macro-spatial information from file names to colData
# and renaming to (S)ectors
colData(bl1)$sectors <- stringr::str_extract(colData(bl1)$sample, "(L[0-9]+L[0-9]+|L[0-9]+)")
colData(bl1)$sectors <- stringr::str_replace_all(colData(bl1)$sectors,
                                                    "L", "S")

# Plotting the UMAP colored by sectors
bl1_umap_sectors_p <- plotUmap(bl1, label = 'sectors')
## Plotting Umap.
## Coloring by: sectors.
bl1_umap_sectors_p

# Creating pie charts plots of the proportions of clones on each sector
bl1_focis <- unique(colData(bl1)$sectors)

bl1_foci_pies <- list()

for (i in seq_along(bl1_focis)) {

  df <- as.data.frame(colData(bl1)) %>%
    dplyr::filter(sectors == bl1_focis[i])

  bl1_foci_pies[[i]] <- ggplot(df) +
    geom_bar(aes(x = "", y = sectors, fill = subclones),
             stat = 'identity') +
    theme_void() +
    scale_fill_manual(values = subclones_pal(),
                      limits = force) +
    coord_polar(theta = "y") +
    ggtitle(bl1_focis[i])

}

wrap_plots(bl1_foci_pies)

# Setting up a theme for the next plot
my_theme <- list(
  ggplot2::theme(
    axis.title.x = element_text(colour = "gray28", size = 20),
    axis.text.x = element_blank(),
    axis.ticks.x = element_blank(),
    axis.title.y = element_text(colour = "gray28", size = 20),
    axis.text.y = element_blank(),
    axis.ticks.y = element_blank(),
    axis.line = element_blank(),
    legend.text = element_text(size = 14),
    panel.border = element_rect(fill = NA, color = 'black')
  ),
  xlab("umap1"),
  ylab("umap2")
)

# Plotting the UMAP colored by sector and using ggrepel to label the subclones
bl1_lfoci_umap <- as.data.frame(reducedDim(bl1, 'umap'))
bl1_lfoci_umap$sectors <- colData(bl1)$sectors
bl1_lfoci_umap$subclones <- as.character(colData(bl1)$subclones)
# Avoiding overplotting of text
bl1_lfoci_umap$subclones <- ifelse(!duplicated(bl1_lfoci_umap$subclones),
                                   bl1_lfoci_umap$subclones, "")

bl1_lfoci_umap_p <- ggplot(bl1_lfoci_umap, aes(V1, V2, label = subclones)) +
  geom_point(aes(fill = sectors), shape = 21,
             stroke = 0.1,
             size = 2.5) +
  ggnewscale::new_scale_fill() +
  geom_text_repel(aes(color = subclones),
                   min.segment.length = 0,
                   box.padding = 0.01,
                   size = 4,
                   max.overlaps = Inf,
                   na.rm = T) +
  scale_color_manual(values = subclones_pal(),
                    limits = force) +
  theme_classic() +
  my_theme
bl1_lfoci_umap_p

# Plotting consensus heatmap with a frequency annotation of sectors by 
# subclone and annotating genes of interest
plotHeatmap(bl1, label = 'subclones', consensus = T, group = 'sectors',
            genes = c(
              "FHIT",
              "CUX1",
              "WWOX",
              "FOXO1",
              "APC",
              "BCL2",
              "KRAS",
              "PGR",
              "PDGFRA",
              "ROS1",
              "PTPN11",
              "CCND1",
              "BTG1",
              "FGFR3",
              "PTEN",
              "FGFR2"
            ))
## Plotting Heatmap.

# Calculating Diversity Indexes by sector
bl1_meta <- colData(bl1)

bl1_meta <- as.data.frame(bl1_meta)
bl1_meta <- bl1_meta[c('subclones', 'sectors')]

bl1_L1_div <- shan_sample(bl1_meta, "S1")
bl1_L2_div <- shan_sample(bl1_meta, "S2")
bl1_L3_div <- shan_sample(bl1_meta, "S3")
bl1_L4L5_div <- shan_sample(bl1_meta, "S4S5")
bl1_L6L7_div <- shan_sample(bl1_meta, "S6S7")
bl1_L8L9_div <- shan_sample(bl1_meta, "S8S9")

bl1_div <- bind_rows(
  bl1_L1_div,
  bl1_L2_div,
  bl1_L3_div,
  bl1_L4L5_div,
  bl1_L6L7_div,
  bl1_L8L9_div
)

# Plotting Shannon Diversity Index
bl1_div_p <- bl1_div %>%
  ggplot() +
  geom_errorbar(aes(
    x = fct_reorder(foci, div),
    ymin = lci,
    ymax = uci
  ),
  width = .1,
  size = 1) +
  geom_point(aes(
    x = fct_reorder(foci, div),
    y = div,
    color = foci
  ), size = 5) +
  cowplot::theme_cowplot() +
  theme(legend.position = "none",
        axis.text.x = element_text(
          angle = 90,
          vjust = 0.5,
          hjust = 1
        )) +
  scale_y_continuous(breaks = scales::pretty_breaks(n = 12)) +
  ylab("shannon diversity index") +
  xlab("") +
  coord_flip()

bl1_div_p

2.2 BL2

2.2.1 Filtering

bl2 <- runVarbin("/mnt/lab/users/dminussi/projects/CopyKit_Manuscript_Code/datasets/BL2/marked_bams/", remove_Y = T)
## Counting reads for genome hg38 and resolution: 220kb
## 61 bam files had less than 10 mean bincounts and were removed.
## Performing GC correction.
## iteration: 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475
## Smoothing bin counts.
## iteration: 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475
## Running segmentation algorithm: CBS for genome hg38
## iteration: 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475
## Merging levels.
## iteration: 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475
## Done.
# Finding diploid cells and sub-setting the copykit object
bl2 <- findAneuploidCells(bl2)
## number of iterations= 24
## Copykit detected 100 that are possibly diploid cells using a resolution of: 0.068
## Added information to colData(CopyKit).
plotHeatmap(bl2, label = c('is_aneuploid'),
            row_split = 'is_aneuploid',
            n_threads = 50)
## Ordering by consensus requires cluster information.
## Switching to hclust.
## No distance matrix detected in the scCNA object.
## Calculating distance matrix with metric: euclidean
## Using 50 cores.
## Access distance matrix with copykit::distMat()
## Done.
## Plotting Heatmap.

bl2 <- bl2[,colData(bl2)$is_aneuploid == TRUE]

# Finding low-quality cells
bl2 <- findOutliers(bl2)
## Calculating correlation matrix.
## Marked 108 cells as outliers.
## Adding information to metadata. Access with colData(scCNA).
## Done.
plotHeatmap(bl2, label = c('outlier'),
            row_split = 'outlier',
            n_threads = 50)
## Ordering by consensus requires cluster information.
## Switching to hclust.
## No distance matrix detected in the scCNA object.
## Calculating distance matrix with metric: euclidean
## Using 50 cores.
## Access distance matrix with copykit::distMat()
## Done.
## Plotting Heatmap.

# obtaining metrics and plotting
bl2 <- runMetrics(bl2)
## Calculating overdispersion.
## iteration: 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375
## Counting breakpoints.
## Done.
bl2_p_metrics <- plotMetrics(
  bl2,
  metric = c(
    "reads_total",
    "percentage_duplicates",
    "overdispersion",
    "breakpoint_count"
  ), label = 'outlier',
  dodge.width = 0.8,
  ncol = 2
) + scale_fill_manual(values = c("TRUE" = "#DA614D",
                                 "FALSE" = "#5F917A"))
## Coloring by: outlier
## Scale for 'fill' is already present. Adding another scale for 'fill', which will replace the existing
## scale.
bl2_p_metrics

# calculating averages from the colData information and statistics
bl2_metrics <- as.data.frame(colData(bl2))
bl2_metrics %>%
  summarise(reads_total_mean = mean(reads_total),
            reads_total_sd = sd(reads_total),
            percent_dup_mean = mean(percentage_duplicates),
            percent_dup_sd = sd(percentage_duplicates)
            )
##   reads_total_mean reads_total_sd percent_dup_mean percent_dup_sd
## 1         975057.5       303911.9       0.08932727     0.01065848
bl2_metrics %>% as.data.frame() %>%
  rstatix::kruskal_test(overdispersion ~ outlier)
## # A tibble: 1 × 6
##   .y.                n statistic    df             p method        
## * <chr>          <int>     <dbl> <int>         <dbl> <chr>         
## 1 overdispersion  1375      32.9     1 0.00000000978 Kruskal-Wallis
bl2_metrics %>% as.data.frame() %>%
  rstatix::kruskal_test(breakpoint_count ~ outlier)
## # A tibble: 1 × 6
##   .y.                  n statistic    df     p method        
## * <chr>            <int>     <dbl> <int> <dbl> <chr>         
## 1 breakpoint_count  1375     0.928     1 0.335 Kruskal-Wallis
# removing low-quality cells
bl2 <- bl2[,colData(bl2)$outlier == FALSE]

2.2.2 UMAP and clustering

# Running UMAP and storing into the copykit object
bl2 <- runUmap(bl2)
## Using assay: logr
## Embedding data with UMAP. Using seed 17
## Access reduced dimensions slot with: reducedDim(scCNA, 'umap').
## Done.
# Grid Search of Jaccard Similarity (cluster stability)
bl2 <- findSuggestedK(bl2)
## Calculating jaccard similarity for k range: 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
## 
## iteration: 123456789101112131415161718192021222324252627
## Suggested k = 11 with median jaccard similarity of: 0.928
bl2_primary_suggestedK_p <- plotSuggestedK(bl2)
bl2_primary_suggestedK_p

# Finding clusters using the suggested k value as per default settings, 
# can be changed with the argument k_subclones
bl2 <- findClusters(bl2)
## Using suggested k_subclones = 11
## Finding clusters, using method: hdbscan
## Found 62 outliers cells (group 'c0')
## Found 19 subclones.
## Done.
# HDBSCAN is an outlier aware clustering algorithm
# in this analysis all cells marked as outliers (c0) from hdbscan are excluded.
bl2 <- bl2[,colData(bl2)$subclones != 'c0']

# Plotting UMAP colored by subclones
bl2_umap_p <- plotUmap(bl2, label = 'subclones')
## Plotting Umap.
## Coloring by: subclones.
bl2_umap_p

# Calculating consensus matrix and consensus phylo, 
# this will be used by copykit to order the subclones on the heatmap
bl2 <- calcConsensus(bl2)
## iteration: 12345678910111213141516171819
bl2 <- runConsensusPhylo(bl2)

2.2.3 Subclones Heatmap

# Plotting copy number heatmap and annotating with subclones
plotHeatmap(bl2, label = c('subclones'))
## Plotting Heatmap.

# Adding macro-spatial information from file names to colData
# and renaming to (S)ectors
colData(bl2)$sectors <- stringr::str_extract(colData(bl2)$sample,
                                                     "L[0-9]+")
colData(bl2)$sectors <- stringr::str_replace(colData(bl2)$sectors, 
                                                     "L", "S")
# Plotting the UMAP colored by sectors
bl2_umap_sectors_p <- plotUmap(bl2, label = 'sectors')
## Plotting Umap.
## Coloring by: sectors.
bl2_umap_sectors_p

# Creating pie charts plots of the proportions of clones on each sector
bl2_focis <- unique(colData(bl2)$sectors)

bl2_foci_pies <- list()

for (i in seq_along(bl2_focis)) {

  df <- as.data.frame(colData(bl2)) %>%
    dplyr::filter(sectors == bl2_focis[i])

  bl2_foci_pies[[i]] <- ggplot(df) +
    geom_bar(aes(x = "", y = sectors, fill = subclones),
             stat = 'identity') +
    theme_void() +
    scale_fill_manual(values = subclones_pal(),
                      limits = force) +
    coord_polar(theta = "y") +
    ggtitle(bl2_focis[i])

}

wrap_plots(bl2_foci_pies)

# Setting up a theme for the next plot
my_theme <- list(
  ggplot2::theme(
    axis.title.x = element_text(colour = "gray28", size = 20),
    axis.text.x = element_blank(),
    axis.ticks.x = element_blank(),
    axis.title.y = element_text(colour = "gray28", size = 20),
    axis.text.y = element_blank(),
    axis.ticks.y = element_blank(),
    axis.line = element_blank(),
    legend.text = element_text(size = 14),
    panel.border = element_rect(fill = NA, color = 'black')
  ),
  xlab("umap1"),
  ylab("umap2")
)

# Plotting the UMAP colored by sector and using ggrepel to label the subclones
bl2_lfoci_umap <- as.data.frame(reducedDim(bl2, 'umap'))
bl2_lfoci_umap$sectors <- colData(bl2)$sectors
bl2_lfoci_umap$subclones <- as.character(colData(bl2)$subclones)
# Avoiding overplotting of text
bl2_lfoci_umap$subclones <- ifelse(!duplicated(bl2_lfoci_umap$subclones),
                                   bl2_lfoci_umap$subclones, "")

bl2_lfoci_umap_p <- ggplot(bl2_lfoci_umap, aes(V1, V2, label = subclones)) +
  geom_point(aes(fill = sectors), shape = 21,
             stroke = 0.1,
             size = 2.5) +
  ggnewscale::new_scale_fill() +
  geom_text_repel(aes(color = subclones),
                   min.segment.length = 0,
                   box.padding = 0.01,
                   size = 4,
                   max.overlaps = Inf,
                   na.rm = T) +
  scale_color_manual(values = subclones_pal(),
                    limits = force) +
  theme_classic() +
  my_theme
bl2_lfoci_umap_p

# Plotting consensus heatmap with a frequency annotation of sectors 
# by subclone and annotating genes of interest
plotHeatmap(bl2, label = 'subclones', consensus = T, group = 'sectors',
           genes = c(
              "CCND1",
              "TP53",
              "GATA1",
              "SOX2",
              "ERBB2",
              "MYC",
              "RB1",
              "PIK3CA",
              "MMP3",
              "BRCA1",
              "BRCA2",
              "FHIT"
            ))
## Plotting Heatmap.

# Calculating Diversity Indexes by sector
bl2_meta <- colData(bl2)

bl2_meta <- as.data.frame(bl2_meta)
bl2_meta <- bl2_meta[c('subclones', 'sectors')]

bl2_L1_div <- shan_sample(bl2_meta, "S1")
bl2_L4_div <- shan_sample(bl2_meta, "S4")
bl2_L5_div <- shan_sample(bl2_meta, "S5")
bl2_L6_div <- shan_sample(bl2_meta, "S6")
bl2_L7_div <- shan_sample(bl2_meta, "S7")

bl2_div <- bind_rows(
  bl2_L1_div,
  bl2_L4_div,
  bl2_L5_div,
  bl2_L6_div,
  bl2_L7_div
)


# Plotting Shannon Diversity Index
bl2_div_p <- bl2_div %>%
  ggplot() +
  geom_errorbar(aes(
    x = fct_reorder(foci, div),
    ymin = lci,
    ymax = uci
  ),
  width = .1,
  size = 1) +
  geom_point(aes(
    x = fct_reorder(foci, div),
    y = div,
    color = foci
  ), size = 5) +
  cowplot::theme_cowplot() +
  theme(legend.position = "none",
        axis.text.x = element_text(
          angle = 90,
          vjust = 0.5,
          hjust = 1
        )) +
  scale_y_continuous(breaks = scales::pretty_breaks(n = 12)) +
  ylab("shannon diversity index") +
  xlab("") +
  coord_flip()

bl2_div_p

The following code analysis the breast metastatic sample with metastasis to the liver and to the pleural effusion. This works as an example on how to employ CopyKit for the analysis of multiple datasets.