Gitlab is now using https://gitlab.lcsb.uni.lu as it's primary address. Please update your bookmarks. FAQ.

Commit 544b92db authored by Aishwarya Alex's avatar Aishwarya Alex
Browse files

working old version- context in Entities

parent 52e49b9f
......@@ -83,6 +83,83 @@ def getSpecies(nodeEntry, speciesDict):
else:
return None
def getEntry(nodeDetails):
print "Getting Entry"
nodeFile="outputs/nodes.txt"
f = open(nodeFile,'r')
lines = f.readlines()
f.close()
speciesMap={}
spId=0
totalSpecies=0
nodeEntry=nodeDetails
if len(lines)>1:
for line in lines[1:]:
print line
lineSplit=line.split()
print lineSplit[1:]
print lineSplit[0]
speciesMap[str(lineSplit[1:])]=lineSplit[0]
totalSpecies=sorted(speciesMap.values())[-1].strip('s')
totalSpecies=int(totalSpecies)
print totalSpecies
spId=getSpecies(nodeDetails, speciesMap)
if spId ==0 or spId is None:
totalSpecies+=1
print "NEW ENTITY : "
print totalSpecies
spId="s"+str(totalSpecies)
speciesMap[nodeEntry]=spId
nodeEntry =spId + "\t" + nodeDetails + "\n"
nFile=open(nodeFile,'a')
nFile.write(nodeEntry)
nFile.close()
nodeEntry = spId + "\t" +nodeDetails + "\n"
print nodeEntry
return spId
def getTermDetails(term,entityDict):
elementName = "" # function@TERM
elementAnnotation = "" # ns@PARAMETER+text
details = "EMPTY"
element = term.get(ns_tag('function'))
parameter = term.find(ns_tag("parameter"))
if element in entityDict.keys():
elementName = entityDict[element]
else:
elementName = element
# LOAD entity DIctionary
alreadyIn = [line.strip() for line in open("outputs/additional/toMapEntities.txt", "r")]
if element not in alreadyIn:
f = open("outputs/additional/toMapEntities.txt", "a")
f.write(element + "\n")
f.close()
if parameter is not None:
belnamespace = parameter.get(ns_tag("ns"))if parameter.get(ns_tag("ns")) is not None else ""
namespaceDict = NT.getNamespaceDict()
miriamnamespace = belnamespace
if namespaceDict.has_key(belnamespace):
miriamnamespace = namespaceDict[belnamespace]
else:
alreadyIn = [line.strip() for line in open("outputs/additional/toAddNamespaces.txt", "r")]
if belnamespace not in alreadyIn:
f = open("outputs/additional/toAddNamespaces.txt", "a")
f.write(belnamespace + "\n")
f.close()
elementAnnotation = parameter.text + "\t" + miriamnamespace + ":" + parameter.text
details=(elementName + "\t" + elementAnnotation) if elementAnnotation and elementName is not "" else details
return details
# function to get parameters for elements (SUBJECT,OBJECT, MODIFIER)
# TO DO: add MODIFIER if SUBJECT/OBJECT is nested
......@@ -95,19 +172,22 @@ def get_details_from_element(node, index, statement_num, evidence, expStatementF
entityDict = NT.getEntityDict()
if term is not None:
if term is not None :
#details=getTermDetails(term,entityDict)
element = term.get(ns_tag('function'))
parameter = term.find(ns_tag("parameter"))
if element in entityDict.keys():
elementName = entityDict[element]
else:
elementName = element
# LOAD entity DIctionary
alreadyIn = [line.strip() for line in open("outputs/additional/toMapEntities.txt", "r")]
if element not in alreadyIn:
f = open("outputs/additional/toMapEntities.txt", "a")
f.write(element + "\n")
f.close()
if parameter is not None:
belnamespace = parameter.get(ns_tag("ns"))if parameter.get(ns_tag("ns")) is not None else ""
namespaceDict = NT.getNamespaceDict()
......@@ -122,7 +202,7 @@ def get_details_from_element(node, index, statement_num, evidence, expStatementF
f.close()
elementAnnotation = parameter.text + "\t" + miriamnamespace + ":" + parameter.text
else:
print remove_ns_tag(node.tag).upper() + " is nested WITH A TERM ## ?? activity(object/subject)/complex"
print remove_ns_tag(node.tag).upper()+": "+elementName + " is nested WITH A TERM ## ?? activity(object/subject)/complex"
nestedTerm = term.find(ns_tag("term"))
print get_node_info_recursive(node)
entry = str(index + 1) + "|" + str(statement_num + 1) + "|" + str(
......@@ -134,15 +214,20 @@ def get_details_from_element(node, index, statement_num, evidence, expStatementF
if "Activity" in elementName:
print "Skipped one level term with Activity:" + elementName
else:
print "Skipped one level :" + elementName
# exit()
print "Complex Components :"
complexComponents=term.findall(ns_tag('term'))
complexDetails=[]
for each in complexComponents:
complexDetails.append(getTermDetails(each,entityDict))
print complexDetails
#exit()
if nestedTerm is not None:
details = get_details_from_element(term, index, statement_num, evidence, expStatementFile)
return details
else:
entry = str(index + 1) + "|" + str(statement_num + 1) + "|" + str(
elementName + " :" + elementAnnotation) + "|" + str(get_node_info_recursive(term)).replace('|',' ') + "|" + str(evidence.encode('utf-8').replace("\r\n", "") + "\n")
entry = str(index + 1) + "|" + str(statement_num + 1) + "|" + str( elementName + " :" + elementAnnotation) #+ "|" + str(get_node_info_recursive(term)).replace('|',' ') + "|" + str(evidence.encode('utf-8').replace("\r\n", "") + "\n")
# print entry
f = open(expStatementFile, 'a')
f.write(entry)
......@@ -161,11 +246,11 @@ def convertXBEL(filepath):
print filepath
nodeFile = "outputs/nodes.txt"
nFile = open(nodeFile, "w")
nFile.write("IDENTIFIER\tTYPE\tNAME\tURN\tSPECIES\tDISEASE\tCELL\tTISSUE\tCELLSTRUCTURE\tCELLLINE\tOTHER_ANNOTATION\n")
nFile.write("IDENTIFIER\tTYPE\tNAME\tURN\n")
nFile.close()
reactionFile = "outputs/reactions.txt"
rFile = open(reactionFile, "w")
rFile.write("IDENTIFIER\tTYPE\tREACTANTS\tMODIFIERS\tPRODUCTS\tMODIFIER_TYPE\tSPECIES\tANNOTATIONS\n")
rFile.write("IDENTIFIER\tTYPE\tREACTANTS\tMODIFIERS\tPRODUCTS\tMODIFIER_TYPE\tSPECIES\tDISEASE\tCELL\tTISSUE\tCELLSTRUCTURE\tCELLLINE\tOTHER_ANNOTATION\n")
rFile.close()
try:
tree = ET.parse(filepath)
......@@ -202,7 +287,7 @@ def convertXBEL(filepath):
annoGroup = statement.find(ns_tag('annotationGroup'))
# get_node_info_recursive(annoGroup)
##get info on the relationship -CURRENTLY :only reference, AVIALABLE :species,and diseases, cellline..
relationship = (statement.get(ns_tag("relationship")) if statement.get(ns_tag("relationship")) is not None else "Complex")
relationship = (statement.get(ns_tag("relationship")) if statement.get(ns_tag("relationship")) is not None else "complexAbundance")
evidence = annoGroup.find(ns_tag("evidence")).text if annoGroup is not None else evidence
citation = annoGroup.find(ns_tag("citation")) if annoGroup is not None else citation
referenceType = citation.get(ns_tag("type")) if annoGroup is not None else referenceType
......@@ -226,84 +311,80 @@ def convertXBEL(filepath):
modifier = ""
product = ""
modifierType = ""
nFile = open(nodeFile, "a")
#nFile = open(nodeFile, "a")
subject=statement.find(ns_tag("subject"))
object =statement.find(ns_tag("object")) if statement.find(ns_tag("object")) is not None else None
nested=object.find(ns_tag("statement")) if object is not None else None
modifier=""
mod=None
entityDict = NT.getEntityDict()
#if statement object is nested, then split to mod(S1) -> (reactant(S2)---product(O2))
if nested is not None:
mod=subject
nestedStatement= object.find(ns_tag("statement"))
subject=nestedStatement.find(ns_tag("subject"))
object =nestedStatement.find(ns_tag("object")) if statement.find(ns_tag("object")) is not None else None
modifierType=relationship
relationship=(nestedStatement.get(ns_tag("relationship")) if nestedStatement.get(ns_tag("relationship")) is not None else "Complex")
entry= get_details_from_element(subject, index,statement_num, evidence, expStatementFile)+"\t"+ annoEntry
spId=getSpecies(entry, speciesMap)
nodeEntry = "s" + str(spId)+ "\t" +entry + "\t" + str(otherAnnotation) + "\n"
if spId is None:
print "NEW ENTITY : reactant"
totalSpecies+=1
spId=totalSpecies
speciesMap[entry]=spId
nodeEntry ="s" + str(spId) + "\t" + entry
nodeEntry = nodeEntry + "\t" + str(otherAnnotation) + "\n"
nFile.write(nodeEntry)
reactant="s" + str(spId)
print "REACTANT : " +nodeEntry
if object is not None:
entry= get_details_from_element(object, index,statement_num, evidence, expStatementFile)+"\t"+ annoEntry
spId=getSpecies(entry, speciesMap)
nodeEntry = "s" + str(spId)+ "\t" +entry + "\t" + str(otherAnnotation) + "\n"
if spId is None:
print "NEW ENTITY : pRODUCT"
totalSpecies+=1
spId=totalSpecies
speciesMap[entry]=spId
nodeEntry ="s" + str(spId) + "\t" + entry
nodeEntry = nodeEntry + "\t" + str(otherAnnotation) + "\n"
nFile.write(nodeEntry)
product = "s" + str(spId)
print "PRODUCT : " +nodeEntry
if mod is not None:
entry= get_details_from_element(mod, index,statement_num, evidence, expStatementFile)+"\t"+ annoEntry
spId=getSpecies(entry, speciesMap)
nodeEntry = "s" + str(spId)+ "\t" +entry + "\t" + str(otherAnnotation) + "\n"
if spId is None:
print "NEW ENTITY : MODIFIER"
totalSpecies+=1
spId=totalSpecies
speciesMap[entry]=spId
nodeEntry ="s" + str(spId) + "\t" + entry
nodeEntry = nodeEntry + "\t" + str(otherAnnotation) + "\n"
nFile.write(nodeEntry)
modifier = "s" + str(spId)
print "MODIFIER : "+nodeEntry
print "\n-----REACTION-----\n"
if modifierType in reactionDict.keys():
modifierType = reactionDict[modifierType]
if relationship in reactionDict.keys():
relationship = reactionDict[relationship]
relationship=(nestedStatement.get(ns_tag("relationship")) if nestedStatement.get(ns_tag("relationship")) is not None else "complexAbundance")
if relationship is 'complexAbundance':
relationship=reactionDict[relationship]
complexComponents=subject.find(ns_tag('term')).findall(ns_tag('term'))
complexDetails=[]
complexList=[]
for each in complexComponents:
complexDetails.append(getTermDetails(each,entityDict))
print complexDetails
for each in complexDetails:
#print each
spId=getEntry(each)
complexList.append(spId)
print "COMPLEX:"+','.join(complexList)
reactionEntry = "r" + str(reactionIdentifier) + "\t" + relationship + "\t" + ','.join(complexList) + "\t" + modifier + "\t" + product + "\t" + modifierType + "\t" + species+"\t"+ referenceType + ":" + reference + "\t" + str(otherAnnotation) + "\n"
print reactionEntry
rFile = open(reactionFile, "a")
rFile.write(reactionEntry)
else:
alreadyIn = [line.strip() for line in open("outputs/additional/toMapReactions.txt", "r")]
if relationship not in alreadyIn:
f = open("outputs/additional/toMapReactions.txt", "a")
f.write(relationship + "\n")
f.close()
reactionEntry = "r" + str(reactionIdentifier) + "\t" + relationship + "\t" + reactant + "\t" + modifier + "\t" + product + "\t" + modifierType + "\t" + species+"\t"+ referenceType + ":" + reference + "\n"
print reactionEntry
rFile = open(reactionFile, "a")
rFile.write(reactionEntry)
nFile.close()
rFile.close()
# if reactionIdentifier==25:
# exit()
entry= get_details_from_element(subject, index,statement_num, evidence, expStatementFile)
print entry
spId=getEntry(entry)
reactant=spId
print "REACTANT : " +entry
if object is not None:
entry= get_details_from_element(object, index,statement_num, evidence, expStatementFile)
print entry
spId=getEntry(entry)
product=spId
print "PRODUCT : " +entry
if mod is not None:
entry= get_details_from_element(mod, index,statement_num, evidence, expStatementFile)
spId=getEntry(entry)
modifier=spId
print "MODIFIER : "+entry
print "\n-----REACTION-----\n"
if modifierType in reactionDict.keys():
modifierType = reactionDict[modifierType]
if relationship in reactionDict.keys():
relationship = reactionDict[relationship]
else:
alreadyIn = [line.strip() for line in open("outputs/additional/toMapReactions.txt", "r")]
if relationship not in alreadyIn:
f = open("outputs/additional/toMapReactions.txt", "a")
f.write(relationship + "\n")
f.close()
reactionEntry = "r" + str(reactionIdentifier) + "\t" + relationship + "\t" + reactant + "\t" + modifier + "\t" + product + "\t" + modifierType + "\t" + species+"\t"+ referenceType + ":" + reference +"\t"+ annoEntry+"\t" + str(otherAnnotation)+"\n"
print reactionEntry
rFile = open(reactionFile, "a")
rFile.write(reactionEntry)
rFile.close()
except Exception as ex:
print " !!!EXCEPTION!!! : " + str(ex.args)
......@@ -312,5 +393,5 @@ def convertXBEL(filepath):
# Convert file
# /home/alex/PhD/Bel2CellD/full_abstract3.xbel
#/home/alex/PhD/Bel2CellD/small_corpus.xbel
#convertXBEL('/home/alex/PhD/Bel2CellD/small_corpus.xbel')
convertXBEL('/home/alex/PhD/APP/Aetionomy_AD.xbel')
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment