Categories
Uncategorized

From ncol to Pajek – Python

Here is a small script that will change an ncol file to a pajek one.

The ncol file

The ncol looks something like this

userA    userB    red
userC    userD    blue
userC    userA    red
        ....
userXA    userE    green
userXD    userFR    blue
userB    userE    green

where each column is divided by a single space, and the last column is the edge color.

The Pajek file

The final pajek file will be formated like this

*Vertices 4513

*Edges
1 2 c red
3 4 c blue
3 1 c red
  ...
63 4 c green
73 34 c blue
2 4 c green

The Python Script

# from ncol to pajek

from re import sub

# your file containing ncols
inputFile='ncolFile.txt'

# this will be he output
outputFile='pajekFile.txt'

# read the ncol File
riFile=open(inputFile,'r')
fullNodes=riFile.readlines()
riFile.close()

# function that creates a dictionary
# to apply the ids
def getUserDict():
    # merges column 1 and two into a set
    # to get all unique nodes
	userSet=set()
	for eachI in fullNodes:
		cLine=sub('\n','',eachI)
		comps=cLine.split(' ')
		userSet.add(comps[0])
		userSet.add(comps[1])
    # Allocates an ID to each node
    # and places it in a dictionary
	theDict={}
	counter=1
	for eachItem in userSet:
		theDict[eachItem]=counter
		counter+=1
	return theDict

# create dictionary using above function
userDict=getUserDict()

# A function to replace node names with IDs
# and then create the pajek file
def changeToID():
    # Open/Make the output file
	wOutputFile=open(outputFile,'w')
    # writes the top Pajek lines
    topPajekLines='*Vertices %d \n\n*Edges\n'%len(userDict)
	wOutputFile.write(topPajekLines)
    # applies the ID to each line
	for eachLine in fullNodes:
		cLine=sub('\n','',eachLine)
		comps=cLine.split(' ')
		fN,tN,cN=comps[0],comps[1],comps[2]
		fID,tID=userDict[fN],userDict[tN]
		outLine=str(fID)+' '+str(tID)+' c '+cN+'\n'
		wOutputFile.write(outLine)
	wOutputFile.close()

# runs the above function script
changeToID()

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s