I have (tab-separated) input files:
id1 id2 ....
id1 id3 ....
id3 id4 ....
id2 id1 ....
id3 id4 ....
.....
I need to
- rearrange
col1
andcol2
by numerical sort. For now I do this in a python script. - sort by
col1
thencol2
. For now I am doing this by taking the output of the python script and using GNUsort
.
My question is the following: is there a way to merge step 1 and step 2 (using GNU sort or any other GNU/Linux command-line tools)?
Is there an efficient alternative GNU/Linux command for step 1?
result:
id1 id2 ....
id1 id2 ....
id1 id3 ....
id3 id4 ....
id3 id4 ....
.....
My code actually works, I am looking to improve its speed.
Here's the Python program:
import argparse
import subprocess
import os
parser = argparse.ArgumentParser(description='')
parser.add_argument('-blast', help='input', required=True)
parser.add_argument('-out', help='output', required=True)
args = parser.parse_args()
def get_tmp():
# return a name for temporary file.
dir = os.listdir(".")
cpt = 0
name = "tmp_{}".format(cpt)
while name in dir:
cpt += 1
name = "tmp_{}".format(cpt)
return name
# get a temporary name
tmp_name = get_tmp()
# open inputfile in reading and output in writing
with open(args.blast) as input_blast, open(tmp_name, 'w') as tmp_file:
for line in input_blast:
spt = line.strip().split()
tmp_file.write('\t'.join(sorted(spt[0:2]) + spt[2:]) + '\n')
# sort by field one and two
child = subprocess.Popen("sort -k1 -k2 {} > temps_sort && mv temps_sort {}".format(os.path.abspath(tmp_name), args.out),shell=True)
child.wait()