-
Notifications
You must be signed in to change notification settings - Fork 0
/
Teamtree_11.R
2137 lines (1772 loc) · 143 KB
/
Teamtree_11.R
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
#TeamTree, vers. 11, June-20-2021
#Created by Frank W. Pfrieger, CNRS University of Strasbourg, Strasbourg, France (fw-pfrieger@gmx.de OR frank.pfrieger@unistra.fr).
#Written using R (Vers. 3.5.3 or higher; https://www.r-project.org/) and additional libraries that must be installed beforehand. The script can be executed by cut/paste in RGui.
#TeamTree reveals the authors working in a field (topic) of research (see Pfrieger, 2021 https://doi.org/10.1101/2020.06.01.128355) based on a list of publications. Required are author names, a publication-specific ID and the year of publication.
#The present version reads files with articles from the bibliometric databases PubMed (*.csv) or Web of Science (*.txt). The source must be indicated (see below).
#--------------------------------------------------------------------------------------------------------------------------------------------------------------------------
#1. SETUP
#----------------------------------------------------------------------------------------------------------------------------------------------------------
#Clear memory
rm(list=ls(all=TRUE))
#Measure execution times (only for part 2. DATA ANALYSIS)
start_time_tot <- Sys.time()
start_time_prep <- Sys.time()
#Load required libraries
library(data.table)
library(dunn.test)
library(eulerr)
library(ggfortify)
library(ggplot2)
library(ggrepel)
library(igraph)
library(plot3D)
#User-defined directories where files are read from/saved to.
User_dir_read <- "d:\\data\\papers\\teamtree\\DBqueries\\"
User_dir_save <- "d:\\data\\papers\\teamtree\\plots"
#Indicate source of publications. Current options are PubMed (T) or Web of Science (F)
Pubmed <- T
#More options
show_plots <- T
save_plots <- F
new_colors <- T
#Remove ambiguous names
RemAmbig <- T
Ambig_names <- "CHEN |GAO |HU |HUANG |JIANG |LEE |LI |LIU |LU |KIM |SHI |SUN |SUNG |WANG |WU |XU |YANG |ZHANG |ZHAO |ZHOU |ZHUO"
#User-defined topic, defines the filename with bibliometric data (Pubmed: Topic.csv | WOS: Topic.txt). Variable also used to define filenames to save data and graphs.
#Topic <- "pub_aplysia"
#Topic <- "wos_aplysia"
#Topic <- "pub_organo"
#Topic <- "wos_organo"
#Topic <- "pub_organo_tiab"
#Topic <- "pub_crispr"
#Topic <- "wos_crispr"
Topic <- "pub_clock"
#Topic <- "wos_clock"
#Topic <- "wos_supra_2021"
#Topic <- "wos_cosmic_2021"
#Topic <- "wos_ice_2021"
#Topic <- "wos_laser_2021c"
#Topic <- "wos_quant_2021"
#Filenames to save TeamTree data
File_TT_data <- paste(User_dir_save, sprintf("%s_TT_data.csv", Topic),sep="\\")
File_TT_rates <- paste(User_dir_save, sprintf("%s_TT_rates.csv", Topic),sep="\\")
File_TT_summary <- paste(User_dir_save, sprintf("%s_TT_summary.csv", Topic),sep="\\")
#Parameters
#Number of ranks to determine "top authors"
Top_n <- 10
#--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
#2. DATA ANALYSIS
#----------------------------------------------------------------------------------------------------------------------------------------------------------
#2.1. Read files, prepare data
#----------------------------------------------------------------------------------------------------------------------------------------------------------
#Read file containing publications from a user-defined folder. Different formats depending on source: Pubmed: csv, Web of Science: txt
#Note: for PubMed only columns with publication ID, authors and year of publication are required. For WOS, citation counts are included as well.
if(Pubmed){
#Read articles from PubMed in csv file.
Filename_publications <- paste(User_dir_read,sprintf("%s.csv", Topic),sep="")
Publications <- fread(file = Filename_publications, header=T,encoding = "UTF-8", select=c(1,3,7))
#Rename columns
colnames(Publications) <- c("Pub_ID","Authors","PY")
Publications <- Publications[!duplicated(Publications$Pub_ID)]
#Remove articles with unwanted author entries (can be extended)
Publications_sel <- Publications[!grepl("No authors listed|et al", Publications$Authors),]
#Remove point at the end of author list (PubMed format)
Publications_sel$Authors <- substring(Publications_sel$Authors,1,nchar(Publications_sel$Authors)-1)
#Extract authors per publication, determine first author, last author and number of authors (Author_count)
Author_all <- strsplit(Publications_sel$Authors, ", ")
Author_first <- toupper(sapply(Author_all, '[', 1))
Author_rev <- sapply(Author_all, 'rev')
Author_last <- toupper(sapply(Author_rev, '[', 1))
} else{
#Read articles from WOS in txt file.
Filename_publications <- paste(User_dir_read,sprintf("%s.txt", Topic),sep="")
Publications <- fread(file = Filename_publications, header=F, select=c(2,33,44,52))
#Rename columns
colnames(Publications) <- c("Authors","PY","TC","Pub_ID")
#Remove articles with unwanted author entries (can be extended)
Publications_sel <- Publications[!grepl("No authors listed|et al", Publications$Authors),]
#Extract authors per publication, determine first author, last author and number of authors (Author_count)
Author_all <- strsplit(Publications_sel$Authors, "; ")
#Required for WOS data
#Assemble family names and initials of First Author
Author_first_temp <- toupper(sapply(Author_all, '[', 1))
Author_first_famname <- sapply(strsplit(Author_first_temp, ", "), '[', 1)
Author_first_init <- sapply(strsplit(Author_first_temp, ", "), '[', 2)
Author_first_init <- ifelse(nchar(Author_first_init)<=2,gsub("\\.","",Author_first_init),Author_first_init)
Author_first_init <- ifelse(nchar(Author_first_init)==2& grepl("[a-z]", substring(Author_first_init,2,2)),substring(Author_first_init,1,1),Author_first_init)
Author_first_init <- ifelse(nchar(Author_first_init)>2&!grepl("\\.|\\s|\\-", Author_first_init),substring(Author_first_init,1,1),Author_first_init)
Author_first_init <- ifelse(grepl(".\\.\\s.\\.", Author_first_init),gsub("\\.|\\s", "",Author_first_init),Author_first_init)
Author_first_init <- ifelse(grepl(".\\..\\.", Author_first_init),gsub("\\.", "",Author_first_init),Author_first_init)
Author_first_init <- ifelse(nchar(Author_first_init)>2&grepl(" ", Author_first_init), paste(substring(Author_first_init,1,1),substring(Author_first_init,regexpr(" ",Author_first_init)+1,regexpr(" ",Author_first_init)+1),sep=""),Author_first_init)
Author_first_init <- ifelse(nchar(Author_first_init)>2&grepl("-", Author_first_init), paste(substring(Author_first_init,1,1),substring(Author_first_init,regexpr("-",Author_first_init)+1,regexpr("-",Author_first_init)+1),sep=""),Author_first_init)
Author_first <- paste(Author_first_famname, ifelse(is.na(Author_first_init),"", Author_first_init), sep=" ")
#Assemble family names and initials of Last Authors.
Author_rev <- sapply(Author_all, 'rev')
Author_last_temp <- toupper(sapply(Author_rev, '[', 1))
Author_last_famname <- sapply(strsplit(Author_last_temp, ", "), '[', 1)
Author_last_init <- sapply(strsplit(Author_last_temp, ", "), '[', 2)
Author_last_init <- ifelse(nchar(Author_last_init)<=2,gsub("\\.","",Author_last_init),Author_last_init)
Author_last_init <- ifelse(nchar(Author_last_init)==2& grepl("[a-z]", substring(Author_last_init,2,2)),substring(Author_last_init,1,1),Author_last_init)
Author_last_init <- ifelse(nchar(Author_last_init)>2&!grepl("\\.|\\s|\\-", Author_last_init),substring(Author_last_init,1,1),Author_last_init)
Author_last_init <- ifelse(grepl(".\\.\\s.\\.", Author_last_init),gsub("\\.|\\s", "",Author_last_init),Author_last_init)
Author_last_init <- ifelse(grepl(".\\..\\.", Author_last_init),gsub("\\.", "",Author_last_init),Author_last_init)
Author_last_init <- ifelse(nchar(Author_last_init)>2&grepl(" ", Author_last_init), paste(substring(Author_last_init,1,1),substring(Author_last_init,regexpr(" ",Author_last_init)+1,regexpr(" ",Author_last_init)+1),sep=""),Author_last_init)
Author_last_init <- ifelse(nchar(Author_last_init)>2&grepl("-", Author_last_init), paste(substring(Author_last_init,1,1),substring(Author_last_init,regexpr("-",Author_last_init)+1,regexpr("-",Author_last_init)+1),sep=""),Author_last_init)
Author_last <- paste(Author_last_famname, ifelse(is.na(Author_last_init),"", Author_last_init), sep=" ")
}
Author_count <- sapply(Author_all, 'length')
#Generate data table listing for each publication: year, first and last author, author count and article identifier (Pub_ID), uses data.table library
DT_pub_temp <- data.table("PY"=Publications_sel$PY, "Authors"=Author_all, "Last"=Author_last, "First"=Author_first, "Author_count"=Author_count, "Pub_ID"=Publications_sel$Pub_ID)
#Remove erroneous entries and remove ambiguous names
if(RemAmbig){
DT_pub <- DT_pub_temp[!is.na(Last) & !grepl(Ambig_names, Last)]
} else{
DT_pub <- DT_pub_temp[!is.na(Last)]
}
#Calculate time required for this part
end_time_prep <- Sys.time()
Duration_prep <- end_time_prep - start_time_prep
#----------------------------------------------------------------------------------------------------------------------------------------------------------
#2.2. Analysis of the publication record per author
#----------------------------------------------------------------------------------------------------------------------------------------------------------
#Start timer for this part
start_time_pub <- Sys.time()
#Get first (publication year, PY_start) and last year (PY_end) of publications per author, calculate publication period, publication count and average author count as last author
PY_end <- DT_pub[,max(as.numeric(PY)), by=Last]
PY_start <- DT_pub[,min(as.numeric(PY)), by=Last]
Pub_period <- DT_pub[,max(as.numeric(PY))-min(as.numeric(PY))+1, by=Last]
Pub_count <- DT_pub[ , .N, by = Last]
Author_count_avg <- DT_pub[,mean(Author_count), by=Last]
#Generate TeamTree database with author names derived from last authors, publication counts and publication periods of last authors
#Other author-specific information will be saved to the database subsequently
D_all <- data.frame("Author_name"=Pub_count$Last,"PC"=Pub_count$N, "PY_start"=PY_start$V1, "PY_end"=PY_end$V1, "Pub_period"=Pub_period$V1, "Author_count_avg"=Author_count_avg$V1)
D_all$PC_annu <- D_all$PC/D_all$Pub_period
#Analyse first author papers of authors
#Select only publications with first authors that published also as last authors
DT_pub_first <- DT_pub[First %in% Last & Author_count >1]
#Determine start, end and period of publication as First author and save to TeamTree database
PY_first_start <- DT_pub_first[,min(as.numeric(PY)), by=First]
D_all$PY_first_start <- PY_first_start$V1[match(D_all$Author_name, PY_first_start$First)]
PY_first_end <- DT_pub_first[,max(as.numeric(PY)), by=First]
D_all$PY_first_end <- PY_first_end$V1[match(D_all$Author_name, PY_first_end$First)]
First_period <- DT_pub_first[,max(as.numeric(PY))-min(as.numeric(PY))+1, by=First]
D_all$First_period <- First_period$V1[match(D_all$Author_name, First_period$First)]
D_all$First_period[is.na(D_all$First_period)] <- 0
#Calculate number of publications (publication count, PC) as first author
Pub_first_count <- DT_pub_first[,.N, by=First]
D_all$PC_first <- Pub_first_count$N[match(D_all$Author_name, Pub_first_count$First)]
D_all$PC_first[is.na(D_all$PC_first)] <- 0
D_all$PC_first_annu <- D_all$PC_first/D_all$First_period
D_all$PC_first_annu[is.na(D_all$PC_first_annu)] <- 0
#Generate Author_index: unique chronologic identifier for each last author with alternating sign for visual display using TeamTrees
D_all <- D_all[order(D_all$PY_start),]
D_all$Author_index <- seq_along(D_all$Author_name)*((-1)^(1+seq_along(D_all$Author_name)))
#Generate Author_color: unique random color for each last author or read from file if stored previously
if(new_colors){
D_all$Author_color <- rgb(r=sample(x=seq(0,255,by=1),size=nrow(D_all), replace=T),g=sample(x=seq(0,255,by=1),size=nrow(D_all), replace=T), b=sample(x=seq(0,255,by=1),size=nrow(D_all), replace=T), maxColorValue=255)
} else {
D_all_temp <- fread(file = File_TT_data, header=T,encoding = "UTF-8")
D_all$Author_color <- D_all_temp$Author_color}
#Calculate annual rates of publication and of last authors entering the field
PY_rates_min <- min(DT_pub$PY)
PY_rates_max <- max(DT_pub$PY)+1
Rates_bins <- seq(PY_rates_min, PY_rates_max, by=1)
Pub_rates <- hist(DT_pub$PY, breaks=Rates_bins, plot=F, right=F)
Author_rates <- hist(D_all$PY_start, breaks=Rates_bins, plot=F, right=F)
#Create database for annual publication/author rates in the field
D_rates <- data.frame(PY_bins=floor(Pub_rates$mids), pubs_count=Pub_rates$counts)
D_rates$PY_bins_norm <- seq_along(D_rates$PY_bins)/(max(D_rates$PY_bins)-min(D_rates$PY_bins))
D_rates$pubs_freq <-Pub_rates$counts/sum(Pub_rates$counts)
D_rates$authors_count <- Author_rates$counts
D_rates$authors_freq <- Author_rates$counts/sum(Author_rates$counts)
#Calculate time required for this part
end_time_pub <- Sys.time()
Duration_pub <- end_time_pub - start_time_pub
#----------------------------------------------------------------------------------------------------------------------------------------------------------
#2.3. Analysis of genealogic connections between authors based on First-Last author pairs considered as ancestor-offspring.
#----------------------------------------------------------------------------------------------------------------------------------------------------------
#Note: Offspring are those last authors whose earliest publication as first author is before the earliest publication as last author and
#where the earliest last author publication of the ancestor is before the earliest last author publication of the offspring
#Start timer for this part
start_time_off <- Sys.time()
#Assemble data
DT_pub_first$PY_ancestor <- D_all$PY_start[match(DT_pub_first$Last, D_all$Author_name)]
DT_pub_first$PY_offspring <- D_all$PY_start[match(DT_pub_first$First, D_all$Author_name)]
DT_pub_first$AI_ancestor <- D_all$Author_index[match(DT_pub_first$Last, D_all$Author_name)]
DT_pub_first$AI_offspring <- D_all$Author_index[match(DT_pub_first$First, D_all$Author_name)]
DT_pub_first$TC_ancestor <- D_all$Author_color[match(DT_pub_first$Last, D_all$Author_name)]
DT_pub_first$TC_offspring <- D_all$Author_color[match(DT_pub_first$First, D_all$Author_name)]
#Select only offspring whose PY_start is after the PY_start of the ancestor and where the earliest publication as first author is before the earliest publication as last author
#Note, the current version ignores cases where PY_ancestor==PY_offspring. It may "break up" some families.
DT_pub_anc_off <- DT_pub_first[ ,.SD[which(PY_ancestor < PY_offspring & PY <= PY_offspring)],]
#Calculate number of pubs per ancestor-offspring pair
DT_pub_anc_off[, PC_anc_off := .N, by=.(Last,First)]
DT_anc_off <- DT_pub_anc_off[ , .SD[which.min(abs(AI_ancestor))], by = First]
#Determine number of offspring per last author (OC, offspring count)
Off_count <- DT_anc_off[ , .N, by = Last]
D_all$OC <- Off_count$N[match(D_all$Author_name, Off_count$Last)]
D_all$OC[is.na(D_all$OC)] <- 0
#Determine start, end and period of author publications with offspring
PY_off_start <- DT_pub_first[,min(as.numeric(PY)), by=Last]
D_all$PY_off_start <- PY_off_start$V1[match(D_all$Author_name, PY_off_start$Last)]
PY_off_end <- DT_pub_first[,max(as.numeric(PY)), by=Last]
D_all$PY_off_end <- PY_off_end$V1[match(D_all$Author_name, PY_off_end$Last)]
Off_period <- DT_pub_first[,max(as.numeric(PY))-min(as.numeric(PY))+1, by=Last]
D_all$Off_period <- Off_period$V1[match(D_all$Author_name, Off_period$Last)]
D_all$Off_period[is.na(D_all$Off_period)] <- 0
#Calculate PC with offspring
Pub_off_count <- DT_pub_first[,.N, by=Last]
D_all$PC_off <- Pub_off_count$N[match(D_all$Author_name, Pub_off_count$Last)]
D_all$PC_off[is.na(D_all$PC_off)] <- 0
D_all$PC_off_annu <- D_all$PC_off/D_all$Off_period
D_all$PC_off_annu[is.na(D_all$PC_off_annu)] <- 0
#Generate family networks using igraph library
setorder(DT_anc_off, PY_ancestor)
Net_fam <- graph_from_data_frame(d=DT_anc_off[,.(Last,First,PC_anc_off)], directed=T)
Net_fam <- simplify(Net_fam, remove.multiple=T, remove.loops=T)
#Get number of connections per node
V(Net_fam)$degree_all <- degree(Net_fam, mode = "all", loops = FALSE, normalized = FALSE)
V(Net_fam)$degree_out <- degree(Net_fam, mode = "out", loops = FALSE, normalized = FALSE)
V(Net_fam)$degree_in <- degree(Net_fam, mode = "in", loops = FALSE, normalized = FALSE)
V(Net_fam)$color <- as.character(D_all$Author_color[match(V(Net_fam)$'name', D_all$Author_name)])
#Determine chronologic index of families based on first generation ancestors
V_ancestors <- V(Net_fam)[V(Net_fam)$degree_in==0]
V(Net_fam)[V(Net_fam)$degree_in==0]$Family_index <- seq_along(V_ancestors)
V(Net_fam)[is.na(V(Net_fam)$Family_index)]$Family_index <- 0
for (i in seq_along(V_ancestors)){
Net_anc <- ego(Net_fam, order=50, nodes = V_ancestors[i], mode = "out", mindist = 0)
V(Net_fam)[unlist(Net_anc)]$Family_index <- V(Net_fam)[V_ancestors[i]]$Family_index
}
#Determine family size
V(Net_fam)$Family_size <- ego_size(Net_fam, order=50,mode="out")
#Determine generation (AG) of each last author starting from first generation ancestors
Generation_name <- unlist(V_ancestors$name)
Generation_index <- rep(1, length(Generation_name))
V_offspring <- adjacent_vertices(Net_fam, V_ancestors, mode = c("out"))
i=1
while (length(V_offspring)>0){
i <- i+1
Generation_name <- append(Generation_name, V(Net_fam)[unlist(V_offspring)]$name)
Generation_index <- append(Generation_index, rep(i, length(V(Net_fam)[unlist(V_offspring)]$name)))
V_ancestors <- V(Net_fam)[unlist(V_offspring)]
V_offspring <- adjacent_vertices(Net_fam, V_ancestors, mode = c("out"))
}
#Save genealogy-related measures
#Internal check: degree out should be the same as OC!
D_all$Family_degree_out <- V(Net_fam)$degree_out[match(D_all$Author_name, V(Net_fam)$name)]
D_all$Family_degree_out[is.na(D_all$Family_degree_out)] <- 0
D_all$Family_degree_in <- V(Net_fam)$degree_in[match(D_all$Author_name, V(Net_fam)$name)]
D_all$Family_degree_in[is.na(D_all$Family_degree_in)] <- 0
D_all$Family_index <- V(Net_fam)$Family_index[match(D_all$Author_name, V(Net_fam)$name)]
D_all$Family_index[is.na(D_all$Family_index)] <- 0
D_all$Family_size <- V(Net_fam)$Family_size[match(D_all$Author_name, V(Net_fam)$name)]
D_all$Family_size[is.na(D_all$Family_size)] <- 0
D_all$AG <- Generation_index[match(D_all$Author_name, Generation_name)]
D_all$AG[is.na(D_all$AG)] <- 0
#Determine annual rates of authors entering as offspring and their publications and add to rates database
D_off <- subset(D_all, D_all$AG>1)
Off_Pubs <- DT_pub[which(DT_pub$Last %in% D_off$Author_name),]
Off_Pubs_histo <- hist(Off_Pubs$PY, breaks=Rates_bins, plot=F, right=F)
Off_Authors_histo <- hist(D_off$PY_start, breaks=Rates_bins, plot=F, right=F)
D_rates$off_pubs_count <- Off_Pubs_histo$counts
D_rates$off_authors_count <- Off_Authors_histo$counts
D_rates$off_pubs_fract <- D_rates$off_pubs_count/D_rates$pubs_count
D_rates$off_authors_fract <- D_rates$off_authors_count/D_rates$authors_count
#Calculate time required for this part
end_time_off <- Sys.time()
Duration_off <- end_time_off - start_time_off
#----------------------------------------------------------------------------------------------------------------------------------------------------------
#2.4. Analysis of collaborative connections between authors based on co-authorship (2nd to nth-1 of article authors)
#----------------------------------------------------------------------------------------------------------------------------------------------------------
#Start timer for this part
start_time_col <- Sys.time()
#Select only publications with more than 2 authors
DT_pub_col <- DT_pub[Author_count>2]
#Generate list of collaborating authors (excluding first and last)
DT_pub_col[,Author_col:=lapply(Authors, function(x) x[2:(length(x)-1)])]
if(Pubmed){
#Author_col <- unlist(DT_pub_col$Author_col)
Author_col <- toupper(unlist(DT_pub_col$Author_col))
}else{
#Assemble Col Author family names and initials for WOS data
Author_col_temp <- toupper(unlist(DT_pub_col$Author_col))
Author_col_famname <- sapply(strsplit(Author_col_temp, ", "), '[', 1)
#Author_col_famname <- paste(substring(Author_col_famname1,1,1),tolower(substring(Author_col_famname1,2)), sep="")
Author_col_init <- sapply(strsplit(Author_col_temp, ", "), '[', 2)
Author_col_init <- ifelse(nchar(Author_col_init)<=2,gsub("\\.","",Author_col_init),Author_col_init)
Author_col_init <- ifelse(nchar(Author_col_init)==2& grepl("[a-z]", substring(Author_col_init,2,2)),substring(Author_col_init,1,1),Author_col_init)
Author_col_init <- ifelse(nchar(Author_col_init)>2&!grepl("\\.|\\s|\\-", Author_col_init),substring(Author_col_init,1,1),Author_col_init)
Author_col_init <- ifelse(grepl(".\\.\\s.\\.", Author_col_init),gsub("\\.|\\s", "",Author_col_init),Author_col_init)
Author_col_init <- ifelse(grepl(".\\..\\.", Author_col_init),gsub("\\.", "",Author_col_init),Author_col_init)
Author_col_init <- ifelse(nchar(Author_col_init)>2&grepl(" ", Author_col_init), paste(substring(Author_col_init,1,1),substring(Author_col_init,regexpr(" ",Author_col_init)+1,regexpr(" ",Author_col_init)+1),sep=""),Author_col_init)
Author_col_init <- ifelse(nchar(Author_col_init)>2&grepl("-", Author_col_init), paste(substring(Author_col_init,1,1),substring(Author_col_init,regexpr("-",Author_col_init)+1,regexpr("-",Author_col_init)+1),sep=""),Author_col_init)
Author_col <- paste(Author_col_famname, ifelse(is.na(Author_col_init),"", Author_col_init), sep=" ")
}
#Generate list repeating last author, year and PubID for each co-author in article (minus first and last author)
Author_col_last <- c(rep(as.character(DT_pub_col$Last), DT_pub_col$Author_count-2))
Author_col_year <- c(rep(DT_pub_col$PY, DT_pub_col$Author_count-2))
Author_col_Pub_ID <- c(rep(DT_pub_col$Pub_ID, DT_pub_col$Author_count-2))
#Create data table with last and collaborating co-author pairs per article
DT_col_temp <- data.table("Last"=Author_col_last, "Col"=Author_col, "PY"=Author_col_year, "Pub_ID"=Author_col_Pub_ID)
#Select only those collaborators that also publish as last authors
DT_col <- DT_col_temp[Col %in% Last]
#Analyse collaborative publications per author
#Collaborations "inward", i.e. last authors are listed as co-authors
#Calculate publication start, end and period
PY_col_in_start <- DT_col[,min(as.numeric(PY)), by=Col]
D_all$PY_col_in_start <- PY_col_in_start$V1[match(D_all$Author_name, PY_col_in_start$Col)]
PY_col_in_end <- DT_col[,max(as.numeric(PY)), by=Col]
D_all$PY_col_in_end <- PY_col_in_end$V1[match(D_all$Author_name, PY_col_in_end$Col)]
Col_in_period <- DT_col[,max(as.numeric(PY))-min(as.numeric(PY))+1, by=Col]
D_all$Col_in_period <- Col_in_period$V1[match(D_all$Author_name, Col_in_period$Col)]
D_all$Col_in_period <- ifelse(is.na(D_all$Col_in_period),0,D_all$Col_in_period)
D_PC_col_in <- DT_col[, .N, by=Col]
D_all$PC_col_in <- D_PC_col_in$N[match(D_all$Author_name, D_PC_col_in$Col)]
D_all$PC_col_in[is.na(D_all$PC_col_in)] <- 0
D_all$PC_col_in_annu <- D_all$PC_col_in/D_all$Col_in_period
D_all$PC_col_in_annu[is.na(D_all$PC_col_in_annu)] <- 0
D_all$R_PC_col_in_PC <- D_all$PC_col_in/D_all$PC
D_all$R_PC_col_in_PC_annu <- D_all$PC_col_in_annu/D_all$PC_annu
#Collaborations "outward", i.e. last authors are listed as last authors and collaborating authors are listed as co-author
#Calculate publication start, end and period
PY_col_out_start <- DT_col[,min(as.numeric(PY)), by=Last]
D_all$PY_col_out_start <- PY_col_out_start$V1[match(D_all$Author_name, PY_col_out_start$Last)]
PY_col_out_end <- DT_col[,max(as.numeric(PY)), by=Last]
D_all$PY_col_out_end <- PY_col_out_end$V1[match(D_all$Author_name, PY_col_out_end$Last)]
Col_out_period <- DT_col[,max(as.numeric(PY))-min(as.numeric(PY))+1, by=Last]
D_all$Col_out_period <- Col_out_period$V1[match(D_all$Author_name, Col_out_period$Last)]
D_all$Col_out_period[is.na(D_all$Col_out_period)] <- 0
#Group DT col by last author and Pub_ID
PC_col_temp <- DT_col[,.N, by=.(Last, Pub_ID)]
#Determine number of out degree collaborative papers per last author
PC_col_out <- PC_col_temp[,.N, by=Last]
D_all$PC_col_out <- PC_col_out$N[match(D_all$Author_name, PC_col_out$Last)]
D_all$PC_col_out[is.na(D_all$PC_col_out)] <- 0
D_all$PC_col_out_annu <- D_all$PC_col_out/D_all$Col_out_period
D_all$PC_col_out_annu[is.na(D_all$PC_col_out_annu)] <- 0
D_all$R_PC_col_out_PC_annu <- D_all$PC_col_out_annu/D_all$PC_annu
D_all$R_PC_col_out_PC <- D_all$PC_col_out/D_all$PC
D_all$PC_col_tot <- D_all$PC_col_out+D_all$PC_col_in
D_all$PC_col_tot[is.na(D_all$PC_col_tot)] <- 0
#Generate data table to establish collaborator network
DT_col_net <- DT_col[ , .N, by = .(Last, Col)]
DT_col_net$L_AI <- D_all$Author_index[match(DT_col_net$Last, D_all$Author_name)]
DT_col_net$L_TC <- D_all$Author_color[match(DT_col_net$Last, D_all$Author_name)]
DT_col_net$L_PY_start <- D_all$PY_start[match(DT_col_net$Last, D_all$Author_name)]
DT_col_net$C_AI <- D_all$Author_index[match(DT_col_net$Col, D_all$Author_name)]
DT_col_net$C_TC <- D_all$Author_color[match(DT_col_net$Col, D_all$Author_name)]
DT_col_net$C_PY_start <- D_all$PY_start[match(DT_col_net$Col, D_all$Author_name)]
#Generate collaborator networks using igraph library and assign vertex/node properties
Net_col <- graph.data.frame(d=DT_col_net[,c(1,2,3)], directed=TRUE, v=D_all$Author_name)
V(Net_col)$degree_all <- degree(Net_col, mode = "all", loops = FALSE, normalized = FALSE)
V(Net_col)$degree_out <- degree(Net_col, mode = "out", loops = FALSE, normalized = FALSE)
V(Net_col)$color <- as.character(D_all$Author_color)
V(Net_col)$Family_size <- D_all$Family_size
V(Net_col)$PC <- D_all$PC
#Assign colors to edges from outgoing vertex/node
E(Net_col)$color <- as.character(D_all$Author_color[match(tail_of(Net_col,E(Net_col))[]$name, D_all$Author_name)])
#Save network data
D_all$degree_all <- degree(Net_col, mode = "all", loops = FALSE, normalized = FALSE)
D_all$degree_out <- degree(Net_col, mode = "out", loops = FALSE, normalized = FALSE)
D_all$degree_in <- degree(Net_col, mode = "in", loops = FALSE, normalized = FALSE)
D_all$R_deg_in_out <- D_all$degree_in/D_all$degree_out
#Select data with authors having collaborative connections
D_CC <- D_all[which(D_all$degree_all > 0),]
#Determine annual rates of collaborative papers and authors and store in rates database
Col_Pubs <- DT_pub[Last %in% D_CC$Author_name]
Col_Pubs_histo <- hist(Col_Pubs$PY, breaks=Rates_bins, plot=F, right=F)
Col_Authors_histo <- hist(D_CC$PY_start, breaks=Rates_bins, plot=F, right=F)
D_rates$col_pubs_count <- Col_Pubs_histo$counts
D_rates$col_authors_count <- Col_Authors_histo$counts
D_rates$col_pubs_fract <- D_rates$col_pubs_count/D_rates$pubs_count
D_rates$col_authors_fract <- D_rates$col_authors_count/D_rates$authors_count
#Calculate time required for this part
end_time_col <- Sys.time()
Duration_col <- end_time_col-start_time_col
#----------------------------------------------------------------------------------------------------------------------------------------------------------
#2.5. Calculation of TeamTree product (TTP), a citation-independent measure of author performance, based on the product of PC, OC and CC values.
#----------------------------------------------------------------------------------------------------------------------------------------------------------
#Start timer for this part
start_time_ttp <- Sys.time()
#TTP with zero values counted
D_all$PCxOCxCC <- D_all$PC * D_all$OC * D_all$degree_all
D_all$PCxOCxCC_rel_max <- D_all$PCxOCxCC/max(D_all$PCxOCxCC)
#TTP with zero values adjusted (inclusive) and check whether sum of PC+OC+CC is larger than product PCxOCxCC
#nozero_PCxOCxCC <- D_all$PC * ifelse(D_all$OC > 0, D_all$OC, 1) * ifelse(D_all$degree_all > 0, D_all$degree_all, 1)
#PCplusOCplusCC <- D_all$PC + D_all$OC + D_all$degree_all
#D_all$iPCxOCxCC <- ifelse(PCplusOCplusCC >= nozero_PCxOCxCC, PCplusOCplusCC, nozero_PCxOCxCC)
#TTP with zero values adjusted (inclusive)
D_all$iPCxOCxCC <- D_all$PC * ifelse(D_all$OC > 0, D_all$OC, 1) * ifelse(D_all$degree_all > 0, D_all$degree_all, 1)
D_all$iPCxOCxCC_rel_max <- D_all$iPCxOCxCC/max(D_all$iPCxOCxCC)
#Get subset of TeamTree database with authors having TTP greater than 0.
D_TTP <- D_all[which(D_all$PCxOCxCC > 0),]
D_TTP$TTP_log <- log10(D_TTP$PCxOCxCC)
#Get subset of TeamTree database with authors having iTTP greater than 1.
D_iTTP <- D_all[which(D_all$iPCxOCxCC > 1),]
D_iTTP$iTTP_log <- log10(D_iTTP$iPCxOCxCC)
#Determine annual rates of pubs/authors with TTP values >0 and save in Rates database
TTP_Pubs <- DT_pub[Last %in% D_TTP$Author_name]
TTP_Pubs_histo <- hist(TTP_Pubs$PY, breaks=Rates_bins, plot=F, right=F)
TTP_Authors_histo <- hist(D_TTP$PY_start, breaks=Rates_bins, plot=F, right=F)
D_rates$TTP_pubs_count <- TTP_Pubs_histo$counts
D_rates$TTP_authors_count <- TTP_Authors_histo$counts
D_rates$TTP_pubs_fract <- D_rates$TTP_pubs_count/D_rates$pubs_count
D_rates$TTP_authors_fract <- D_rates$TTP_authors_count/D_rates$authors_count
#Determine annual rates of pubs/authors with iTTP values >1 and save in Rates database
iTTP_Pubs <- DT_pub[Last %in% D_iTTP$Author_name]
iTTP_Pubs_histo <- hist(iTTP_Pubs$PY, breaks=Rates_bins, plot=F, right=F)
iTTP_Authors_histo <- hist(D_iTTP$PY_start, breaks=Rates_bins, plot=F, right=F)
D_rates$iTTP_pubs_count <- iTTP_Pubs_histo$counts
D_rates$iTTP_authors_count <- iTTP_Authors_histo$counts
D_rates$iTTP_pubs_fract <- D_rates$iTTP_pubs_count/D_rates$pubs_count
D_rates$iTTP_authors_fract <- D_rates$iTTP_authors_count/D_rates$authors_count
#Calculate time required for this part
end_time_ttp <- Sys.time()
Duration_ttp <- end_time_ttp - start_time_ttp
#------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
#Calculation of total count of citations and H index for authors (only WOS data)
if(Pubmed){} else{
DT_pub_cit <- data.table("PY"=Publications_sel$PY, "Authors"=Author_all, "Last"=Author_last, "Author_count"=Author_count, "TC"=Publications_sel$TC, "Pub_ID"=Publications_sel$Pub_ID)
#Generate list of authors
DT_pub_cit[,Author_cit:=lapply(Authors, function(x) x[1:length(x)])]
Author_cit_temp <- toupper(unlist(DT_pub_cit$Author_cit))
#Adjust author names for WOS data
Author_cit_famname <- sapply(strsplit(Author_cit_temp, ", "), '[', 1)
#Author_cit_famname <- paste(substring(Author_cit_famname1,1,1),tolower(substring(Author_cit_famname1,2)), sep="")
Author_cit_init <- sapply(strsplit(Author_cit_temp, ", "), '[', 2)
Author_cit_init <- ifelse(nchar(Author_cit_init)<=2,gsub("\\.","",Author_cit_init),Author_cit_init)
Author_cit_init <- ifelse(nchar(Author_cit_init)==2& grepl("[a-z]", substring(Author_cit_init,2,2)),substring(Author_cit_init,1,1),Author_cit_init)
Author_cit_init <- ifelse(nchar(Author_cit_init)>2&!grepl("\\.|\\s|\\-", Author_cit_init),substring(Author_cit_init,1,1),Author_cit_init)
Author_cit_init <- ifelse(grepl(".\\.\\s.\\.", Author_cit_init),gsub("\\.|\\s", "",Author_cit_init),Author_cit_init)
Author_cit_init <- ifelse(grepl(".\\..\\.", Author_cit_init),gsub("\\.", "",Author_cit_init),Author_cit_init)
Author_cit_init <- ifelse(nchar(Author_cit_init)>2&grepl(" ", Author_cit_init), paste(substring(Author_cit_init,1,1),substring(Author_cit_init,regexpr(" ",Author_cit_init)+1,regexpr(" ",Author_cit_init)+1),sep=""),Author_cit_init)
Author_cit_init <- ifelse(nchar(Author_cit_init)>2&grepl("-", Author_cit_init), paste(substring(Author_cit_init,1,1),substring(Author_cit_init,regexpr("-",Author_cit_init)+1,regexpr("-",Author_cit_init)+1),sep=""),Author_cit_init)
Author_cit <- paste(Author_cit_famname, ifelse(is.na(Author_cit_init),"", Author_cit_init), sep=" ")
#Generate vectors repeating last author, year and PubID for each author in article
Author_cit_last <- c(rep(as.character(DT_pub_cit$Last), DT_pub_cit$Author_count))
Author_cit_year <- c(rep(DT_pub_cit$PY, DT_pub_cit$Author_count))
Author_cit_Pub_ID <- c(rep(DT_pub_cit$Pub_ID, DT_pub_cit$Author_count))
Author_cit_TC <- c(rep(DT_pub_cit$TC, DT_pub_cit$Author_count))
#Create data table with last author and all authors per article
DT_cit_temp <- data.table("Last"=Author_cit_last, "AU"=Author_cit, "PY"=Author_cit_year, "TC"=Author_cit_TC, "Pub_ID"=Author_cit_Pub_ID)
#Restrict to entries with last authors
DT_cit <- DT_cit_temp[AU %in% Last]
PC_tot <- DT_cit[ ,length(Pub_ID), by =AU]
Cit_sum <- DT_cit[ ,sum(TC), by =AU]
H_ind <- DT_cit[TC>0,tail(which(1:length(TC) <=sort(TC,decreasing=T)),1), by=AU]
D_all$PC_tot <- PC_tot$V1[match(D_all$Author_name, PC_tot$AU)]
D_all$Cit_sum <- Cit_sum$V1[match(D_all$Author_name, Cit_sum$AU)]
D_all$Cit_sum_rel_max <- log10(D_all$Cit_sum)/max(log10(D_all$Cit_sum), na.rm=T)
D_all$H_ind <- H_ind$V1[match(D_all$Author_name, H_ind$AU)]
D_all$H_ind_rel_max <- D_all$H_ind/max(D_all$H_ind, na.rm=T)
}
#save TeamTree database, no new entries from here on
write.csv(D_all, file=File_TT_data, row.names=FALSE)
#----------------------------------------------------------------------------------------------------------------------------------------------------------
#2.6. Analysis of field-specific publication and author entry rates in different categories
#----------------------------------------------------------------------------------------------------------------------------------------------------------
#Categories include
#a) Newcomers and established authors
#b) Authors entering as offspring/collaborative, offspring only, collaborative only and the rest
#c) Authors publishing one article
start_time_dyn <- Sys.time()
#Determine publication rates of newcomers and save to database
Pub_new <- DT_pub[DT_pub[,.I[PY==min(PY)], keyby=Last]$V1]
Pub_new_off_col <- Pub_new[Last %in% D_CC$Author_name & Last %in% D_off$Author_name]
Pub_new_col <- Pub_new[Last %in% D_CC$Author_name & !Last %in% D_off$Author_name]
Pub_new_off <- Pub_new[!Last %in% D_CC$Author_name & Last %in% D_off$Author_name]
Pub_new_rest <- Pub_new[!Last %in% D_CC$Author_name & !Last %in% D_off$Author_name]
Pub_rates_new <- hist(Pub_new$PY, breaks=Rates_bins, plot=F, right=F)
Pub_rates_new_off_col <- hist(Pub_new_off_col$PY, breaks=Rates_bins, plot=F, right=F)
Pub_rates_new_col <- hist(Pub_new_col$PY, breaks=Rates_bins, plot=F, right=F)
Pub_rates_new_off <- hist(Pub_new_off$PY, breaks=Rates_bins, plot=F, right=F)
Pub_rates_new_rest <- hist(Pub_new_rest$PY, breaks=Rates_bins, plot=F, right=F)
D_rates$Pub_rates_new_count <- Pub_rates_new$counts
D_rates$Pub_rates_new_off_col_count <- Pub_rates_new_off_col$counts
D_rates$Pub_rates_new_col_count <- Pub_rates_new_col$counts
D_rates$Pub_rates_new_off_count <- Pub_rates_new_off$counts
D_rates$Pub_rates_new_rest_count <- Pub_rates_new_rest$counts
#Determine number of authors entering the field per year and save to database
Author_new <- Pub_new[,.N, keyby=.(Last,PY)]
Author_new_off_col <- Author_new[Last %in% D_CC$Author_name & Last %in% D_off$Author_name]
Author_new_col <- Author_new[Last %in% D_CC$Author_name & !Last %in% D_off$Author_name]
Author_new_off <- Author_new[!Last %in% D_CC$Author_name & Last %in% D_off$Author_name]
Author_new_rest <- Author_new[!Last %in% D_CC$Author_name & !Last %in% D_off$Author_name]
Author_rates_new <- hist(Author_new$PY, breaks=Rates_bins, plot=F, right=F)
Author_rates_new_off_col <- hist(Author_new_off_col$PY, breaks=Rates_bins, plot=F, right=F)
Author_rates_new_col <- hist(Author_new_col$PY, breaks=Rates_bins, plot=F, right=F)
Author_rates_new_off <- hist(Author_new_off$PY, breaks=Rates_bins, plot=F, right=F)
Author_rates_new_rest <- hist(Author_new_rest$PY, breaks=Rates_bins, plot=F, right=F)
Author_rates_single <- hist(D_all[which(D_all$PC==1),]$PY_start, breaks=Rates_bins, plot=F, right=F)
D_rates$Author_rates_new_count <- Author_rates_new$counts
D_rates$Author_rates_new_off_col_count <- Author_rates_new_off_col$counts
D_rates$Author_rates_new_col_count <- Author_rates_new_col$counts
D_rates$Author_rates_new_off_count <- Author_rates_new_off$counts
D_rates$Author_rates_new_rest_count <- Author_rates_new_rest$counts
D_rates$Author_rates_single <- Author_rates_single$counts
#Determine rates of entering authors with more than one publication and save to database
Author_new_mult <- Author_new[which(!Last %in% D_all[which(D_all$PC==1),]$Author_name),]
Author_new_mult_off_col <- Author_new_mult[Last %in% D_CC$Author_name & Last %in% D_off$Author_name]
Author_new_mult_col <- Author_new_mult[Last %in% D_CC$Author_name & !Last %in% D_off$Author_name]
Author_new_mult_off <- Author_new_mult[!Last %in% D_CC$Author_name & Last %in% D_off$Author_name]
Author_new_mult_rest <- Author_new_mult[!Last %in% D_CC$Author_name & !Last %in% D_off$Author_name]
Author_rates_mult_new <- hist(Author_new_mult$PY, breaks=Rates_bins, plot=F, right=F)
Author_rates_mult_new_off_col <- hist(Author_new_mult_off_col$PY, breaks=Rates_bins, plot=F, right=F)
Author_rates_mult_new_col <- hist(Author_new_mult_col$PY, breaks=Rates_bins, plot=F, right=F)
Author_rates_mult_new_off <- hist(Author_new_mult_off$PY, breaks=Rates_bins, plot=F, right=F)
Author_rates_mult_new_rest <- hist(Author_new_mult_rest$PY, breaks=Rates_bins, plot=F, right=F)
D_rates$Author_rates_mult_new_count <- Author_rates_mult_new$counts
D_rates$Author_rates_mult_new_off_col_count <- Author_rates_mult_new_off_col$counts
D_rates$Author_rates_mult_new_col_count <- Author_rates_mult_new_col$counts
D_rates$Author_rates_mult_new_off_count <- Author_rates_mult_new_off$counts
D_rates$Author_rates_mult_new_rest_count <- Author_rates_mult_new_rest$counts
#Analyse publications of authors leaving the field
Pub_end <- DT_pub[DT_pub[,.I[PY==max(PY)], keyby=Last]$V1]
Pub_end_off_col <- Pub_end[Last %in% D_CC$Author_name & Last %in% D_off$Author_name]
Pub_end_col <- Pub_end[Last %in% D_CC$Author_name & !Last %in% D_off$Author_name]
Pub_end_off <- Pub_end[!Last %in% D_CC$Author_name & Last %in% D_off$Author_name]
Pub_end_rest <- Pub_end[!Last %in% D_CC$Author_name & !Last %in% D_off$Author_name]
Pub_rates_end <- hist(Pub_end$PY, breaks=Rates_bins, plot=F, right=F)
Pub_rates_end_off_col <- hist(Pub_end_off_col$PY, breaks=Rates_bins, plot=F, right=F)
Pub_rates_end_col <- hist(Pub_end_col$PY, breaks=Rates_bins, plot=F, right=F)
Pub_rates_end_off <- hist(Pub_end_off$PY, breaks=Rates_bins, plot=F, right=F)
Pub_rates_end_rest <- hist(Pub_end_rest$PY, breaks=Rates_bins, plot=F, right=F)
D_rates$Pub_rates_end_count <- Pub_rates_end$counts
D_rates$Pub_rates_end_off_col_count <- Pub_rates_end_off_col$counts
D_rates$Pub_rates_end_col_count <- Pub_rates_end_col$counts
D_rates$Pub_rates_end_off_count <- Pub_rates_end_off$counts
D_rates$Pub_rates_end_rest_count <- Pub_rates_end_rest$counts
#Analyse authors leaving the field
Author_end <- Pub_end[,.N, keyby=.(Last,PY)]
Author_end_off_col <- Author_end[Last %in% D_CC$Author_name & Last %in% D_off$Author_name]
Author_end_col <- Author_end[Last %in% D_CC$Author_name & !Last %in% D_off$Author_name]
Author_end_off <- Author_end[!Last %in% D_CC$Author_name & Last %in% D_off$Author_name]
Author_end_rest <- Author_end[!Last %in% D_CC$Author_name & !Last %in% D_off$Author_name]
Author_rates_end <- hist(Author_end$PY, breaks=Rates_bins, plot=F, right=F)
Author_rates_end_off_col <- hist(Author_end_off_col$PY, breaks=Rates_bins, plot=F, right=F)
Author_rates_end_col <- hist(Author_end_col$PY, breaks=Rates_bins, plot=F, right=F)
Author_rates_end_off <- hist(Author_end_off$PY, breaks=Rates_bins, plot=F, right=F)
Author_rates_end_rest <- hist(Author_end_rest$PY, breaks=Rates_bins, plot=F, right=F)
Author_rates_single <- hist(D_all[which(D_all$PC==1),]$PY_start, breaks=Rates_bins, plot=F, right=F)
#Test validity
Author_rates_end_chk <- hist(D_all$PY_end, breaks=Rates_bins, plot=F, right=F)
D_rates$Author_rates_end_count_chk <- Author_rates_end_chk$counts
#Save to database
D_rates$Author_rates_end_count <- Author_rates_end$counts
D_rates$Author_rates_end_off_col_count <- Author_rates_end_off_col$counts
D_rates$Author_rates_end_col_count <- Author_rates_end_col$counts
D_rates$Author_rates_end_off_count <- Author_rates_end_off$counts
D_rates$Author_rates_end_rest_count <- Author_rates_end_rest$counts
#Determine publication rates from established (old) authors already present in the field
Pub_old <- DT_pub[DT_pub[,.I[PY>min(PY)], keyby=Last]$V1]
Pub_old_off_col <- Pub_old[Last %in% D_CC$Author_name & Last %in% D_off$Author_name]
Pub_old_col <- Pub_old[Last %in% D_CC$Author_name & !Last %in% D_off$Author_name]
Pub_old_off <- Pub_old[!Last %in% D_CC$Author_name & Last %in% D_off$Author_name]
Pub_old_rest <- Pub_old[!Last %in% D_CC$Author_name & !Last %in% D_off$Author_name]
#Determine rates per year
Pub_rates_old <- hist(Pub_old$PY, breaks=Rates_bins, plot=F, right=F)
Pub_rates_old_off_col <- hist(Pub_old_off_col$PY, breaks=Rates_bins, plot=F, right=F)
Pub_rates_old_col <- hist(Pub_old_col$PY, breaks=Rates_bins, plot=F, right=F)
Pub_rates_old_off <- hist(Pub_old_off$PY, breaks=Rates_bins, plot=F, right=F)
Pub_rates_old_rest <- hist(Pub_old_rest$PY, breaks=Rates_bins, plot=F, right=F)
#Save to database
D_rates$Pub_rates_old_count <- Pub_rates_old$counts
D_rates$Pub_rates_old_off_col_count <- Pub_rates_old_off_col$counts
D_rates$Pub_rates_old_col_count <- Pub_rates_old_col$counts
D_rates$Pub_rates_old_off_count <- Pub_rates_old_off$counts
D_rates$Pub_rates_old_rest_count <- Pub_rates_old_rest$counts
#Identify established/old authors
Author_old <- Pub_old[,.N, keyby=.(Last,PY)]
Author_old_off_col <- Author_old[Last %in% D_CC$Author_name & Last %in% D_off$Author_name]
Author_old_col <- Author_old[Last %in% D_CC$Author_name & !Last %in% D_off$Author_name]
Author_old_off <- Author_old[!Last %in% D_CC$Author_name & Last %in% D_off$Author_name]
Author_old_rest <- Author_old[!Last %in% D_CC$Author_name & !Last %in% D_off$Author_name]
#Determine rates per year
Author_rates_old <- hist(Author_old$PY, breaks=Rates_bins, plot=F, right=F)
Author_rates_old_off_col <- hist(Author_old_off_col$PY, breaks=Rates_bins, plot=F, right=F)
Author_rates_old_col <- hist(Author_old_col$PY, breaks=Rates_bins, plot=F, right=F)
Author_rates_old_off <- hist(Author_old_off$PY, breaks=Rates_bins, plot=F, right=F)
Author_rates_old_rest <- hist(Author_old_rest$PY, breaks=Rates_bins, plot=F, right=F)
#Save to database
D_rates$Author_rates_old_count <- Author_rates_old$counts
D_rates$Author_rates_old_off_col_count <- Author_rates_old_off_col$counts
D_rates$Author_rates_old_col_count <- Author_rates_old_col$counts
D_rates$Author_rates_old_off_count <- Author_rates_old_off$counts
D_rates$Author_rates_old_rest_count <- Author_rates_old_rest$counts
#Save rates to file, no new entries from here on, currently disabled
write.csv(D_rates, file=File_TT_rates, row.names=F)
end_time_dyn <- Sys.time()
Duration_dyn <- end_time_dyn - start_time_dyn
end_time_tot <- Sys.time()
Duration_tot <- end_time_tot - start_time_tot
#Show processing time for different subroutines
Duration <- data.frame(Prep=Duration_prep,
Pub=Duration_pub,
Off=Duration_off,
Col=Duration_col,
TTP=Duration_ttp,
Dyn=Duration_dyn,
Tot=Duration_tot)
Duration
#----------------------------------------------------------------------------------------------------------------------------------------------------------
#3. VISUALIZATION
#----------------------------------------------------------------------------------------------------------------------------------------------------------
#The following code produces graphs shown in the preprint (Pfrieger, 2021 https://doi.org/10.1101/2020.06.01.128355) and additional visuals not included in the manuscript.
#----------------------------------------------------------------------------------------------------------------------------------------------------------
#3.1. Parameters for visual display
#----------------------------------------------------------------------------------------------------------------------------------------------------------
Alpha_all = 0.5
Label_size = 16
Names_size = 12
Line_width = 2.5
Legend_position_1 = c(0.8,0.3)
Legend_position_2 = c(0.5,0.9)
Legend_position_3 = c(0.9,0.8)
Legend_position_4 = c(0.4,0.3)
Legend_position_5 = c(0.5,0.2)
Legend_text_size = 40
Tick_length = 0.6
Tick_years <- 10
Tick_AI <- 1000
Tick_rate <- 500
Tick_PC <- 20
Tick_OC <- 10
Tick_PC_off <- 20
Tick_AG <- 2
Tick_FS <- 10
Tick_AC <- 2
Tick_CC <- 20
Tick_PC_col <- 20
Tick_pub_period <- 20
AI_max <- max(D_all$Author_index)
AI_min <- min(D_all$Author_index)
PY_min <- min(D_all$PY_start)-1
PY_max <- max(DT_pub$PY)+1
#Aplysia
#AI_min=-1850
#AI_max=1850
#PY_min <- min(D_all$PY_start)-1
#PY_max <- max(DT_pub$PY)+1
#CRISPR
#AI_min <- -11500
#AI_max <- 11500
#PY_min <- 1999
#PY_max <- 2022
#Organoids
#AI_min <- -10750
#AI_max <- 10750
#PY_min <- 1939
#PY_max <- 2022
Plot_height_PY <- (PY_max-PY_min)*4
Plot_width_AI <- AI_max/23
Plot_div_height <- 60
Plot_histo_width <- 80
Plot_names_width <- 300
Plot_names_height <- 150
#----------------------------------------------------------------------------------------------------------------------------------------------------------
#3.2. Generation of TeamTree graphs (TTGs), a new type of visual, that provides an ad-hoc view on different properties of the workforce.
#----------------------------------------------------------------------------------------------------------------------------------------------------------
#Generate data table with publication years and publication count per year for each last author/author
D_chrono <- DT_pub[ , .N, by = .(Last, PY)]
D_chrono$Author_index <- D_all$Author_index[match(D_chrono$Last, D_all$Author_name)]
D_chrono$Author_color <- D_all$Author_color[match(D_chrono$Last, D_all$Author_name)]
setnames(D_chrono, "N", "PC_annual")
#Plot parameters
P_pub_size_min <- min(D_chrono$PC_annual)
P_pub_size_max <- max(D_chrono$PC_annual)
#Get authors with Top_n PC values
D_PC_sorted <- D_all[order(-D_all$PC),]
D_PC_sorted$PC_rank <- seq_along(D_PC_sorted$PC)
D_PC_sorted$legend <- paste(D_PC_sorted$PC_rank,D_PC_sorted$Author_name,sep=" ")
D_PC_top <- D_PC_sorted[1:Top_n,]
D_PC_top_minus <- subset(D_PC_top, D_PC_top$Author_index <0)
D_PC_top_plus <- subset(D_PC_top, D_PC_top$Author_index >0)
#Bubbleplot PC size
D_PC_top$PC_norm <- round(D_PC_top$PC/max(D_PC_top$PC)*100,1)
P_PC_top_size_norm_min <- min(D_PC_top$PC_norm)
P_PC_top_size_norm_max <- max(D_PC_top$PC_norm)
P_PC_top_size_min <- min(D_PC_top$PC)
P_PC_top_size_max <- max(D_PC_top$PC)
#Plotfiles
File_P_pub <- sprintf("%s_P_pub.tiff", Topic)
File_P_pub_nopr <- sprintf("%s_P_pub_nopr.tiff", Topic)
File_P_pub_top <- sprintf("%s_P_pub_top.tiff", Topic)
File_P_PC_names <- sprintf("%s_P_PC_names.tiff", Topic)
File_P_pub_rates <- sprintf("%s_P_pub_rates.tiff", Topic)
File_P_PC <- sprintf("%s_P_PC.tiff", Topic)
File_P_PC_histo <- sprintf("%s_P_PC_histo.tiff", Topic)
#Visualize publication records of authors: The TeamTree visual, version for CLOCK field
P_pub <- ggplot()
P_pub <- P_pub + geom_line(data = D_chrono, aes(x = Author_index, y = PY, group = Author_index), color = "gray75", size = 0.5)
P_pub <- P_pub + geom_point(data = D_chrono, aes(x = Author_index, y = PY, group = Author_index, size = PC_annual, color=D_chrono$Author_color), shape = 16, alpha = Alpha_all)
P_pub <- P_pub + scale_size_continuous(range = c(P_pub_size_min/1.5, P_pub_size_max/1.5), breaks = c(P_pub_size_min, P_pub_size_max))
#P_pub <- P_pub + geom_text_repel(data = D_PC_top_minus, aes(x = Author_index, y = PY_start, label = PC_rank, color = Author_color), nudge_x = Tick_AI*-0.5, nudge_y = -3, hjust = 0, vjust = 0, size = Label_size, inherit.aes=FALSE)
#P_pub <- P_pub + geom_text_repel(data = D_PC_top_plus, aes(x = Author_index, y = PY_start, label = PC_rank, color = Author_color), nudge_x = Tick_AI*0.5, nudge_y = -3, hjust = 0, vjust = 0, size = Label_size, inherit.aes=FALSE)
P_pub <- P_pub + scale_fill_identity()
P_pub <- P_pub + scale_colour_identity()
P_pub <- P_pub + theme_bw()
P_pub <- P_pub + theme(panel.border = element_blank(), panel.grid.major = element_blank(), panel.grid.minor = element_blank(), axis.line = element_line(colour = "black", size = Line_width))
P_pub <- P_pub + theme(legend.text = element_text(size = Legend_text_size), legend.position = Legend_position_1, legend.title = element_blank(), legend.background=element_rect(fill=NA), legend.key=element_blank())
P_pub <- P_pub + theme(axis.ticks.length = unit(Tick_length, "cm"), axis.title = element_blank(), axis.text = element_blank(), axis.ticks = element_line(color = "black", size = Line_width))
P_pub <- P_pub + theme(plot.margin=grid::unit(c(0,0,0,0), "mm"))
P_pub <- P_pub + scale_x_continuous(limits = c(AI_min, AI_max), breaks = seq(-50000,50000, by=Tick_AI))
P_pub <- P_pub + scale_y_continuous(limits = c(PY_min,PY_max), breaks = seq(1700, 2040, by = Tick_years))
if(show_plots){P_pub}
if(save_plots){ggsave(File_P_pub, path = User_dir_save, height = Plot_height_PY, width = Plot_width_AI, units = "mm", dpi=300)}
#Visualize top PC authors with TTG for CLOCK field
P_pub_top <- ggplot()
P_pub_top <- P_pub_top + geom_line(data = D_all, aes(x = Author_index, y = PY_start), size=0.5, color="gray75")
P_pub_top <- P_pub_top + geom_point(data = D_all, aes(x = Author_index, y = PY_start, color = Author_color), size=2.0, shape = 16)
P_pub_top <- P_pub_top + geom_point(data = D_PC_top, aes(x = Author_index, y = PY_start, color = Author_color, size = PC), shape = 16, alpha=0.6)
P_pub_top <- P_pub_top + scale_size(range = c(P_PC_top_size_min/3, P_PC_top_size_max/3), breaks = c(P_PC_top_size_min, P_PC_top_size_max))
P_pub_top <- P_pub_top + geom_text_repel(data = D_PC_top_minus, aes(x = Author_index, y = PY_start, label = PC_rank, color = Author_color), nudge_x = Tick_AI*-1.5, nudge_y = 0, hjust = 0, vjust = 0, size = Label_size, inherit.aes=FALSE)
P_pub_top <- P_pub_top + geom_text_repel(data = D_PC_top_plus, aes(x = Author_index, y = PY_start, label = PC_rank, color = Author_color), nudge_x = Tick_AI*2, nudge_y = -1, hjust = 0, vjust = 0, size = Label_size, inherit.aes=FALSE)
P_pub_top <- P_pub_top + scale_fill_identity()
P_pub_top <- P_pub_top + scale_colour_identity()
P_pub_top <- P_pub_top + theme_bw()
P_pub_top <- P_pub_top + theme(panel.border = element_blank(), panel.grid.major = element_blank(), panel.grid.minor = element_blank(), axis.line = element_line(colour = "black", size = Line_width))
P_pub_top <- P_pub_top + theme(legend.text = element_text(size = Legend_text_size), legend.position = c(0.5,0.85), legend.title = element_blank(), legend.background=element_rect(fill=NA), legend.key=element_blank())
P_pub_top <- P_pub_top + theme(axis.ticks.length = unit(Tick_length, "cm"), axis.title = element_blank(), axis.text = element_blank(), axis.ticks = element_line(color = "black", size = Line_width))
P_pub_top <- P_pub_top + theme(plot.margin=grid::unit(c(0,0,0,0), "mm"))
P_pub_top <- P_pub_top + scale_x_continuous(limits = c(AI_min, AI_max), breaks = seq(-50000,50000, by=Tick_AI))
P_pub_top <- P_pub_top + scale_y_continuous(limits = c(PY_min,PY_max), breaks = seq(1700, 2040, by = Tick_years))
if(show_plots){P_pub_top}
if(save_plots){ggsave(File_P_pub_top, path = User_dir_save, height = Plot_height_PY, width = Plot_width_AI, units = "mm", dpi=300)}
#Visualize TeamTree graph without publication record
P_pub <- ggplot()
P_pub <- P_pub + geom_line(data = D_all, aes(x = Author_index, y = PY_start), color= "gray75", size=0.5)
P_pub <- P_pub + geom_point(data = D_all, aes(x = Author_index, y = PY_start, group = Author_index, color=Author_color), shape = 16, size=4)
P_pub <- P_pub + scale_fill_identity()
P_pub <- P_pub + scale_colour_identity()
P_pub <- P_pub + theme_bw()
P_pub <- P_pub + theme(panel.border = element_blank(), panel.grid.major = element_blank(), panel.grid.minor = element_blank(), axis.line = element_line(colour = "black", size = Line_width))
P_pub <- P_pub + theme(legend.text = element_text(size = Legend_text_size), legend.position = Legend_position_1, legend.title = element_blank(), legend.background=element_rect(fill=NA), legend.key=element_blank())
P_pub <- P_pub + theme(axis.ticks.length = unit(Tick_length, "cm"), axis.title = element_blank(), axis.text = element_blank(), axis.ticks = element_line(color = "black", size = Line_width))
P_pub <- P_pub + theme(plot.margin=grid::unit(c(0,0,0,0), "mm"))
P_pub <- P_pub + coord_cartesian(expand=FALSE)
P_pub <- P_pub + scale_x_continuous(limits = c(AI_min, AI_max), breaks = seq(-50000,50000, by=Tick_AI))
P_pub <- P_pub + scale_y_continuous(limits = c(PY_min,PY_max), breaks = seq(1700, 2040, by = Tick_years))
if(show_plots){P_pub}
if(save_plots){ggsave(File_P_pub_nopr, path = User_dir_save, height = Plot_height_PY, width = Plot_width_AI, units = "mm", dpi=300)}
#Visualize publication records of authors with Top_n PC values
P_pub_top <- ggplot()
P_pub_top <- P_pub_top + geom_line(data = D_chrono, aes(x = Author_index, y = PY, group = Author_index), color = "gray75", size = 0.5)
P_pub_top <- P_pub_top + geom_point(data = D_chrono, aes(x = Author_index, y = PY, group = Author_index), size=2, color = "gray75", shape = 16, alpha = Alpha_all)
P_pub_top <- P_pub_top + geom_line(data = D_all, aes(x = Author_index, y = PY_start), size=0.5, color="gray75")
P_pub_top <- P_pub_top + geom_point(data = D_all, aes(x = Author_index, y = PY_start, color = Author_color), size=2.0, shape = 16)
P_pub_top <- P_pub_top + geom_point(data = D_PC_top, aes(x = Author_index, y = PY_start, color = Author_color, size = PC), shape = 16, alpha=Alpha_all)
#Organoids
#P_pub_top <- P_pub_top + scale_size(range = c(P_PC_top_size_min/2, P_PC_top_size_max/2), breaks = c(P_PC_top_size_min, P_PC_top_size_max))
#CRISPR
P_pub_top <- P_pub_top + scale_size(range = c(P_PC_top_size_min/4, P_PC_top_size_max/4), breaks = c(P_PC_top_size_min, P_PC_top_size_max))
#Aplysia
#P_pub_top <- P_pub_top + scale_size(range = c(P_PC_top_size_min/5, P_PC_top_size_max/5), breaks = c(P_PC_top_size_min, P_PC_top_size_max))
#P_pub_top <- P_pub_top + scale_size_continuous(range = c(P_pub_size_min/1.5, P_pub_size_max/1.5), breaks = c(P_pub_size_min, P_pub_size_max))
#P_pub_top <- P_pub_top + scale_size_continuous(range = c(P_pub_size_min, P_pub_size_max), breaks = c(P_pub_size_min, P_pub_size_max))
#P_pub_top <- P_pub_top + geom_text_repel(data = D_PC_top_minus, aes(x = Author_index, y = PY_start, label = PC_rank, color = Author_color), nudge_x = Tick_AI*-1, nudge_y = -3, hjust = 0, vjust = 0, size = Label_size, inherit.aes=FALSE)
#CRISPR
P_pub_top <- P_pub_top + geom_text_repel(data = D_PC_top_minus, aes(x = Author_index, y = PY_start, label = PC_rank, color = Author_color), nudge_x = Tick_AI*-2, nudge_y = -3, hjust = 0, vjust = 0, size = Label_size, inherit.aes=FALSE)
#CRISPR
P_pub_top <- P_pub_top + geom_text_repel(data = D_PC_top_plus, aes(x = Author_index, y = PY_start, label = PC_rank, color = Author_color), nudge_x = Tick_AI, nudge_y = -3, hjust = 0, vjust = 0, size = Label_size, inherit.aes=FALSE)
#Organoids
#P_pub_top <- P_pub_top + geom_text_repel(data = D_PC_top_plus, aes(x = Author_index, y = PY_start, label = PC_rank, color = Author_color), nudge_x = 2*Tick_AI, nudge_y = -0, hjust = 0, vjust = 0, size = Label_size, inherit.aes=FALSE)
#Aplysia
#P_pub_top <- P_pub_top + geom_text_repel(data = D_PC_top_plus, aes(x = Author_index, y = PY_start, label = PC_rank, color = Author_color), nudge_x = 2*Tick_AI, nudge_y = 0, hjust = 1, vjust = 0, size = Label_size, inherit.aes=FALSE)
P_pub_top <- P_pub_top + scale_fill_identity()
P_pub_top <- P_pub_top + scale_colour_identity()
P_pub_top <- P_pub_top + theme_bw()
P_pub_top <- P_pub_top + theme(panel.border = element_blank(), panel.grid.major = element_blank(), panel.grid.minor = element_blank(), axis.line = element_line(colour = "black", size = Line_width))
#Organoids, CRISPR
P_pub_top <- P_pub_top + theme(legend.text = element_text(size = Legend_text_size), legend.position = Legend_position_1, legend.title = element_blank(), legend.background=element_rect(fill=NA), legend.key=element_blank())
#Aplysia
#P_pub_top <- P_pub_top + theme(legend.text = element_text(size = Legend_text_size), legend.position = Legend_position_4, legend.title = element_blank(), legend.background=element_rect(fill=NA), legend.key=element_blank())
P_pub_top <- P_pub_top + theme(axis.ticks.length = unit(Tick_length, "cm"), axis.title = element_blank(), axis.text = element_blank(), axis.ticks = element_line(color = "black", size = Line_width))
P_pub_top <- P_pub_top + theme(plot.margin=grid::unit(c(0,0,0,0), "mm"))
#P_pub_top <- P_pub_top + coord_cartesian(expand=FALSE)
P_pub_top <- P_pub_top + scale_x_continuous(limits = c(AI_min, AI_max), breaks = seq(-50000,50000, by=Tick_AI))
P_pub_top <- P_pub_top + scale_y_continuous(limits = c(PY_min,PY_max), breaks = seq(1700, 2040, by = Tick_years))
if(show_plots){P_pub_top}
if(save_plots){ggsave(File_P_pub_top, path = User_dir_save, height = Plot_height_PY, width = Plot_width_AI, units = "mm", dpi=300)}
#Plot names of authors with Top_n publication counts
P_PC_names <- ggplot(D_PC_top)
P_PC_names <- P_PC_names + geom_text(aes(x=rep(0, times=10), y=rev(PC_rank), label=legend), , color="black", family="Arial", size=Names_size, parse=F, hjust=0, nudge_x=-15)
P_PC_names <- P_PC_names + theme_bw()
P_PC_names <- P_PC_names + theme(panel.border = element_blank(), panel.grid.major = element_blank(), panel.grid.minor = element_blank(), axis.line = element_blank(), panel.background = element_blank(), plot.background = element_blank())
P_PC_names <- P_PC_names + theme(legend.text = element_blank(), legend.title = element_blank(), axis.ticks = element_blank(), axis.title = element_blank(), axis.text = element_blank())
if(show_plots){P_PC_names}
if(save_plots){ggsave(File_P_PC_names, path = User_dir_save, height = Plot_names_height, width = Plot_names_width, units = "mm", dpi=300)}
#Visualize annual rates of publications/new authors
P_pub_rates <- ggplot()
P_pub_rates <- P_pub_rates + geom_col(data=D_rates[-nrow(D_rates),], aes(x=PY_bins, y = pubs_count), size=1, color=NA, fill="black")
P_pub_rates <- P_pub_rates + geom_col(data=D_rates[-nrow(D_rates),], aes(x=PY_bins, y = authors_count), size=1.5, color=NA, fill="orange")
P_pub_rates <- P_pub_rates + theme_bw()
P_pub_rates <- P_pub_rates + theme(panel.border = element_blank(), panel.grid.major = element_blank(), panel.grid.minor = element_blank(), axis.line = element_line(colour = "black", size = Line_width))
P_pub_rates <- P_pub_rates + theme(axis.ticks.length = unit(Tick_length, "cm"), axis.title = element_blank(), axis.text = element_blank(), axis.ticks = element_line(color = "black", size = Line_width))