##################################
#                                #
# Last modified 2020/09/11       # 
#                                #
# Georgi Marinov                 #
#                                # 
##################################

import sys
import os
import string

def getReverseComplement(preliminarysequence):
    
    DNA = {'A':'T','T':'A','G':'C','C':'G','N':'N','a':'t','t':'a','g':'c','c':'g','n':'n'}
    sequence=''
    for j in range(len(preliminarysequence)):
        sequence=sequence+DNA[preliminarysequence[len(preliminarysequence)-j-1]]
    return sequence

def run():

    if len(sys.argv) < 1:
        print 'usage: python %s fastq' % sys.argv[0]
        print '\tthe fastq file can be in .gz or .bz2 format, but it has to end with these suffixes' 
        sys.exit(1)

    fastq = sys.argv[1]

    i=0
    if fastq.endswith('.bz2'):
        cmd = 'bzip2 -cd ' + fastq
    elif fastq.endswith('.gz'):
        cmd = 'zcat ' + fastq
    else:
        cmd = 'cat ' + fastq
    p = os.popen(cmd, "r")
    line = 'line'
    i=1
    while line != '':
        line = p.readline()
        if line == '':
            break
        if i == 1 and line[0]!='@':
            print 'fastq file broken, exiting'
            sys.argv(1)
        if i == 1 and line[0]=='@':
            print line.strip()
            i=2
            continue
        if i == 2:
            print getReverseComplement(line.strip())
            i=3
            continue
        if i == 3 and line[0]=='+':
            print line.strip()
            i = 4
            continue
        if i == 4:
            print line.strip()[::-1]
            i=1
            continue
   
run()
