#Code written by William Doonan 2019
#This code randomizes a community structure (Infomap here)
#and will end with a data structure called citDistFirst which contains
#the necessary information
#Only Infomap will be randomized to save time when running the code.

#Required libraries
library("igraph")
library(plyr)
library(splitstackshape)
library(rapport)
library(rapportools)
library(pracma)

#functions
sortByClass<- function(class,x){
	index<- NULL
	x<- as.character(x)
	index<- grep(paste("^",class,"$|\\D",class,"$|^",class,"\\D|\\D",class,"\\D",sep=""),x)
	return(index)
}

delta_t<- function(x,t){
	if(t>1){
		x$ind<- c(1:length(x[,1]))
		ifelse(t<1+max(x$seq),xt<- x[x$seq==t,],return("No patent at this lag"))
		xt1<- x[(xt$ind-1),]
		xt$dellag<- xt$lag-xt1$lag
		return(xt)
	}else{
		return(x[x$seq==1,])
	}
}

#Inventor data
yeardataX<- read.table("InventorData1995to2009.csv",T,sep=",")
#Citation data
Citations_data<- read.table("Cite_Liam_10.csv",TRUE, sep=",")

Citations_data$lag<- (Citations_data$citing_y- Citations_data$cited_y)*12 + Citations_data$citing_m- Citations_data$cited_m

#This is the graph
collabGraphBig<- read.graph("pythonCollabGraphPlaces1995to1999Classes257and438and326.graphml",format="graphml")

#Finding the communities in the graph
#Normal community structure
infmp<- cluster_infomap(collabGraphBig,e.weights=E(collabGraphBig)$weight)
membersi<- membership(infmp)
#Randomized
infmpRand<- infmp
membersir<- membersi
membersirand<- infmpRand$membership
membersirand<- sample(membersirand)
infmp$membership<- membersirand
membersir[c(1:length(membersir))]<- membersirand

nodes<- V(collabGraphBig)$name

classes<- c(257,438,326)
indicie<- NULL
for(i in 1:length(classes)){
	indicie<- c(indicie,sortByClass(classes[i],yeardataX$Class))
}
indicies<- unique(indicie)
yeardataX<- yeardataX[indicies,]

#Getting rid of names that arn't in the graph to begin with
yeardataX$nameMatch<- paste(yeardataX$Firstname,yeardataX$Lastname,yeardataX$City,sep=" ")
matches<- match(yeardataX$nameMatch,nodes)
mathced<- matches %in% NA
matched<- mathced %in% FALSE
yeardataX<- yeardataX[matched,]

#Convert Patents column from factors to numbers and remove NA's
yeardataX$Patent<- as.numeric(as.character(yeardataX$Patent))
yeardataX<- yeardataX[FALSE %in% is.na(yeardataX$Patent),]

#Having a full file and one that has only unique patent data
yeardataXX<- yeardataX
InvOrder<- ddply(yeardataXX,.(Patent),.fun=nrow)


yeardataXX$inventorIndex<- match(yeardataXX$nameMatch,nodes)
yeardataXX$groupinfmp<- membersi[yeardataXX$inventorIndex]
yeardataXX$groupinfmpR<- membersir[yeardataXX$inventorIndex]


#If citation file doesnt have a 'patent' column which represents the cited patents
Citations_data$patent<- Citations_data$cited

#Deleting citing and cited patents from the citation data that are not made by people in the graph 
matches<- match(Citations_data$citing,InvOrder$Patent)
mathced<- matches %in% NA
matched<- mathced %in% FALSE
Citations_data<- Citations_data[matched,]

matches<- match(Citations_data$patent,InvOrder$Patent)
mathced<- matches %in% NA
matched<- mathced %in% FALSE
Citations_data<- Citations_data[matched,]

#Assigning the citing and cited patents the community they belong to
#Citations_data$citingGroup<- yeardataX$group[match(Citations_data$citing,yeardataX$Patent)]
#Citations_data$citedGroup<- yeardataX$group[match(Citations_data$patent,yeardataX$Patent)]
cit<- Citations_data

#Assigning the number of inventors on a singular patent for both citing and cited
cit$citingNumInv<- InvOrder$V1[match(cit$citing,InvOrder$Patent)]
cit$citedNumInv<- InvOrder$V1[match(cit$patent,InvOrder$Patent)]
cit$freq<- cit$citingNumInv*cit$citedNumInv
cit$identification<- c(1:length(cit[,1]))

citBig<- expandRows(cit,"freq")

#Organising the cited inventors
citcited<- data.frame(cit$patent,cit$citedNumInv)
citcited$InvNumbers<- citcited$cit.citedNumInv
citcited<- expandRows(citcited,"cit.citedNumInv")
citcited$index<- match(citcited$cit.patent,yeardataXX$Patent)
citcited$seq<- sequence(rle(as.character(citcited$index))$lengths)
citcited$num<- citcited$seq%%citcited$InvNumbers
citcited$index<- citcited$index+citcited$num
citcited$inventors<- yeardataXX$nameMatch[citcited$index]
citcited$citedGroupInfmp<- yeardataXX$groupinfmp[citcited$index]
citcited$citedGroupInfmpR<- yeardataXX$groupinfmpR[citcited$index]
citedreps<- rep(cit$citingNumInv,cit$citedNumInv)
citcited$reps<- citedreps
citcitedBig<- expandRows(citcited,"reps")

#Organising the cited inventors
citciting<- data.frame(cit$citing,cit$citingNumInv)
citciting$InvNumbers<- citciting$cit.citingNumInv
citciting<- expandRows(citciting,"cit.citingNumInv")
citciting$index<- match(citciting$cit.citing,yeardataXX$Patent)
citciting$seq<- sequence(rle(as.character(citciting$index))$lengths)
citciting$num<- citciting$seq%%citciting$InvNumbers
citciting$index<- citciting$index+citciting$num
citciting$inventors<- yeardataXX$nameMatch[citciting$index]
citciting$citingGroupInfmp<- yeardataXX$groupinfmp[citciting$index]
citciting$citingGroupInfmpR<- yeardataXX$groupinfmpr[citciting$index]
citingreps<- rep(cit$citedNumInv,cit$citingNumInv)
citciting$reps<- citingreps
#Ordering the inventors properly
citcitingBig<- citciting[rep(seq_along(citciting$reps),citciting$reps),]
citcitingBig$ID<- unlist(lapply(citciting$reps,seq_len))
citcitingBig$ID<- sprintf(paste("%0",nchar(max(citcitingBig$ID)),".0f",sep=""),citcitingBig$ID)
citcitingBig$ID<- paste(citcitingBig$cit.citing,citcitingBig$ID,sep=".")

citcitingBig<- citcitingBig[order(citcitingBig$ID),]

#Adding the inventors to citBig
citBig$InventorsCiting<- citcitingBig$inventors
citBig$InventorsCited<- citcitedBig$inventors


citBig$citingGroupInfmp<- citcitingBig$citingGroupInfmp
citBig$citingGroupInfmpR<- citcitingBig$citingGroupInfmpR

citBig$citedGroupInfmp<- citcitedBig$citedGroupInfmp
citBig$citedGroupInfmpR<- citcitedBig$citedGroupInfmpR

citBig$ID<- c(1:length(citBig[,1]))

#Deterining in-communityness and self citations
citBig$selfcheck<- ddply(citBig,.(ID),.fun=function(x)strcmp(x$InventorsCiting,x$InventorsCited))$V1
citBig$groupcheckInfmp<- citBig$citingGroupInfmp==citBig$citedGroupInfmp
citBig$groupcheckInfmpR<- citBig$citingGroupInfmpR==citBig$citedGroupInfmpR

citDist<- ddply(citBig,.(citing,cited),.fun=function(x)x[which.min(x$ID), ])

extr<- ddply(citBig,.(citing,cited,identification),.fun=function(x)ifelse(TRUE %in% x$selfcheck,return(1),return(0)))
extr2<- ddply(citBig,.(citing,cited,identification),.fun=function(x)ifelse(TRUE %in% x$groupcheckInfmp,return(1),return(0)))
extr3<- ddply(citBig,.(citing,cited,identification),.fun=function(x)ifelse(TRUE %in% x$groupcheckInfmpR,return(1),return(0)))

citDist$selfcit<-extr$V1[citDist$identification]
citDist$ingroupInfmp<-extr2$V1[citDist$identification]
citDist$ingroupInfmpR<-extr3$V1[citDist$identification]


citDist<- citDist[citDist$lag<=120,]
citDist<- citDist[citDist$cited_y<2000,]

citDist<- citDist[order(citDist$cited),]
citDist$Index<- c(1:length(citDist$cited))
temp<- data.frame(num=citDist$cited,val=citDist$lag,ind=citDist$Index)
dpp<- ddply(temp,.(num,val),fun=order)
citDist<- citDist[dpp$ind,]
citDist$seq<- sequence(rle(citDist$cited)$lengths)

citDistFirst<- delta_t(citDist,1)



