Mercurial > repos > devteam > get_flanks
diff get_flanks.py @ 3:2fdec558c935 draft
planemo upload commit 33927a87ba2eee9bf0ecdd376a66241b17b3d734
author | devteam |
---|---|
date | Tue, 13 Oct 2015 12:50:28 -0400 |
parents | 0c66884f0cac |
children | dd9315a56c09 |
line wrap: on
line diff
--- a/get_flanks.py Mon Apr 14 09:17:59 2014 -0400 +++ b/get_flanks.py Tue Oct 13 12:50:28 2015 -0400 @@ -9,15 +9,17 @@ -o, --off=N: Offset """ -import sys, re, os +import sys from bx.cookbook import doc_optparse -from galaxy.tools.util.galaxyops import * +from galaxy.tools.util.galaxyops import parse_cols_arg + def stop_err( msg ): sys.stderr.write( msg ) sys.exit() -def main(): + +def main(): try: if int( sys.argv[3] ) < 0: raise Exception @@ -30,26 +32,26 @@ chr_col_1, start_col_1, end_col_1, strand_col_1 = parse_cols_arg( options.cols ) inp_file, out_file, size, direction, region = args if strand_col_1 <= 0: - strand = "+" #if strand is not defined, default it to + + strand = "+" # if strand is not defined, default it to + except: stop_err( "Metadata issue, correct the metadata attributes by clicking on the pencil icon in the history item." ) try: offset = int(options.off) size = int(size) - except: + except: stop_err( "Invalid offset or length entered. Try again by entering valid integer values." ) - fo = open(out_file,'w') - + fo = open(out_file, 'w') + skipped_lines = 0 first_invalid_line = 0 invalid_line = None elems = [] - j=0 + j = 0 for i, line in enumerate( file( inp_file ) ): line = line.strip() if line and (not line.startswith( '#' )) and line != '': - j+=1 + j += 1 try: elems = line.split('\t') #if the start and/or end columns are not numbers, skip that line. @@ -76,15 +78,15 @@ elems[end_col_1] = str(int(elems[start_col_1]) + size) assert int(elems[start_col_1]) > 0 and int(elems[end_col_1]) > 0 fo.write( "%s\n" % '\t'.join( elems ) ) - + elif direction == 'Downstream': if strand == '-': if region == 'start': - elems[end_col_1] = str(int(elems[end_col_1]) - offset) - elems[start_col_1] = str( int(elems[end_col_1]) - size ) + elems[end_col_1] = str(int(elems[end_col_1]) - offset) + elems[start_col_1] = str( int(elems[end_col_1]) - size ) else: - elems[end_col_1] = str(int(elems[start_col_1]) - offset) - elems[start_col_1] = str( int(elems[end_col_1]) - size ) + elems[end_col_1] = str(int(elems[start_col_1]) - offset) + elems[start_col_1] = str( int(elems[end_col_1]) - size ) elif strand == '+': if region == 'start': elems[start_col_1] = str(int(elems[start_col_1]) + offset) @@ -94,31 +96,31 @@ elems[end_col_1] = str(int(elems[start_col_1]) + size) assert int(elems[start_col_1]) > 0 and int(elems[end_col_1]) > 0 fo.write( "%s\n" % '\t'.join( elems ) ) - + elif direction == 'Both': if strand == '-': if region == 'start': start = str(int(elems[end_col_1]) - offset) end1 = str(int(start) + size) end2 = str(int(start) - size) - elems[start_col_1]=start - elems[end_col_1]=end1 + elems[start_col_1] = start + elems[end_col_1] = end1 assert int(elems[start_col_1]) > 0 and int(elems[end_col_1]) > 0 fo.write( "%s\n" % '\t'.join( elems ) ) - elems[start_col_1]=end2 - elems[end_col_1]=start + elems[start_col_1] = end2 + elems[end_col_1] = start assert int(elems[start_col_1]) > 0 and int(elems[end_col_1]) > 0 fo.write( "%s\n" % '\t'.join( elems ) ) elif region == 'end': start = str(int(elems[start_col_1]) - offset) end1 = str(int(start) + size) end2 = str(int(start) - size) - elems[start_col_1]=start - elems[end_col_1]=end1 + elems[start_col_1] = start + elems[end_col_1] = end1 assert int(elems[start_col_1]) > 0 and int(elems[end_col_1]) > 0 fo.write( "%s\n" % '\t'.join( elems ) ) - elems[start_col_1]=end2 - elems[end_col_1]=start + elems[start_col_1] = end2 + elems[end_col_1] = start assert int(elems[start_col_1]) > 0 and int(elems[end_col_1]) > 0 fo.write( "%s\n" % '\t'.join( elems ) ) else: @@ -126,12 +128,12 @@ end1 = str(int(start1) + size) start2 = str(int(elems[start_col_1]) - offset) end2 = str(int(start2) - size) - elems[start_col_1]=start1 - elems[end_col_1]=end1 + elems[start_col_1] = start1 + elems[end_col_1] = end1 assert int(elems[start_col_1]) > 0 and int(elems[end_col_1]) > 0 fo.write( "%s\n" % '\t'.join( elems ) ) - elems[start_col_1]=end2 - elems[end_col_1]=start2 + elems[start_col_1] = end2 + elems[end_col_1] = start2 assert int(elems[start_col_1]) > 0 and int(elems[end_col_1]) > 0 fo.write( "%s\n" % '\t'.join( elems ) ) elif strand == '+': @@ -139,24 +141,24 @@ start = str(int(elems[start_col_1]) + offset) end1 = str(int(start) - size) end2 = str(int(start) + size) - elems[start_col_1]=end1 - elems[end_col_1]=start + elems[start_col_1] = end1 + elems[end_col_1] = start assert int(elems[start_col_1]) > 0 and int(elems[end_col_1]) > 0 fo.write( "%s\n" % '\t'.join( elems ) ) - elems[start_col_1]=start - elems[end_col_1]=end2 + elems[start_col_1] = start + elems[end_col_1] = end2 assert int(elems[start_col_1]) > 0 and int(elems[end_col_1]) > 0 fo.write( "%s\n" % '\t'.join( elems ) ) elif region == 'end': start = str(int(elems[end_col_1]) + offset) end1 = str(int(start) - size) end2 = str(int(start) + size) - elems[start_col_1]=end1 - elems[end_col_1]=start + elems[start_col_1] = end1 + elems[end_col_1] = start assert int(elems[start_col_1]) > 0 and int(elems[end_col_1]) > 0 fo.write( "%s\n" % '\t'.join( elems ) ) - elems[start_col_1]=start - elems[end_col_1]=end2 + elems[start_col_1] = start + elems[end_col_1] = end2 assert int(elems[start_col_1]) > 0 and int(elems[end_col_1]) > 0 fo.write( "%s\n" % '\t'.join( elems ) ) else: @@ -164,12 +166,12 @@ end1 = str(int(start1) - size) start2 = str(int(elems[end_col_1]) + offset) end2 = str(int(start2) + size) - elems[start_col_1]=end1 - elems[end_col_1]=start1 + elems[start_col_1] = end1 + elems[end_col_1] = start1 assert int(elems[start_col_1]) > 0 and int(elems[end_col_1]) > 0 fo.write( "%s\n" % '\t'.join( elems ) ) - elems[start_col_1]=start2 - elems[end_col_1]=end2 + elems[start_col_1] = start2 + elems[end_col_1] = end2 assert int(elems[start_col_1]) > 0 and int(elems[end_col_1]) > 0 fo.write( "%s\n" % '\t'.join( elems ) ) except: @@ -183,7 +185,7 @@ stop_err( "Data issue: click the pencil icon in the history item to correct the metadata attributes." ) if skipped_lines > 0: print 'Skipped %d invalid lines starting with #%dL "%s"' % ( skipped_lines, first_invalid_line, invalid_line ) - print 'Location: %s, Region: %s, Flank-length: %d, Offset: %d ' %( direction, region, size, offset ) - + print 'Location: %s, Region: %s, Flank-length: %d, Offset: %d ' % ( direction, region, size, offset ) + if __name__ == "__main__": main()