Skip to content
Snippets Groups Projects
__init__.py 13.9 KiB
Newer Older
Tiago Peixoto's avatar
Tiago Peixoto committed
#! /usr/bin/env python
# -*- coding: utf-8 -*-
#
# Copyright (C) 2020-2024 Tiago de Paula Peixoto <tiago@skewed.de>
Tiago Peixoto's avatar
Tiago Peixoto committed
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551
#
# This program is free software: you can redistribute it and/or modify it under
# the terms of the GNU Affero General Public License as published by the Free
# Software Foundation, either version 3 of the License, or (at your option) any
# later version.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE.  See the GNU Affero General Public License for more
# details.
#
# You should have received a copy of the GNU Affero General Public License along
# with this program.  If not, see <http://www.gnu.org/licenses/>.

from .. import *

import rpy2.robjects as robjects
import tempfile
import shutil
import numpy as np

title = "Animal dominance archive (2022)"
description = """Animal dominance interaction data published over a century of research. The archive contains 434 agonistic interaction datasets, totaling over 241,000 interactions. A directed edge (i,j) corresponds to an antagonist interaction between i (winner) and j (loser). If a 'weight' edge property map exists, it counts the number of such interactions."""
tags = ['Social', 'Animal', 'Weighted']
url = 'https://github.com/DomArchive/DomArchive'
citation = [('Strauss Eli D., DeCasien Alex R., Galindo Gabriela, Hobson Elizabeth A., Shizuka Daizaburo and Curley James P., "DomArchive: a century of published dominance data", Phil. Trans. R. Soc. B377: 20200436 (2022)',
             'https://doi.org/10.1098/rstb.2020.0436')]
icon_hash = None
upstream_prefix = 'https://github.com/DomArchive/DomArchive/raw/master/data'

names = [
    'Adcock_2015a',
    'Adcock_2015b',
    'Alados_1992a',
    'Alados_1992b',
    'Alados_1992c',
    'Alados_1992d',
    'Alados_1992e',
    'Allee_1954',
    'Anderson_2016a',
    'Anderson_2016d',
    'Appleby_1983',
    'Archie_2006a',
    'Archie_2006b',
    'Archie_2006c',
    'Archie_2006d',
    'Archie_2006e',
    'Archie_2006f',
    'Archie_2006g',
    'Archie_2006h',
    'Archie_2006i',
    'Arlet_2015f',
    'Barette_1986',
    'Bennett_1939',
    'BergstromFedigan_2010b',
    'BergstromFedigan_2010c',
    'Berman_2004a',
    'Berman_2004b',
    'Berman_2004c',
    'Blatrix_2004b',
    'Blatrix_2004c',
    'Blatrix_2004d',
    'Blatrix_2004e',
    'Bonanni_2007a',
    'Bonanni_2007b',
    'Bonanni_2007c',
    'Bonanni_2017a',
    'Bonanni_2017b',
    'Bonanni_2017c',
    'Bonanni_2017d',
    'Bromley_1991',
    'Cafazzo_2010a',
    'Cafazzo_2010b',
    'Cheney_1977',
    'Cheney_1977b',
    'CluttonBrock_1976',
    'Collias_1950a',
    'Collias_1950b',
    'Collias_1950c',
    'Collias_1951a',
    'Collias_1951b',
    'Collias_1951c',
    'Correa_2013a',
    'Correa_2013d',
    'Cote_2000',
    'Cote_2000b',
    'Cote_2000c',
    'Cote_2000d',
    'Cui_2014',
    'David_2003a',
    'David_2003b',
    'DeLaFuente_2019',
    'DeLaO_2019a',
    'DeLaO_2019b',
    'DeLaO_2019c',
    'DeLaO_2019d',
    'deVries_2006',
    'deWaal_1977a',
    'deWaal_1977b',
    'deWaal_1985',
    'Diniz_2019',
    'Douglis_1948a',
    'Douglis_1948b',
    'Dubosq2013a',
    'Dubosq2013b',
    'Dubosq2013c',
    'Dubosq2013d',
    'Dubosq2013e',
    'Dubosq2013f',
    'Ellard_1989',
    'Essler_2016a',
    'Essler_2016c',
    'Essler_2016d',
    'Fairbanks_1994a',
    'Fairbanks_1994b',
    'Fairbanks_1994c',
    'Fairbanks_1994d',
    'Fairbanks_1994e',
    'Farentinos_1972a',
    'Farentinos_1972b',
    'Farentinos_1972c',
    'Farentinos_1972d',
    'Farentinos_1972e',
    'Farentinos_1972f',
    'Foerster_2016a',
    'Foerster_2016b',
    'Fournier_1995',
    'Fournier_1995b',
    'Frank_1986a',
    'Frank_1986b',
    'Franz_2015a',
    'Franz_2015b',
    'Franz_2015c',
    'Franz_2015d',
    'Franz_2015e',
    'Funkhouser_2018a',
    'Funkhouser_2018b',
    'Harcourt_1979',
    'Harcourt_1989',
    'Hartzler_1970',
    'Hass_1991',
    'Hass_1991b',
    'Hass_1991c',
    'Hausfater_1975a',
    'Hausfater_1975b',
    'Hausfater_1975c',
    'Hausfater_1982a',
    'Hausfater_1982b',
    'Hayaki_1989a',
    'Hayaki_1989b',
    'Heitor_2006',
    'HeitorVicente_2010',
    'Hewitt_2009a',
    'Hewitt_2009b',
    'Hewitt_2009c',
    'Hewitt_2009d',
    'Hirotani_1994',
    'Hirsch_2007a',
    'Hirsch_2007b',
    'Hirsch_2007c',
    'Hirsch_2007d',
    'Hobson_2015a',
    'Hobson_2015b',
    'Holekamp_1991',
    'Holekamp_1993a',
    'Holekamp_1993b',
    'Isbell_1998a',
    'Isbell_1998b',
    'Ito_1993a',
    'Ito_1993b',
    'Ito_1993c',
    'Izar_2006',
    'IzawaWatanabe_2011',
    'Jenks_1995a',
    'Jenks_1995b',
    'Johnson_2017',
    'Jones_1980a',
    'Jones_1980b',
    'Kaufmann_1974',
    'Kikkawa_1980',
    'Koenig_2004',
    'Kohda_1991',
    'Kolodziejczyk_2005',
    'Korstjens_2002',
    'Koutnik_1981',
    'Koutnik_1981b',
    'Kurvers_2009',
    'Lahti_1994a',
    'Lahti_1994b',
    'Langbein_2004a',
    'Langbein_2004b',
    'Langbein_2004c',
    'Langbein_2004d',
    'Langley_2018',
    'Lee_1979a',
    'Lee_1979b',
    'Lee_2017a',
    'Lee_2017b',
    'Lee_2017c',
    'Lee_2017d',
    'Lee_2017e',
    'Lee_2017f',
    'Lee_2018a',
    'Lee_2018b',
    'Lee_2018c',
    'Lee_2018d',
    'Lee_2018e',
    'Lee_2019a',
    'Lee_2019b',
    'Lee_2019c',
    'Lee_2019d',
    'Lee_2019e',
    'Lott_1979',
    'Lott_1987',
    'Lu_2008a',
    'Lu_2008b',
    'Malherbe_2007',
    'Marler_1955',
    'Masure_1934',
    'Masure_1934a',
    'Masure_1934b',
    'Mather_1985',
    'Matsuda_2012',
    'McCune_2019a',
    'McCune_2019c',
    'McCune_2019d',
    'McCune_2019e',
    'McCune_2019f',
    'McDougall_2010',
    'McMahan_1984',
    'Miller_2010a',
    'Miller_2010b',
    'Miller_2010c',
    'Moller_1987a',
    'Moller_1987b',
    'Moller_1987c',
    'Monnin_1999',
    'Moore_1978a',
    'Moore_1978b',
    'Murray_2007',
    'Mwamende_2009a',
    'Mwamende_2009b',
    'Myrberg_1972',
    'Myrberg_1974',
    'Nakano_1994',
    'Nakano_1995',
    'Natoli_1991',
    'Nelissen_1985a',
    'Nelissen_1985b',
    'NewtonFisher_2004a',
    'NewtonFisher_2004b',
    'Norscia_2015a',
    'Norscia_2015b',
    'Norscia_2015c',
    'Norscia_2015d',
    'Norscia_2015e',
    'Norscia_2015f',
    'Norscia_2015g',
    'Norscia_2015h',
    'Norscia_2015i',
    'Norscia_2015j',
    'Ortius_1995',
    'Oshea_1976',
    'Owens_1996',
    'Painter_2018',
    'Paoli_2006',
    'Pardi_1948a',
    'Pardi_1948b',
    'Parsons_1980a',
    'Parsons_1980b',
    'Patterson_1977',
    'Payne_2003',
    'Poisbleau_2005a',
    'Poisbleau_2005b',
    'Poisbleau_2005c',
    'Poisbleau_2006a',
    'Poisbleau_2006b',
    'Post_1992',
    'Prieto_1978',
    'Reason_1988',
    'Richter_2009',
    'RizaldiWatanabe_2010a',
    'RizaldiWatanabe_2010b',
    'Robbins_2008',
    'Roell_1978',
    'Rossler_2017',
    'Rovero_1999a',
    'Rovero_1999b',
    'Russell_1970a',
    'Russell_1970b',
    'Rutberg_1986a',
    'Rutberg_1986b',
    'Samuels_1987',
    'Samuels_1987b',
    'Sandel_2017a',
    'Sandel_2017b',
    'SatohOhkawara_2008a',
    'SatohOhkawara_2008b',
    'SatohOhkawara_2008c',
    'ScheinFohrman_1955',
    'SchjelderupEbbe_1922a',
    'SchjelderupEbbe_1922b',
    'ScottLockhard_1999a',
    'ScottLockhard_1999b',
    'Seibert_2001',
    'Setchell_2005a',
    'Setchell_2005b',
    'SetiaVanSchaik_2007',
    'Seyfarth_1976a',
    'Seyfarth_1976b',
    'Seyfarth_1976c',
    'Seyfarth_1976d',
    'Seyfarth_1976e',
    'Seyfarth_1976f',
    'Sharpe_2013a',
    'Sharpe_2013b',
    'Sharpe_2013c',
    'Sharpe_2013d',
    'Shimoji_2014a',
    'Shimoji_2014b',
    'Shimoji_2014c',
    'Shoemaker_1939',
    'Silk_2019a',
    'Slotow_1993',
    'Smith_1976',
    'Smith_2011a',
    'Solberg_1997a',
    'Solberg_1997b',
    'Solberg_1997c',
    'SomersNel_1998a',
    'SomersNel_1998b',
    'SomersNel_1998c',
    'Stamps_1978',
    'Sterck_1997',
    'Strauss_2019a',
    'Strauss_2019b',
    'Strauss_2019c',
    'Strauss_2019d',
    'StrayerCummins_1980',
    'StrayerCummins_1980b',
    'StrayerCummins_1980c',
    'StrayerCummins_1980d',
    'Struhsaker_1967a',
    'Struhsaker_1967b',
    'Sullivan_1982',
    'Surbeck_2011',
    'SurbeckHohmann_2013b',
    'Tamm_1977',
    'Tamura_1988',
    'Tarvin_1997',
    'Thompson_1960a',
    'Thompson_1960b',
    'Thompson_1960c',
    'Thompson_1960d',
    'Thompson_1960e',
    'Thompson_1993a',
    'Thompson_1993b',
    'Tilson_1984',
    'Tong_2020a',
    'Tong_2020b',
    'Tong_2020c',
    'Tong_2020d',
    'Tong_2020e',
    'Tong_2020f',
    'Tong_2020g',
    'Tong_2020h',
    'Tong_2020i',
    'Tong_2020j',
    'Torr_1996a',
    'Torr_1996b',
    'Torr_1996c',
    'Trebouet_2019a',
    'Trebouet_2019b',
    'Trebouet_2019c',
    'Trebouet_2019d',
    'Trunzer_1999',
    'Utt_2008a',
    'ValLaillet_2008a',
    'ValLaillet_2008b',
    'ValLaillet_2008c',
    'ValLaillet_2008d',
    'ValLaillet_2008e',
    'ValLaillet_2008f',
    'VanDessel_2018a',
    'VanDessel_2018b',
    'VanDierendonck_1995',
    'VanHooffWensing_1987',
    'Varley_1966',
    'Varley_1966b',
    'Vervaecke_2000',
    'Vervaecke_2010a',
    'Vervaecke_2010b',
    'Vervaecke_2010c',
    'Vervaecke_2010d',
    'Vilette_2020',
    'Waterhouse_1976a',
    'Waterhouse_1976b',
    'Waterhouse_1976c',
    'Watson_1970a',
    'Watson_1970b',
    'Watt_1986a',
    'Watt_1986b',
    'Watt_1986c',
    'Watt_1986d',
    'Watt_1986e',
    'Watt_1986f',
    'Watts_1985',
    'Watts_1994a',
    'Watts_1994b',
    'Watts_1994c',
    'Wells_1979',
    'WestEberhard_1986',
    'White_2007a',
    'White_2007b',
    'White_2007c',
    'Wikberg_2013a',
    'Wikberg_2013b',
    'Wikberg_2013c',
    'Wikberg_2013d',
    'Wikberg_2013e',
    'Wikberg_2013f',
    'Williams_1972',
    'Williamson_2016a',
    'Williamson_2016b',
    'Williamson_2016c',
    'Williamson_2016d',
    'Williamson_2016e',
    'Williamson_2016f',
    'Williamson_2016g',
    'Williamson_2016h',
    'Williamson_2016i',
    'Williamson_2016j',
    'Williamson_2016k',
    'Williamson_2017a',
    'Williamson_2017b',
    'Williamson_2017c',
    'Williamson_2017d',
    'Williamson_2017e',
    'Williamson_2017f',
    'Williamson_2017g',
    'Williamson_2017h',
    'Williamson_2017i',
    'Williamson_2017j',
    'Williamson_2017k',
    'Williamson_2017l',
    'Williamson_2017m',
    'Williamson_2017n',
    'Williamson_2017o',
    'Williamson_2019a',
    'Williamson_2019b',
    'Williamson_2019c',
    'Williamson_2019d',
    'Williamson_2019e',
    'Williamson_2019f',
    'Williamson_2019g',
    'Williamson_2019h',
    'Wittemeyer_2007',
    'Wittig_2003',
    'Yasukawa_1983a',
    'Yasukawa_1983b',
    'Yasukawa_1983c',
    'Yasukawa_1983d',
    'Yasukawa_1983e',
    'Yasukawa_1983f',
    'Yasukawa_1983g',
    'Yasukawa_1983h',
    'Zine_2000'
]


files = [('dom.data.RData', name, None) for name in names]

def fetch_upstream(force=False):
    return fetch_upstream_files(__name__.split(".")[-1], upstream_prefix, files,
                                force)

@cache_network()
@coerce_props()
@annotate()
def parse(alts=None):
    global files
    name = __name__.split(".")[-1]
    for fnames, alt, fmt in files:
        if alts is not None and alt not in alts:
            continue
        if isinstance(fnames, str):
            fnames = [fnames]
        with ExitStack() as stack:
            fs = [stack.enter_context(open_upstream_file(name, fn, "rb")) for fn in fnames]

            g = Graph()

            cols = ["order", "species",
                    "common_name", "study_site", "captivity", "sex", "age",
                    "measure", "data_location", "countbinary", "repeat_group",
                    "groupid", "matrix_edgelist", "edgelist_time_meaning",
                    "note", "full_citation"]

            for c in cols:
                g.gp[c] = g.new_gp("string")

            with tempfile.NamedTemporaryFile() as temp:
                shutil.copyfileobj(fs[0], temp)
                temp.flush()
                robjects.r["load"](temp.name)

                for x in robjects.r["dom.data"]:
                    if str(np.array(x[1][0])[0]) == alt:
                        edge_list = str(np.array(x[1][13])[0]) == "Edgelist"
                        a = np.array(x[0])
                        if edge_list:
                            g.ep.time = g.new_ep("double")
                            if len(a) > 3:
                                g.ep.date = g.new_ep("string")
                            def edges():
                                for vals in a.T:
                                    if len(a) > 3:
                                        yield vals[1], vals[2], vals[0], vals[3]
                                    else:
                                        yield vals[1], vals[2], vals[0]
                            try:
                                g.add_edge_list(edges(),
                                                eprops=[g.ep.time, g.ep.date] if len(a) > 3
                                                else [g.ep.time])
                            except ValueError:
                                g.vp.name = g.add_edge_list(edges(),
                                                            hashed=True,
                                                            eprops=[g.ep.time, g.ep.date] if len(a) > 3
                                                            else [g.ep.time])
                        else:
                            g.ep.weight = g.new_ep("int")

                            a[np.isnan(a)] = 0
                            a[a < 0] = 0
                            idx = a.nonzero()
                            ws = a[idx]
                            g.add_edge_list(np.array([idx[0], idx[1], ws]).T,
                                            eprops=[g.ep.weight])

                        for i, val in enumerate(x[1]):
                            if i == 0:
                                continue
                            if i > len(cols):
                                break
                            g.gp[cols[i-1]] = str(np.array(val)[0])

        yield alt, g