##################################
#                                #
# Last modified 2023/08/14       # 
#                                #
# Georgi Marinov                 #
#                                # 
##################################

import sys
import os
import random

try:
	import psyco
	psyco.full()
except:
	pass

def run():

    if len(sys.argv) < 4:
        print 'usage: python %s R1 R2 I1 I2 prefix' % sys.argv[0]
        print '\tThe script can read compressed files as long as they have the correct suffix - .bz2 or .gz'
        print '\tThe script will print to stdout'
        sys.exit(1)

    R1 = sys.argv[1]
    R2 = sys.argv[2]
    I1 = sys.argv[3]
    I2 = sys.argv[4]

    if R1.endswith('.bz2'):
        cmd1 = 'bzip2 -cd ' + R1
        p1 = os.popen(cmd1, "r")
    elif R1.endswith('.gz'):
        cmd1 = 'gunzip -c ' + R1
        p1 = os.popen(cmd1, "r")
    else:
        cmd1 = 'cat ' + R1
        p1 = os.popen(cmd1, "r")

    if R2.endswith('.bz2'):
        cmd2 = 'bzip2 -cd ' + R2
        p2 = os.popen(cmd2, "r")
    elif R2.endswith('.gz'):
        cmd2 = 'gunzip -c ' + R2
        p2 = os.popen(cmd2, "r")
    else:
        cmd2 = 'cat ' + R2
        p2 = os.popen(cmd2, "r")

    if I1.endswith('.bz2'):
        cmd3 = 'bzip2 -cd ' + I1
        p3 = os.popen(cmd3, "r")
    elif I1.endswith('.gz'):
        cmd3 = 'gunzip -c ' + I1
        p3 = os.popen(cmd3, "r")
    else:
        cmd3 = 'cat ' + I1
        p3 = os.popen(cmd3, "r")

    if I2.endswith('.bz2'):
        cmd4 = 'bzip2 -cd ' + I2
        p4 = os.popen(cmd4, "r")
    elif I2.endswith('.gz'):
        cmd4 = 'gunzip -c ' + I2
        p4 = os.popen(cmd4, "r")
    else:
        cmd4 = 'cat ' + I2
        p4 = os.popen(cmd4, "r")

    lineR1_1 = 'line'
    while lineR1_1 != '':
        lineR1_1 = p1.readline()
        if lineR1_1 == '':
            break
        lineR1_2 = p1.readline()
        lineR1_3 = p1.readline()
        lineR1_4 = p1.readline()

        lineR2_1 = p2.readline()
        lineR2_2 = p2.readline()
        lineR2_3 = p2.readline()
        lineR2_4 = p2.readline()

        lineI1_1 = p3.readline()
        lineI1_2 = p3.readline()
        lineI1_3 = p3.readline()
        lineI1_4 = p3.readline()

        lineI2_1 = p4.readline()
        lineI2_2 = p4.readline()
        lineI2_3 = p4.readline()
        lineI2_4 = p4.readline()

        R1ID = lineR1_1.strip()
        R1seq = lineR1_2.strip()
        R1q = lineR1_4.strip()

        R2seq = lineR2_2.strip()
        R2q = lineR2_4.strip()

        I1seq = lineI1_2.strip()

        I2seq = lineI2_2.strip()

        newID = R1ID + ':' + I1seq + '+' + I2seq

        outline = newID + '\t' + R1seq  + '\t' + R1q + '\t' + R2seq + '\t' + R2q
        print outline

run()