1 Supporting Information

2

3 Guillermo Friis and Borja Milá

4

5 divergence, sexual dichromatism and the role of sexual selection in the rapid

6 postglacial radiation of dark-eyed juncos (Aves: Passerellidae)

7

8 Sequencing and assembly of the sexual chromosome

9 To recover the Z chromosome for which DoveTails Hi-C sequencing failed, we

10 assembled a consensus draft genome by combining low-coverage genomes of eight

11 different junco individuals from a parallel study, consisting in 2 samples of J. bairdi, 2

12 J. p. palliatus, 2 J. h. caniceps, 1 J. h. thurberi and 1 J. h. dorsalis. Libraries for seven

13 of the genomes were prepared with the Kapa Library Preparation Kit (Kapa Biosystems,

14 Inc.) using TruSeq-style adapters (Faircloth and Glenn 2012). They were pooled after

15 random shearing and individual barcoding and sequenced in a single lane of an Illumina

16 HiSeq platform. The eighth genome, corresponding to the dorsalis individual, was

17 sequenced at a higher coverage by means of two 101-bp paired-end shotgun libraries

18 and two 101-bp mate-paired libraries with insert sizes of 8 Kb in length at Macrogen

19 Inc. The TruSeq Nano DNA Kit (Illumina) was used for the preparation of the shotgun

20 libraries, while the mate-paired libraries were prepared with Nextera Mate Pair Kit

21 (Illumina). We used FASTQC (Andrews 2010) to evaluate the quality of the sequenced

22 data, and quality filtering was carried out with NextClip (Leggett et al. 2013) in the case

23 of the mate paired libraries. For the rest of them we used Trimmomatic (Bolger et al.

24 2014), applying a sliding window filtering approach with a size of 4 bp and a phred

25 quality score threshold of 25. We also set a minimum length of 50 bp, below which

1

26 reads were filtered out after trimming. We used the software SOAPDENOVO2 (Luo et

27 al. 2012) to perform the assembly. The average insert size for each library was

28 estimated in a preliminary run, and we set a Kmer size of 27 and minimum edge

29 coverage of 2. Gaps that emerged during the scaffolding process were removed with the

30 GapCloser tool from SOAPDENOVO2. Finally, we filtered out all the scaffolds shorter

31 than 500 bp so the genome was functional as a mapping reference. The final assembled

32 genome had 37,904 scaffolds with an N50 of 147,816 bases, a L50 of 1,951 scaffolds, a

33 total size of 1.09 Gb, 17.5 Mb of missing sites, and an overall coverage of ~56X, as

34 computed with VCFTOOLS version 0.1.13 (Danecek et al. 2011). To recover the Z

35 chromosome specifically, we mapped the 37,904 scaffolds against the zebra finch

36 (Taeniopygia guttata) genome v87 available in Ensembl (Yates et al. 2016) by means of

37 the Chromosembler tool available in Satsuma (Grabherr et al. 2010). The assembled Z

38 chromosome was 71.1 Mb long.

39

40

41

42

43

44

2

45 Table S1. List of samples sequenced by genotyping-by-sequencing (GBS). State 46 abbreviations are the following: Virginia (VA), Wyoming (WY), California (CA), 47 Colorado (CO), Utah (UT), Maine (ME), New Hampshire (NH), New Mexico (NM), 48 Nevada (NV), , Utah (UT), Wyoming (WY), and Arizona (AZ) in the USA; British 49 Columbia (BC) in Canada; and Chihuahua (CHIH), Mexico City (CM) and Durango 50 (DGO), in Mexico. 51

Taxonomy Field name State Country LAT LONG Collection date SEX J. h. aikeni 05N0522 WY USA 44.48614 -104.45108 7/13/2005 M J. h. aikeni 05N0523 WY USA 44.48614 -104.45108 7/13/2005 M J. h. aikeni 05N0524 WY USA 44.59476 -104.41141 7/13/2005 M J. h. aikeni 05N0525 WY USA 44.59476 -104.41141 7/14/2005 M J. h. aikeni 05N0526 WY USA 44.59476 -104.41141 7/14/2005 M J. h. aikeni 05N0527 WY USA 44.50185 -104.45238 7/14/2005 M J. h. aikeni 05N0528 WY USA 44.50185 -104.45238 7/14/2005 M J. h. aikeni 05N0529 WY USA 44.50185 -104.45238 7/14/2005 M J. h. aikeni 05N0531 WY USA 44.48349 -107.45811 7/14/2005 M J. h. aikeni 05N0532 WY USA 44.48349 -107.45811 7/14/2005 M J. h. aikeni 05N0534 WY USA 44.48349 -107.45811 7/14/2005 M J. h. aikeni 05N0535 WY USA 44.48349 -107.45811 7/14/2005 M J. h. caniceps 14-0368 CO USA 6/12/2014 M J. h. caniceps 14-0379 CO USA 37.80702 -107.90788 6/13/2014 M J. h. caniceps 14-0380 CO USA 37.80536 -107.90508 6/13/2014 M J. h. caniceps 14-0365 CO USA 6/12/2014 M J. h. caniceps 14-0366 CO USA 6/12/2014 M J. h. caniceps 14-0367 CO USA 6/12/2014 M J. h. caniceps 14-0369 CO USA 6/12/2014 M J. h. caniceps 14-0370 CO USA 6/12/2014 M J. h. caniceps 14-0371 CO USA 6/12/2014 M J. h. caniceps 14-0373 CO USA 6/12/2014 M J. h. caniceps 14-0374 CO USA 37.80629 -107.90349 6/13/2014 M J. h. caniceps 14-0375 CO USA 37.80491 -107.90621 6/13/2014 M J. h. caniceps 14-0376 CO USA 37.80715 -107.90481 6/13/2014 M J. h. caniceps 14-0405 CO USA 38.24164 -105.66933 6/18/2014 M J. h. caniceps 14-0406 CO USA 38.2527 -105.67553 6/18/2014 M J. h. caniceps 14-0407 CO USA 38.25445 -105.67116 6/18/2014 M J. h. caniceps 14-0408 CO USA 6/18/2014 M J. h. caniceps 14-0411 CO USA 38.25371 -105.66551 6/19/2014 M J. h. caniceps 14-0412 CO USA 38.05197 -105.53458 6/19/2014 M J. h. caniceps 14-0413 CO USA 38.05247 -105.53736 6/19/2014 M J. h. caniceps 14-0404 CO USA 38.25366 -105.67282 6/18/2014 M J. h. caniceps 12-158 NV USA 39.29206 -117.11548 4/28/2012 M J. h. caniceps 12-159 NV USA 39.29206 -117.11548 4/28/2012 M

3

J. h. caniceps 12-160 NV USA 39.29206 -117.11548 4/29/2012 M J. h. caniceps 14-0395 UT USA 39.33038 -106.66054 6/17/2014 M J. h. caniceps 14-0383 UT USA 6/15/2014 M J. h. caniceps 14-0384 UT USA 39.30839 -106.63971 6/15/2014 M J. h. caniceps 14-0385 UT USA 39.30835 -106.64185 6/16/2014 M J. h. caniceps 14-0388 UT USA 39.30939 -106.64325 6/16/2014 M J. h. caniceps 14-0389 UT USA 39.31047 -106.64441 6/16/2014 M J. h. caniceps 14-0390 UT USA 39.31016 -106.64459 6/16/2014 M J. h. caniceps 14-0391 UT USA 39.30652 -106.64223 6/16/2014 M J. h. caniceps 14-0392 UT USA 39.30553 -106.64254 6/16/2014 M J. h. caniceps 14-0393 UT USA 6/16/2014 M J. h. caniceps 14-0394 UT USA 6/16/2014 M J. h. caniceps 14-0396 UT USA 39.33174 -106.66293 6/17/2014 M J. h. caniceps 14-0387 UT USA 39.30991 -106.64361 6/16/2014 M J. h. caniceps 14-0397 UT USA 39.33203 -106.6641 6/17/2014 M J. h. caniceps 14-0398 UT USA 39.33216 -106.66359 6/17/2014 M J. h. caniceps 14-0399 UT USA 39.33311 -106.66375 6/17/2014 M J. h. caniceps 14-0401 UT USA 39.33472 -106.6643 6/17/2014 M J. h. caniceps 14-0402 UT USA 39.33547 -106.66398 6/17/2014 M J. h. caniceps 14-0381 UT USA 39.30025 -106.60064 6/15/2014 M J. h. caniceps 14-0382 UT USA 39.30839 -106.63971 6/15/2014 M J. h. caniceps 05N0548 UT USA 40.68944 -110.89545 7/16/2005 M J. h. caniceps 05N0549 UT USA 40.68944 -110.89545 7/16/2005 M J. h. caniceps 05N0550 UT USA 40.68944 -110.89545 7/16/2005 M J. h. caniceps 05N0551 UT USA 40.66553 -110.96137 7/16/2005 M J. h. caniceps 05N0552 UT USA 40.66553 -110.96137 7/16/2005 M J. h. caniceps 05N0554 UT USA 37.49955 -112.54743 7/17/2005 M J. h. caniceps 05N0557 UT USA 37.49955 -112.54743 7/17/2005 M J. h. caniceps 05N0560 UT USA 37.48084 -112.57668 7/18/2005 M J. h. caniceps 06N25689 UT USA 37.52909 -112.75322 6/11/2006 M J. h. caniceps 05N0558 UT USA 37.48084 -112.57668 7/17/2005 M J. h. caniceps 05N0562 UT USA 37.48084 -112.57668 7/18/2005 M J. h. caniceps 05N0564 UT USA 37.48084 -112.57668 7/18/2005 M J. h. caniceps 06N25679 UT USA 37.52909 -112.75322 6/10/2006 M J. h. caniceps 06N25699 UT USA 40.44078 -111.62781 6/12/2006 M J. h. caniceps 06N26252 UT USA 40.44078 -111.62781 6/12/2006 M J. h. caniceps 05N0540 UT USA 40.8046 -110.87705 7/15/2005 M J. h. caniceps 05N0541 UT USA 40.8046 -110.87705 7/15/2005 M J. h. caniceps 05N0544 UT USA 40.8046 -110.87705 7/16/2005 M J. h. caniceps 05N0545 UT USA 40.8046 -110.87705 7/16/2005 M J. h. caniceps 05N0553 UT USA 40.66553 -110.96137 7/16/2005 M J. h. caniceps 05N0539 UT USA 40.8046 -110.87705 7/15/2005 M J. h. caniceps 13-221 UT USA 40.62484 -111.60172 6/20/2013 M J. h. caniceps 13-216 UT USA 40.61301 -111.58719 6/19/2013 M J. h. caniceps 13-217 UT USA 40.60906 -111.56644 6/20/2013 M J. h. caniceps 13-218 UT USA 40.60906 -111.56644 6/20/2013 M

4

J. h. carolinensis 12-122 VA USA 4/19/2012 M J. h. carolinensis 12-123 VA USA 4/19/2012 M J. h. carolinensis 12-124 VA USA 4/19/2012 M J. h. carolinensis 12-132 VA USA 4/20/2012 M J. h. carolinensis 12-133 VA USA 4/20/2012 M J. h. carolinensis 12-135 VA USA 4/20/2012 M J. h. carolinensis 12-136 VA USA 4/20/2012 M J. h. carolinensis 12-127 VA USA 4/19/2012 M J. h. carolinensis 12-104 VA USA 4/13/2012 M J. h. carolinensis 12-106 VA USA 4/13/2012 M J. h. carolinensis 12-107 VA USA 4/13/2012 M J. h. carolinensis 12-108 VA USA 4/13/2012 M J. h. carolinensis 12-117 VA USA 4/17/2012 M J. h. carolinensis 12-118 VA USA 4/17/2012 M J. h. carolinensis 12-120 VA USA 4/19/2012 M J. h. carolinensis 12-126 VA USA 4/19/2012 M J. h. carolinensis 12-128 VA USA 4/19/2012 M J. h. carolinensis 12-129 VA USA 4/19/2012 M J. h. carolinensis 12-131 VA USA 4/19/2012 M J. h. carolinensis 12-134 VA USA 4/20/2012 M J. h. carolinensis 12-102 VA USA 4/13/2012 M J. h. carolinensis 12-103 VA USA 4/13/2012 M J. h. dorsalis 06N25589 AZ USA 6/2/2006 M J. h. dorsalis 06N25592 AZ USA 6/2/2006 M J. h. dorsalis 06N25703 AZ USA 35.34008 -111.60216 6/5/2006 M J. h. dorsalis 06N25594 AZ USA 6/2/2006 M J. h. dorsalis 06N25599 AZ USA 35.34008 -111.60216 6/4/2006 M J. h. dorsalis 06N25600 AZ USA 35.34008 -111.60216 6/5/2006 M J. h. dorsalis 06N25706 AZ USA 35.34008 -111.60216 6/5/2006 M J. h. dorsalis 06N25705 AZ USA 35.34008 -111.60216 6/5/2006 M J. h. dorsalis 06N25709 AZ USA 35.34008 -111.60216 6/5/2006 M J. h. dorsalis 06N25708 AZ USA 35.34008 -111.60216 6/5/2006 M J. h. dorsalis 06N25710 AZ USA 35.34008 -111.60216 6/5/2006 M J. h. dorsalis 06N25711 AZ USA 35.34008 -111.60216 6/5/2006 M J. h. dorsalis 06N25715 AZ USA 35.34008 -111.60216 6/6/2006 M J. h. dorsalis 06N25721 AZ USA 35.34008 -111.60216 6/6/2006 M J. h. dorsalis 14-0360 AZ USA 34.0202 -109.6303 6/10/2014 M J. h. dorsalis 14-0359 AZ USA 6/10/2014 M J. h. dorsalis 14-0361 AZ USA 34.00172 -109.48156 6/10/2014 M J. h. dorsalis 14-0362 AZ USA 6/11/2014 M J. h. dorsalis 14-0364 AZ USA 6/11/2014 M J. h. dorsalis 05N0577 AZ USA 34.01638 -109.6199 7/20/2005 M J. h. dorsalis 05N0580 AZ USA 33.92689 -109.45343 7/21/2005 M J. h. dorsalis 05N0583 AZ USA 33.92689 -109.45343 7/21/2005 M J. h. dorsalis 05N0584 AZ USA 33.92689 -109.45343 7/21/2005 M J. h. dorsalis 05N0585 AZ USA 33.92689 -109.45343 7/21/2005 U

5

J. h. dorsalis J. h. dorsalis 14-0483 NM USA 32.90121 -105.771 6/26/2014 M J. h. dorsalis 14-0494 NM USA 32.90373 -105.7671 6/27/2014 M J. h. dorsalis 14-0495 NM USA 32.90369 -105.76512 6/27/2014 M J. h. dorsalis 14-0496 NM USA 32.9029 -105.76464 6/27/2014 M J. h. dorsalis 14-0472 NM USA 6/25/2014 M J. h. dorsalis 14-0473 NM USA 6/25/2014 M J. h. dorsalis 14-0476 NM USA 32.90695 -105.75463 6/26/2014 M J. h. dorsalis 14-0477 NM USA 32.90584 -105.75482 6/26/2014 M J. h. dorsalis 14-0478 NM USA 6/26/2014 M J. h. dorsalis 14-0479 NM USA 6/26/2014 M J. h. dorsalis 14-0480 NM USA 32.90328 -105.77072 6/26/2014 M J. h. dorsalis 14-0484 NM USA 32.90157 -105.77323 6/26/2014 M J. h. dorsalis 14-0482 NM USA 32.90183 -105.77006 6/26/2014 M J. h. dorsalis 14-0471 NM USA 6/25/2014 M J. h. dorsalis 14-0474 NM USA 6/25/2014 M J. h. dorsalis 14-0481 NM USA 32.90222 -105.76966 6/26/2014 M J. h. dorsalis 14-0485 NM USA 32.9011 -105.77427 6/26/2014 M J. h. dorsalis 14-0487 NM USA 32.90103 -105.77613 6/26/2014 M J. h. dorsalis 14-0488 NM USA 32.90107 -105.77643 6/26/2014 M J. h. dorsalis 14-0489 NM USA 32.9033 -105.77563 6/26/2014 M J. h. dorsalis 14-0490 NM USA 32.90257 -105.77441 6/26/2014 M J. h. dorsalis 14-0491 NM USA 32.90338 -105.77255 6/26/2014 M J. h. dorsalis 14-0492 NM USA 32.90388 -105.77158 6/27/2014 M J. h. hyemalis 03N1302 ME USA 7/7/2003 M J. h. hyemalis 03N1317 ME USA 7/9/2003 M J. h. hyemalis 14-0549 NH USA 44.25072 -71.22351 7/14/2014 M J. h. hyemalis 14-0545 NH USA 44.2562 -71.23405 7/12/2014 M J. h. hyemalis 14-0546 NH USA 44.16999 -71.69966 7/13/2014 M J. h. hyemalis 14-0547 NH USA 44.24863 -71.22486 7/14/2014 M J. h. hyemalis 14-0550 NH USA 44.25362 -71.22355 7/15/2014 M J. h. hyemalis 14-0552 NH USA 44.24479 -71.47019 7/16/2014 M J. h. hyemalis 14-0553 NH USA 44.24836 -71.47334 7/16/2014 M J. h. hyemalis 14-0554 NH USA 44.24383 -71.4695 7/16/2014 M J. h. hyemalis 14-0555 NH USA 44.24383 -71.4695 7/17/2014 M J. h. hyemalis 14-0557 NH USA 7/17/2014 M J. h. hyemalis 14-0558 NH USA 44.25124 -71.48004 7/17/2014 M J. h. hyemalis 14-0559 NH USA 44.25204 -71.47765 7/17/2014 M J. h. mearnsi 05N0516 WY USA 44.19644 -107.21337 7/13/2005 M J. h. mearnsi 05N0505 WY USA 7/11/2005 M J. h. mearnsi 05N0507 WY USA 7/11/2005 M J. h. mearnsi 05N0512 WY USA 7/12/2005 M J. h. mearnsi 05N0515 WY USA 44.19644 -107.21337 7/13/2005 M J. h. mearnsi 05N0517 WY USA 44.19644 -107.21337 7/13/2005 M J. h. mearnsi 05N0520 WY USA 44.19644 -107.21337 7/13/2005 M J. h. mearnsi 05N0521 WY USA 44.19644 -107.21337 7/13/2005 M

6

J. h. mearnsi 05N0506 WY USA 7/11/2005 M J. h. mearnsi 05N0508 WY USA 7/12/2005 M J. h. mearnsi 05N0509 WY USA 7/12/2005 M J. h. mearnsi 05N0510 WY USA 7/12/2005 M J. h. oreganus 04N5030 BC Canada 9/2/2004 F J. h. oreganus 04N5057 BC Canada 9/3/2004 F J. h. oreganus 04N5059 BC Canada 9/3/2004 M J. h. oreganus 04N5062 BC Canada 9/3/2004 F J. h. oreganus 04N5013 BC Canada 8/31/2004 F J. h. oreganus 04N5026 BC Canada 9/2/2004 M J. h. oreganus 04N5053 BC Canada 9/3/2004 M J. h. oreganus 04N5058 BC Canada 9/3/2004 M J. h. oreganus 04N5031 BC Canada 9/2/2004 U J. h. oreganus 04N5035 BC Canada 9/2/2004 U J. h. oreganus 04N5043 BC Canada 9/3/2004 U J. h. oreganus 04N5045 BC Canada 9/3/2004 M J. h. oreganus 04N5050 BC Canada 9/3/2004 F J. h. oreganus 04N5051 BC Canada 9/3/2004 F J. h. oreganus 04N5052 BC Canada 9/3/2004 F J. h. oreganus 04N5054 BC Canada 9/3/2004 F J. p. palliatus 06N25557 AZ USA 32.66614 -109.87522 5/31/2006 M J. p. palliatus 06N26535 AZ USA 32.66614 -109.87522 5/31/2006 M J. p. palliatus 04N4220 CHIH Mexico 6/11/2004 M J. p. palliatus 02N9529 DGO Mexico 6/2/2002 M J. p. palliatus 02N9531 DGO Mexico 6/2/2002 M J. p. palliatus 04N4006 DGO Mexico 23.8667 -105.1668 5/26/2004 M J. p. palliatus 01N5790 DGO Mexico 4/24/2001 M J. p. palliatus 02N9539 DGO Mexico 6/2/2002 M J. p. phaeonotus 00N5612 CM Mexico 12/23/2000 F J. p. phaeonotus 00N5615 CM Mexico 12/23/2000 M J. p. phaeonotus 00N5617 CM Mexico 12/23/2000 M J. p. phaeonotus 00N5621 CM Mexico 12/23/2000 U J. p. phaeonotus 00N5622 CM Mexico 12/23/2000 M J. p. phaeonotus 00N5630 CM Mexico 12/23/2000 U J. p. phaeonotus 00N5637 CM Mexico 12/26/2000 U J. p. phaeonotus 01N5948 CM Mexico 5/10/2001 M J. h. thurberi 12-202 CA USA 32.84765 -116.4491 5/11/2012 M J. h. thurberi 12-203 CA USA 32.8645 -116.46175 5/11/2012 M J. h. thurberi 12-204 CA USA 32.86898 -116.46018 5/11/2012 M J. h. thurberi 12-210 CA USA 32.86256 -116.46487 5/11/2012 M J. h. thurberi 12-213 CA USA 32.84879 -116.42419 5/12/2012 M J. h. thurberi 12-215 CA USA 32.84756 -116.42448 5/12/2012 M J. h. thurberi 12-219 CA USA 32.84643 -116.4243 5/12/2012 M J. h. thurberi 14-0497 CA USA 38.82505 -120.37835 6/29/2014 M J. h. thurberi 14-0511 CA USA 38.83284 -120.26291 6/29/2014 M J. h. thurberi 14-0513 CA USA 38.83289 -120.26358 6/29/2014 M

7

J. h. thurberi 14-0514 CA USA 38.83351 -120.26261 6/29/2014 M J. h. thurberi 14-0499 CA USA 38.82297 -120.37865 6/29/2014 M J. h. thurberi 14-0502 CA USA 38.82803 -120.37603 6/29/2014 M J. h. thurberi 14-0507 CA USA 38.83181 -120.263 6/29/2014 M J. h. thurberi 14-0508 CA USA 38.8308 -120.26171 6/29/2014 M J. h. thurberi 14-0510 CA USA 38.8301 -120.26115 6/29/2014 M J. h. thurberi 14-0512 CA USA 38.83284 -120.26291 6/29/2014 M J. h. thurberi 14-0498 CA USA 38.82271 -120.37752 6/29/2014 M J. h. thurberi 14-0500 CA USA 38.82371 -120.37702 6/29/2014 M J. h. thurberi 14-0501 CA USA 38.8285 -120.37569 6/29/2014 M J. h. thurberi 14-0503 CA USA 38.82844 -120.378 6/29/2014 M J. h. thurberi 14-0504 CA USA 38.83266 -120.2731 6/29/2014 M J. h. thurberi 14-0505 CA USA 38.83279 -120.27221 6/29/2014 M J. h. thurberi 14-0506 CA USA 38.83373 -120.27158 6/29/2014 M J. h. thurberi 14-0509 CA USA 38.8301 -120.26115 6/29/2014 M J. h. thurberi 12-200 CA USA 32.84776 -116.44913 5/10/2012 M J. h. thurberi 12-223 CA USA 32.8543 -116.45394 5/12/2012 M J. h. thurberi 12-224 CA USA 32.85575 -116.45451 5/12/2012 M J. h. thurberi 12-228 CA USA 32.84436 -116.44865 5/13/2012 M J. h. thurberi 12-206 CA USA 32.86322 -116.46498 5/11/2012 M J. h. thurberi 12-209 CA USA 32.86256 -116.46487 5/11/2012 M J. h. thurberi 12-211 CA USA 32.83725 -116.45386 5/11/2012 M J. h. thurberi 12-216 CA USA 32.84756 -116.42448 5/12/2012 M J. h. thurberi 12-217 CA USA 32.84692 -116.4245 5/12/2012 M 52

53

8

54 Figure S1. Pairwise plots of the first four principal components from a PCA of 11,698

55 genome-wide SNPs. Marker correspond to phaeonotus (golden ), palliatus

56 (dark ), dorsalis (), caniceps (), mearnsi (dark ), thurberi (

57 ), oreganus (dark blue), aikeni (light ), carolinensis (pistachio green) and

58 hyemalis (dark green). Percentage of variance explained by each PC is noted in the

59 diagonal cells.

60

61

62

9

63 Figure S2. Plot of per-SNP posterior probability of divergence mediated by selection

64 (shown as 1 – Q-value as computed by BayeScan) including all northern forms of junco.

65 Loci above the dotted line (in ) are those below a false discovery rate of 10%.

66

67

68

10

69 Appendix I. 70 List of museum specimens used for morphological and colorimetric measurements. 71 AMNH: American Museum of Natural History; LAMNH: Los Angeles Museum of 72 Natural History; MLZ: Moore Laboratory of Zoology at Occidental College; MVZ: 73 Museum of Vertebrate Zoology, University of California Berkeley; NMNH: National 74 Museum of Natural History; SDMNH: San Diego Museum of Natural History. 75 Catalog Collection Taxon Country State Sex Number AMNH 397940 alticola GU GUAT M AMNH 397941 alticola GU GUAT M AMNH 397942 alticola GU GUAT M AMNH 397943 alticola GU GUAT F AMNH 397945 alticola GU GUAT M AMNH 397946 alticola GU GUAT M AMNH 397947 alticola GU GUAT F AMNH 397948 alticola GU GUAT M AMNH 397953 alticola GU GUAT F AMNH 397954 alticola GU GUAT F AMNH 397955 alticola GU GUAT M AMNH 397956 alticola GU GUAT M AMNH 397957 alticola GU GUAT M AMNH 397958 alticola GU GUAT M AMNH 399317 alticola GU GUAT M AMNH 399318 alticola GU GUAT M AMNH 399319 alticola GU GUAT F AMNH 399320 alticola GU GUAT F AMNH 399321 alticola GU GUAT M AMNH 402875 bairdi MX BCN F AMNH 402877 bairdi MX BCN F AMNH 762795 bairdi MX BCN F AMNH 762796 bairdi MX BCN F AMNH 762801 bairdi MX BCN F AMNH 762803 bairdi MX BCN M AMNH 762807 bairdi MX BCN F AMNH 114055 caniceps US CO F AMNH 402985 caniceps US CO M AMNH 402988 caniceps US CO M AMNH 402954 caniceps US NM M AMNH 402955 caniceps US NM M AMNH 402956 caniceps US NM M AMNH 402957 caniceps US NM M AMNH 402958 caniceps US NM M AMNH 402959 caniceps US NM F

11

AMNH 402960 caniceps US NM F AMNH 402961 caniceps US NM M AMNH 402962 caniceps US NM M AMNH 402964 caniceps US NM M AMNH 402967 caniceps US NM M AMNH 52894 dorsalis US AZ F AMNH 403041 dorsalis US NM M AMNH 403045 dorsalis US NM F AMNH 403047 dorsalis US NM F AMNH 403048 dorsalis US NM F AMNH 403049 dorsalis US NM F AMNH 403051 dorsalis US NM M AMNH 762928 dorsalis US NM F AMNH 119492 hyemalis CA AB F AMNH 402265 hyemalis CA QB F AMNH 762771 insularis MX BCN M AMNH 762773 insularis MX BCN F AMNH 762774 insularis MX BCN F AMNH 762775 insularis MX BCN M AMNH 762777 insularis MX BCN M AMNH 762783 insularis MX BCN F AMNH 762784 insularis MX BCN F AMNH 762785 insularis MX BCN F AMNH 762786 insularis MX BCN M AMNH 762788 insularis MX BCN M AMNH 88827 mearnsi US AZ F AMNH 402904 mearnsi US AZ M AMNH 402907 mearnsi US AZ M AMNH 402908 mearnsi US AZ F AMNH 402912 mearnsi US AZ F AMNH 402913 mearnsi US AZ M AMNH 461710 mearnsi US AZ M AMNH 762815 mearnsi US AZ F AMNH 762820 mearnsi US AZ M AMNH 88576 thurberi US CA F AMNH 88585 thurberi US CA M AMNH 88597 thurberi US CA F AMNH 88607 thurberi US CA M AMNH 88680 thurberi US CA F AMNH 88693 thurberi US CA M AMNH 88695 thurberi US CA F AMNH 88696 thurberi US CA F AMNH 393147 vulcani CR CR M AMNH 393148 vulcani CR CR M AMNH 393151 vulcani CR CR F AMNH 393154 vulcani CR CR F

12

AMNH 393157 vulcani CR CR M AMNH 811944 vulcani CR CR M AMNH 811945 vulcani CR CR F AMNH 811946 vulcani CR CR M AMNH 811947 vulcani CR CR F LAMNH 9311 aikeni US CO M LAMNH 9312 aikeni US CO F LAMNH 4938 aikeni US MT M LAMNH 4939 aikeni US MT F LAMNH 17931 alticola GU GUAT M LAMNH 17932 alticola GU GUAT M LAMNH 17933 alticola GU GUAT F LAMNH 66487 thurberi US CA F LAMNH 66488 thurberi US CA M LAMNH 15661 vulcani CR CR M LAMNH 15662 vulcani CR CR M LAMNH 15663 vulcani CR CR M LAMNH 15664 vulcani CR CR F LAMNH 15665 vulcani CR CR F LAMNH 15666 vulcani CR CR F MLZ 37011 alticola MX CHIS M MLZ 37089 alticola MX CHIS M MLZ 37110 alticola MX CHIS F MLZ 37122 alticola MX CHIS M MLZ 37335 alticola MX CHIS M MLZ 37396 alticola MX CHIS F MLZ 37428 alticola MX CHIS M MLZ 45188 fulvescens MX CHIS M MLZ 45189 fulvescens MX CHIS M MLZ 45190 fulvescens MX CHIS M MLZ 45191 fulvescens MX CHIS M MLZ 45192 fulvescens MX CHIS F MLZ 45193 fulvescens MX CHIS M MLZ 56826 fulvescens MX CHIS M MLZ 56836 fulvescens MX CHIS F MLZ 56839 fulvescens MX CHIS M MLZ 56845 fulvescens MX CHIS F MLZ 56882 fulvescens MX CHIS F MLZ 56885 fulvescens MX CHIS F MLZ 56912 fulvescens MX CHIS F MLZ 56913 fulvescens MX CHIS M MLZ 56920 fulvescens MX CHIS M MLZ 56979 fulvescens MX CHIS M MLZ 57047 fulvescens MX CHIS M MLZ 66869 fulvescens MX CHIS M MLZ 66871 fulvescens MX CHIS M

13

MLZ 9149 palliatus MX CHIH F MLZ 9151 palliatus MX CHIH M MLZ 9152 palliatus MX CHIH F MLZ 9153 palliatus MX CHIH F MLZ 18462 palliatus MX CHIH M MLZ 18464 palliatus MX CHIH F MLZ 18465 palliatus MX CHIH F MLZ 18727 palliatus MX CHIH M MLZ 18728 palliatus MX CHIH M MLZ 18730 palliatus MX CHIH F MLZ 18733 palliatus MX CHIH F MLZ 18938 palliatus MX CHIH M MLZ 19023 palliatus MX CHIH M MLZ 19025 palliatus MX CHIH M MLZ 58335 palliatus MX DGO F MLZ 58349 palliatus MX DGO F MLZ 58383 palliatus MX DGO M MLZ 45911 phaeonotus MX GRO M MLZ 46115 phaeonotus MX GRO M MLZ 46126 phaeonotus MX GRO F MLZ 46143 phaeonotus MX GRO F MLZ 46205 phaeonotus MX GRO M MLZ 46267 phaeonotus MX GRO M MLZ 46270 phaeonotus MX GRO F MLZ 46291 phaeonotus MX GRO M MLZ 46301 phaeonotus MX GRO F MLZ 46310 phaeonotus MX GRO M MLZ 46329 phaeonotus MX GRO M MLZ 46331 phaeonotus MX GRO M MLZ 46332 phaeonotus MX GRO F MLZ 32595 phaeonotus MX MEX F MLZ 32598 phaeonotus MX MEX M MLZ 32600 phaeonotus MX MEX F MLZ 32601 phaeonotus MX MEX F MLZ 32618 phaeonotus MX MEX M MLZ 32644 phaeonotus MX MEX F MLZ 32668 phaeonotus MX MEX F MLZ 32671 phaeonotus MX MEX M MLZ 32853 phaeonotus MX MEX M MLZ 32854 phaeonotus MX MEX F MLZ 32870 phaeonotus MX MEX F MLZ 41233 phaeonotus MX MEX F MLZ 41241 phaeonotus MX MEX M MLZ 41242 phaeonotus MX MEX F MLZ 37781 phaeonotus MX OAX F MLZ 37910 phaeonotus MX OAX M

14

MLZ 37911 phaeonotus MX OAX F MLZ 65357 phaeonotus MX OAX F MLZ 65358 phaeonotus MX OAX M MLZ 57241 phaeonotus MX VER M MLZ 57315 phaeonotus MX VER M MVZ 79982 aikeni US MT M MVZ 98927 aikeni US MT M MVZ 98928 aikeni US MT F MVZ 98929 aikeni US MT F MVZ 98945 aikeni US MT F MVZ 98946 aikeni US MT F MVZ 98949 aikeni US MT M MVZ 98950 aikeni US MT M MVZ 98951 aikeni US MT M MVZ 98952 aikeni US MT M MVZ 98953 aikeni US MT M MVZ 98954 aikeni US MT F MVZ 140262 aikeni US SD M MVZ 80605 alticola MX CHIS M MVZ 55520 bairdi MX BCS M MVZ 55521 bairdi MX BCS F MVZ 55522 bairdi MX BCS M MVZ 55523 bairdi MX BCS M MVZ 55524 bairdi MX BCS M MVZ 55525 bairdi MX BCS F MVZ 55526 bairdi MX BCS M MVZ 55527 bairdi MX BCS M MVZ 55528 bairdi MX BCS M MVZ 55529 bairdi MX BCS F MVZ 55530 bairdi MX BCS M MVZ 55531 bairdi MX BCS M MVZ 55532 bairdi MX BCS F MVZ 55533 bairdi MX BCS F MVZ 55534 bairdi MX BCS M MVZ 55540 bairdi MX BCS M MVZ 55547 bairdi MX BCS M MVZ 133330 caniceps US NV M MVZ 133331 caniceps US NV F MVZ 133332 caniceps US NV M MVZ 133334 caniceps US NV F MVZ 133335 caniceps US NV M MVZ 133336 caniceps US NV F MVZ 133338 caniceps US NV F MVZ 133343 caniceps US NV F MVZ 133344 caniceps US NV F MVZ 27745 dorsalis US AZ F

15

MVZ 58182 dorsalis US AZ M MVZ 58183 dorsalis US AZ M MVZ 58197 dorsalis US AZ M MVZ 58200 dorsalis US AZ M MVZ 58201 dorsalis US AZ M MVZ 58202 dorsalis US AZ M MVZ 58203 dorsalis US AZ F MVZ 58204 dorsalis US AZ M MVZ 83086 dorsalis US AZ F MVZ 106178 dorsalis US AZ M MVZ 59041 dorsalis US NM F? MVZ 106175 dorsalis US NM F MVZ 106176 dorsalis US NM F MVZ 121543 fulvescens MX CHIS M MVZ 121544 fulvescens MX CHIS F MVZ 121545 fulvescens MX CHIS M MVZ 121546 fulvescens MX CHIS M MVZ 121547 fulvescens MX CHIS M MVZ 155375 fulvescens MX CHIS M MVZ 155376 fulvescens MX CHIS M MVZ 60911 hyemalis CA AB M MVZ 60913 hyemalis CA AB F MVZ 60914 hyemalis CA AB F MVZ 60918 hyemalis CA AB M MVZ 60919 hyemalis CA AB M MVZ 60920 hyemalis CA AB F MVZ 60921 hyemalis CA AB M MVZ 129425 hyemalis US AK M MVZ 137476 hyemalis US AK M MVZ 137478 hyemalis US AK M MVZ 141646 hyemalis US AK M MVZ 141647 hyemalis US AK M MVZ 141648 hyemalis US AK M MVZ 141649 hyemalis US AK F MVZ 141650 hyemalis US AK M MVZ 141651 hyemalis US AK F MVZ 141652 hyemalis US AK M MVZ 141653 hyemalis US AK F MVZ 141654 hyemalis US AK F MVZ 141655 hyemalis US AK F MVZ 158636 hyemalis US AK M MVZ 158638 hyemalis US AK M MVZ 98959 mearnsi US MT F MVZ 98960 mearnsi US MT M MVZ 98961 mearnsi US MT M MVZ 98963 mearnsi US MT F

16

MVZ 98965 mearnsi US MT M MVZ 98967 mearnsi US MT M MVZ 98968 mearnsi US MT F MVZ 98970 mearnsi US MT M MVZ 98971 mearnsi US MT M MVZ 98972 mearnsi US MT F MVZ 98973 mearnsi US MT M MVZ 98974 mearnsi US MT M MVZ 98975 mearnsi US MT M MVZ 98976 mearnsi US MT F MVZ 98977 mearnsi US MT F MVZ 98979 mearnsi US MT M MVZ 98981 mearnsi US MT M MVZ 98982 mearnsi US MT F MVZ 98983 mearnsi US MT F MVZ 98984 mearnsi US MT M MVZ 98985 mearnsi US MT F MVZ 98986 mearnsi US MT M MVZ 98987 mearnsi US MT F MVZ 23850 thurberi US CA M MVZ 23852 thurberi US CA F MVZ 23853 thurberi US CA F MVZ 23854 thurberi US CA M MVZ 23855 thurberi US CA F MVZ 23856 thurberi US CA M MVZ 48533 thurberi US CA M MVZ 48534 thurberi US CA M MVZ 48535 thurberi US CA M MVZ 48537 thurberi US CA M MVZ 48538 thurberi US CA F MVZ 62288 thurberi US CA M MVZ 62289 thurberi US CA F MVZ 62290 thurberi US CA M MVZ 63018 thurberi US CA M MVZ 63019 thurberi US CA F NMNH 143998 alticola GU GUAT M NMNH 143999 alticola GU GUAT F NMNH 144000 alticola GU GUAT F NMNH 144002 alticola GU GUAT F NMNH 144004 alticola GU GUAT F NMNH 144006 alticola GU GUAT F NMNH 144007 alticola GU GUAT F NMNH 144008 alticola GU GUAT F NMNH 144009 alticola GU GUAT F NMNH 144012 alticola GU GUAT M NMNH 144013 alticola GU GUAT F

17

NMNH 66902 caniceps US CO M NMNH 197005 caniceps US CO M NMNH 407352 caniceps US CO M NMNH 594743 caniceps US CO F NMNH 158395 caniceps US NV M NMNH 158396 caniceps US NV M NMNH 594831 caniceps US UT F NMNH 338221 carolinensis US GA M NMNH 363310 carolinensis US GA F NMNH 379431 carolinensis US GA M NMNH 337681 carolinensis US KY F NMNH 337683 carolinensis US KY M NMNH 165410 carolinensis US MD M NMNH 301819 carolinensis US NC M NMNH 301830 carolinensis US NC F NMNH 357985 carolinensis US NC F NMNH 594780 carolinensis US NC F NMNH 351546 carolinensis US TN M NMNH 351548 carolinensis US TN F NMNH 351549 carolinensis US TN F NMNH 351553 carolinensis US TN M NMNH 348231 carolinensis US VA M NMNH 348232 carolinensis US VA M NMNH 350791 carolinensis US VA F NMNH 350795 carolinensis US VA F NMNH 357371 carolinensis US VA M NMNH 601455 carolinensis US VA F NMNH 601503 carolinensis US VA F NMNH 348835 carolinensis US WV M NMNH 394306 carolinensis US WV M NMNH 138509 dorsalis US AZ M NMNH 196997 dorsalis US NM M NMNH 143899 fulvescens MX CHIS F NMNH 143900 fulvescens MX CHIS F NMNH 143904 fulvescens MX CHIS M NMNH 143908 fulvescens MX CHIS M NMNH 143910 fulvescens MX CHIS F NMNH 143913 fulvescens MX CHIS M NMNH 194248 fulvescens MX CHIS F NMNH 194249 fulvescens MX CHIS M NMNH 194250 fulvescens MX CHIS F NMNH 157808 hyemalis CA AB M NMNH 183205 hyemalis CA AB F NMNH 183207 hyemalis CA AB F NMNH 239638 hyemalis US AK F NMNH 529945 hyemalis US AK M

18

NMNH 529946 hyemalis US AK F NMNH 529950 hyemalis US AK M NMNH 397093 hyemalis US ME M NMNH 149682 hyemalis US NH M NMNH 381876 hyemalis CA NL M NMNH 381877 hyemalis CA NL F NMNH 381878 hyemalis CA NL F NMNH 394025 hyemalis CA NL M NMNH 394027 hyemalis CA NL M NMNH 394032 hyemalis CA NL F NMNH 351666 hyemalis CA NS M NMNH 351667 hyemalis CA NS M NMNH 351668 hyemalis CA NS F NMNH 192969 hyemalis CA NT F NMNH 194847 hyemalis CA NT F NMNH 197003 hyemalis CA NT F NMNH 594692 hyemalis CA QB M NMNH 397454 mearnsi US ID F NMNH 418382 mearnsi US ID F NMNH 464080 mearnsi US ID F NMNH 268391 mearnsi US MT F NMNH 268392 mearnsi US MT F NMNH 268562 mearnsi US MT F NMNH 268563 mearnsi US MT M NMNH 268564 mearnsi US MT F NMNH 268635 mearnsi US MT M NMNH 268636 mearnsi US MT F NMNH 268637 mearnsi US MT M NMNH 466133 mearnsi US MT M NMNH 138544 mearnsi US WY M NMNH 228420 mearnsi US WY M NMNH 228421 mearnsi US WY M NMNH 481974 mearnsi US WY M NMNH 138578 oreganus US AK F NMNH 164921 oreganus US AK M NMNH 164922 oreganus US AK F NMNH 164924 oreganus US AK M NMNH 165163 oreganus US AK M NMNH 170234 oreganus US AK M NMNH 186190 oreganus US AK M NMNH 186244 oreganus US AK F NMNH 186245 oreganus US AK M NMNH 186246 oreganus US AK M NMNH 186247 oreganus US AK M NMNH 186248 oreganus US AK M NMNH 186249 oreganus US AK F

19

NMNH 186659 oreganus US AK M NMNH 186660 oreganus US AK F NMNH 271183 oreganus US AK F NMNH 286561 oreganus US AK M NMNH 594696 oreganus US AK F NMNH 157701 oreganus CA BC F NMNH 163944 oreganus CA BC F NMNH 166843 oreganus CA BC F NMNH 442631 oreganus US OR F NMNH 363653 oreganus US WA M NMNH 133545 thurberi US CA M NMNH 133546 thurberi US CA M NMNH 133551 thurberi US CA F NMNH 133553 thurberi US CA M NMNH 133554 thurberi US CA M NMNH 133555 thurberi US CA F NMNH 133556 thurberi US CA M NMNH 133856 thurberi US CA M NMNH 133857 thurberi US CA F NMNH 133858 thurberi US CA F NMNH 134269 thurberi US CA F NMNH 134270 thurberi US CA F NMNH 134272 thurberi US CA M NMNH 134273 thurberi US CA F NMNH 134275 thurberi US CA M NMNH 87790 vulcani CR CR F NMNH 199479 vulcani CR CR M NMNH 199483 vulcani CR CR M NMNH 199484 vulcani CR CR M NMNH 199485 vulcani CR CR M NMNH 199486 vulcani CR CR F NMNH 199490 vulcani CR CR F NMNH 199492 vulcani CR CR M NMNH 199493 vulcani CR CR F NMNH 200138 vulcani CR CR M NMNH 200139 vulcani CR CR F NMNH 486209 vulcani CR CR M NMNH 486210 vulcani CR CR F NMNH 486212 vulcani CR CR F NMNH 486213 vulcani CR CR F NMNH 486218 vulcani CR CR F NMNH 486219 vulcani CR CR M NMNH 486222 vulcani CR CR M NMNH 486223 vulcani CR CR M NMNH 486224 vulcani CR CR F NMNH 486225 vulcani CR CR M

20

SDMNH 1025 aikeni US CO M SDMNH 1026 aikeni US CO F SDMNH 28412 aikeni US CO M SDMNH 28413 aikeni US CO F SDMNH 28414 aikeni US CO F SDMNH 28416 aikeni US CO F SDMNH 43930 aikeni CA NB M SDMNH 44244 aikeni CA NB F SDMNH 28693 bairdi MX BCS M SDMNH 28694 bairdi MX BCS F SDMNH 28695 bairdi MX BCS F SDMNH 28696 bairdi MX BCS M SDMNH 34517 bairdi MX BCS M SDMNH 34518 bairdi MX BCS M SDMNH 35378 bairdi MX BCS M SDMNH 35379 bairdi MX BCS M SDMNH 35380 bairdi MX BCS F SDMNH 35382 bairdi MX BCS M SDMNH 35385 bairdi MX BCS F SDMNH 35386 bairdi MX BCS M SDMNH 16256 dorsalis US AZ M SDMNH 16278 dorsalis US AZ M SDMNH 16284 dorsalis US AZ M SDMNH 34483 dorsalis US AZ F SDMNH 34484 dorsalis US AZ F SDMNH 34485 dorsalis US AZ F? SDMNH 12631 insularis MX BCN M SDMNH 12632 insularis MX BCN M SDMNH 12634 insularis MX BCN M SDMNH 12635 insularis MX BCN M SDMNH 12637 insularis MX BCN M SDMNH 12640 insularis MX BCN M SDMNH 14922 insularis MX BCN F SDMNH 14923 insularis MX BCN M SDMNH 14924 insularis MX BCN M SDMNH 14925 insularis MX BCN M SDMNH 14926 insularis MX BCN F SDMNH 14927 insularis MX BCN F SDMNH 14928 insularis MX BCN F SDMNH 14929 insularis MX BCN F SDMNH 14932 insularis MX BCN F SDMNH 14950 insularis MX BCN F SDMNH 14951 insularis MX BCN M SDMNH 28692 insularis MX BCN M SDMNH 34476 insularis MX BCN M SDMNH 34477 insularis MX BCN M

21

SDMNH 1056 palliatus US AZ M SDMNH 14653 palliatus US AZ F SDMNH 14687 palliatus US AZ M SDMNH 14809 palliatus US AZ F SDMNH 14810 palliatus US AZ M SDMNH 14812 palliatus US AZ M SDMNH 14813 palliatus US AZ M SDMNH 15770 palliatus US AZ M SDMNH 15771 palliatus US AZ F SDMNH 15772 palliatus US AZ F SDMNH 15776 palliatus US AZ F SDMNH 15837 palliatus US AZ F SDMNH 34489 palliatus US AZ F SDMNH 34490 palliatus US AZ F SDMNH 34495 palliatus US AZ M SDMNH 34496 palliatus US AZ M SDMNH 34497 palliatus US AZ M SDMNH 34498 palliatus US AZ F SDMNH 34499 palliatus US AZ M SDMNH 34501 palliatus US AZ F 76 77

22

78 Appendix II. R Scripts 79 80 A. Code for computing colorimetric variables from spectral data applying the avian 81 visual model by Stoddard and Prum (2008), based on Goldsmith's (1990) tetrahedral 82 for spectral data. 83 library(pavo) 84 85 ##------86 # Extraction of spectra and AV variables 87 ##------88 89 jspecs <- getspec( 'D:/PhD/5. Papers/3. DEJU_sexual/2017_02_22 90 Sexual_reset/1. Muestrario', 91 ext="txt", decimal=".", subdir = T, subdir.names = 92 T, lim = c(300, 700), fast = T) 93 94 95 #explorespec(jspecs[,c(1:15)], by=3, lwd=2) 96 97 jsplit1 <- strsplit(names(jspecs), "/") 98 vecagg <- array(NA, length(names(jspecs))) 99 100 jsplit2 = jsplit1 101 for (i in 2:length(names(jspecs))){ 102 jsplit2[[i]][3] <- ifelse(jsplit2[[i]][3] == "Spec00000" | 103 jsplit2[[i]][3] == "Spec00001" | jsplit2[[i]][3] == "Spec00002", 104 "Crown", jsplit2[[i]][3]) 105 jsplit2[[i]][3] <- ifelse(jsplit2[[i]][3] == "Spec00003" | 106 jsplit2[[i]][3] == "Spec00004" | jsplit2[[i]][3] == "Spec00005", 107 "Nape", jsplit2[[i]][3]) 108 jsplit2[[i]][3] <- ifelse(jsplit2[[i]][3] == "Spec00006" | 109 jsplit2[[i]][3] == "Spec00007" | jsplit2[[i]][3] == "Spec00008", 110 "Back", jsplit2[[i]][3]) 111 jsplit2[[i]][3] <- ifelse(jsplit2[[i]][3] == "Spec00009" | 112 jsplit2[[i]][3] == "Spec00010" | jsplit2[[i]][3] == "Spec00011", 113 "Breast", jsplit2[[i]][3]) 114 jsplit2[[i]][3] <- ifelse(jsplit2[[i]][3] == "Spec00012" | 115 jsplit2[[i]][3] == "Spec00013" | jsplit2[[i]][3] == "Spec00014", 116 "Belly", jsplit2[[i]][3]) 117 jsplit2[[i]][3] <- ifelse(jsplit2[[i]][3] == "Spec00015", "Flank", 118 jsplit2[[i]][3]) 119 } 120 121 for (i in 2:length(names(jspecs))){ 122 vecagg[i] <- paste(jsplit2[[i]][1], jsplit2[[i]][2], 123 jsplit2[[i]][3], sep="_") 124 } 125 126 jmspecs <- aggspec(jspecs, by=vecagg[-1], FUN=mean) 127 128 #---- For VULCANI 129 jspecs.vulc <- getspec( 'D:/PhD/5. Papers/3. DEJU_sexual/2017_02_22 130 Sexual_reset/1.1 VULCANI', 131 ext="txt", decimal=".", subdir = T, subdir.names = 132 T, lim = c(300, 700), fast = T) 133 134 jsplit1.vulc <- strsplit(names(jspecs.vulc), "/")

23

135 vecagg.vulc <- array(NA, length(names(jspecs.vulc))) 136 137 jsplit2.vulc = jsplit1.vulc 138 for (i in 2:length(names(jspecs.vulc))){ 139 jsplit2.vulc[[i]][3] <- ifelse(jsplit2.vulc[[i]][3] == "Spec00000" | 140 jsplit2.vulc[[i]][3] == "Spec00001" | jsplit2.vulc[[i]][3] == 141 "Spec00002", "Crown", jsplit2.vulc[[i]][3]) 142 jsplit2.vulc[[i]][3] <- ifelse(jsplit2.vulc[[i]][3] == "Spec00003" | 143 jsplit2.vulc[[i]][3] == "Spec00004" | jsplit2.vulc[[i]][3] == 144 "Spec00005", "Nape", jsplit2.vulc[[i]][3]) 145 jsplit2.vulc[[i]][3] <- ifelse(jsplit2.vulc[[i]][3] == "Spec00006" | 146 jsplit2.vulc[[i]][3] == "Spec00007" | jsplit2.vulc[[i]][3] == 147 "Spec00008" 148 | jsplit2.vulc[[i]][3] == "Spec00009" | 149 jsplit2.vulc[[i]][3] == "Spec00010" | jsplit2.vulc[[i]][3] == 150 "Spec00011", "Back", jsplit2.vulc[[i]][3]) 151 jsplit2.vulc[[i]][3] <- ifelse(jsplit2.vulc[[i]][3] == "Spec00012" | 152 jsplit2.vulc[[i]][3] == "Spec00013" | jsplit2.vulc[[i]][3] == 153 "Spec00014", "Breast", jsplit2.vulc[[i]][3]) 154 jsplit2.vulc[[i]][3] <- ifelse(jsplit2.vulc[[i]][3] == "Spec00015" | 155 jsplit2.vulc[[i]][3] == "Spec00016" | jsplit2.vulc[[i]][3] == 156 "Spec00017", "Belly", jsplit2.vulc[[i]][3]) 157 jsplit2.vulc[[i]][3] <- ifelse(jsplit2.vulc[[i]][3] == "Spec00018", 158 "Flank", jsplit2.vulc[[i]][3]) 159 } 160 161 for (i in 2:length(names(jspecs.vulc))){ 162 vecagg.vulc[i] <- paste(jsplit2.vulc[[i]][1], jsplit2.vulc[[i]][2], 163 jsplit2.vulc[[i]][3], sep="_") 164 } 165 166 jmspecs.vulc <- aggspec(jspecs.vulc, by=vecagg.vulc[-1], FUN=mean) 167 168 169 jmspecs.all <- merge(jmspecs, jmspecs.vulc) 170 jmspecs.all.fix <- procspec(jmspecs.all, opt='smooth', span = 0.1, 171 fixneg='addmin') 172 173 ##------174 # Reordering 175 ##------176 jmspecs.table <- t(jmspecs.all.fix) 177 colnames(jmspecs.table) <- jmspecs.table[1,] 178 jmspecs.table <- jmspecs.table[-1,] 179 180 sample.data <- 181 as.data.frame(t(as.data.frame(strsplit(row.names(jmspecs.table), 182 '_')))) 183 colnames(sample.data) <- c('Morph', 'ID', 'Patch') 184 row.names(sample.data) <- c() 185 186 sample.data$Sex <- 187 as.data.frame(sapply(strsplit(as.character(sample.data$ID), ""), tail, 188 1)) 189 colnames(sample.data[,4]) <- 'Sex' 190 row.names(sample.data) <- paste(sample.data$Morph, sample.data$ID, 191 sample.data$Patch, sep = '_') 192 193 194 spectral.data <- merge(sample.data, jmspecs.table, by = 'row.names') 195 spectral.data <- spectral.data[,-1]

24

196 197 spectral.data$ID <- gsub('[A-Z]', '', spectral.data$ID) 198 spectral.data$ID <- factor(as.character(spectral.data$ID)) 199 200 nlevels(spectral.data$ID) 201 202 #---- wl_patch matrix 203 204 spectral.dataset <- matrix(nrow = 665, ncol = 3+401*6 ) 205 206 dimnames(spectral.dataset) <- list(NULL,c("ID","Morph","Sex", 207 paste(300:700, 208 levels(spectral.data$Patch)[1], sep="_"), 209 paste(300:700, 210 levels(spectral.data$Patch)[2], sep="_"), 211 paste(300:700, 212 levels(spectral.data$Patch)[3], sep="_"), 213 paste(300:700, 214 levels(spectral.data$Patch)[4], sep="_"), 215 paste(300:700, 216 levels(spectral.data$Patch)[5], sep="_"), 217 paste(300:700, 218 levels(spectral.data$Patch)[6], sep="_"))) 219 220 spectral.dataset[,1] <- as.character(levels(factor(spectral.data$ID))) 221 222 for (i in 1:nrow(spectral.data)) { 223 224 line <- match(as.character(spectral.data[i,2]), 225 as.character(spectral.dataset[,1])) 226 variables <- paste(300:700, spectral.data[i,3], sep="_") 227 spectral.dataset[line,variables] <- 228 as.character(spectral.data[i,5:405]) 229 230 } 231 232 match <- match(spectral.dataset[,1],spectral.data[,2]) 233 spectral.dataset[,2] <- as.character(spectral.data$Morph[match]) 234 spectral.dataset[,3] <- as.character(spectral.data$Sex[match]) 235 236 write.table(spectral.dataset, 'CompleteSpectra_bypatch.txt', row.names 237 = F, col.names = T, sep = '\t', quote = F) 238 239 ##------240 # Computing AVM variables 241 ##------242 cone.sim <- vismodel(jmspecs.all.fix, 243 visual = "bluetit", 244 achromatic = "bt.dc", 245 illum = "forestshade", 246 trans = "bluetit", 247 qcatch = "Qi", 248 bkg = "ideal", 249 vonkries = T, 250 relative = T) 251 252 visual.avm <- colspace(cone.sim, space = "auto") 253 norm.bright <- as.data.frame(colSums(jmspecs.all.fix[,2:3991])/401) 254 colnames(norm.bright) <- 'Norm_Bright' 255 visual <- merge(visual.avm, norm.bright, by = 'row.names') 256 row.names(visual) <- visual$Row.names

25

257 visual <- visual[,-1] 258 259 visual.data <- 260 as.data.frame(t(as.data.frame(strsplit(row.names(visual), '_')))) 261 colnames(visual.data) <- c('Morph', 'ID', 'Patch') 262 row.names(visual.data) <- c() 263 264 visual.data$Sex <- 265 as.data.frame(sapply(strsplit(as.character(visual.data$ID), ""), tail, 266 1)) 267 colnames(visual.data[,4]) <- 'Sex' 268 row.names(visual.data) <- paste(visual.data$Morph, visual.data$ID, 269 visual.data$Patch, sep = '_') 270 271 272 avm.variables <- merge(visual.data, visual, by = 'row.names') 273 avm.variables <- avm.variables[,-1] 274 275 avm.variables$ID <- gsub('[A-Z]', '', avm.variables$ID) 276 avm.variables$ID <- factor(as.character(avm.variables$ID)) 277 278 nlevels(avm.variables$ID) 279 280 #---- Ordering dataset 281 avm.dataset <- matrix(nrow = 665, ncol = 3+17*6 ) 282 283 dimnames(avm.dataset) <- list(NULL,c("ID","Morph","Sex", 284 paste(colnames(visual), 285 levels(avm.variables$Patch)[1], sep="_"), 286 paste(colnames(visual), 287 levels(avm.variables$Patch)[2], sep="_"), 288 paste(colnames(visual), 289 levels(avm.variables$Patch)[3], sep="_"), 290 paste(colnames(visual), 291 levels(avm.variables$Patch)[4], sep="_"), 292 paste(colnames(visual), 293 levels(avm.variables$Patch)[5], sep="_"), 294 paste(colnames(visual), 295 levels(avm.variables$Patch)[6], sep="_"))) 296 297 avm.dataset[,1] <- as.character(levels(factor(avm.variables$ID))) 298 299 300 for (i in 1:nrow(avm.variables)) { 301 302 line <- match(as.character(avm.variables[i,2]), 303 as.character(avm.dataset[,1])) 304 variables <- paste(colnames(visual), avm.variables[i,3], sep="_") 305 avm.dataset[line,variables] <- as.character(avm.variables[i,5:21]) 306 307 } 308 309 match <- match(avm.dataset[,1],avm.variables[,2]) 310 avm.dataset[,2] <- as.character(avm.variables$Morph[match]) 311 avm.dataset[,3] <- as.character(avm.variables$Sex[match]) 312 313 write.table(avm.dataset, 'CompleteAVM_bypatch.txt', row.names = F, 314 col.names = T, sep = '\t', quote = F) 315 316 #---- Only informtaive AVM variables 317 avm.4varsdataset <- avm.dataset[,1:3]

26

318 319 for (i in 0:5) { 320 321 avm.4varsdataset <- cbind(avm.4varsdataset, avm.dataset[ 322 ,c((3+(12+(i*17))), (3+(13+(i*17))), (3+(16+(i*17))), 323 (3+(17+(i*17))))]) 324 325 } 326 327 write.table(avm.4varsdataset, 'AVM_4variables_CompleteSet.txt', 328 row.names = F, col.names = T, quote = F, sep = '\t') 329 330 331 B. Code for phenotypic analysis including multivariate analyses on morphometric and 332 colorimetric data, Mahalanobis distances and Mantel tests. 333 334 library(MASS) 335 library(xlsx) 336 library(SNPRelate) 337 library(vegan) 338 library(klaR)# Detach after use, masks rda vegan function 339 library(HDMD) 340 library(fields) 341 library(gdata) 342 343 ##------344 # 1. Morphometrics, per species DFA and mahalanobis 345 ##------346 347 sample.list <- read.table('RAD_samplelist.txt', header = T) 348 museum.data <- read.xlsx('Data_museums.xlsx', header = T, 349 as.data.frame = T, sheetName = 'Data_museums') 350 351 sample.data <- merge(sample.list, museum.data) 352 names(sample.data) 353 sample.data <- sample.data[,c(1:3, 12:14, 16:18)] 354 355 males.data <- na.omit(sample.data[sample.data$Sex=='M',]) 356 males.data$ID <- as.character(males.data$ID) 357 males.data[,4:9] <- data.frame(sapply(males.data[4:9], function(x) 358 as.numeric(as.character(x)))) 359 males.data[,4:9] <- log(males.data[,4:9]) 360 males.data <- droplevels(males.data) 361 str(males.data) 362 363 males.data$Species <- factor(males.data$Species) 364 speciesdfa.rad <- lda(Species ~ Wing + Tail + Tar + Xcul + Width + 365 Depth, males.data) 366 367 capture.output(speciesdfa.rad, 368 file='DFAmorph_radperspecies_allvariables_REPORT.txt') 369 370 color.figure <- read.table('colors_figure.txt', header = T, row.names 371 = 1, sep = '\t') 372 color.figure <- color.figure[order(row.names(color.figure)),] 373 colvec <- rgb(red = color.figure, names = row.names(color.figure), 374 maxColorValue = 255) 375

27

376 speciesdfa.rad.LD1<-predict(speciesdfa.rad)$x[,1] 377 speciesdfa.rad.LD2<-predict(speciesdfa.rad)$x[,2] 378 speciesdfa.rad.LD3<-predict(speciesdfa.rad)$x[,3] 379 speciesdfa.rad.LD4<-predict(speciesdfa.rad)$x[,4] 380 speciesdfa.rad.LD5<-predict(speciesdfa.rad)$x[,5] 381 speciesdfa.rad.LD6<-predict(speciesdfa.rad)$x[,6] 382 383 speciesdfa.rad.LD <- cbind( 384 speciesdfa.rad.LD1, 385 speciesdfa.rad.LD2, 386 speciesdfa.rad.LD3, 387 speciesdfa.rad.LD4 388 ) 389 390 speciesdfa.rad.centroids <- aggregate(speciesdfa.rad.LD ~ Species, 391 males.data, mean) 392 colnames(speciesdfa.rad.centroids) <- c('Species', 'LD1', 'LD2', 393 'LD3', 'LD4') 394 395 plot(speciesdfa.rad.LD2, speciesdfa.rad.LD1, col = 396 colvec[males.data$Species], pch = '') 397 points(speciesdfa.rad.LD2, speciesdfa.rad.LD1, col = 398 colvec[males.data$Species], pch = 16) 399 points(speciesdfa.rad.centroids$LD2, speciesdfa.rad.centroids$LD1, col 400 = '', pch = 15) 401 legend("topright", legend=levels(males.data$Species), pch= 16, 402 col=colvec) 403 404 plot(speciesdfa.rad.LD3, speciesdfa.rad.LD4, col = 405 colvec[males.data$Species], pch = '') 406 points(speciesdfa.rad.LD3, speciesdfa.rad.LD4, col = 407 colvec[males.data$Species], pch = 16) 408 points(speciesdfa.rad.centroids$LD3, speciesdfa.rad.centroids$LD4, col 409 = 'black', pch = 15) 410 legend("topright", legend=levels(males.data$Species), pch= 16, 411 col=colvec) 412 413 414 ## Mahalanobis 415 males.mahamorph <- males.data[,4:9] 416 species <- as.factor(males.data$Species) 417 maha.morphtest <- pairwise.mahalanobis(males.mahamorph, grouping = 418 species, digits = 4 ) 419 maha.morphtest 420 421 D.mahalanobis.morph <- sqrt(maha.morphtest$distance) 422 colnames(D.mahalanobis.morph) <- row.names(maha.morphtest$means) 423 row.names(D.mahalanobis.morph) <- row.names(maha.morphtest$means) 424 425 ##------426 # 2. Color DFA 427 ##------428 429 ## Building dataset 430 avm4.complete <- read.table('AVM_4variables_COMPLETE.txt', header = T) 431 avm4.complete[,1:3] <- lapply(avm4.complete[,1:3], as.character) 432 433 avm4.df <- avm4.complete[avm4.complete$Species %in% c('F_PHAEONOTUS', 434 'G_PALLIATUS', 435 'H_DORSALIS', 436 'I_CANICEPS',

28

437 'J_THURBERI', 438 'J_OREGANUS', 439 'K_MEARNSI', 440 'L_AIKENI', 441 'M_CAROLINENSIS', 442 'N_HYEMALIS'),] 443 444 ## Remove outliers with boxplot 445 avm4.split.list <- split(avm4.df, avm4.df$Species) 446 for (i in 1:length(avm4.split.list)) { 447 for (k in 4:27) { 448 449 outliers <- boxplot.stats(avm4.split.list[[i]][[k]], coef = 450 10)$out 451 avm4.split.list[[i]][[k]] <- replace(avm4.split.list[[i]][[k]], 452 avm4.split.list[[i]][[k]] %in% outliers, NA) 453 454 } 455 } 456 457 458 avm4.rmout.df <- do.call('rbind', avm4.split.list) 459 avm4.rmout.df <- na.omit(avm4.rmout.df) 460 row.names(avm4.rmout.df) <- seq(1:nrow(avm4.rmout.df)) 461 avm4.rmout.df$Species <- as.factor(avm4.rmout.df$Species) 462 463 avm4.rmout.males.df <- avm4.rmout.df[avm4.rmout.df$Sex=='M',] 464 str(avm4.rmout.males.df) 465 466 ## DFA 467 avm4.radspecies.dfa <- lda(Species ~., avm4.rmout.males.df[,c(2, 468 4:ncol(avm4.rmout.males.df))]) 469 avm4.radspecies.dfa 470 471 capture.output(avm4.radspecies.dfa, 472 file='DFAcolor_radperspecies_allvariables_REPORT.txt') 473 474 avm4.radspeciesdfa.LD1<-predict(avm4.radspecies.dfa)$x[,1] 475 avm4.radspeciesdfa.LD2<-predict(avm4.radspecies.dfa)$x[,2] 476 avm4.radspeciesdfa.LD3<-predict(avm4.radspecies.dfa)$x[,3] 477 avm4.radspeciesdfa.LD4<-predict(avm4.radspecies.dfa)$x[,4] 478 radspeciesdfa.centroids <- aggregate(cbind( 479 avm4.radspeciesdfa.LD1, avm4.radspeciesdfa.LD2, 480 avm4.radspeciesdfa.LD3, avm4.radspeciesdfa.LD4) 481 ~ Species, avm4.rmout.males.df, mean) 482 colnames(radspeciesdfa.centroids) <- c('Species', 'LD1', 'LD2', 'LD3', 483 'LD4') 484 485 plot(-avm4.radspeciesdfa.LD2, avm4.radspeciesdfa.LD1, col = 486 colvec[avm4.rmout.males.df$Species], pch = '') 487 points(-avm4.radspeciesdfa.LD2, avm4.radspeciesdfa.LD1, col = 488 colvec[avm4.rmout.males.df$Species], pch = 16) 489 points(-radspeciesdfa.centroids$LD2, radspeciesdfa.centroids$LD1, col 490 = 'black', pch = 15) 491 legend("bottomleft", legend=levels(avm4.rmout.males.df$Species), pch= 492 16, col=colvec) 493 494 plot(-avm4.radspeciesdfa.LD3, avm4.radspeciesdfa.LD4, col = 495 colvec[avm4.rmout.males.df$Species], pch = '') 496 points(-avm4.radspeciesdfa.LD3, avm4.radspeciesdfa.LD4, col = 497 colvec[avm4.rmout.males.df$Species], pch = 16)

29

498 points(-radspeciesdfa.centroids$LD3, radspeciesdfa.centroids$LD4, col 499 = 'black', pch = 15) 500 legend("bottomright", legend=levels(avm4.rmout.males.df$Species), pch= 501 16, col=colvec) 502 503 dfa.radspecies.distanceDF <- 504 as.matrix(dist(radspeciesdfa.centroids[,2:3], method = "euclidean", 505 diag = T, upper = T)) 506 colnames(dfa.radspecies.distanceDF) <- radspeciesdfa.centroids$Species 507 rownames(dfa.radspecies.distanceDF) <- radspeciesdfa.centroids$Species 508 509 ## Mahalanobis 510 males.mahacolor <- avm4.rmout.males.df[,4:ncol(avm4.rmout.males.df)] 511 species <- as.factor(avm4.rmout.males.df$Species) 512 maha.colortest <- pairwise.mahalanobis(males.mahacolor, grouping = 513 species, digits = 4 ) 514 515 D.mahalanobis.color <- sqrt(maha.colortest$distance) 516 colnames(D.mahalanobis.color) <- row.names(maha.colortest$means) 517 row.names(D.mahalanobis.color) <- row.names(maha.colortest$means) 518 D.mahalanobis.color 519 520 521 ##------522 ## 3. DFA by sex and DimScore computation 523 ##------524 525 ## Building dataset, removing outliers and DFA 526 ##------527 avm4.complete <- read.table('AVM_4variables_COMPLETE.txt', header = T) 528 avm4.complete[,1:3] <- lapply(avm4.complete[,1:3], as.character) 529 avm4.complete <-avm4.complete[avm4.complete$Species %in% 530 c('A_VULCANI', 531 'B_BAIRDI', 532 533 'C_ALTICOLA', 534 535 'D_INSULARIS', 536 537 'E_FULVESCENS', 538 539 'F_PHAEONOTUS', 540 541 'G_PALLIATUS', 542 543 'H_DORSALIS', 544 545 'I_CANICEPS', 546 547 'J_THURBERI', 548 549 'J_OREGANUS', 550 551 'K_MEARNSI', 552 'L_AIKENI', 553 554 'M_CAROLINENSIS', 555 556 'N_HYEMALIS'),] 557 558 avm4.split.list <- split(avm4.complete, avm4.complete$Species)

30

559 for (i in 1:length(avm4.split.list)) { 560 for (k in 4:27) { 561 562 outliers <- boxplot.stats(avm4.split.list[[i]][[k]], coef = 563 10)$out 564 avm4.split.list[[i]][[k]] <- replace(avm4.split.list[[i]][[k]], 565 avm4.split.list[[i]][[k]] %in% outliers, NA) 566 567 } 568 } 569 570 571 avm4.rmout.complete <- do.call('rbind', avm4.split.list) 572 avm4.rmout.complete <- na.omit(avm4.rmout.complete) 573 row.names(avm4.rmout.complete) <- seq(1:nrow(avm4.rmout.complete)) 574 avm4.rmout.complete$Species <- 575 droplevels(as.factor(avm4.rmout.complete$Species)) 576 summary(avm4.rmout.complete$Species) 577 578 ## DFA 579 avm4.allsexdfa <- lda(Sex ~., 580 avm4.rmout.complete[,3:ncol(avm4.rmout.complete)]) 581 582 avm4.allsexdfa.LD <- as.data.frame(predict(avm4.allsexdfa)$x[,1]) 583 colnames(avm4.allsexdfa.LD) <- 'DF_1' 584 avm4.rmout.complete_DF1.df <- cbind(avm4.rmout.complete[,1:3], 585 avm4.allsexdfa.LD) 586 587 588 ## Compute DimScore for lineal regression with LAT 589 ##------590 # Males 591 colormales.allsexdfa.df <- 592 avm4.rmout.complete_DF1.df[avm4.rmout.complete_DF1.df$Sex == 'M', c(2, 593 4)] 594 colormales.allsexdfa.mean <- aggregate(colormales.allsexdfa.df[,2], 595 list(colormales.allsexdfa.df$Species), mean) 596 597 colnames(colormales.allsexdfa.mean)[1:2] <- c('Species', 'Mean_DF1') 598 row.names(colormales.allsexdfa.mean) <- 599 colormales.allsexdfa.mean$Species 600 colormales.allsexdfa.mean <- 601 colormales.allsexdfa.mean[order(row.names(colormales.allsexdfa.mean)), 602 ] 603 604 # Females 605 colorfemales.allsexdfa.df <- 606 avm4.rmout.complete_DF1.df[avm4.rmout.complete_DF1.df$Sex == 'F', c(2, 607 4)] 608 colorfemales.allsexdfa.mean <- 609 aggregate(colorfemales.allsexdfa.df[,2], 610 list(colorfemales.allsexdfa.df$Species), mean) 611 612 colnames(colorfemales.allsexdfa.mean)[1:2] <- c('Species', 'Mean_DF1') 613 row.names(colorfemales.allsexdfa.mean) <- 614 colorfemales.allsexdfa.mean$Species 615 colorfemales.allsexdfa.mean <- 616 colorfemales.allsexdfa.mean[order(row.names(colorfemales.allsexdfa.mea 617 n)), ] 618 619 # Dichromatism score and Dim dataset computation

31

620 dimscore.allsexdfa.df <- 621 as.data.frame(abs(colorfemales.allsexdfa.mean$Mean_DF1 - 622 colormales.allsexdfa.mean$Mean_DF1)) 623 names(dimscore.allsexdfa.df) <- 'Dim_score' 624 row.names(dimscore.allsexdfa.df) <- 625 row.names(colorfemales.allsexdfa.mean) 626 summary(dimscore.allsexdfa.df) 627 628 ## Linear regression with LAT 629 ##------630 color.figure.all <- read.table('colors_figure_ALL.txt', header = T, 631 row.names = 1, sep = '\t') 632 color.figure.all <- 633 color.figure.all[order(row.names(color.figure.all)),] 634 colvec.all <- rgb(red = color.figure.all, names = 635 row.names(color.figure.all), maxColorValue = 255) 636 637 gbs.alllat.centroids <- read.table('GBSsamples_latcentroids.txt', 638 header = T) 639 gbs.alllat.centroids <- 640 gbs.alllat.centroids[gbs.alllat.centroids$Species %in% 641 row.names(dimscore.allsexdfa.df),] 642 643 dimscore.allsexdfa.lat <- cbind(dimscore.allsexdfa.df, 644 gbs.alllat.centroids) 645 dimscore.allsexdfa.lat$Species <- 646 droplevels(as.factor(dimscore.allsexdfa.lat$Species)) 647 str(dimscore.allsexdfa.lat) 648 649 plot(dimscore.allsexdfa.lat$LAT, dimscore.allsexdfa.lat$Dim_score, 650 pch= 16, 651 xlab = 'Latitude', ylab = 'Dimorphism Score', 652 col = colvec.all[dimscore.allsexdfa.lat$Species] 653 ) 654 abline(lm(Dim_score ~ LAT, dimscore.allsexdfa.lat)) 655 text(dimscore.allsexdfa.lat$LAT, 656 dimscore.allsexdfa.lat$Dim_score+0.02, 657 labels = dimscore.allsexdfa.lat$Species) 658 659 dimlat.allsexdfa.reg <- lm(Dim_score ~ LAT, dimscore.allsexdfa.lat) 660 summary(dimlat.allsexdfa.reg) 661 cor(dimscore.allsexdfa.lat$Dim_score, dimscore.allsexdfa.lat$LAT) 662 663 ## Standardize and center 664 ##------665 avm4.rmout.complete_DF1.df$DF_meanCR <- vector(mode = 'numeric', 666 length = nrow(avm4.rmout.complete_DF1.df )) 667 avm4.rmout.complete_DF1.list <- split(avm4.rmout.complete_DF1.df , 668 avm4.rmout.complete_DF1.df$Species) 669 670 for (i in 1:length(unique(avm4.rmout.complete_DF1.df$Species))) { 671 meancorrect.list <- 672 by(avm4.rmout.complete_DF1.list[[i]][[ncol(avm4.rmout.complete_DF1.df) 673 -1]], 674 avm4.rmout.complete_DF1.list[[i]][[3]], mean) 675 meancorrect.factor <- ((as.numeric(meancorrect.list[2]) - 676 as.numeric(meancorrect.list[1]))/2) + as.numeric(meancorrect.list[1]) 677 678 avm4.rmout.complete_DF1.list[[i]][[ncol(avm4.rmout.complete_DF1.df)]] 679 <- as.numeric(

32

680 681 avm4.rmout.complete_DF1.list[[i]][[ncol(avm4.rmout.complete_DF1.df)- 682 1]]) - meancorrect.factor 683 } 684 685 avm4.rmout.complete_DF1CR.df <- do.call('rbind', 686 avm4.rmout.complete_DF1.list) 687 avm4.rmout.complete_DF1CR.df <- 688 avm4.rmout.complete_DF1CR.df[order(avm4.rmout.complete_DF1CR.df$Specie 689 s),] 690 row.names(avm4.rmout.complete_DF1CR.df) <- 691 seq(1:nrow(avm4.rmout.complete_DF1CR.df)) 692 693 ## Write table to plot male - female differences 694 write.table(avm4.rmout.complete_DF1CR.df[,c(2:3, 695 ncol(avm4.rmout.complete_DF1CR.df))], 696 'DisfactorCR_allsexdfa_allvar.txt', 697 row.names = F, col.names = T, sep = '\t', quote = F) 698 699 700 ##------701 ## 4. DimScore computation 702 ##------703 avm4.dimscore <- avm4.rmout.df[avm4.rmout.df$Sex == 'F' | 704 avm4.rmout.df$Sex == 'M',] 705 colnames(avm4.dimscore) 706 707 sexdfa.rad<- lda(Sex ~ ., avm4.dimscore[,c(3, 4:ncol(avm4.dimscore))]) 708 LDsexrad <- as.data.frame(predict(sexdfa.rad)$x[,1]) 709 colnames(LDsexrad) <- 'DF_1' 710 datasetrad_DF1.df <- cbind(avm4.dimscore, LDsexrad) 711 712 datasetrad_DF1.df$DF_meanCR <- vector(mode = 'numeric', length = 713 nrow(datasetrad_DF1.df )) 714 datasetrad_DF1.list <- split(datasetrad_DF1.df , 715 datasetrad_DF1.df$Species) 716 colnames(datasetrad_DF1.df) 717 718 for (i in 1:length(unique(datasetrad_DF1.df$Species))) { 719 720 meancorrect.list <- by(datasetrad_DF1.list[[i]][[28]], 721 datasetrad_DF1.list[[i]][[3]], mean) 722 meancorrect.factor <- ((as.numeric(meancorrect.list[2]) - 723 as.numeric(meancorrect.list[1]))/2) + as.numeric(meancorrect.list[1]) 724 datasetrad_DF1.list[[i]][[29]] <- 725 as.numeric(datasetrad_DF1.list[[i]][[28]]) - meancorrect.factor 726 727 } 728 729 datasetrad_DF1CR.df <- do.call('rbind', datasetrad_DF1.list) 730 datasetrad_DF1CR.df <- 731 datasetrad_DF1CR.df[order(datasetrad_DF1CR.df$Species),] 732 row.names(datasetrad_DF1CR.df) <- seq(1:nrow(datasetrad_DF1CR.df)) 733 734 ## DimScore 735 # Males 736 color.malesrad <- datasetrad_DF1CR.df[datasetrad_DF1CR.df$Sex == 'M', 737 c(2, 4:29)] 738 colormalesrad.mean <- aggregate(color.malesrad[, 2:27], 739 list(color.malesrad$Species), mean) 740

33

741 colnames(colormalesrad.mean)[1] <- 'Species' 742 row.names(colormalesrad.mean) <- colormalesrad.mean$Species 743 colormalesrad.mean <- 744 colormalesrad.mean[order(row.names(colormalesrad.mean)), ] 745 746 # Females 747 color.femalesrad <- datasetrad_DF1CR.df[datasetrad_DF1CR.df$Sex == 748 'F', c(2, 4:29)] 749 colorfemalesrad.mean <- aggregate(color.femalesrad[, 2:27], 750 list(color.femalesrad$Species), mean) 751 752 colnames(colorfemalesrad.mean)[1] <- 'Species' 753 row.names(colorfemalesrad.mean) <- colorfemalesrad.mean$Species 754 colorfemalesrad.mean <- 755 colorfemalesrad.mean[order(row.names(colorfemalesrad.mean)), ] 756 757 # Computing Dichromatism score 758 dim.scorerad <- as.data.frame(abs(colorfemalesrad.mean$DF_meanCR - 759 colormalesrad.mean$DF_meanCR)) 760 names(dim.scorerad) <- 'Dim_score' 761 row.names(dim.scorerad) <- row.names(colormalesrad.mean) 762 write.table(dim.scorerad, 'DimScore_radiation.txt', col.names = T, 763 row.names = T, sep = '\t', quote = F) 764 765 ##------766 # 3. Mantel tests 767 ##------768 dim.distances <- read.table('DimScoreSUM_RadALLVAR.txt', header=T, 769 sep='\t', dec='.', row.names = 1) 770 colnames(dim.distances) <- row.names(dim.distances) 771 772 nei.distances <- read.table('nei_rad.txt', header=T, sep='\t', 773 dec='.', row.names = 1) 774 775 776 ## Mantel Mahalanobis color vs. Dim 777 mantel.mahacolordim <- mantel(D.mahalanobis.color, dim.distances, 778 permutations=9999) 779 mantel.mahacolordim 780 781 part.mantel.mahacolordim <- mantel.partial(xdis=D.mahalanobis.color, 782 ydis=dim.distances, zdis=nei.distances, permutations=9999) 783 part.mantel.mahacolordim 784 785 mantel_error.NEIdim <- mantel(dim.distances, nei.distances, 786 permutations=9999) 787 mantel_error.NEIdim 788 789 790 ##------791 # 4. Data plotting 792 ##------793 794 ##----- Heatmap 795 D.mahalanobis.morph <- 796 D.mahalanobis.morph[dim(D.mahalanobis.morph)[1]:1,dim(D.mahalanobis.mo 797 rph)[1]:1] 798 D.mahalanobis.color <- 799 D.mahalanobis.color[dim(D.mahalanobis.color)[1]:1,dim(D.mahalanobis.co 800 lor)[1]:1] 801

34

802 D.color.morph <- lowerUpper(D.mahalanobis.morph, D.mahalanobis.color, 803 diff = F) 804 Plot.color.morph <- apply(X = D.color.morph, MARGIN = 2, rev) 805 806 text_grid <- apply(X=D.color.morph, MARGIN = 2, rev) 807 text_grid <- t(text_grid) 808 image.plot(z = t(Plot.color.morph), 809 col = rev(heat.colors(100)), 810 cex = 0.7, legend.cex = 0.7) 811 box() 812 813 for (x in 1:10){ 814 for (y in 1:10){ 815 text((x-1)/9, (y-1)/9, sprintf("%0.2f", text_grid[x,y]), cex = 816 0.7) 817 } 818 } 819 820 821 ##------Linear regression with Nei 822 823 # Names 824 names.matrix <- matrix(nrow = 10, ncol = 10) 825 for (x in 1:10){ 826 for (y in 1:10){ 827 names.matrix[x,y] <- paste(row.names(D.mahalanobis.color)[y], 828 colnames(D.mahalanobis.color)[x], 829 sep = 'vs') 830 } 831 } 832 upperTriangle(names.matrix, diag=T) = NA 833 834 # Nei 835 nei.distances <- 836 nei.distances[dim(nei.distances)[1]:1,dim(nei.distances)[1]:1] 837 nei.matrix <- as.matrix(nei.distances) 838 upperTriangle(nei.matrix, diag=T) = NA 839 840 # Color 841 color.matrix <- D.mahalanobis.color 842 upperTriangle(color.matrix, diag=T) = NA 843 844 # Morph 845 morph.matrix <- D.mahalanobis.morph 846 upperTriangle(morph.matrix, diag=T) = NA 847 848 # Build matrix for linear regression 849 lineal.matrix <- matrix(nrow = 3, ncol = 45) 850 colnames(lineal.matrix) <- na.omit(c(names.matrix)) 851 row.names(lineal.matrix) <- c('Nei', 'Color', 'Morph') 852 853 lineal.matrix[1,] <- na.omit(c(nei.matrix)) 854 lineal.matrix[2,] <- na.omit(c(color.matrix)) 855 lineal.matrix[3,] <- na.omit(c(morph.matrix)) 856 857 # Linear regression 858 lineal.df <- as.data.frame(t(lineal.matrix)) 859 860 plot(lineal.df$Color ~ lineal.df$Nei, pch='', 861 xlim=c(min(lineal.df$Nei-0.005), max(lineal.df$Nei+0.005)), 862 ylim=c(min(lineal.df$Morph-0.5), max(lineal.df$Color+0.5)),

35

863 xlab="Nei's Distance", 864 ylab='Mahalanobis distance') 865 points(x=lineal.df$Nei, y=lineal.df$Color, pch =16, 866 col ='chartreuse3' 867 ) 868 lm.colornei <- lm(Color ~ Nei, lineal.df) 869 abline(lm.colornei) 870 871 summary(lm.colornei) 872 cor(lineal.df$Color, lineal.df$Nei) 873 874 875 points(x=lineal.df$Nei, y=lineal.df$Morph, pch =16, 876 col ='chocolate1' 877 ) 878 lm.morphnei <- lm(Morph ~ Nei, lineal.df) 879 abline(lm.morphnei) 880 881 summary(lm.morphnei) 882 cor(lineal.df$Morph, lineal.df$Nei) 883 884

885 C. Code for conducting a PCA on selectively neutral, independent SNPs and FST and 886 Nei’s genetic distance computation. 887 library(SNPRelate) 888 library(gdsfmt) 889 library(seqRFLP) 890 library(coda) 891 892 #------893 ## LD pruning and PCA 894 #------895 vcf.fn <- 896 'SexualSelDEJUYEJU_biall_dp450_q40_maf002_ppmiss075_hwe00001.vcf' 897 snpgdsVCF2GDS (vcf.fn, "snps2.gds", method="copy.num.of.ref") 898 snpgdsSummary("snps2.gds") 899 900 genofile <- openfn.gds('snps2.gds') 901 sample.id <- read.gdsn(index.gdsn(genofile, "sample.id")) 902 903 snpset <- snpgdsLDpruning(genofile, ld.threshold=0.2, autosome.only = 904 FALSE) 905 snpset.id <- unlist(snpset) 906 907 # Load a file with the pops 908 gbs.dataset <- read.table ('DEJUYEJUx8_samplepopnames.txt', header = 909 F, sep = '\t') 910 colnames(gbs.dataset) <- c('GBS.name', 'Pop') 911 912 samples.dataset <- subset(gbs.dataset, GBS.name %in% sample.id) 913 samples.sorted <- samples.dataset[order(samples.dataset$GBS.name), ] 914 samples.sorted.unique <- subset (samples.sorted, 915 !duplicated(samples.sorted$GBS.name)) 916 917 pop_code <- samples.sorted.unique$Pop 918 pop_code <- factor(pop_code) 919 unique(pop_code) 920

36

921 # PCA 922 pca <- snpgdsPCA(genofile, sample.id = NULL, autosome.only = FALSE, 923 #snp.id = snpset.id, 924 remove.monosnp = TRUE, maf = NaN, missing.rate = NaN, 925 eigen.cnt = 32, 926 num.thread = 1, bayesian = FALSE, need.genmat = 927 FALSE, 928 genmat.only = FALSE, verbose = TRUE) 929 930 # Saving eigenvectors for plotting 931 tab <- data.frame(sample.id = pca$sample.id, 932 pop = factor(pop_code)[match(pca$sample.id, 933 sample.id)], 934 EV1 = pca$eigenvect[,1], 935 EV2 = pca$eigenvect[,2], 936 EV3 = pca$eigenvect[,3], 937 EV4 = pca$eigenvect[,4], 938 EV5 = pca$eigenvect[,5], 939 EV6 = pca$eigenvect[,6], 940 EV7 = pca$eigenvect[,7], 941 EV8 = pca$eigenvect[,8], 942 EV9 = pca$eigenvect[,9], 943 EV10 = pca$eigenvect[,10], 944 stringsAsFactors = FALSE) 945 946 # Writting out EVs for other analyses 947 write.table (tab, file = 'Eigenvectors.txt', row.names = F, sep = 948 '\t', quote = F) 949 950 # Setting colors 951 color.figure <- read.table('colors_figure.txt', header = T, row.names 952 = 1, sep = '\t') 953 color.figure <- color.figure[order(row.names(color.figure)),] 954 colvec <- rgb(red = color.figure, names = row.names(color.figure), 955 maxColorValue = 255) 956 tab <- tab[order(tab$pop),] 957 958 # Plotting 2 PCs 959 plot(x=tab$EV1, y=tab$EV2, xlab="PC1", ylab="PC2", pch='') 960 points(x=tab$EV1, y=tab$EV2, pch =21, 961 col = colvec[tab$pop], 962 bg = colvec[tab$pop] 963 ) 964 965 legend("bottomleft", legend=row.names(color.figure), col=colvec, pch = 966 16) 967 968 # PCA_centroids 969 all.centroids.pca <- aggregate(cbind(tab$EV1, tab$EV2, tab$EV3) ~ pop, 970 tab, mean) 971 row.names(all.centroids.pca) <- all.centroids.pca$pop 972 points(x=all.centroids.pca$V1, y=-all.centroids.pca$V2, col= 'Black', 973 pch = 15) 974 975 # Plotting multiple PCs and explained percentage 976 pc.percent <- 100 * pca$eigenval[1:4]/sum(pca$eigenval, na.rm=T) 977 pc.percent 978 979 lbls <- paste("PC", 1:4, "\n", format(pc.percent[1:4], digits=2), "%", 980 sep="") 981 pairs(tab[,3:6], col=colvec[tab$pop], labels=lbls, pch = 16)

37

982 983 #------984 ## Fst and Distance 985 #------986 987 ## Importing data and populations from a SNP table (built with TASSEL 988 5) 989 SNPs.table <- read.table 990 ('SexualSelDEJUYEJU_biall_dp450_q40_maf002_ppmiss075_hwe00001_neutral0 991 1.txt', 992 header = F, sep = '\t', quote = '', 993 row.names = 1, na.strings = 'N') 994 SNPs.table <- SNPs.table[-1,] 995 SNPs.table <-SNPs.table[order(row.names(SNPs.table)),] 996 997 str(SNPs.table) 998 row.names(SNPs.table) 999 1000 pops.table <- read.table ('DEJUYEJUx8_samplepopnames.txt', header = F, 1001 sep = '\t', quote = '') 1002 pops.table <- pops.table[order(pops.table$V1),] 1003 pops <- pops.table$V2 1004 1005 row.names(SNPs.table) == pops.table$V1 1006 1007 ## Computing distances 1008 SNPs.genind <- df2genind(SNPs.table, sep='\t', pop=unlist(pops), 1009 ploidy=as.integer(2), type=c("codom")) 1010 SNPs.genpop <- genind2genpop(SNPs.genind, quiet=FALSE, 1011 process.other=FALSE, other.action=mean) 1012 1013 # Method stands for the type of distance you want to compute, have a 1014 look on the manual 1015 print(dist.genpop(SNPs.genpop, method = 1, diag = FALSE, upper = 1016 FALSE)) 1017 nei.capture <- capture.output(print(dist.genpop(SNPs.genpop, method = 1018 1, diag = FALSE, upper = FALSE))) 1019 write.table(nei.capture, 'nei_rad.txt', row.names = T, col.names = T, 1020 sep = '\t', quote = F) 1021 1022 # Fst (from hierfstat package, although based on adegenet (?)) 1023 Fst.matrix.adegenet <- pairwise.fst(SNPs.genind, pop = unlist(pops), 1024 res.type="matrix") 1025 Fst.matrix.adegenet 1026 write.table(Fst.matrix.adegenet, 'Fst_rad_hierfstat.txt', row.names = 1027 T, col.names = T, sep = '\t', quote = F) 1028 1029 1030 D. Code for BayeScan Manhattan plot and redundancy analyses (RDA). 1031 library(MASS) 1032 library(xlsx) 1033 library(SNPRelate) 1034 library(vegan) 1035 library(ggfortify) 1036 library(gdsfmt) 1037 library(seqRFLP) 1038 library(coda) 1039 library(plyr) 1040 library(qqman)

38

1041 library(adegenet) 1042 library(hierfstat) 1043 library(StAMPP) 1044 1045 1046 1047 #****************** SNPS ANALYSES ******************# 1048 1049 ## Load workspace from phenotype anlyses 1050 source('ByaeScan/plot_R.r', encoding='UTF-8') 1051 1052 ##------1053 # 1. BayeScan 1054 ##------1055 1056 # BayeScan SNP sets construction 1057 ##------1058 mydata <- 1059 read.table('SexualSelDEJUYEJU_biall_dp450_q40_maf002_ppmiss075_hwe0000 1060 1.sel', colClasses = 'numeric') 1061 snps.fst <- 1062 read.table('SexualSelDEJUYEJU_biall_dp450_q40_maf002_ppmiss075_hwe0000 1063 1_fst.txt', 1064 colClasses = 'numeric') 1065 1066 mydata <- mcmc(mydata,thin=100) 1067 1068 ## Checking convergence 1069 plot(mydata) 1070 pairs(data.frame(mydata)) 1071 summary(mydata) 1072 autocorr.diag(mydata) 1073 autocorr.plot(mydata) 1074 1075 effectiveSize(mydata) 1076 geweke.diag(mydata, frac1=0.1, frac2=0.5) 1077 geweke.plot(mydata, frac1 = 0.1, frac2 = 0.5, nbins = 20, pvalue = 1078 0.05, auto.layout = TRUE) 1079 heidel.diag(mydata, eps=0.1, pvalue=0.05) 1080 1081 ## Plot setting the False Discovery Rate threshold to 0.1 1082 summary(snps.fst$qval) 1083 results <- 1084 plot_bayescan('SexualSelDEJUYEJU_biall_dp450_q40_maf002_ppmiss075_hwe0 1085 0001_fst.txt', 1086 #highlight = results$outliers, 1087 add_text = F, size = 0.1, 1088 FDR=0.1) 1089 1090 results$nb_outliers 1091 1092 snps.fst.outliers <- snps.fst[results$outliers,] 1093 snps.fst.outliers$SNP_ID <- row.names(snps.fst.outliers) 1094 1095 snps.fst.selected <- snps.fst.outliers[snps.fst.outliers$alpha>0,] 1096 snps.fst.balanced <- snps.fst.outliers[snps.fst.outliers$alpha<0,] 1097 1098 ## Building vcf subsets of SNPs depending on their selection signal 1099 1100 # Extract vcf header

39

1101 lines <- 1102 readLines("SexualSelDEJUYEJU_biall_dp450_q40_maf002_ppmiss075_hwe00001 1103 .recode.vcf") 1104 id <- grep("^#.*", lines) 1105 lines.head<-lines[id] 1106 1107 # Writing subsets 1108 vcf.table <- 1109 read.table("SexualSelDEJUYEJU_biall_dp450_q40_maf002_ppmiss075_hwe0000 1110 1.recode.vcf", header = F) 1111 1112 vcf.table.neutral <- vcf.table[-(results$outliers), ] 1113 write.table(vcf.table.neutral, 1114 file = 1115 'SexualSelDEJUYEJU_biall_dp450_q40_maf002_ppmiss075_hwe00001_neutral01 1116 .vcf', 1117 quote = F, sep = '\t', row.names = F, col.names = F) 1118 1119 vcf.table.selected <- vcf.table[snps.fst.selected$SNP_ID, ] 1120 write.table(vcf.table.selected, 1121 file = 1122 'SexualSelDEJUYEJU_biall_dp450_q40_maf002_ppmiss075_hwe00001_selected0 1123 1.vcf', 1124 quote = F, sep = '\t', row.names = F, col.names = F) 1125 1126 1127 1128 ## Manhattan plots 1129 #------1130 snps.fst$Xqval <- 1-snps.fst$qval 1131 snps.fst$SNP <- 1:nrow(snps.fst) 1132 snp.data <- cbind(vcf.table[,1:2], snps.fst) 1133 colnames(snp.data)[1] <- 'CHROM' 1134 colnames(snp.data)[2] <- 'POS' 1135 unique(snp.data$CHROM) 1136 1137 snp.data <- snp.data[!grepl(glob2rx('*_random'), snp.data$CHROM), ] 1138 snp.data <- snp.data[grep('LG', snp.data$CHROM, invert = T), ] 1139 snp.data <- snp.data[grep('1B', snp.data$CHROM, invert = T), ] 1140 snp.data <- snp.data[grep("Un", snp.data$CHROM, fixed=T, invert = 1141 TRUE), ] 1142 snp.data <- snp.data[grep("MT", snp.data$CHROM, fixed=T, invert = 1143 TRUE), ] 1144 snp.data$CHROM <- droplevels(snp.data$CHROM) 1145 1146 snp.data$CHROM_ord <- gsub('CHROM', '', snp.data$CHROM) 1147 1148 revalue(snp.data$CHROM_ord, c('1A' = '1.5')) -> snp.data$CHROM_ord 1149 revalue(snp.data$CHROM_ord, c('4A' = '4.5')) -> snp.data$CHROM_ord 1150 revalue(snp.data$CHROM_ord, c('Z' = '29')) -> snp.data$CHROM_ord 1151 1152 snp.data$CHROM_ord <- as.numeric(as.character(snp.data$CHROM_ord)) 1153 snp.data <- snp.data[order(snp.data$CHROM_ord), ] 1154 1155 unique(snp.data$CHROM_ord) 1156 unique(snp.data$CHROM) 1157 1158 # Separates SNPs in balanced and selected and draw Manhattan Plots 1159 snps.divergent <- snp.data[snp.data$alpha > 0, ] 1160 snps.balanced <- snp.data[snp.data$alpha < 0, ] 1161 col.topo <- topo.colors(2, alpha=1)

40

1162 palette(col.topo) 1163 1164 # Plots 1165 manhattan(snp.data, chr = "CHROM_ord", bp="POS", p = "fst", ylab = '', 1166 snp = "SNP", suggestiveline=0.9, highlight = 1167 results$outliers, 1168 col = seq(1, 2, by=1), chrlabs = NULL, logp = F) 1169 1170 manhattan(snps.divergent, chr = "CHROM_ord", bp="POS", p = "Xqval", 1171 ylab = '', 1172 snp = "SNP", suggestiveline=0.9, highlight = 1173 results$outliers, 1174 col = seq(1, 2, by=1), chrlabs = NULL, logp = F) 1175 1176 1177 1178 1179 #****************** RDAs ******************# 1180 1181 1182 ##------1183 # RDA with SNPs under selection: LOAD 012 (computed with vcftools) AND 1184 COMPUTE MEANS 1185 ##------1186 1187 snps_SELrad.dataset <- 1188 read.table('SexualSelDEJUYEJU_biall_dp450_q40_maf002_ppmiss075_hwe0000 1189 1_selected01.012', 1190 header = F, row.names = 1, sep = '\t', 1191 na.strings = '-1') 1192 snps_SELrad.indv <- 1193 read.table('SexualSelDEJUYEJU_biall_dp450_q40_maf002_ppmiss075_hwe0000 1194 1_selected01.012.indv') 1195 row.names(snps_SELrad.dataset) <- snps_SELrad.indv$V1 1196 snps_SELrad.dataset$Samples <- row.names(snps_SELrad.dataset) 1197 1198 snps_SELrad.dataset <- merge(gbs.dataset, snps_SELrad.dataset, by.x = 1199 'GBS.name', by.y = 'Samples') 1200 snps_SELrad.dataset$Species <- factor(snps_SELrad.dataset$Pop) 1201 snps_SELrad.dataset <- snps_SELrad.dataset[,c(1,116,3:115)] 1202 unique(snps_SELrad.dataset$Species) 1203 1204 snps_SELrad.dataset.mean <- aggregate(snps_SELrad.dataset[, 1205 3:ncol(snps_SELrad.dataset)], list(snps_SELrad.dataset$Species), mean, 1206 na.rm = T) 1207 snps_SELrad.dataset.freq <- as.data.frame((matrix(0, nrow = 1208 nrow(snps_SELrad.dataset.mean), ncol = 1209 ncol(snps_SELrad.dataset.mean)))) 1210 1211 for (j in 2:ncol(snps_SELrad.dataset.mean)) { 1212 for (i in 1:nrow(snps_SELrad.dataset.mean)) { 1213 snps_SELrad.dataset.freq[i,j] <- (snps_SELrad.dataset.mean[i,j]/2) 1214 } 1215 } 1216 1217 row.names(snps_SELrad.dataset.freq) <- 1218 snps_SELrad.dataset.mean$Group.1 1219 snps_SELrad.dataset.freq <- snps_SELrad.dataset.freq[,-1] 1220 length(snps_SELrad.dataset.freq[ , 1221 colSums(is.na(snps_SELrad.dataset.freq)) != 0])

41

1222 snps_SELrad.dataset.freq <- snps_SELrad.dataset.freq[ , 1223 colSums(is.na(snps_SELrad.dataset.freq)) == 0] 1224 1225 snps_SELrad.dataset.freq <- 1226 snps_SELrad.dataset.freq[order(row.names(snps_SELrad.dataset.freq)),] 1227 1228 1229 1230 # RDA: DimScore 1231 ##------1232 rad.lat <- read.table('GBSsamples_radlatcentroids.txt', header = T, 1233 sep = '\t') 1234 dim.score <- read.table('DimScore_radiation.txt', header = T, sep = 1235 '\t') 1236 rad.lat$LAT <- as.numeric(rad.lat$LAT) 1237 1238 rda.dimscore <- rda(snps_SELrad.dataset.freq ~ dim.score$Dim_score) 1239 1240 anova.dimscore <- anova.cca(rda.dimscore) 1241 adRsquare.dimscore <- RsquareAdj(rda.dimscore) 1242 1243 vif.cca(rda.dimscore) 1244 plot(rda.dimscore) 1245 1246 points(rda.dimscore, choices = c(1,2), display = "wa", 1247 col = colvec, 1248 bg = colvec, 1249 pch = 16) 1250 1251 with(rad.lat, legend("topleft", legend = rad.lat$Species, 1252 bty = "n", col = colvec, pch = 16, pt.bg = colvec)) 1253 1254 summary(rda.dimscore) 1255 coef(rda.dimscore) 1256 capture.output(summary(rda.dimscore), file = 1257 'Report_RDAdimscore_selsnps.txt') 1258 1259 # RDAs: LAT 1260 ##------1261 rda.lat <- rda(snps_SELrad.dataset.freq ~ rad.lat$LAT) 1262 1263 anova.lat <- anova.cca(rda.lat) 1264 adRsquare.lat <- RsquareAdj(rda.lat)$adj.r.squared 1265 1266 vif.cca(rda.lat) 1267 plot(rda.lat) 1268 1269 points(rda.lat, choices = c(1,2), display = "wa", 1270 col = colvec, 1271 bg = colvec, 1272 pch = 16) 1273 1274 with(rad.lat, legend("topleft", legend = rad.lat$Species, 1275 bty = "n", col = colvec, pch = 16, pt.bg = 1276 colvec)) 1277 1278 summary(rda.lat) 1279 coef(rda.lat) 1280 capture.output(summary(rda.lat), file = 'Report_RDAlat_selsnps.txt') 1281 1282 # Variance partition and fractions significance

42

1283 ##------1284 spe.part <- varpart(snps_SELrad.dataset.freq, dim.score$Dim_score, 1285 rad.lat$LAT) 1286 spe.part 1287 plot(spe.part) 1288 1289 # Test of fractions [a+b+c] All variance explained by both Dim and LAT 1290 rda(snps_SELrad.dataset.freq ~ rad.lat$LAT+dim.score$Dim_score) 1291 anova.abc <- anova.cca(rda(snps_SELrad.dataset.freq ~ 1292 rad.lat$LAT+dim.score$Dim_score)) 1293 1294 # Test of fractions [a+b] All variance explained by Dim 1295 anova.dimscore 1296 1297 # Test of fractions [a+b] All variance explained by LAT 1298 anova.lat 1299 1300 # Test of fraction [A] Variance explained only by Dim, partial LAT 1301 rda(snps_SELrad.dataset.freq ~ dim.score$Dim_score + 1302 Condition(rad.lat$LAT)) 1303 anova.a <- anova.cca(rda(snps_SELrad.dataset.freq ~ 1304 dim.score$Dim_score + Condition(rad.lat$LAT))) 1305 1306 # Test of fraction [C] Variance explained only by Dim, partial LAT 1307 rda(snps_SELrad.dataset.freq ~ rad.lat$LAT + 1308 Condition(dim.score$Dim_score)) 1309 anova.c <- anova.cca(rda(snps_SELrad.dataset.freq ~ rad.lat$LAT + 1310 Condition(dim.score$Dim_score))) 1311 1312 1313 1314 1315

43

1316 References cited in Supporting Information

1317 Andrews, S. 2010. FastQC: a quality control tool for high throughput sequence data.

1318 Bolger, A. M., M. Lohse, and B. Usadel. 2014. Trimmomatic: a flexible trimmer for

1319 Illumina sequence data. Bioinformatics:btu170.

1320 Danecek, P., A. Auton, G. Abecasis, C. A. Albers, E. Banks, M. A. DePristo, R. E.

1321 Handsaker, G. Lunter, G. T. Marth, and S. T. Sherry. 2011. The variant call

1322 format and VCFtools. Bioinformatics 27:2156-2158.

1323 Faircloth, B. C. and T. C. Glenn. 2012. Not all sequence tags are created equal:

1324 designing and validating sequence identification tags robust to indels. PloS one

1325 7:e42543.

1326 Goldsmith, T. H. 1990. Optimization, Constraint, and History in the Evolution of Eyes.

1327 Quarterly Review of Biology 65:281-322.

1328 Grabherr, M. G., P. Russell, M. Meyer, E. Mauceli, J. Alföldi, F. Di Palma, and K.

1329 Lindblad-Toh. 2010. Genome-wide synteny through highly sensitive sequence

1330 alignment: Satsuma. Bioinformatics 26:1145-1151.

1331 Leggett, R. M., B. J. Clavijo, L. Clissold, M. D. Clark, and M. Caccamo. 2013.

1332 NextClip: an analysis and read preparation tool for Nextera Long Mate Pair

1333 libraries. Bioinformatics:btt702.

1334 Luo, R., B. Liu, Y. Xie, Z. Li, W. Huang, J. Yuan, G. He, Y. Chen, Q. Pan, and Y. Liu.

1335 2012. SOAPdenovo2: an empirically improved memory-efficient short-read de

1336 novo assembler. Gigascience 1:18.

1337 Stoddard, M. C. and R. O. Prum. 2008. Evolution of avian plumage color in a

1338 tetrahedral color space: A phylogenetic analysis of new world buntings.

1339 American Naturalist 171:755-776.

44

1340 Yates, A., W. Akanni, M. R. Amode, D. Barrell, K. Billis, D. Carvalho-Silva, C.

1341 Cummins, P. Clapham, S. Fitzgerald, and L. Gil. 2016. Ensembl 2016. Nucleic

1342 acids research:gkv1157.

1343

45