Attachment 'Cytoscape.py'
from Bio import File
from InteractionRecord import InteractionRecord
import string
import os
from types import *
class InteractionReader:
##"""Iterator that returns InteractionRecords from interaction file (such
## as Cytoscape 'sif' file):
## gene1 <tab> gene2
## OR
## gene1 <tab> type <tab> gene2
##"""
def __init__(self,handle):
if type(handle) is not FileType and type(handle) is not InstanceType:
raise ValueError, "I expected a file handle or file-like object"
self._uhandle = File.UndoHandle(handle)
# find the start of data
while self._uhandle.peekline() == "":
line = self._uhandle.readline()
self._n = 0
def next():
line = self._uhandle.readline()
if not line:
return None
fields = string.split(line.rstrip(),'\t')
if len(fields == 3):
source = fields[0]
type = fields[1]
target = fields[2]
else:
# often interaction files have many fields
# but first two are interactors
source = fields[0]
target = fields[1]
self._n = self._n + 1
return InteractionRecord(source=source,target=target,type=type)
def __getitem__(self,i):
# wrapper to the normal Python "for spam in list:" idiom
assert i == self._n # forward iteration only!
x = self.next()
if x is None:
raise IndexError, i
return x
class InteractionWriter:
"""Takes InteractionRecords and writes to simple interaction file (Cytoscape compatible)"""
def __init__(self, handle):
self._handle = handle
self.type_dict = {'pp':
['pp','two-hybrid','two hybrid','immunoprecipitation','affinity chromatography'],
'gg':
['gg','phenotypic suppression','phenotypic enhancement','synthetic lethal'],
'pd':
['pd','protein-dna','dna-binding','dna binding']}
def write(self,record):
type = ''
for key,labels in self.type_dict.items():
if record.type.lower() in labels:
type = key
if not type:
line = '\t'.join([record.source,record.target])
else:
line = '\t'.join([record.source,type,record.target])
self._handle.write(line+'\n')
def write_records(self,records):
for record in records:
self.write(record)
##also write edge properties, node properties files
class NodePropertiesReader:
pass
class NodePropertiesWriter:
pass
You are not allowed to view this page.
