Skip to content

Commit

Permalink
Merge pull request #464 from cclauss/print-function
Browse files Browse the repository at this point in the history
Use print() function in both Python 2 and Python 3
  • Loading branch information
ursky authored Jul 28, 2023
2 parents 83e3fe3 + 7581ea1 commit c4a23f0
Show file tree
Hide file tree
Showing 35 changed files with 140 additions and 107 deletions.
1 change: 0 additions & 1 deletion bin/metaWRAP

This file was deleted.

7 changes: 4 additions & 3 deletions bin/metawrap-scripts/add_bins_to_blobplot.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
# THis script takes in a blobplot text file and and number of bins as input, and annotates
# each line in the blob file with its bin name (if that contig exists in one of the bins)

from __future__ import print_function
import sys, os

# load the binned contigs:
Expand All @@ -19,11 +20,11 @@
if line.split("\t")[0]=="seqid":
for i, field in enumerate(line.strip().split("\t")):
if field=="taxlevel_phylum": phylum_column=i
print line.strip() + "\tbin\tbinned_yes_no\tbinned_phylum"
print(line.strip() + "\tbin\tbinned_yes_no\tbinned_phylum")
elif line.split("\t")[0] in contig_bins:
phylum=line.split("\t")[phylum_column]
print "\t".join([line.strip(), contig_bins[line.split("\t")[0]], "Binned", phylum])
print("\t".join([line.strip(), contig_bins[line.split("\t")[0]], "Binned", phylum]))

else:
print "\t".join([line.strip(), "Unbinned", "Unbinned", "Unbinned"])
print("\t".join([line.strip(), "Unbinned", "Unbinned", "Unbinned"]))

1 change: 1 addition & 0 deletions bin/metawrap-scripts/binning_refiner.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
# And the publication: https://www.ncbi.nlm.nih.gov/pubmed/28186226


from __future__ import print_function
import os
import glob
import shutil
Expand Down
5 changes: 3 additions & 2 deletions bin/metawrap-scripts/blobology/rm_short_contigs.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
#! /usr/bin/env python

from __future__ import print_function
import sys

f=open(sys.argv[1])
for line in f:
if line.startswith(">"):
cut=line.split("_")
if int(cut[3])>999:
print line.strip()
print(line.strip())
else: quit
else: print line.strip()
else: print(line.strip())
3 changes: 2 additions & 1 deletion bin/metawrap-scripts/choose_best_bin.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#!/usr/bin/env python2.7
from __future__ import print_function
import sys

# This script takes in the reassembled_bins.stats file of the binning module and choses the best possible
Expand Down Expand Up @@ -34,7 +35,7 @@


for i in best_bins:
print i+'.'+best_bins[i][0]
print(i+'.'+best_bins[i][0])



3 changes: 2 additions & 1 deletion bin/metawrap-scripts/classify_bins.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#!/usr/bin/env python2.7
from __future__ import print_function
import sys,os

def add_to_tree( tree, tax_list, length ):
Expand Down Expand Up @@ -60,5 +61,5 @@ def traverse(tree, taxonomy, weight):

#print tax_tree
consensus=traverse(tax_tree, [], 0)
print filename + "\t" + ";".join(consensus)
print(filename + "\t" + ";".join(consensus))

15 changes: 8 additions & 7 deletions bin/metawrap-scripts/consolidate_two_sets_of_bins.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#!/usr/bin/env python2.7
from __future__ import print_function
import sys, os

'''
Expand All @@ -18,7 +19,7 @@
x=float(sys.argv[7])

# load a list of good bins (>70% complete, <10% contaminated) to save time (wont look at bad bins later on).
print "Loading list of good bins (comp>" + str(c)+ "%, cont<" + str(x) + "%)"
print("Loading list of good bins (comp>" + str(c)+ "%, cont<" + str(x) + "%)")
good_bins_1={}
good_bins_2={}
for line in open(sys.argv[3]):
Expand Down Expand Up @@ -50,7 +51,7 @@
'''


print "load in the info about the contigs in each bin..."
print("load in the info about the contigs in each bin...")
for bin_file in good_bins_1:
bins_1[bin_file]={}
contig_len=0
Expand Down Expand Up @@ -82,7 +83,7 @@



print "make all bossible comparisons between the two bin sets, and record total % idential length"
print("make all bossible comparisons between the two bin sets, and record total % idential length")
all_bin_pairs={}
for bin_1 in good_bins_1:
all_bin_pairs[bin_1]={}
Expand All @@ -107,7 +108,7 @@
all_bin_pairs[bin_1][bin_2]=max([ratio_1, ratio_2])


print "load in completion and contamination scores of all the bins"
print("load in completion and contamination scores of all the bins")
bins_1_stats={}
bins_2_stats={}
bins_1_summary={}
Expand All @@ -131,7 +132,7 @@


# go through all good bins and chose best ones
print "go through first group, pull out identical bins from second group, and choose best"
print("go through first group, pull out identical bins from second group, and choose best")
os.system("mkdir "+sys.argv[5])
new_summary_file=bins_1_summary["header"]
bins_2_matches={}
Expand All @@ -154,7 +155,7 @@
os.system(cmd)
bin_ct+=1

print "retrieve bins from second group that were not found in first group"
print("retrieve bins from second group that were not found in first group")
for bin_2 in bins_2_stats:
if bins_2_stats[bin_2][0]<c or bins_2_stats[bin_2][1]>x: continue
if bin_2 in bins_2_matches: continue
Expand All @@ -167,6 +168,6 @@
f = open(sys.argv[5]+".stats", 'w')
f.write(new_summary_file)

print "There were " + str(bin_ct) + " bins cherry-picked from the original sets!"
print("There were " + str(bin_ct) + " bins cherry-picked from the original sets!")


7 changes: 4 additions & 3 deletions bin/metawrap-scripts/dereplicate_contigs_in_bins.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
#!/usr/bin/env python2.7
from __future__ import print_function
import sys, os

# Usage: ./script.py bins.stats binsFolder outFolder

# load in bin completion and contamination scores
print "Loading in bin completion and contamination scores..."
print("Loading in bin completion and contamination scores...")
bin_scores={}
for line in open(sys.argv[1]):
if "completeness" in line: continue
Expand All @@ -13,7 +14,7 @@
bin_scores[cut[0]]=score

# load in contigs in each bin
print "Loading in contigs in each bin..."
print("Loading in contigs in each bin...")
contig_mapping={}
for bin_file in os.listdir(sys.argv[2]):
bin_name=".".join(bin_file.split("/")[-1].split(".")[:-1])
Expand All @@ -29,7 +30,7 @@


# go over the bin files again and make a new dereplicated version of each bin file
print "Making a new dereplicated version of each bin file"
print("Making a new dereplicated version of each bin file")
os.system("mkdir "+sys.argv[3])
for bin_file in os.listdir(sys.argv[2]):
bin_name=".".join(bin_file.split("/")[-1].split(".")[:-1])
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#!/usr/bin/env python2.7
from __future__ import print_function
import sys, os

complement = {'A': 'T', 'C': 'G', 'G': 'C', 'T': 'A', 'a':'t', 't':'a', 'c':'g', 'g':'c', 'N':'N', 'n':'n', '*':'*'}
Expand All @@ -9,7 +10,7 @@ def rev_comp(seq):
return rev_comp[::-1]

# load bin contigs
print "loading contig to bin mappings..."
print("loading contig to bin mappings...")
contig_bins={}
for bin_file in os.listdir(sys.argv[1]):
if bin_file.endswith(".fa") or bin_file.endswith(".fasta"):
Expand All @@ -21,7 +22,7 @@ def rev_comp(seq):
# store the read names and what bins they belong in in these dictionaries
# strict stores only perfectly aligning reads and permissive stores any aligned reads

print "Parsing sam file and writing reads to appropriate files depending what bin they alligned to..."
print("Parsing sam file and writing reads to appropriate files depending what bin they alligned to...")
files={}
opened_bins={}
for line in sys.stdin:
Expand Down Expand Up @@ -60,9 +61,9 @@ def rev_comp(seq):
files[sys.argv[2]+"/"+bin_name+".nanopore.fastq"].write('@' + cut[0] + "/1" + "\n" + cut[9] + "\n+\n" + cut[10] + "\n")


print "closing files"
print("closing files")
for f in files:
files[f].close()


print "Finished splitting reads!"
print("Finished splitting reads!")
3 changes: 2 additions & 1 deletion bin/metawrap-scripts/filter_out_fastq_reads.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#!/usr/bin/env python2.7
from __future__ import print_function
import sys

# this script takes in a list of fastq read names and picks them out of a .fastq file.
Expand All @@ -20,7 +21,7 @@
# if soi in line: found=True

if found==True:
print line
print(line)
sys.stdout.flush()


Expand Down
9 changes: 5 additions & 4 deletions bin/metawrap-scripts/filter_reads_for_bin_reassembly.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#!/usr/bin/env python2.7
#usage:
# bwa mem -a assembly.fa reads_1.fastq reads_2.fastq | ./filter_reads_for_bin_reassembly.py original_bin_folder reads_1.fastq reads_2.fastq output_dir
from __future__ import print_function
import sys, os
strict_snp_cutoff = int(sys.argv[3])
permissive_snp_cutoff = int(sys.argv[4])
Expand All @@ -13,7 +14,7 @@ def rev_comp(seq):
return rev_comp[::-1]

# load bin contigs
print "loading contig to bin mappings..."
print("loading contig to bin mappings...")
contig_bins={}
for bin_file in os.listdir(sys.argv[1]):
if bin_file.endswith(".fa") or bin_file.endswith(".fasta"):
Expand All @@ -25,7 +26,7 @@ def rev_comp(seq):
# store the read names and what bins they belong in in these dictionaries
# strict stores only perfectly aligning reads and permissive stores any aligned reads

print "Parsing sam file and writing reads to appropriate files depending what bin they alligned to..."
print("Parsing sam file and writing reads to appropriate files depending what bin they alligned to...")
files={}
opened_bins={}
for line in sys.stdin:
Expand Down Expand Up @@ -106,11 +107,11 @@ def rev_comp(seq):
files[sys.argv[2]+"/"+bin_name+".permissive_2.fastq"].write('@' + R_cut[0] + "/2" + "\n" + R_cut[9] + "\n+\n" + R_cut[10] + "\n")


print "closing files"
print("closing files")
for f in files:
files[f].close()


print "Finished splitting reads!"
print("Finished splitting reads!")


3 changes: 2 additions & 1 deletion bin/metawrap-scripts/fix_config_naming.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#!/usr/bin/env python2.7
from __future__ import print_function
import sys

for line in open(sys.argv[1]):
Expand All @@ -7,4 +8,4 @@
if c=="=": c="_"
sys.stdout.write(c)
else:
print line.rstrip()
print(line.rstrip())
5 changes: 3 additions & 2 deletions bin/metawrap-scripts/fix_megahit_contig_naming.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#!/usr/bin/env python2.7
from __future__ import print_function
import sys
import textwrap

Expand All @@ -25,5 +26,5 @@


for k in sorted(dic, key=lambda k: len(dic[k]), reverse=True):
print k
print textwrap.fill(dic[k], 100, break_on_hyphens = False)
print(k)
print(textwrap.fill(dic[k], 100, break_on_hyphens = False))
11 changes: 6 additions & 5 deletions bin/metawrap-scripts/gather-counts.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
C. Titus Brown, 11/2015
"""
from __future__ import print_function
import os, os.path
import sys
import csv
Expand All @@ -19,9 +20,9 @@ def process_quant_file(root, filename, outname):
"""
Convert individual quant.sf files into .counts files (transcripts\tcount).
"""
print >>sys.stderr, 'Loading counts from:', root, filename
print('Loading counts from:', root, filename, file=sys.stderr)
outfp = open(outname, 'w')
print >>outfp, "transcript\tcount"
print("transcript\tcount", file=outfp)

d = {}
full_file = os.path.join(root, filename)
Expand All @@ -30,7 +31,7 @@ def process_quant_file(root, filename, outname):
continue
name, length, eff_length, tpm, count = line.strip().split('\t')

print >>outfp, "%s\t%s" % (name, float(tpm))
print("%s\t%s" % (name, float(tpm)), file=outfp)


def main():
Expand All @@ -43,7 +44,7 @@ def main():
quantlist = []

start_dir = '.'
print >>sys.stderr, 'Starting in:', os.path.abspath(start_dir)
print('Starting in:', os.path.abspath(start_dir), file=sys.stderr)
for root, dirs, files in os.walk('.'):
for filename in files:
if filename.endswith('quant.sf'):
Expand All @@ -54,7 +55,7 @@ def main():

break

print ",\n".join([ "\"%s\"" % i for i in sorted(quantlist)])
print(",\n".join([ "\"%s\"" % i for i in sorted(quantlist)]))

if __name__ == '__main__':
main()
9 changes: 5 additions & 4 deletions bin/metawrap-scripts/interleave_fastq.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
Usage:
interleave-fasta fasta_file1 fasta_file2
"""
from __future__ import print_function

import sys

Expand All @@ -15,20 +16,20 @@ def interleave(f1, f2):
line = f1.readline()
if line.strip() == "":
break
print line.strip()
print(line.strip())

for i in xrange(3):
print f1.readline().strip()
print(f1.readline().strip())

for i in xrange(4):
print f2.readline().strip()
print(f2.readline().strip())

if __name__ == '__main__':
try:
file1 = sys.argv[1]
file2 = sys.argv[2]
except:
print __doc__
print(__doc__)
sys.exit(1)

if file1[-2:] == "gz":
Expand Down
1 change: 1 addition & 0 deletions bin/metawrap-scripts/kraken2_translate.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#!/usr/bin/env python
from __future__ import print_function
import sys
import os

Expand Down
3 changes: 2 additions & 1 deletion bin/metawrap-scripts/kraken_to_krona.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#!/usr/bin/env python2.7
from __future__ import print_function
import sys
# This script takes in a translated kraken file of either contigs (from SPAdes) or reads, and parses it into a format for ktImportText to produce a kronachart.
data={}
Expand All @@ -24,5 +25,5 @@


for tax in data:
print str(data[tax]) + "\t" + tax
print(str(data[tax]) + "\t" + tax)

Loading

0 comments on commit c4a23f0

Please sign in to comment.