#!/usr/bin/env python2.7

from __future__ import division
import os,sys,argparse,re,commands,subprocess,datetime,shutil,glob

def get_analysis_option():
        my_cmdparser = argparse.ArgumentParser(description='run anno and novel prediction for sRNA seq')
        my_cmdparser.add_argument('-o','--outdir',action='store',dest='outdir',default='Align',type=str,help='analysis and result directory path')
        my_cmdparser.add_argument('-S','--sample',action='store',dest='sample_name',metavar='sample name',help='sample name for preifx')
	my_cmdparser.add_argument('-t',action='store',dest='cpu_number',default='4',type=str,help='cpu number')
	my_cmdparser.add_argument('-s','--shell',action='store',dest='shell',type=str,help='shell directory path')
        my_cmdparser.add_argument('--version',action='version',version='%(prog)s 1.0 ')
        my_cmdparser.add_argument('-C','--config',action='store',dest='config_file',default='./config.txt',help='config file')
        my_cmdparser.add_argument('--reads',action='store',dest='reads',help='r1,r2 are splited ","')
        my_cmdparser.add_argument('-M','--multi',action='store_true',dest='multi',default=False,help='run multi process for task,default is False. Be ware of that,multiprocess may have some problem')
	
        options=my_cmdparser.parse_args()
        configure={i:getattr(options,i) for i in dir(options) if (i[0] != '_' and i not in ('ensure_value', 'read_file','read_module'))}
        if configure['config_file']:
                COMMON_index = ''
                configure['config_file'] = os.path.abspath(configure['config_file'])
                with open(configure['config_file'],'r') as f:
                        for i in f:
                                if re.search(r'^\s*$',i):continue
                                i=i.strip()
                                if i.startswith('#'):
                                        if i.startswith('#!') and re.search('PIPE_RESOURCE_PATH',i):
                                                COMMON_index = i.split(':')[1].strip()
                                        continue
                                line=i.split('=')
                                configure.setdefault('path',{})[line[0].strip()]=line[1].strip().replace('PIPE_RESOURCE_PATH',COMMON_index)
        else:
                print "CONFIG FILE LOST,PLEASE CHECK!"
                sys.exit()
        current_path=os.getcwd()
	for i in ('shell','outdir'):
	        if re.search(r'^[^/]', configure[i]):
			configure[i] = os.path.abspath(configure[i])
                if not os.path.isdir(configure[i]):
                        os.makedirs(configure[i])
        return configure

def get_align_anno_predict_shell(configure):
        reads_dict = {}
	sample_name = configure['sample_name']
        out_shell_dir = configure['shell']
	out_shell_name = os.path.join(out_shell_dir,'2.align.anno.sh')
        out_shell = open(out_shell_name,'w')
        sample_reads=configure['reads']
	outdir = configure['outdir']
	bowtie_para_4_ncgb = ' -n 2 -l 7 -e 70 '
	bowtie_para_4_Rfam = ' -n 3 -l 10 -e 70 '
	bowtie_para_4_mature=' -v 1 -l 15 -e 70 '
	bowtie_para_4_piRNA=' -n 0 -l 18 -e 100 '
	bowtie_para_4_genome = '-v 1 '
	bowtie_path = os.path.join(configure['path']['bowtie_dir'],'bowtie')
	out_shell.write('#! /usr/bin/env bash\necho "start at `date` " > {0}.log\n'.format(out_shell_name))
##add force large(more than 4GB genome align for bowtie)
	bowtie_align_large_index_flag = ''
	for i in range(1,2):
		i=str(i)
####add bowtie-build-l acccess for geneme size larger than 4GB####
		tmp_ref_file = ''.join([configure['path']['ref'],'.',i,'.ebwt','*'])
		tmp_ref_list = glob.glob(tmp_ref_file)
		assert(os.path.isfile(tmp_ref_list[0])),'chech the path and the completily of ref genome'
		if tmp_ref_list[0].endswith('ebwtl'):
			bowtie_para_4_genome += ' --large-index '
	out_shell.write('{0} {1} {2} {4} -f --sam-nohead --sam-nosq -p {3} '.format(bowtie_path,configure['path']['ref'],sample_reads,configure['cpu_number'],bowtie_para_4_genome))
	Align_dir = os.path.join(outdir,'Single_Sample',sample_name,'1.Align2DB')
	Anno_dir = os.path.join(outdir,'Single_Sample',sample_name,'2.Anno')
	Align_prefix = os.path.join(Align_dir,sample_name)
	out_shell.write(' 2>{0}.align.genome.log -S {0}.genome.sam && '.format(Align_prefix))
	out_shell.write('python {0} -o {1} -t genome --sam {2}.genome.sam --reads {3} -C {4} --shell_dir {5} \n'.format(configure['path']['genome_annotation'],Anno_dir,Align_prefix,sample_reads,configure['config_file'],out_shell_dir))
	out_shell.write('{0} {1} {2} -f -a -p {3} {4} --sam-nohead --sam-nosq -S {5}.ncgb.sam 2>{5}.align.ncgb.log && '.format(bowtie_path,configure['path']['ncgb'],sample_reads,configure['cpu_number'],bowtie_para_4_ncgb,Align_prefix))
	out_shell.write('python {0} -o {1} -t ncgb --sam {2}.ncgb.sam --reads {3} -C {4} --shell_dir {5} \n'.format(configure['path']['genome_annotation'],Anno_dir,Align_prefix,sample_reads,configure['config_file'],out_shell_dir))
	out_shell.write('{0} {1} {2} -f -a -p {3} {4} --sam-nohead --sam-nosq -S {5}.Rfam.sam 2>{5}.align.Rfam.log && '.format(bowtie_path,configure['path']['Rfam'],sample_reads,configure['cpu_number'],bowtie_para_4_Rfam,Align_prefix))
	out_shell.write('python {0} -o {1} -t Rfam --sam {2}.Rfam.sam --reads {3} -C {4} --shell_dir {5} \n'.format(configure['path']['genome_annotation'],Anno_dir,Align_prefix,sample_reads,configure['config_file'],out_shell_dir))
	out_shell.write('{0} {1} {2} -f -a -p {3} {4} --sam-nohead --sam-nosq -S {5}.hairpin.sam 2>{5}.align.hairpin.log && '.format(bowtie_path,configure['path']['hairpin'],sample_reads,configure['cpu_number'],bowtie_para_4_mature,Align_prefix))
	out_shell.write('python {0} -o {1} -t mature --sam {2}.hairpin.sam --reads {3} -C {4} --shell_dir {5} \n'.format(configure['path']['genome_annotation'],Anno_dir,Align_prefix,sample_reads,configure['config_file'],out_shell_dir))
	out_shell.write('{0} {1} {2} -f -a -p {3} {4} --sam-nohead --sam-nosq -S {5}.piRNA.sam 2>{5}.align.piRNA.log && '.format(bowtie_path,configure['path']['piRNA'],sample_reads,configure['cpu_number'],bowtie_para_4_piRNA,Align_prefix))
	out_shell.write('python {0} -o {1} -t piRNA --sam {2}.piRNA.sam --reads {3} -C {4} --shell_dir {5} \n'.format(configure['path']['genome_annotation'],Anno_dir,Align_prefix,sample_reads,configure['config_file'],out_shell_dir))

	combine_shell_name = os.path.join(out_shell_dir,'3.combine.anno.sh')
	combine_shell = open(combine_shell_name,'w')
	combine_shell.write('#! /usr/bin/env bash\npython {0} -o {1} -t tag_anno --reads {3} -C {4} --shell_dir {5} \n'.format(configure['path']['genome_annotation'],Anno_dir,Align_prefix,sample_reads,configure['config_file'],out_shell_dir))
	
	predict_dir=os.path.join(outdir,'Single_Sample',sample_name,'3.Predict')
	predict_shell_name = os.path.join(out_shell_dir,'4.predict.sh')
	predict_shell = open(predict_shell_name,'w')
	predict_shell.write('#! /usr/bin/env bash\nmkdir -p {0} && ln -fs {1} {0} && cd {0} \n'.format(predict_dir,os.path.join(Anno_dir,'unpredict_reads.fa')))
	predict_shell.write('export PERL5LIB="$PERL5LIB:{0}/required_packages/PDF-API2-0.73/lib/perl5/site_perl/5.8.8/" && export PATH="$PATH:{0}/current/:{1}/:{0}/required_packages/randfold-2.0:{2}/Progs/"\n'.format(configure['path']['mirdeep_dir'],configure['path']['bowtie_dir'],configure['path']['ViennaRNA_dir']))
	predict_shell.write('perl {0}/current/mapper.pl unpredict_reads.fa -c -i -j -l 18 -m -r 5 -v -n -o 7 -s reads_collapsed.fa -p {1} -t reads_collapsed_vs_genome.arf && perl {0}/current/miRDeep2.pl reads_collapsed.fa {1} reads_collapsed_vs_genome.arf {2} none {3} -g -1 -b 0 -v -t {4} -d -r result -u 1>{5}.log 2>{5}.detail.log && '.format(configure['path']['mirdeep_dir'],configure['path']['ref'],configure['path']['mature'],configure['path']['hairpin'],configure['abbr'],predict_shell_name))
	predict_shell.write('python {0} --indir {1} -o {1}/novel.fa --stat {1}/novel.stat --loci {1}/loci.out \n'.format(configure['path']['novel_fa_miRdeep'],predict_dir))
	
	if not __name__ == '__main__':
		return [out_shell_name, combine_shell_name, predict_shell_name]


if __name__ == '__main__':
	if len(sys.argv) ==1 :
		sys.argv.append('-h')
		get_analysis_option()
	else:
		configure=get_analysis_option()
		get_align_trans_statics_shell(configure)


#!/usr/bin/env python2.7

from __future__ import division
import os,sys,argparse,re,commands,subprocess,datetime,shutil,glob

def get_analysis_option():
        my_cmdparser = argparse.ArgumentParser(description='run anno and novel prediction for sRNA seq')
        my_cmdparser.add_argument('-o','--outdir',action='store',dest='outdir',default='Align',type=str,help='analysis and result directory path')
        my_cmdparser.add_argument('-S','--sample',action='store',dest='sample_name',metavar='sample name',help='sample name for preifx')
	my_cmdparser.add_argument('-t',action='store',dest='cpu_number',default='4',type=str,help='cpu number')
	my_cmdparser.add_argument('-s','--shell',action='store',dest='shell',type=str,help='shell directory path')
        my_cmdparser.add_argument('--version',action='version',version='%(prog)s 1.0 ')
        my_cmdparser.add_argument('-C','--config',action='store',dest='config_file',default='./config.txt',help='config file')
        my_cmdparser.add_argument('--reads',action='store',dest='reads',help='r1,r2 are splited ","')
        my_cmdparser.add_argument('-M','--multi',action='store_true',dest='multi',default=False,help='run multi process for task,default is False. Be ware of that,multiprocess may have some problem')
	
        options=my_cmdparser.parse_args()
        configure={i:getattr(options,i) for i in dir(options) if (i[0] != '_' and i not in ('ensure_value', 'read_file','read_module'))}
        if configure['config_file']:
                COMMON_index = ''
                configure['config_file'] = os.path.abspath(configure['config_file'])
                with open(configure['config_file'],'r') as f:
                        for i in f:
                                if re.search(r'^\s*$',i):continue
                                i=i.strip()
                                if i.startswith('#'):
                                        if i.startswith('#!') and re.search('PIPE_RESOURCE_PATH',i):
                                                COMMON_index = i.split(':')[1].strip()
                                        continue
                                line=i.split('=')
                                configure.setdefault('path',{})[line[0].strip()]=line[1].strip().replace('PIPE_RESOURCE_PATH',COMMON_index)
        else:
                print "CONFIG FILE LOST,PLEASE CHECK!"
                sys.exit()
        current_path=os.getcwd()
	for i in ('shell','outdir'):
	        if re.search(r'^[^/]', configure[i]):
			configure[i] = os.path.abspath(configure[i])
                if not os.path.isdir(configure[i]):
                        os.makedirs(configure[i])
        return configure

def get_align_anno_predict_shell(configure):
        reads_dict = {}
	sample_name = configure['sample_name']
        out_shell_dir = configure['shell']
	out_shell_name = os.path.join(out_shell_dir,'2.align.anno.sh')
        out_shell = open(out_shell_name,'w')
        sample_reads=configure['reads']
	outdir = configure['outdir']
	bowtie_para_4_ncgb = ' -n 2 -l 7 -e 70 '
	bowtie_para_4_Rfam = ' -n 3 -l 10 -e 70 '
	bowtie_para_4_mature=' -v 1 -l 15 -e 70 '
	bowtie_para_4_piRNA=' -n 0 -l 18 -e 100 '
	bowtie_para_4_genome = '-v 1 '
	bowtie_path = os.path.join(configure['path']['bowtie_dir'],'bowtie')
	out_shell.write('#! /usr/bin/env bash\necho "start at `date` " > {0}.log\n'.format(out_shell_name))
##add force large(more than 4GB genome align for bowtie)
	bowtie_align_large_index_flag = ''
	for i in range(1,2):
		i=str(i)
####add bowtie-build-l acccess for geneme size larger than 4GB####
		tmp_ref_file = ''.join([configure['path']['ref'],'.',i,'.ebwt','*'])
		tmp_ref_list = glob.glob(tmp_ref_file)
		assert(os.path.isfile(tmp_ref_list[0])),'chech the path and the completily of ref genome'
		if tmp_ref_list[0].endswith('ebwtl'):
			bowtie_para_4_genome += ' --large-index '
	out_shell.write('{0} {1} {2} {4} -f --sam-nohead --sam-nosq -p {3} '.format(bowtie_path,configure['path']['ref'],sample_reads,configure['cpu_number'],bowtie_para_4_genome))
	Align_dir = os.path.join(outdir,'Single_Sample',sample_name,'1.Align2DB')
	Anno_dir = os.path.join(outdir,'Single_Sample',sample_name,'2.Anno')
	Align_prefix = os.path.join(Align_dir,sample_name)
	out_shell.write(' 2>{0}.align.genome.log -S {0}.genome.sam && '.format(Align_prefix))
	out_shell.write('python {0} -o {1} -t genome --sam {2}.genome.sam --reads {3} -C {4} --shell_dir {5} \n'.format(configure['path']['genome_annotation'],Anno_dir,Align_prefix,sample_reads,configure['config_file'],out_shell_dir))
	out_shell.write('{0} {1} {2} -f -a -p {3} {4} --sam-nohead --sam-nosq -S {5}.ncgb.sam 2>{5}.align.ncgb.log && '.format(bowtie_path,configure['path']['ncgb'],sample_reads,configure['cpu_number'],bowtie_para_4_ncgb,Align_prefix))
	out_shell.write('python {0} -o {1} -t ncgb --sam {2}.ncgb.sam --reads {3} -C {4} --shell_dir {5} \n'.format(configure['path']['genome_annotation'],Anno_dir,Align_prefix,sample_reads,configure['config_file'],out_shell_dir))
	out_shell.write('{0} {1} {2} -f -a -p {3} {4} --sam-nohead --sam-nosq -S {5}.Rfam.sam 2>{5}.align.Rfam.log && '.format(bowtie_path,configure['path']['Rfam'],sample_reads,configure['cpu_number'],bowtie_para_4_Rfam,Align_prefix))
	out_shell.write('python {0} -o {1} -t Rfam --sam {2}.Rfam.sam --reads {3} -C {4} --shell_dir {5} \n'.format(configure['path']['genome_annotation'],Anno_dir,Align_prefix,sample_reads,configure['config_file'],out_shell_dir))
	out_shell.write('{0} {1} {2} -f -a -p {3} {4} --sam-nohead --sam-nosq -S {5}.hairpin.sam 2>{5}.align.hairpin.log && '.format(bowtie_path,configure['path']['hairpin'],sample_reads,configure['cpu_number'],bowtie_para_4_mature,Align_prefix))
	out_shell.write('python {0} -o {1} -t mature --sam {2}.hairpin.sam --reads {3} -C {4} --shell_dir {5} \n'.format(configure['path']['genome_annotation'],Anno_dir,Align_prefix,sample_reads,configure['config_file'],out_shell_dir))
	out_shell.write('{0} {1} {2} -f -a -p {3} {4} --sam-nohead --sam-nosq -S {5}.piRNA.sam 2>{5}.align.piRNA.log && '.format(bowtie_path,configure['path']['piRNA'],sample_reads,configure['cpu_number'],bowtie_para_4_piRNA,Align_prefix))
	out_shell.write('python {0} -o {1} -t piRNA --sam {2}.piRNA.sam --reads {3} -C {4} --shell_dir {5} \n'.format(configure['path']['genome_annotation'],Anno_dir,Align_prefix,sample_reads,configure['config_file'],out_shell_dir))

	combine_shell_name = os.path.join(out_shell_dir,'3.combine.anno.sh')
	combine_shell = open(combine_shell_name,'w')
	combine_shell.write('#! /usr/bin/env bash\npython {0} -o {1} -t tag_anno --reads {3} -C {4} --shell_dir {5} \n'.format(configure['path']['genome_annotation'],Anno_dir,Align_prefix,sample_reads,configure['config_file'],out_shell_dir))
	
	predict_dir=os.path.join(outdir,'Single_Sample',sample_name,'3.Predict')
	predict_shell_name = os.path.join(out_shell_dir,'4.predict.sh')
	predict_shell = open(predict_shell_name,'w')
	predict_shell.write('#! /usr/bin/env bash\nmkdir -p {0} && ln -fs {1} {0} && cd {0} \n'.format(predict_dir,os.path.join(Anno_dir,'unpredict_reads.fa')))
	predict_shell.write('export PERL5LIB="$PERL5LIB:{0}/required_packages/PDF-API2-0.73/lib/perl5/site_perl/5.8.8/" && export PATH="$PATH:{0}/current/:{1}/:{0}/required_packages/randfold-2.0:{2}/Progs/"\n'.format(configure['path']['mirdeep_dir'],configure['path']['bowtie_dir'],configure['path']['ViennaRNA_dir']))
	predict_shell.write('perl {0}/current/mapper.pl unpredict_reads.fa -c -i -j -l 18 -m -r 5 -v -n -o 7 -s reads_collapsed.fa -p {1} -t reads_collapsed_vs_genome.arf && perl {0}/current/miRDeep2.pl reads_collapsed.fa {1} reads_collapsed_vs_genome.arf {2} none {3} -g -1 -b 0 -v -t {4} -d -r result -u 1>{5}.log 2>{5}.detail.log && '.format(configure['path']['mirdeep_dir'],configure['path']['ref'],configure['path']['mature'],configure['path']['hairpin'],configure['abbr'],predict_shell_name))
	predict_shell.write('python {0} --indir {1} -o {1}/novel.fa --stat {1}/novel.stat --loci {1}/loci.out \n'.format(configure['path']['novel_fa_miRdeep'],predict_dir))
	
	if not __name__ == '__main__':
		return [out_shell_name, combine_shell_name, predict_shell_name]


if __name__ == '__main__':
	if len(sys.argv) ==1 :
		sys.argv.append('-h')
		get_analysis_option()
	else:
		configure=get_analysis_option()
		get_align_trans_statics_shell(configure)


#!/usr/bin/env python2.7 ##add force large(more than 4GB genome align for bowtie) ####add bowtie-build-l acccess for geneme size larger than 4GB####