From 0a555037e6d5692d7b86260007b847bfc3af6420 Mon Sep 17 00:00:00 2001 From: Ido Bar Date: Sun, 16 Jun 2024 01:05:28 +1000 Subject: [PATCH 1/5] Remove usage of itertools --- rapclust/eqnet.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/rapclust/eqnet.py b/rapclust/eqnet.py index c7c589e..d4af697 100644 --- a/rapclust/eqnet.py +++ b/rapclust/eqnet.py @@ -1,7 +1,6 @@ from __future__ import print_function def buildNetFile(sampdirs, netfile, cutoff, auxDir, writecomponents=False): - import itertools import pandas as pd import numpy as np import os @@ -22,7 +21,7 @@ def buildNetFile(sampdirs, netfile, cutoff, auxDir, writecomponents=False): numSamp = 0 tot = 0 eqClasses = {} - for sffile, eqfile in itertools.izip(sffiles, eqfiles): + for sffile, eqfile in zip(sffiles, eqfiles): quant = pd.read_table(sffile) quant.set_index('Name', inplace=True) From a523904e14978324c64e0da3725e0bfc74fb4fc4 Mon Sep 17 00:00:00 2001 From: Ido Bar Date: Sun, 16 Jun 2024 01:42:17 +1000 Subject: [PATCH 2/5] Convert eqnet.py to Python3 --- rapclust/eqnet.py | 40 ++++++++++++++++++---------------------- 1 file changed, 18 insertions(+), 22 deletions(-) diff --git a/rapclust/eqnet.py b/rapclust/eqnet.py index d4af697..f823d3a 100644 --- a/rapclust/eqnet.py +++ b/rapclust/eqnet.py @@ -1,4 +1,3 @@ -from __future__ import print_function def buildNetFile(sampdirs, netfile, cutoff, auxDir, writecomponents=False): import pandas as pd @@ -31,13 +30,13 @@ def buildNetFile(sampdirs, netfile, cutoff, auxDir, writecomponents=False): numEq = int(ifile.readline().rstrip()) logging.info("quant file: {}; eq file: {}; # tran = {}; # eq = {}".format(sffile, eqfile, numTran, numEq)) if firstSamp: - for i in xrange(numTran): + for i in range(numTran): tnames.append(ifile.readline().rstrip()) diagCounts = np.zeros(len(tnames)) sumCounts = np.zeros(len(tnames)) ambigCounts = np.zeros(len(tnames)) else: - for i in xrange(numTran): + for i in range(numTran): ifile.readline() # for easy access to quantities of interest @@ -47,8 +46,8 @@ def buildNetFile(sampdirs, netfile, cutoff, auxDir, writecomponents=False): epsilon = np.finfo(float).eps sumCounts = np.maximum(sumCounts, estCount) - for i in xrange(numEq): - toks = map(int, ifile.readline().rstrip().split('\t')) + for i in range(numEq): + toks = list(map(int, ifile.readline().rstrip().split('\t'))) nt = toks[0] tids = tuple(toks[1:-1]) count = toks[-1] @@ -84,7 +83,7 @@ def buildNetFile(sampdirs, netfile, cutoff, auxDir, writecomponents=False): # Go through the weightMap and remove any edges that # have endpoints with too few mapping reads ## - for k,v in weightDict.iteritems(): + for k,v in weightDict.items(): c0, c1 = diagCounts[k[0]], diagCounts[k[1]] a0, a1 = ambigCounts[k[0]], ambigCounts[k[1]] if a0 + a1 > epsilon and a0 > cutoff and a1 > cutoff: @@ -118,8 +117,8 @@ def nearEnd(tup): for olfile in orphanLinkFiles: for l in open(olfile): left, right = l.rstrip().split(':') - lp = [map(int, i.split(',')) for i in left.rstrip('\t').split('\t')] - rp = [map(int, i.split(',')) for i in right.split('\t')] + lp = [list(map(int, i.split(','))) for i in left.rstrip('\t').split('\t')] + rp = [list(map(int, i.split(','))) for i in right.split('\t')] lp = [t[0] for t in filter(nearEnd, lp)] rp = [t[0] for t in filter(nearEnd, rp)] if len(lp) == 1 and len(rp) == 1: @@ -138,7 +137,7 @@ def nearEnd(tup): tnamesFilt = [] relabel = {} - for i in xrange(len(estCount)): + for i in range(len(estCount)): if (diagCounts[i] > cutoff): relabel[i] = len(tnamesFilt) tnamesFilt.append(tnames[i]) @@ -159,7 +158,7 @@ def nearEnd(tup): def writeEdgeList(weightDict, tnames, ofile, G): useGraph = G is not None - for k,v in weightDict.iteritems(): + for k,v in weightDict.items(): ofile.write("{}\t{}\t{}\n".format(tnames[k[0]], tnames[k[1]], v)) if useGraph: G.add_edge(tnames[k[0]], tnames[k[1]]) @@ -172,7 +171,7 @@ def writePajek(weightDict, tnames, relabel, ofile): ofile.write("{}\t\"{}\"\n".format(i, n)) ofile.write("*Edges\n") print("There are {} edges\n".format(len(weightDict))) - for k,v in weightDict.iteritems(): + for k,v in weightDict.items(): ofile.write("{}\t{}\t{}\n".format(relabel[k[0]], relabel[k[1]], v)) #ofile.write("{}\t{}\t{}\n".format(tnames[k[0]], tnames[k[1]], v)) #if k[0] != k[1]: @@ -201,15 +200,15 @@ def readEqClass(eqfile, eqCollection): print("file: {}; # tran = {}; # eq = {}".format(eqfile, numTran, numEq)) if not eqCollection.hasNames: tnames = [] - for i in xrange(numTran): + for i in range(numTran): tnames.append(ifile.readline().rstrip()) eqCollection.setNames(tnames) else: - for i in xrange(numTran): + for i in range(numTran): ifile.readline() - for i in xrange(numEq): - toks = map(int, ifile.readline().rstrip().split('\t')) + for i in range(numEq): + toks = list(map(int, ifile.readline().rstrip().split('\t'))) nt = toks[0] tids = tuple(toks[1:-1]) count = toks[-1] @@ -218,7 +217,7 @@ def readEqClass(eqfile, eqCollection): def getCountsFromEquiv(eqCollection): countDict = {} tn = eqCollection.tnames - for tids, count in eqCollection.eqClasses.iteritems(): + for tids, count in eqCollection.eqClasses.items(): for t in tids: if tn[t] in countDict: countDict[tn[t]] += count @@ -250,7 +249,7 @@ def filterGraph(expDict, netfile, ofile, auxDir): logger = logging.getLogger("rapclust") # Get just the set of condition names - conditions = expDict.keys() + conditions = list(expDict.keys()) logging.info("conditions = {}".format(conditions)) #for cond in conditions: @@ -272,7 +271,7 @@ def filterGraph(expDict, netfile, ofile, auxDir): eqClasses = {} for cond in conditions: print(expDict[cond]) - for sampNum, sampPath in expDict[cond].iteritems(): + for sampNum, sampPath in expDict[cond].items(): if cond not in eqClasses: eqClasses[cond] = EquivCollection() eqPath = os.path.sep.join([sampPath, auxDir, "/eq_classes.txt"]) @@ -289,7 +288,7 @@ def filterGraph(expDict, netfile, ofile, auxDir): numTrimmed = 0 with open(netfile) as f, open(ofile, 'w') as ofile: data = pd.read_table(f, header=None) - for i in tqdm(range(len(data))): + for i in tqdm(list(range(len(data)))): count += 1 #print("\r{} done".format(count), end="") #Alternative hypo @@ -321,6 +320,3 @@ def filterGraph(expDict, netfile, ofile, auxDir): else: numTrimmed += 1 logging.info("Trimmed {} edges".format(numTrimmed)) - - - From 3421cc1ed3910e0d8a1bdae2e1bb73d3345455c5 Mon Sep 17 00:00:00 2001 From: IdoBar Date: Mon, 17 Jun 2024 00:51:12 +1000 Subject: [PATCH 3/5] Import itertools --- bin/RapClust | 2 +- rapclust/eqnet.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/bin/RapClust b/bin/RapClust index eae743a..9c4b22a 100755 --- a/bin/RapClust +++ b/bin/RapClust @@ -3,7 +3,7 @@ import sys import os import logging import coloredlogs - +import itertools import click import argparse import yaml diff --git a/rapclust/eqnet.py b/rapclust/eqnet.py index f823d3a..5886e30 100644 --- a/rapclust/eqnet.py +++ b/rapclust/eqnet.py @@ -2,7 +2,7 @@ def buildNetFile(sampdirs, netfile, cutoff, auxDir, writecomponents=False): import pandas as pd import numpy as np - import os + import os,itertools import logging logger = logging.getLogger("rapclust") From 4f3353aed0ff5c3278b035fbe5881f958a3c2444 Mon Sep 17 00:00:00 2001 From: Ido Bar Date: Wed, 21 May 2025 10:01:34 +1000 Subject: [PATCH 4/5] Fix JSON write file --- bin/RapClust | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/bin/RapClust b/bin/RapClust index eae743a..36fb9a0 100755 --- a/bin/RapClust +++ b/bin/RapClust @@ -8,13 +8,22 @@ import click import argparse import yaml import json - +import numpy as np from rapclust import eqnet CONTEXT_SETTINGS = dict(help_option_names=['-h', '--help']) @click.command(context_settings=CONTEXT_SETTINGS) @click.option('--config', required=True, help='Config file describing the experimental setup') +def myconverter(obj): + if isinstance(obj, np.integer): + return int(obj) + elif isinstance(obj, np.floating): + return float(obj) + elif isinstance(obj, np.ndarray): + return obj.tolist() + elif isinstance(obj, datetime.datetime): + return obj.__str__() def processQuant(config): logging.basicConfig(level=logging.INFO) logger = logging.getLogger("rapclust") @@ -95,9 +104,10 @@ def processQuant(config): stats['max clust size'] = sizes.max() stats['mean clust size'] = sizes.mean() stats['num clusts'] = len(sizes) - + jsonstr = json.dumps(stats, default=myconverter) with open(summaryFile, 'w') as ofile: - json.dump(stats, ofile) + ofile.write(jsonstr) + # json.dump(stats, ofile, default=myconverter) if __name__ == "__main__": #parser = argparse.ArgumentParser(description='Cluster de novo assemblies.') From fa801ead5046a3ade81d1d2599662a63dd1c8755 Mon Sep 17 00:00:00 2001 From: IdoBar Date: Wed, 21 May 2025 15:23:50 +1000 Subject: [PATCH 5/5] Fixed function definition and YAML load warning --- bin/RapClust | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/bin/RapClust b/bin/RapClust index 7bc38c6..1cfa7a5 100755 --- a/bin/RapClust +++ b/bin/RapClust @@ -13,8 +13,6 @@ from rapclust import eqnet CONTEXT_SETTINGS = dict(help_option_names=['-h', '--help']) -@click.command(context_settings=CONTEXT_SETTINGS) -@click.option('--config', required=True, help='Config file describing the experimental setup') def myconverter(obj): if isinstance(obj, np.integer): return int(obj) @@ -24,6 +22,10 @@ def myconverter(obj): return obj.tolist() elif isinstance(obj, datetime.datetime): return obj.__str__() + +@click.command(context_settings=CONTEXT_SETTINGS) +@click.option('--config', required=True, help='Config file describing the experimental setup') + def processQuant(config): logging.basicConfig(level=logging.INFO) logger = logging.getLogger("rapclust") @@ -32,7 +34,7 @@ def processQuant(config): #config = args.config cfg = None with open(config, 'r') as yamCfg: - cfg = yaml.load(yamCfg) + cfg = yaml.load(yamCfg,Loader=yaml.FullLoader) # A list of all sample directories sampleDirs = []