Skip to content

Commit

Permalink
first working version
Browse files Browse the repository at this point in the history
  • Loading branch information
coleslaw481 committed Mar 31, 2020
1 parent b04535e commit b5f4afe
Show file tree
Hide file tree
Showing 2 changed files with 109 additions and 2 deletions.
98 changes: 96 additions & 2 deletions cdhidef/cdhidefcmd.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import traceback
import subprocess
import uuid
import csv
import shutil

DEFAULT_ERR_MSG = ('Did not get any clusters from HiDeF. This could be ' +
Expand Down Expand Up @@ -58,6 +59,99 @@ def run_hidef_cmd(cmd):
return p.returncode, out, err


def get_max_node_id(nodes_file):
"""
:param nodes_file:
:return:
"""
maxval = None
with open(nodes_file, 'r') as csvfile:
linereader = csv.reader(csvfile, delimiter='\t')
for row in linereader:
for node in row[2].split(' '):
if maxval is None:
maxval = int(node)
continue
curval = int(node)
if curval > maxval:
maxval = curval
return maxval


def write_members_for_row(out_stream, row, cur_node_id):
"""
:param out_stream:
:param row:
:param cur_node_id:
:return:
"""
for node in row[2].split(' '):
out_stream.write(str(cur_node_id) + ',' +
node + ',c-m;')

def update_cluster_node_map(cluster_node_map, cluster, max_node_id):
"""
:param cluster_node_map:
:param cluster:
:param max_node_id:
:return:
"""
if cluster not in cluster_node_map:
max_node_id += 1
cluster_node_map[cluster] = max_node_id
cur_node_id = max_node_id
else:
cur_node_id = cluster_node_map[cluster]
return max_node_id, cur_node_id


def write_communities(out_stream, edge_file, cluster_node_map):
"""
:param out_stream:
:param edgefile:
:return:
"""
with open(edge_file, 'r') as csvfile:
linereader = csv.reader(csvfile, delimiter='\t')
for row in linereader:
out_stream.write(str(cluster_node_map[row[0]]) + ',' +
str(cluster_node_map[row[1]]) + ',c-c;')


def convert_hidef_output_to_cdaps(out_stream, tmpdir):
"""
Looks for x.nodes and x.edges in `tmpdir` directory
to generate output in COMMUNITYDETECTRESULT format:
https://github.com/idekerlab/communitydetection-rest-server/wiki/COMMUNITYDETECTRESULT-format
which is
:param out_stream: output stream to write results
:type out_stream: file like object
:param tmpdir:
:type tmpdir: str
:return:
"""
nodefile = os.path.join(tmpdir, 'x.nodes')
max_node_id = get_max_node_id(nodefile)
cluster_node_map = {}
with open(nodefile, 'r') as csvfile:
linereader = csv.reader(csvfile, delimiter='\t')
for row in linereader:
max_node_id, cur_node_id = update_cluster_node_map(cluster_node_map,
row[0],
max_node_id)
write_members_for_row(out_stream, row,
cur_node_id)
edge_file = os.path.join(tmpdir, 'x.edges')
write_communities(out_stream, edge_file, cluster_node_map)
out_stream.write('\n')
return None


def run_hidef(theargs):
"""
Expand All @@ -78,8 +172,7 @@ def run_hidef(theargs):
try:
outval = os.path.join(tmpdir, 'x')
cmdargs.extend(['--o', outval])
sys.stderr.write('tmpdir: ' + tmpdir + '\n')
sys.stderr.write('Running ' + str(cmdargs) + '\n')
sys.stderr.write('Running ' + str(' '.join(cmdargs)) + '\n')
sys.stderr.flush()
cmdecode, cmdout, cmderr = run_hidef_cmd(cmdargs)

Expand All @@ -94,6 +187,7 @@ def run_hidef(theargs):
if len(cmderr) > 0:
sys.stderr.write('Error output from cmd: ' + str(cmderr) + '\n')

convert_hidef_output_to_cdaps(sys.stdout, tmpdir)
sys.stdout.flush()
return 0
finally:
Expand Down
13 changes: 13 additions & 0 deletions tests/test_cdhidef.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,18 @@
import unittest
import tempfile
import shutil
import io
from unittest.mock import MagicMock
from cdhidef import cdhidefcmd


class TestCdhidef(unittest.TestCase):

TEST_DIR = os.path.dirname(__file__)

HUNDRED_NODE_DIR = os.path.join(TEST_DIR, 'data',
'100node_example')

def setUp(self):
pass

Expand Down Expand Up @@ -85,6 +91,13 @@ def test_createtmpdir(self):
finally:
shutil.rmtree(temp_dir)

def test_convert_hidef_output_to_cdaps_with_hundred(self):
f_out = io.StringIO()
res = cdhidefcmd.convert_hidef_output_to_cdaps(f_out,
TestCdhidef.HUNDRED_NODE_DIR)
self.assertEqual(None, res)
self.assertEqual(3629, len(f_out.getvalue()))


if __name__ == '__main__':
sys.exit(unittest.main())

0 comments on commit b5f4afe

Please sign in to comment.