aboutsummaryrefslogtreecommitdiffstats
path: root/python/refcat/tasks.py
blob: 071184e04199a1ca44cc26dcc230e16fc26e7db3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
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
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
#!/usr/bin/env python3
"""
Set of luigi tasks to derive a citation graph.

    $ refcat.pyz

                  ____           __
       ________  / __/________ _/ /_
      / ___/ _ \/ /_/ ___/ __ `/ __/
     / /  /  __/ __/ /__/ /_/ / /_
    /_/   \___/_/  \___/\__,_/\__/

    Command line entry point for running various data tasks.

        $ refcat.pyz [COMMAND | TASK] [OPTIONS]

    Commands: ls, ll, deps, tasks, files, config, cat, completion

    To run a task, e.g. URLList:

        $ refcat.pyz URLList

    To run a subcommand, e.g. to show task dependencies:

        $ refcat.pyz deps URLList

    To install completion run:

        $ source <(refcat.pyz completion)

    VERSION   0.1.0
    SETTINGS  /home/tir/.config/refcat/settings.ini
    BASE      /bigger/.cache
    TMPDIR    /bigger/tmp

    BrefZipArxiv                        Refs
    BrefZipDOI                          RefsArxiv
    BrefZipFuzzy                        RefsDOI
    BrefZipPMCID                        RefsMapped
    BrefZipPMID                         RefsPMCID
    FatcatArxiv                         RefsPMID
    FatcatDOI                           RefsToRelease
    FatcatMapped                        RefsWithUnstructured
    FatcatPMCID                         RefsWithoutIdentifiers
    FatcatPMID                          ReleaseExportExpanded
    MAGPapers                           ReleaseExportReduced
    OpenLibraryAuthorMapping            URLList
    OpenLibraryAuthors                  URLTabs
    OpenLibraryDump                     UnmatchedMapped
    OpenLibraryEditions                 UnmatchedOpenLibraryMatchTable
    OpenLibraryEditionsByWork           UnmatchedRefs
    OpenLibraryEditionsMapped           UnmatchedRefsToRelease
    OpenLibraryEditionsToRelease        UnmatchedResolveJournalNames
    OpenLibraryWorks                    UnmatchedResolveJournalNamesMapped
    OpenLibraryWorksSorted              WikipediaCitationsMinimalDataset
    Refcat

------------------------------------------------------------------------

Overview
--------

* raw input "tasks" as luigi.ExternalTask
* derivationss

Note: We mostly use some shell pipelines with UNIX and custom tools (see: skate); we
may get rid of this "python layer" altogether, if we converged on what to
build. The most common pattern is "map-reduce", e.g. derive a key from docs,
combine the results from e.g. two such key extractions and apply some
reduction, e.g. output schema generation.

Various schema
--------------

* release (fatcat database export)
* ref (one document per reference)
* OL editions (open library editions)
* OL authors (open library authors)
* wiki (a particular wikipedia reference export)
* biblioref (or bref, the schema we store the citation graph in, ATM)

Some operations, e.g. "fuzzy verification" require both compared documents to
be release entities. This means, that we need to convert different formats into
the release format at some point.

Mappers
-------

For catalog (fatcat) and refs, we extract ids:

* doi
* pmid
* pmcid
* arxiv

We run fuzzy title matching and verification. Here, we need to convert refs to
releases to be able to run verify (could implement a verification for various
schemas, too -- but release seems complete enough).

For OL we need to fuse authors into the editions dataset first.

Reducers
--------

Exact mode for ids:

* doi
* pmid
* pmcid
* arxiv

For fuzzy matching, we use "fuzzy" mode (and keep only exact and strong matches).

Config
------

Config (e.g. raw input data) taken from $HOME/.config/refcat/settings.ini.

TODO
----

* [ ] partial (hold)

Prepared resolver for journal abbreviations; most entries have some journal
name, so use journal name or issn (extra step) to group candidates per journal.
Journals may on average have 1K publications (few have 100K+); then for each
candidate ref find most likely match in the releases of a journal.

Also, many partial records do have more information in unstructured; parse this
out first.

* [x] OL fuzzy

Beside 200K links via ISBN, about 10M links via title. Many "year" mismatches,
which might indicate different editions (debug this later).

* [ ] unmatched (in a final pass)

We can match by id and key, e.g. extract id and key, sort and merge (id, key)
from graph, and if not available use raw input.

> QA things

* [x] find duplicates and clean them up
* [x] generate stats on match types

TODO: Unmatched
---------------

* raw refs may contain duplicates (e.g. "crossref" and "grobid")
* refs should appear in order as they are found in the paper; can we guarantee that?

Idea was that "source release ident + ref index" should allow completeness and
order. "crossref" and "grobid" order may vary.

In any way, we may want the raw ref blob sorted by (source) release ident -
it's already sorted by work ident. We do have a work ident for all brefs as
well, so we need to sort the combined bref blob by work id.

    bref blob        raw ref blob
       work_id           work_id

    For each work_id we want to know, for what entries we found some ID
    somewhere. For all others, we want to include them from the raw ref; need
    to convert from ref to bref on the fly.

    Comparison by e.g. identifiers or title. Make sure it's kind of unique.

We should end up with 1.
"""

import argparse
import collections
import datetime
import json
import logging
import multiprocessing
import os
import sys
import tempfile

import grobid_tei_xml
import luigi
import requests

from refcat.base import BaseTask, Zstd, shellout
from refcat.settings import settings

# Directory structure will be like `base/tag/task/file.ext`, and we use an
# isodate (e.g. 2021-01-01) as a convention for tag. That way we can keep old
# pipeline results around, if needed.
#
# We also carry the date as a parameter in all tasks (we should probably get
# rid of it, it is not needed). In order to match the dates, we use the
# following date_from_tag parsing with fallback.
try:
    date_from_tag = datetime.datetime.strptime(settings.TAG, "%Y-%m-%d").date()
except ValueError:
    date_from_tag = datetime.date.today()

# Raw inputs are luigi.ExternalTask instances.  We can use settings.ini entries
# to configure paths for raw inputs.


class Refcat(BaseTask):
    """
    A base tasks for all refcat related tasks.
    """
    BASE = settings.BASE
    TAG = settings.TAG  # e.g. "2021-07-28", but can be anything; TODO: converge on a pattern or simplify!

    date = luigi.DateParameter(default=date_from_tag, description="a versioning help, will be part of filename")
    tmpdir = luigi.Parameter(default=settings.TMPDIR, description="set tempdir", significant=False)
    n = luigi.IntParameter(default=multiprocessing.cpu_count(), significant=False)

    @property
    def logger(self):
        """
        Return the logger. Module logging uses singleton internally, so no worries.
        """
        return logging.getLogger('refcat')


class Refs(luigi.ExternalTask, Refcat):
    """
    Compressed (zstd) references, as of 01/2021 containing ~1.8B docs; this
    might increase in a next version. This comes from a custom derivation from
    an "heavy intermediate" format in a scholar pipeline.

    As of 07/2021, we have 2,507,793,772 raw refs.
    """
    def output(self):
        return luigi.LocalTarget(path=settings.REFS_FILE, format=Zstd)


class ReleaseExportExpanded(luigi.ExternalTask, Refcat):
    """
    Fatcat database release export, zstd version, from e.g.
    https://archive.org/details/fatcat_snapshots_and_exports
    """
    def output(self):
        return luigi.LocalTarget(path=settings.RELEASE_EXPORT_EXPANDED_FILE, format=Zstd)


class WikipediaCitationsMinimalDataset(luigi.ExternalTask, Refcat):
    """
    From https://archive.org/details/wikipedia_citations_2020-07-14 (Wikipedia
    Citations: A comprehensive dataset of citations with identifiers extracted
    from English Wikipedia); http://doi.org/10.5281/zenodo.3940692.

    Dataset contains parquet, but we want JSON here:

    $ parquet-tools cat --json minimal_dataset.parquet > minimal_dataset.json

    Contains (07/2021) around 29276667 rows.

    Rough id type distribution:

        2160819 ISBN
        1442176 DOI
         825970 PMID
         353425 ISSN
         279369 PMC
         185742 OCLC
         181375 BIBCODE
         110921 JSTOR
          47601 ARXIV
          15202 LCCN
          12878 MR
           8270 ASIN
           6293 OL
           3790 SSRN
           3013 ZBL

    The minimal version looks like this:

        {
          "type_of_citation": "citation",
          "page_title": "List of R1a frequency by population",
          "Title": "High-resolution phylogenetic analysis ...",
          "ID_list": "{PMID=15944443, DOI=10.1093/molbev/msi185}"
        }

    An updated version: wikipedia-citations-enwiki-20211201, with better ID extraction.
    """
    def output(self):
        return luigi.LocalTarget(path=os.path.join(settings.WIKIPEDIA_CITATIONS, "minimal_dataset.json"))

class WikipediaCitations20211201(luigi.ExternalTask, Refcat):
    """
    Update wikipedia citations dataset: https://archive.org/details/wikipedia-citations-enwiki-20211201

    Example line:

    {
      "revision_id": 991003499,
      "refs": [
	{
	  "Authors": [
	    {
	      "first": "Liévin",
	      "last": "Ndayizeye"
	    },
	    {
	      "first": "Benoît",
	      "last": "Nzigidahera"
	    },
	    {
	      "first": "Abdelaziz Elamin",
	      "last": "Gesmallah"
	    }
	  ],
	  "CitationClass": "journal",
	  "Date": "2019-03-27",
	  "ID_list": {
	    "DOI": "10.1007/s42690-019-00013-w",
	    "ISSN": "1742-7592"
	  },
	  "Issue": "2",
	  "Pages": "125-130",
	  "Periodical": "International Journal of Tropical Insect Science",
	  "PublisherName": "Springer Science and Business Media LLC",
	  "Title": "Current distribution of Bactrocera latifrons Hendel in the different agro-ecological zones of Burundi",
	  "Volume": "39"
	}
      ],
      "site_name": "enwiki",
      "page_title": "List of Bactrocera species"
    }
    """
    def output(self):
        return luigi.LocalTarget(path=os.path.join(settings.WIKIPEDIA_CITATIONS_20211201,
                                                   "enwiki-20211201-pages-articles.citations.json"))

class OpenLibraryEditions(luigi.ExternalTask, Refcat):
    """
    Editions file (converted to zstd) https://openlibrary.org/developers/dumps.
    """
    def output(self):
        return luigi.LocalTarget(path=settings.OL_DUMP_EDITIONS, format=Zstd)


class OpenLibraryAuthors(luigi.ExternalTask, Refcat):
    """
    Author dump (converted to zstd), from
    https://openlibrary.org/developers/dumps.
    """
    def output(self):
        return luigi.LocalTarget(path=settings.OL_DUMP_AUTHORS, format=Zstd)


class MAGPapers(luigi.ExternalTask, Refcat):
    """
    Microsoft Academic dump as archived, e.g.
    https://archive.org/details/mag-2020-06-25 - we want this mainly for
    comparisons.
    """
    def output(self):
        return luigi.LocalTarget(path=os.path.join(settings.MAG, "Papers.txt.gz"), format=Zstd)


class OpenCitations(luigi.ExternalTask, Refcat):
    """
    OpenCitations distributes a zip file containing zip files containing files
    with doi-doi lines.

    We prepare the raw file to have a single zstd compressed file to work with.

    Raw data looks like:

    oci,citing,cited,creation,timespan,journal_sc,author_sc
    02003080406360106010101060909370200010237070005020502-02001000106361937231430122422370200000837000737000200,10.3846/16111699.2012.705252,10.1016/j.neucom.2008.07.020,2012-10-04,P3Y0M,no,no
    02003080406360106010101060909370200010237070005020502-0200308040636010601016301060909370200000837093701080963010908,10.3846/16111699.2012.705252,10.3846/1611-1699.2008.9.189-198,2012-10-04,P4Y0M4D,yes,no
    02003080406360106010101060909370200010237070005020502-02001000106361937102818141224370200000737000237000003,10.3846/16111699.2012.705252,10.1016/j.asieco.2007.02.003,2012-10-04,P5Y6M,no,no
    02003080406360106010101060909370200010237070005020502-02003080406360106010101060909370200010137050505030808,10.3846/16111699.2012.705252,10.3846/16111699.2011.555388,2012-10-04,P1Y5M22D,yes,no
    ...

    Combine, e.g. via:

    $ find . -name "*.csv" -exec cat {} + | grep -v '^oci,' | zstd -c -T0 > coci.csv.zst
    """
    def output(self):
        return luigi.LocalTarget(path=settings.COCI, format=Zstd)


# ----8< Derivations

#
# Augmentation and reductions of raw data
# ---------------------------------------
#


class RefsWithUnstructured(Refcat):
    """
    Augment refs with data from biblio.unstructured - do this first, so we can
    use it in all subsequent steps. Do some basic cleanup.
    """
    def requires(self):
        return Refs()

    def run(self):
        output = shellout("""
                          zstdcat -T0 {input} |
                          skate-cleanup -c ref |
                          skate-from-unstructured |
                          zstd -T0 -c > {output}
                          """,
                          input=self.input().path)
        luigi.LocalTarget(output).move(self.output().path)

    def output(self):
        return luigi.LocalTarget(path=self.path(ext="json.zst"), format=Zstd)


class ReleaseExportReduced(Refcat):
    """
    Reduce fatcat exported dataset size, stripping some heavy fields (110min).
    """
    def requires(self):
        return ReleaseExportExpanded()

    def run(self):
        output = shellout("""
                          zstdcat -T0 {input} |
                          parallel --block 10M -j 16 --pipe
                              "jq -rc 'del(.files) | del(.refs) | del(.container.extra)'" |
                          zstd -T0 > {output}
                          """,
                          input=self.input().path)
        luigi.LocalTarget(output).move(self.output().path)

    def output(self):
        return luigi.LocalTarget(path=self.path(ext="json.zst"), format=Zstd)


class ReleaseIdentDOIList(Refcat):
    """
    Create TSV (ident, doi).
    """
    def requires(self):
        return ReleaseExportExpanded()

    def run(self):
        output = shellout("""
                          zstdcat -T0 {input} |
                          parallel --block 10M -j 20 --pipe
                              "jq -rc '[.ident, .ext_ids.doi] | @tsv' | LC_ALL=C grep -F '.'" > {output}
                          """,
                          input=self.input().path)
        luigi.LocalTarget(output).move(self.output().path)

    def output(self):
        return luigi.LocalTarget(path=self.path(ext="tsv"))


class ReleaseIdentDOIMapping(Refcat):
    """
    Create a mapping database from release ident to DOI. 21min.
    """
    def requires(self):
        return ReleaseIdentDOIList()

    def run(self):
        output = shellout("""tabby -C -o {output} {input}""", input=self.input().path)
        luigi.LocalTarget(output).move(self.output().path)

    def output(self):
        return luigi.LocalTarget(path=self.path(ext="db"))


class BrefWithDOI(Refcat):
    """
    Take the paper matches and add source and target DOI. 1660min.
    """
    def requires(self):
        return {
            "bref": Bref(),
            "mapping": ReleaseIdentDOIMapping(),
        }

    def run(self):
        output = shellout("""
                          zstdcat {bref} |
                          tabby -A -db {mapping}
                              -m source_release_ident:source_doi
                              -m target_release_ident:target_doi
                          | zstd -c -T0 > {output}
                          """,
                          bref=self.input().get("bref").path,
                          mapping=self.input().get("mapping").path)
        luigi.LocalTarget(output).move(self.output().path)

    def output(self):
        return luigi.LocalTarget(path=self.path(ext="json.zst"), format=Zstd)


class BrefDOITable(Refcat):
    """
    Extract a table with source ident, target ident, source doi, target doi.
    """
    def requires(self):
        return BrefWithDOI()

    def run(self):
        output = shellout("""
                          zstdcat -T0 {input} |
                          skate-map -B -m bidt |
                          zstd -c -T0 > {output}
                          """,
                          input=self.input().path)
        luigi.LocalTarget(output).move(self.output().path)

    def output(self):
        return luigi.LocalTarget(path=self.path(ext="tsv.zst"))


class UnmatchedRefs(Refcat):
    """
    File with not yet considered refs (e.g. no title, doi, ...); around
    260,749,705. Note that this is a lower bound, since docs with titles may
    not be matched as well.

    Note, that this data contains refs, which have more information, just
    hidden in "unstructured" field. TODO: Parse all unparsed field data.
    """
    def requires(self):
        return RefsWithUnstructured()

    def run(self):
        output = shellout("""
                          zstdcat -T0 {input} |
                          parallel -j {n} --block 10M --pipe
                              "jq -rc 'select(.biblio.doi == null and
                                              .biblio.title == null and
                                              .biblio.pmid == null and
                                              .biblio.pmcid == null and
                                              .biblio.arxiv_id == null)'" |
                          zstd -T0 -c > {output}""",
                          n=self.n,
                          input=self.input().path)
        luigi.LocalTarget(output).move(self.output().path)

    def output(self):
        return luigi.LocalTarget(path=self.path(ext="json.zst"), format=Zstd)


class RefsWithoutIdentifiers(Refcat):
    """
    All references, which do not have an identifier.
    """
    def requires(self):
        return RefsWithUnstructured()

    def run(self):
        output = shellout("""
                          zstdcat -T0 {input} |
                          parallel -j {n} --block 10M --pipe
                              "jq -rc 'select(.biblio.doi == null and
                                              .biblio.pmid == null and
                                              .biblio.pmcid == null and
                                              .biblio.arxiv_id == null)'" |
                          zstd -T0 -c > {output}""",
                          n=self.n,
                          input=self.input().path)
        luigi.LocalTarget(output).move(self.output().path)

    def output(self):
        return luigi.LocalTarget(path=self.path(ext="json.zst"), format=Zstd)


#
# Generate URL list for CDX lookup
# --------------------------------
#


class URLTabs(Refcat):
    """
    Extract (work ident, release ident, url, doc) from refs (519m45.710s, about
    55k docs/s); sorted by url.
    """
    def requires(self):
        return RefsWithUnstructured()

    def run(self):
        output = shellout("""
                          zstdcat -T0 {input} |
                          skate-map -m ru -skip-on-empty 3 |
                          LC_ALL=C sort -T {tmpdir} -k3,3 -S25% |
                          zstd -T0 -c > {output}
                          """,
                          n=self.n,
                          tmpdir=self.tmpdir,
                          input=self.input().path)
        luigi.LocalTarget(output).move(self.output().path)

    def output(self):
        return luigi.LocalTarget(path=self.path(ext="tsv.zst"), format=Zstd)


class URLTabsCleaned(Refcat):
    """
    URLTabs, cleaned, sorted by url. Notes: https://is.gd/C7upZq
    """
    def requires(self):
        return URLTabs()

    def run(self):
        output = shellout("""
                          zstdcat -T0 {input} |
                          skate-cleanup -c url -allow http,https -X -B -S -f 3 |
                          LC_ALL=C sort -T {tmpdir} -k3,3 -S25% |
                          zstd -T0 -c > {output}
                          """,
                          n=self.n,
                          tmpdir=self.tmpdir,
                          input=self.input().path)
        luigi.LocalTarget(output).move(self.output().path)

    def output(self):
        return luigi.LocalTarget(path=self.path(ext="tsv.zst"), format=Zstd)


class URLList(Refcat):
    """
    List of mostly cleaned, unique URLs from refs.
    """
    def requires(self):
        return URLTabsCleaned()

    def run(self):
        output = shellout("""
                          zstdcat -T0 {input} |
                          cut -f 3 |
                          zstd -T0 -c > {output}
                          """,
                          n=self.n,
                          tmpdir=self.tmpdir,
                          input=self.input().path)
        luigi.LocalTarget(output).move(self.output().path)

    def output(self):
        return luigi.LocalTarget(path=self.path(ext="tsv.zst"), format=Zstd)


#
# Mapping tasks
# -------------
#


class RefsDOI(Refcat):
    """
    Sorted (doi, doc) tuples from refs. 225m48.755s
    """
    def requires(self):
        return RefsWithUnstructured()

    def run(self):
        output = shellout("""
                          zstdcat -T0 {input} |
                          skate-map -m ff -x biblio.doi -skip-on-empty 1 |
                          skate-cleanup -S -c doi -f 1 |
                          LC_ALL=C sort -T {tmpdir} -k1,1 -S25% |
                          zstd -T0 -c > {output}
                          """,
                          n=self.n,
                          tmpdir=self.tmpdir,
                          input=self.input().path)
        luigi.LocalTarget(output).move(self.output().path)

    def output(self):
        return luigi.LocalTarget(path=self.path(ext="tsv.zst"), format=Zstd)


class RefsPMID(Refcat):
    """
    Sorted (pmid, doc) tuples from refs; PMID is an integer,
    https://www.ncbi.nlm.nih.gov/pmc/pmctopmid/
    """
    def requires(self):
        return RefsWithUnstructured()

    def run(self):
        output = shellout("""
                          zstdcat -T0 {input} |
                          skate-map -m ff -x biblio.pmid -skip-on-empty 1 |
                          LC_ALL=C sort -T {tmpdir} -k1,1 -S25% |
                          zstd -T0 -c > {output}
                          """,
                          n=self.n,
                          tmpdir=self.tmpdir,
                          input=self.input().path)
        luigi.LocalTarget(output).move(self.output().path)

    def output(self):
        return luigi.LocalTarget(path=self.path(ext="tsv.zst"), format=Zstd)


class RefsPMCID(Refcat):
    """
    Sorted (pmcid, doc) tuples from refs, e.g. PMC2860560,
    https://www.ncbi.nlm.nih.gov/pmc/pmctopmid/
    """
    def requires(self):
        return RefsWithUnstructured()

    def run(self):
        output = shellout("""
                          zstdcat -T0 {input} |
                          skate-map -m ff -x biblio.pmcid -skip-on-empty 1 |
                          LC_ALL=C sort -T {tmpdir} -k1,1 -S25% |
                          zstd -T0 -c > {output}
                          """,
                          n=self.n,
                          tmpdir=self.tmpdir,
                          input=self.input().path)
        luigi.LocalTarget(output).move(self.output().path)

    def output(self):
        return luigi.LocalTarget(path=self.path(ext="tsv.zst"), format=Zstd)


class RefsArxiv(Refcat):
    """
    Sorted (arxiv, doc) tuples from refs, e.g. 1802.3912, ...
    """
    def requires(self):
        return RefsWithUnstructured()

    def run(self):
        output = shellout("""
                          zstdcat -T0 {input} |
                          skate-map -m ff -x biblio.arxiv_id -skip-on-empty 1 |
                          LC_ALL=C sort -T {tmpdir} -k1,1 -S25% |
                          zstd -T0 -c > {output}
                          """,
                          n=self.n,
                          tmpdir=self.tmpdir,
                          input=self.input().path)
        luigi.LocalTarget(output).move(self.output().path)

    def output(self):
        return luigi.LocalTarget(path=self.path(ext="tsv.zst"), format=Zstd)


#
# Generate (key, doc) from fatcat
# -------------------------------
#


class FatcatDOI(Refcat):
    """
    DOI from fatcat.
    """
    def requires(self):
        return ReleaseExportReduced()

    def run(self):
        output = shellout("""
                          zstdcat -T0 {input} |
                          skate-map -m ff -x ext_ids.doi -skip-on-empty 1 |
                          skate-cleanup -S -c doi -f 1 |
                          LC_ALL=C sort -T {tmpdir} -k1,1 -S25% |
                          zstd -T0 -c > {output}
                          """,
                          n=self.n,
                          tmpdir=self.tmpdir,
                          input=self.input().path)
        luigi.LocalTarget(output).move(self.output().path)

    def output(self):
        return luigi.LocalTarget(path=self.path(ext="tsv.zst"), format=Zstd)


class FatcatPMID(Refcat):
    """
    PMID from fatcat.
    """
    def requires(self):
        return ReleaseExportReduced()

    def run(self):
        output = shellout("""
                          zstdcat -T0 {input} |
                          skate-map -m ff -x ext_ids.pmid -skip-on-empty 1 |
                          LC_ALL=C sort -T {tmpdir} -k1,1 -S25% |
                          zstd -T0 -c > {output}
                          """,
                          n=self.n,
                          tmpdir=self.tmpdir,
                          input=self.input().path)
        luigi.LocalTarget(output).move(self.output().path)

    def output(self):
        return luigi.LocalTarget(path=self.path(ext="tsv.zst"), format=Zstd)


class FatcatPMCID(Refcat):
    """
    PMCID from fatcat.
    """
    def requires(self):
        return ReleaseExportReduced()

    def run(self):
        output = shellout("""
                          zstdcat -T0 {input} |
                          skate-map -m ff -x ext_ids.pmcid -skip-on-empty 1 |
                          LC_ALL=C sort -T {tmpdir} -k1,1 -S25% |
                          zstd -T0 -c > {output}
                          """,
                          n=self.n,
                          tmpdir=self.tmpdir,
                          input=self.input().path)
        luigi.LocalTarget(output).move(self.output().path)

    def output(self):
        return luigi.LocalTarget(path=self.path(ext="tsv.zst"), format=Zstd)


class FatcatArxiv(Refcat):
    """
    Arxiv from fatcat.
    """
    def requires(self):
        return ReleaseExportReduced()

    def run(self):
        output = shellout("""
                          zstdcat -T0 {input} |
                          skate-map -m ff -x extra.arxiv.base_id -skip-on-empty 1 |
                          LC_ALL=C sort -T {tmpdir} -k1,1 -S25% |
                          zstd -T0 -c > {output}
                          """,
                          n=self.n,
                          tmpdir=self.tmpdir,
                          input=self.input().path)
        luigi.LocalTarget(output).move(self.output().path)

    def output(self):
        return luigi.LocalTarget(path=self.path(ext="tsv.zst"), format=Zstd)


#
# Key extraction for fuzzy matching
# ---------------------------------
#


class FatcatMapped(Refcat):
    """
    Fatcat mapped "tsand".
    """
    mapper = luigi.Parameter(default="ts", description="mapper short name")

    def requires(self):
        return ReleaseExportReduced()

    def run(self):
        output = shellout("""
                          zstdcat -T0 {input} |
                          skate-map -m {mapper} -skip-on-empty 1 |
                          LC_ALL=C sort -T {tmpdir} -k1,1 -S25% |
                          zstd -T0 -c > {output}
                          """,
                          mapper=self.mapper,
                          tmpdir=self.tmpdir,
                          input=self.input().path)
        luigi.LocalTarget(output).move(self.output().path)

    def output(self):
        return luigi.LocalTarget(path=self.path(ext="tsv.zst"), format=Zstd)


class RefsToRelease(Refcat):
    """
    Convert refs to release, since fuzzy verification works on release entities
    currently.
    """
    def requires(self):
        return RefsWithUnstructured()

    def run(self):
        output = shellout("""
                          zstdcat -T0 {input} |
                          skate-conv -f ref -w 24 -b 100000 |
                          zstd -T0 -c > {output}
                          """,
                          input=self.input().path)
        luigi.LocalTarget(output).move(self.output().path)

    def output(self):
        return luigi.LocalTarget(path=self.path(ext="tsv.zst"), format=Zstd)


class RefsMapped(Refcat):
    """
    Apply mapper on refs. 281min (about 100k/s).
    """
    mapper = luigi.Parameter(default="ts", description="mapper short name")

    def requires(self):
        return RefsToRelease()

    def run(self):
        output = shellout("""
                          zstdcat -T0 {input} |
                          skate-map -m {mapper} -skip-on-empty 1 |
                          LC_ALL=C sort -T {tmpdir} -k1,1 -S25% |
                          zstd -T0 -c > {output}
                          """,
                          n=self.n,
                          mapper=self.mapper,
                          tmpdir=self.tmpdir,
                          input=self.input().path)
        luigi.LocalTarget(output).move(self.output().path)

    def output(self):
        return luigi.LocalTarget(path=self.path(ext="tsv.zst"), format=Zstd)


#
# Biblioref generation from identifier matches
# --------------------------------------------
#


class BrefZipDOI(Refcat):
    """
    Run skate-reduce from two files.
    """
    def requires(self):
        return {
            "refs": RefsDOI(),
            "fatcat": FatcatDOI(),
        }

    def run(self):
        output = shellout(r"""
                          skate-reduce -m exact -r doi -F <(zstdcat -T0 {refs}) -L <(zstdcat -T0 {fatcat}) |
                          zstd -c -T0 > {output}
                          """,
                          refs=self.input().get("refs").path,
                          fatcat=self.input().get("fatcat").path)
        luigi.LocalTarget(output).move(self.output().path)

    def output(self):
        return luigi.LocalTarget(path=self.path(ext="json.zst"), format=Zstd)


class BrefZipPMID(Refcat):
    """
    Run skate-reduce from two files.
    """
    def requires(self):
        return {
            "refs": RefsPMID(),
            "fatcat": FatcatPMID(),
        }

    def run(self):
        output = shellout(r"""
                          skate-reduce -m exact -r pmid -F <(zstdcat -T0 {refs}) -L <(zstdcat -T0 {fatcat}) |
                          zstd -c -T0 > {output}
                          """,
                          refs=self.input().get("refs").path,
                          fatcat=self.input().get("fatcat").path)
        luigi.LocalTarget(output).move(self.output().path)

    def output(self):
        return luigi.LocalTarget(path=self.path(ext="json.zst"), format=Zstd)


class BrefZipPMCID(Refcat):
    """
    Run skate-reduce from two files.
    """
    def requires(self):
        return {
            "refs": RefsPMCID(),
            "fatcat": FatcatPMCID(),
        }

    def run(self):
        output = shellout(r"""
                          skate-reduce -m exact -r pmcid -F <(zstdcat -T0 {refs}) -L <(zstdcat -T0 {fatcat}) |
                          zstd -c -T0 > {output}
                          """,
                          refs=self.input().get("refs").path,
                          fatcat=self.input().get("fatcat").path)
        luigi.LocalTarget(output).move(self.output().path)

    def output(self):
        return luigi.LocalTarget(path=self.path(ext="json.zst"), format=Zstd)


class BrefZipArxiv(Refcat):
    """
    Run skate-reduce from two files.
    """
    def requires(self):
        return {
            "refs": RefsArxiv(),
            "fatcat": FatcatArxiv(),
        }

    def run(self):
        output = shellout(r"""
                          skate-reduce -m exact -r arxiv -F <(zstdcat -T0 {refs}) -L <(zstdcat -T0 {fatcat}) |
                          zstd -c -T0 > {output}
                          """,
                          refs=self.input().get("refs").path,
                          fatcat=self.input().get("fatcat").path)
        luigi.LocalTarget(output).move(self.output().path)

    def output(self):
        return luigi.LocalTarget(path=self.path(ext="json.zst"), format=Zstd)


#
# Biblioref generation from fuzzy matching
# ----------------------------------------
#


class BrefZipFuzzy(Refcat):
    """
    Run skate-reduce from two files, fuzzy mode; 1039m55.350s, skate-reduce not
    parallelized yet.
    """
    mapper = luigi.Parameter(default="ts", description="mapper short name")

    def requires(self):
        return {
            "refs": RefsMapped(mapper=self.mapper),
            "fatcat": FatcatMapped(mapper=self.mapper),
        }

    def run(self):
        output = shellout(r"""
                          skate-reduce -m fuzzy -F <(zstdcat -T0 {refs}) -L <(zstdcat -T0 {fatcat}) |
                          zstd -c -T0 > {output}
                          """,
                          refs=self.input().get("refs").path,
                          fatcat=self.input().get("fatcat").path)
        luigi.LocalTarget(output).move(self.output().path)

    def output(self):
        return luigi.LocalTarget(path=self.path(ext="json.zst"), format=Zstd)


#
# Open Library Fuzzy matching (OL editions -> release, key extraction)
# --------------------------------------------------------------------
#


class OpenLibraryAuthorMapping(Refcat):
    """
    Create an OL author id to author name TSV mapping. Output like:

    /authors/OL1000002A     Īfilīn Farīd Jūrj Yārid
    /authors/OL1000025A     Khālid ibn Aḥmad Sulaymān
    /authors/OL1000435A     Muḥammad Shawqī ibn Ibrāhīm Makkī
    /authors/OL1000449A     Fāris Mūsá Muṭṭalib Mashāqbah
    """
    def requires(self):
        return OpenLibraryAuthors()

    def run(self):
        output = shellout("""
                          zstdcat -T0 {input} |
                          LC_ALL=C cut -f 5 |
                          jq -rc '[.key, .name]|@tsv' |
                          zstd -T0 > {output}
                          """,
                          input=self.input().path)
        luigi.LocalTarget(output).move(self.output().path)

    def output(self):
        return luigi.LocalTarget(path=self.path(ext="tsv.zst"), format=Zstd)


class OpenLibraryEditionsToRelease(Refcat):
    """
    Turn Open Library editions into release entities with author mapping.
    """
    def requires(self):
        return {
            "oled": OpenLibraryEditions(),
            "map": OpenLibraryAuthorMapping(),
        }

    def run(self):
        output = shellout("""
                          zstdcat -T0 {input} |
                          cut -f5 |
                          skate-conv -B -f oled -Xa <(zstdcat -T0 {map}) |
                          zstd -T0 -c > {output}
                          """,
                          input=self.input().get("oled").path,
                          map=self.input().get("map").path)
        luigi.LocalTarget(output).move(self.output().path)

    def output(self):
        return luigi.LocalTarget(path=self.path(ext="json.zst"), format=Zstd)


class OpenLibraryEditionsMapped(Refcat):
    """
    A mapped open library editions set.
    """
    mapper = luigi.Parameter(default="ts", description="mapper short name")

    def requires(self):
        return OpenLibraryEditionsToRelease()

    def run(self):
        output = shellout("""
                          zstdcat -T0 {input} |
                          skate-map -m {mapper} -skip-on-empty 1 |
                          LC_ALL=C sort -T {tmpdir} -k1,1 -S25% |
                          zstd -T0 -c > {output}
                          """,
                          n=self.n,
                          mapper=self.mapper,
                          tmpdir=self.tmpdir,
                          input=self.input().path)
        luigi.LocalTarget(output).move(self.output().path)

    def output(self):
        return luigi.LocalTarget(path=self.path(ext="tsv.zst"), format=Zstd)


class BrefOpenLibraryZipISBN(Refcat):
    """
    Run skate-reduce from two files.
    """
    def requires(self):
        return {
            "refs": RefsMapped(mapper="isbn"),
            "ol": OpenLibraryReleaseMapped(mapper="isbn"),
        }

    def run(self):
        output = shellout(r"""
                          skate-reduce -m rere -r isbn -F <(zstdcat -T0 {refs}) -L <(zstdcat -T0 {ol}) |
                          zstd -c -T0 > {output}
                          """,
                          refs=self.input().get("refs").path,
                          ol=self.input().get("ol").path)
        luigi.LocalTarget(output).move(self.output().path)

    def output(self):
        return luigi.LocalTarget(path=self.path(ext="json.zst"), format=Zstd)


#
# Open Library Fuzzy
# ------------------
#


class UnmatchedMapped(Refcat):
    """
    Map unmatched refs (converted to release schema on the fly) to container
    names to do approximate matches with OL. 221m55.746s.
    """
    def requires(self):
        return RefsWithoutIdentifiers()

    def run(self):
        output = shellout("""
                          zstdcat -T0 {input} |
                          skate-conv -f ref |
                          skate-map -m rcns -skip-on-empty 1 |
                          LC_ALL=C sort -T {tmpdir} -S25% -k1,1 |
                          zstd -T0 -c > {output}
                          """,
                          tmpdir=self.tmpdir,
                          input=self.input().path)
        luigi.LocalTarget(output).move(self.output().path)

    def output(self):
        return luigi.LocalTarget(path=self.path(ext="tsv.zst"), format=Zstd)


class UnmatchedOpenLibraryMatchTable(Refcat):
    """
    Run matching and write tabular results to file. 158m15.856s.

    Total rows: 139507963, exact/strong matches: 11777185.

    93924122 different
    33779488 ambiguous
    11670030 strong
      107155 exact

    """
    def requires(self):
        return {
            "unmatched": UnmatchedMapped(),  # We could include a bit more here, namely records with titles.
            "ol": OpenLibraryEditionsMapped(),
        }

    def run(self):
        output = shellout("""
                          skate-reduce -m oledt
                              -O <(zstdcat -T0 {ol})
                              -F <(zstdcat -T0 {unmatched}) |
                          zstd -c > {output}
                          """,
                          ol=self.input().get("ol").path,
                          unmatched=self.input().get("unmatched").path)
        luigi.LocalTarget(output).move(self.output().path)

    def output(self):
        return luigi.LocalTarget(path=self.path(ext="tsv.zst"), format=Zstd)


class BrefZipOpenLibrary(Refcat):
    """
    Export fuzzy matches to open library targets. 178m23.701s, finds 11777185
    matches; but many false negatives.
    """
    def requires(self):
        return {
            "unmatched": UnmatchedMapped(),
            "ol": OpenLibraryEditionsMapped(),
        }

    def run(self):
        output = shellout("""
                          skate-reduce -m oled
                              -O <(zstdcat -T0 {ol})
                              -F <(zstdcat -T0 {unmatched}) |
                          zstd -c > {output}
                          """,
                          ol=self.input().get("ol").path,
                          unmatched=self.input().get("unmatched").path)
        luigi.LocalTarget(output).move(self.output().path)

    def output(self):
        return luigi.LocalTarget(path=self.path(ext="json.zst"), format=Zstd)


class OpenLibraryReleaseMapped(Refcat):
    """
    OL is small compared to the whole ref corpus, 3m28.841s.
    """
    mapper = luigi.Parameter(default="isbn", description="mapper short name")

    def requires(self):
        return OpenLibraryEditionsToRelease()

    def run(self):
        output = shellout("""
                          zstdcat -T0 {input} |
                          skate-map -m {mapper} -skip-on-empty 1 |
                          LC_ALL=C sort -T {tmpdir} -k1,1 -S25% |
                          zstd -T0 -c > {output}
                          """,
                          mapper=self.mapper,
                          tmpdir=self.tmpdir,
                          input=self.input().path)
        luigi.LocalTarget(output).move(self.output().path)

    def output(self):
        return luigi.LocalTarget(path=self.path(ext="tsv.zst"), format=Zstd)


#
# Combined Bref File
#
class Bref(Refcat):
    """
    Combine bref files from various sources. Note that we want to include any
    dataset which points from fatcat to an external dataset - in order to later
    fuse the rest of the unmatched entries with the matches.
    """
    def requires(self):
        return {
            "doi": BrefZipDOI(),
            "pmid": BrefZipPMID(),
            "pmcid": BrefZipPMCID(),
            "arxiv": BrefZipArxiv(),
            "fuzzy": BrefZipFuzzy(),
            "openlibrary-isbn": BrefOpenLibraryZipISBN(),
            "openlibrary-fuzzy": BrefZipOpenLibrary(),
        }

    def run(self):
        _, tmpf = tempfile.mkstemp()
        for k, v in self.input().items():
            self.logger.debug("adding {}".format(k))
            shellout("""cat "{}" >> {}""".format(v.path, tmpf))
        luigi.LocalTarget(tmpf).move(self.output().path)

    def output(self):
        return luigi.LocalTarget(path=self.path(ext="json.zst"), format=Zstd)


# Final Assembly
# --------------
#
# Currently, "BrefCombined" is the result of the "Bref" matches and the raw
# refs. The joined dataset should be directly indexable into elasticsearch in
# fatcat_refs schema.


class BrefSortedByWorkID(Refcat):
    """
    Sort by work id. Keep only docs that actually have a work id. 237m45.094s.
    """
    def requires(self):
        return Bref()

    def run(self):
        output = shellout("""
                 zstdcat -T0 {bref} |
                 skate-map -skip-on-empty 1 -B -m ff -x source_work_ident |
                 LC_ALL=C sort -T {tmpdir} -S25% -k1,1 | zstd -c -T0 > {output}
                 """,
                          tmpdir=self.tmpdir,
                          bref=self.input().path)
        luigi.LocalTarget(output).move(self.output().path)

    def output(self):
        return luigi.LocalTarget(path=self.path(ext="json.zst"), format=Zstd)


class RefsByWorkID(Refcat):
    """
    Key raw refs by work id. Since data is already sorted by work id, this can
    skip the sorting step. 174m13.837s (~170K extractions/s).

    Seems, ordering is off, e.g. BrefSortedByWorkID starts with "22222dgdnzgxpmeq77nyyuj2x4".
    """
    def requires(self):
        return Refs()

    def run(self):
        output = shellout("""
                          zstdcat -T0 {input} |
                          skate-map -m ff -x work_ident |
                          LC_ALL=C sort -T {tmpdir} -S25% -k1,1 |
                          zstd -c -T0 > {output}
                          """,
                          tmpdir=self.tmpdir,
                          input=self.input().path)
        luigi.LocalTarget(output).move(self.output().path)

    def output(self):
        return luigi.LocalTarget(path=self.path(ext="tsv.zst"), format=Zstd)


class BrefCombined(Refcat):
    """
    TODO: We'll need another final assembly of ref and non-ref matches.

    Merge the raw references from papers with our biblioref format, such that
    we include all non-matched items and also consider duplicates.

    This is basically a reduce step, where we group by work id (since the raw
    refs were already sorted by work id).

    Data points: version 2021-05-06 results in 1,323,614,061 docs (77G
    compressed; about 285G when indexed in ES7); version 2021-07-06 contained
    1,865,637,767 docs (116G).

    Data point: 72G matches, 170G unmatched (compressed); about 3.8B docs
    (close to 300k docs/s):

        real    214m12.019s
        user    1482m46.429s
        sys     114m9.439s

    Result file is 116G compressed (9M/s); 1,865,637,767 docs; 797,304,866,555b.
    """
    def requires(self):
        return {
            "refs": RefsByWorkID(),
            "matched": BrefSortedByWorkID(),
        }

    def run(self):
        ts = datetime.datetime.now().strftime("%Y%m%d%H%M%S")
        logfile = os.path.join(self.tmpdir, "refcat-bref-combined-{}.log.gz".format(ts))
        output = shellout("""
                          skate-reduce -log {logfile} -m unmatched
                          -B <(zstdcat -T0 {matched})
                          -F <(zstdcat -T0 {refs}) | zstd -c -T0 > {output}
                          """,
                          logfile=logfile,
                          matched=self.input().get("matched").path,
                          refs=self.input().get("refs").path)
        luigi.LocalTarget(output).move(self.output().path)

    def output(self):
        return luigi.LocalTarget(path=self.path(ext="json.zst"), format=Zstd)


# Stats from BrefCombined
# =======================
#
# Calculate stats off the match result and other comparisons.
#
# TODO:
#
# [ ] match status and reason freq table
# * [ ] [A] minimal source-target ident set (plus status, reason), sort by work ident
# * [ ] [B] fatcat db source ident plus ext id sorted by work ident
# * [ ] [C] turn [A] and [B] into a DOI to DOI match table (sorted by source doi) -- we only have source ident doi, not target ident doi (!)
# * [ ] [D] sort COCI by citing (or cited)
# * [ ] [E] compare COCI and "ASC" doi matches (as set ops, only COCI, only "ASC", etc


class ExtraMatchedByWorkIdent(Refcat):
    """
    Matched sorted by source work ident. 309m6.832s.
    """
    def requires(self):
        return BrefCombined()

    def run(self):
        output = shellout("""
                          zstdcat -T0 {input} |
                          skate-map -m bref -skip-on-empty 1 |
                          LC_ALL=C sort -T {tmpdir} -k1,1 -S25% |
                          zstd -T0 -c > {output}
                          """,
                          tmpdir=self.tmpdir,
                          input=self.input().path)
        luigi.LocalTarget(output).move(self.output().path)

    def output(self):
        return luigi.LocalTarget(path=self.path(ext="json.zst"), format=Zstd)


class ExtraReleaseByWorkIdent(Refcat):
    """
    Fatcat entries by work id. 68m54.340s.
    """
    def requires(self):
        return ReleaseExportReduced()

    def run(self):
        output = shellout("""
                          zstdcat -T0 {input} |
                          skate-map -m rewo -skip-on-empty 1 |
                          LC_ALL=C sort -T {tmpdir} -k1,1 -S25% |
                          zstd -T0 -c > {output}
                          """,
                          tmpdir=self.tmpdir,
                          input=self.input().path)
        luigi.LocalTarget(output).move(self.output().path)

    def output(self):
        return luigi.LocalTarget(path=self.path(ext="json.zst"), format=Zstd)


# Extra
# -----
#
# Tinking with suffix arrays to pluck out journal names from abbreviations, etc.
#
# TODO: Be more principled, some stats on how many refs we could match this way.


class UnmatchedRefsToRelease(Refcat):
    """
    Convert unmatched refs to releases.
    """
    def requires(self):
        return UnmatchedRefs()

    def run(self):
        output = shellout("""
                          zstdcat -T0 {input} |
                          skate-conv -f ref |
                          zstd -T0 -c > {output}
                          """,
                          input=self.input().path)
        luigi.LocalTarget(output).move(self.output().path)

    def output(self):
        return luigi.LocalTarget(path=self.path(ext="tsv.zst"), format=Zstd)


class UnmatchedResolveJournalNames(Refcat):
    """
    Try to resolve journal names so we can match against both abbreviations.
    Keep only the resolved docs (for now).
    """
    def requires(self):
        return UnmatchedRefsToRelease()

    def run(self):
        output = shellout("""
                          zstdcat -T0 {input} |
                          skate-resolve-journal-name -R -f 1 -B -A {abbrev} |
                          zstd -T0 -c > {output}
                          """,
                          abbrev=settings.JOURNAL_ABBREVIATIONS,
                          input=self.input().path)
        luigi.LocalTarget(output).move(self.output().path)

    def output(self):
        return luigi.LocalTarget(path=self.path(ext="tsv.zst"), format=Zstd)


class UnmatchedResolveJournalNamesMapped(Refcat):
    """
    Take the augmented unmatched refs data and map the container names (abbrev
    and full).
    """
    def requires(self):
        return UnmatchedResolveJournalNames()

    def run(self):
        output = shellout("""
                          zstdcat -T0 {input} |
                          skate-map -m vcns -skip-on-empty 1 |
                          LC_ALL=C sort -T {tmpdir} -k1,1 -S25% |
                          zstd -T0 -c > {output}
                          """,
                          tmpdir=self.tmpdir,
                          input=self.input().path)
        luigi.LocalTarget(output).move(self.output().path)

    def output(self):
        return luigi.LocalTarget(path=self.path(ext="tsv.zst"), format=Zstd)


# Wikipedia related tasks; pages referencing papers we know about, e.g.
# Wiki_page -> target_release_ident.
#
# Using prepared datasets, just using DOI for the moment.
# TODO: use more than just DOI.


class WikipediaDOI(Refcat):
    """
    Sorted DOI keys from wikipedia. Takes about a minute.
    """
    def requires(self):
        return WikipediaCitationsMinimalDataset()

    def run(self):
        output = shellout("""
                          skate-wikipedia-doi < {input} |
                          LC_ALL=C sort -T {tmpdir} -S 20% -k1,1 |
                          zstd -T0 -c > {output}
                          """,
                          tmpdir=self.tmpdir,
                          input=self.input().path)
        luigi.LocalTarget(output).move(self.output().path)

    def output(self):
        return luigi.LocalTarget(path=self.path(ext="tsv.zst"), format=Zstd)

class Wikipedia20211201DOI(Refcat):
    """
    Updated wikipedia citations dataset.

    {"type_of_citation":"cite journal","page_title":"Abortion in
    Alabama","Title":"Why We Should Stop Using the Term \"Elective
    Abortion\"","ID_list":"{PMID=30585581, DOI=10.1001/amajethics.2018.1175}"}
    """
    def requires(self):
        return WikipediaCitations20211201()

    def run(self):
        with tempfile.NamedTemporaryFile(delete=False) as tf:
            with self.input().open() as handle:
                for line in handle:
                    doc = json.loads(line)
                    for i, ref in enumerate(doc.get("refs", [])):
                        if not "ID_list" in ref:
                            continue
                        if not "DOI" in ref["ID_list"]:
                            continue
                        doi = ref["ID_list"]["DOI"]
                        reduced = doc
                        reduced["refs"] = []
                        reduced["index"] = i
                        reduced["Title"] = ref.get("Title")
                        fields = [doi, doc["page_title"], json.dumps(reduced)]
                        tf.write("\t".join(fields) + "\n")
        output = shellout("LC_ALL=C sort -S30% {input} > {output}", input=tf.name)
        luigi.LocalTarget(output).move(self.output().path)

    def output(self):
        return luigi.LocalTarget(path=self.path(ext="tsv"))

class BrefZipWikiDOI(Refcat):
    """
    Generate biblioref for inbound wikipedia articles through exact matches.

    Yields about 1125638 edges.
    """
    def requires(self):
        return {
            # "wiki": WikipediaDOI(),
            "wiki": Wikipedia20211201DOI(),
            "fatcat": FatcatDOI(),
        }

    def run(self):
        output = shellout("""
                          skate-reduce -m wiki -W {wiki} -L <(zstdcat -T0 {fatcat}) |
                          zstd -T0 -c > {output}
                          """,
                          wiki=self.input().get("wiki").path,
                          fatcat=self.input().get("fatcat").path)
        # output = shellout("""
        #                   skate-reduce -m wiki -W <(zstdcat -T0 {wiki}) -L <(zstdcat -T0 {fatcat}) |
        #                   zstd -T0 -c > {output}
        #                   """,
        #                   wiki=self.input().get("wiki").path,
        #                   fatcat=self.input().get("fatcat").path)
        luigi.LocalTarget(output).move(self.output().path)

    def output(self):
        return luigi.LocalTarget(path=self.path(ext="json.zst"), format=Zstd)


# Grobid reparse via grobid_tei_xml


class UnmatchedRefsReparse(Refcat):
    """
    Reparse unmatched refs which have an unstructured field; about 190M/270M
    unmatched, currently. We have more unmatched - these are only the ones
    where we do not have a title.
    """
    def requires(self):
        return UnmatchedRefs()

    def run(self):
        with self.output().open("w") as output:
            with self.input().open() as f:
                for i, line in enumerate(f):
                    if i % 100000 == 0:
                        self.logger.debug("@{}".format(i))
                    doc = json.loads(line)
                    if not "biblio" in doc:
                        continue
                    if not "unstructured" in doc["biblio"]:
                        continue
                    unstructured = doc["biblio"]["unstructured"]
                    if len(unstructured) < 5:
                        continue
                    grobid_resp = requests.post(
                        "https://grobid.qa.fatcat.wiki/api/processCitation",
                        data={
                            'citations': unstructured,
                            'consolidateCitations': 0,
                        },
                        timeout=10.0,
                    )
                    grobid_resp.raise_for_status()
                    citations = grobid_tei_xml.parse_citations_xml(grobid_resp.text)
                    if len(citations) == 0:
                        continue
                    # self.logger.debug("[parsing] {} sent, {} from grobid, for {}, {}".format(
                    #     len(unstructured), len(grobid_resp.text), unstructured, citations))
                    data = json.dumps(citations[0].to_dict())
                    output.write(data.encode("utf-8"))
                    output.write(b"\n")


    def output(self):
        return luigi.LocalTarget(path=self.path(ext="json.zst"), format=Zstd)


# Wayback related, extract URL, query CDX.
#
# TODO: Make CDX lookup more, genenic, maybe a separate library or tool or mass
# query utility via hadoop streaming or the like.


class RefsURL(Refcat):
    """
    Extract (url, doc), sort by url.
    """
    def requires(self):
        return RefsWithUnstructured()

    def run(self):
        output = shellout("""
                          zstdcat -T0 {input} |
                          skate-map -m ur -skip-on-empty 1 |
                          skate-cleanup -c url -allow http,https -X -B -S -f 1 |
                          LC_ALL=C sort -T {tmpdir} -k1,1 -S25% |
                          zstd -T0 -c > {output}
                          """,
                          n=self.n,
                          tmpdir=self.tmpdir,
                          input=self.input().path)
        luigi.LocalTarget(output).move(self.output().path)

    def output(self):
        return luigi.LocalTarget(path=self.path(ext="tsv.zst"), format=Zstd)


class CDXURL(Refcat):
    """
    Stub implementation of ad-hoc CDX. We only consider a subset of documents.
    """
    cache = luigi.Parameter(default="/magna/.cache/skate/cdx", significant=False)
    limit = luigi.IntParameter(default=80000, significant=False)

    def requires(self):
        return RefsURL()

    def run(self):
        output = shellout("""
                 zstdcat -T0 {input} |
                 LC_ALL=C cut -f 1 |
                 LC_ALL=C head -n {limit} |
                 skate-cdx-lookup -q -s 50ms -c {cache} -j -B |
                 skate-map -m cdxu |
                 LC_ALL=C sort -u -T {tmpdir} -k1,1 -S25% |
                 zstd -c -T0 > {output}
                 """,
                          tmpdir=self.tmpdir,
                          limit=self.limit,
                          input=self.input().path,
                          cache=self.cache,
                          ignoremap={141: "todo: root cause"})

        luigi.LocalTarget(output).move(self.output().path)

    def output(self):
        return luigi.LocalTarget(path=self.path(ext="tsv.zst"), format=Zstd)


class BrefZipWayback(Refcat):
    def requires(self):
        return {
            "refs": RefsURL(),
            "cdx": CDXURL(),
        }

    def run(self):
        output = shellout("""
                 skate-reduce -m wb -F <(zstdcat -T0 {refs}) -C <(zstdcat -T0 {cdx}) | zstd -c -T0 > {output}
                 """,
                          refs=self.input().get("refs").path,
                          cdx=self.input().get("cdx").path)

        luigi.LocalTarget(output).move(self.output().path)

    def output(self):
        return luigi.LocalTarget(path=self.path(ext="tsv.zst"), format=Zstd)