<commit_before>import argparse
import glsapiutil

HOSTNAME = ''
VERSION = ''
BASE_URI = ''

DEBUG = True
api = None

def getObjectDOM( uri ):

    global CACHE
    #global api
    if uri not in CACHE.keys():
        #print uri
        thisXML = api.getResourceByURI( uri )
        # print thisXML
        thisDOM = parseString( thisXML )
        CACHE[ uri ] = thisDOM

    return CACHE[ uri ]


def setupGlobalsFromURI( uri ):

	global HOSTNAME
	global VERSION
	global BASE_URI

	tokens = uri.split( "/" )
	HOSTNAME = "/".join(tokens[0:3])
	VERSION = tokens[4]
	BASE_URI = "/".join(tokens[0:5]) + "/"

	if DEBUG is True:
		print HOSTNAME
		print BASE_URI

def revcom(step, aid, out):


def main():
    global args

    parser = argparse.ArgumentParser(prog='useq_modify_samplesheet', description='''
    This script is used to modify a samplesheet to work with either NextSeq or MiSeq/HiSeq. Currently
    all it does is reverse complement the barcodes when needed''')


    parser.add_argument('-u','--user', help='User name', required=True)
    parser.add_argument('-p','--password', help='Password', required=True)

    parser.add_argument('-s','--step', help='Step URI', required=True)
    parser.add_argument('-a','--aid', help='Artifact ID', required=True)
    parser.add_argument('-o', '--output_file',  nargs='?', type=argparse.FileType('w'), default=sys.stdout, help='Output file path (default=stdout)')

    args = parser.parse_args()

    api = glsapiutil.glsapiutil()
    api.setHostname( HOSTNAME )
    api.setVersion( VERSION )
    api.setup( args.user, args.password )

    setupGlobalsFromURI( args.step )

    
if __name__ == "__main__":

    main()



#
# def update_samplesheet(lims, process_id, artifact_id, output_file):
#     """Update illumina samplesheet."""<commit_msg>Finished script to reverse complement barcodes after samplesheet generation<commit_after>import argparse
# from optparse import OptionParser
import glsapiutil
import sys
from xml.dom.minidom import parseString, parse

HOSTNAME = ''
VERSION = ''
BASE_URI = ''

DEBUG = True
api = None
args = None
CACHE = {}

def getObjectDOM( uri ):

	global CACHE
	#global api
	if uri not in CACHE.keys():

		thisXML = api.getResourceByURI( uri )

		thisDOM = parseString( thisXML )
		CACHE[ uri ] = thisDOM

	return CACHE[ uri ]


def setupGlobalsFromURI( uri ):

	global HOSTNAME
	global VERSION
	global BASE_URI

	tokens = uri.split( "/" )
	HOSTNAME = "/".join(tokens[0:3])
	VERSION = tokens[4]
	BASE_URI = "/".join(tokens[0:5]) + "/"

	if DEBUG is True:
		print HOSTNAME
		print BASE_URI

def revcom(step, aid, output_file):

    revcompl = lambda x: ''.join([{'A':'T','C':'G','G':'C','T':'A'}[B] for B in x][::-1])
    samplesheet_artifact = getObjectDOM(BASE_URI + 'artifacts/' + aid)
    samplesheet_uri = samplesheet_artifact.getElementsByTagName("file:file")[0].getAttribute("uri")
    header = ''
    for line in api.getResourceByURI(samplesheet_uri + '/download').rstrip().split('\n'):
        if line.startswith('Sample_ID'):
            header = line.rstrip().split(',')
            output_file.write('{line}\n'.format(line=line))
            # print '{line}\n'.format(line=line)
        elif header:
            data = line.rstrip().split(',')

            i7_index = None
            i5_index = None
            if 'index2' in header:
                i5_index =header.index('index2')
                data[i5_index] = revcompl(data[i5_index])
            else:
                i7_index = header.index('index')
                data[i7_index] = revcompl(data[i7_index])

            output_file.write('{line}\n'.format(line=','.join(data)))

        else:
            output_file.write('{line}\n'.format(line=line))


def main():

    global args
    global api

    parser = argparse.ArgumentParser(prog='useq_modify_samplesheet', description='''
    This script is used to modify a samplesheet to work with either NextSeq or MiSeq/HiSeq. Currently
    all it does is reverse complement the barcodes when needed''')


    parser.add_argument('-u','--user', help='User name', required=True)
    parser.add_argument('-p','--password', help='Password', required=True)

    parser.add_argument('-s','--step', help='Step URI', required=True)
    parser.add_argument('-a','--aid', help='Artifact ID', required=True)
    parser.add_argument('-o','--output_file',  nargs='?', type=argparse.FileType('w'), default=sys.stdout, help='Output file path (default=stdout)')

    args = parser.parse_args()


    setupGlobalsFromURI( args.step )
    api = glsapiutil.glsapiutil()
    api.setHostname( HOSTNAME )
    api.setVersion( VERSION )
    api.setup( args.user, args.password )


    revcom(args.step, args.aid, args.output_file)

if __name__ == "__main__":
    main()



#
# def update_samplesheet(lims, process_id, artifact_id, output_file):
#     """Update illumina samplesheet."""
{'fraction_non_alphanumeric': 0.14385532264693793, 'fraction_numerical': 0.004521167283189478, 'mean_word_length': 3.3767985611510793, 'pattern_counts': {'":': 2, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 1}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6598198', 'n_tokens_mistral': 1632, 'n_tokens_neox': 1567, 'n_words': 372}