Mercurial > repos > devteam > concat
comparison gops_concat.py @ 0:8aa939ace6ba
Imported from capsule None
| author | devteam |
|---|---|
| date | Tue, 01 Apr 2014 10:53:46 -0400 |
| parents | |
| children | d491589307e7 |
comparison
equal
deleted
inserted
replaced
| -1:000000000000 | 0:8aa939ace6ba |
|---|---|
| 1 #!/usr/bin/env python | |
| 2 """ | |
| 3 Concatenate two bed files. The concatenated files are returned in the | |
| 4 same format as the first. If --sameformat is specified, then all | |
| 5 columns will be treated as the same, and all fields will be saved, | |
| 6 although the output will be trimmed to match the primary input. In | |
| 7 addition, if --sameformat is specified, missing fields will be padded | |
| 8 with a period(.). | |
| 9 | |
| 10 usage: %prog in_file_1 in_file_2 out_file | |
| 11 -1, --cols1=N,N,N,N: Columns for chrom, start, end, strand in first file | |
| 12 -2, --cols2=N,N,N,N: Columns for chrom, start, end, strand in second file | |
| 13 -s, --sameformat: All files are precisely the same format. | |
| 14 """ | |
| 15 | |
| 16 import sys, traceback, fileinput | |
| 17 from warnings import warn | |
| 18 from bx.intervals import * | |
| 19 from bx.intervals.io import * | |
| 20 from bx.intervals.operations.concat import * | |
| 21 from bx.cookbook import doc_optparse | |
| 22 from galaxy.tools.util.galaxyops import * | |
| 23 | |
| 24 assert sys.version_info[:2] >= ( 2, 4 ) | |
| 25 | |
| 26 def main(): | |
| 27 sameformat=False | |
| 28 upstream_pad = 0 | |
| 29 downstream_pad = 0 | |
| 30 | |
| 31 options, args = doc_optparse.parse( __doc__ ) | |
| 32 try: | |
| 33 chr_col_1, start_col_1, end_col_1, strand_col_1 = parse_cols_arg( options.cols1 ) | |
| 34 chr_col_2, start_col_2, end_col_2, strand_col_2 = parse_cols_arg( options.cols2 ) | |
| 35 if options.sameformat: sameformat = True | |
| 36 in_file_1, in_file_2, out_fname = args | |
| 37 except: | |
| 38 doc_optparse.exception() | |
| 39 | |
| 40 g1 = NiceReaderWrapper( fileinput.FileInput( in_file_1 ), | |
| 41 chrom_col=chr_col_1, | |
| 42 start_col=start_col_1, | |
| 43 end_col=end_col_1, | |
| 44 strand_col=strand_col_1, | |
| 45 fix_strand=True ) | |
| 46 | |
| 47 g2 = NiceReaderWrapper( fileinput.FileInput( in_file_2 ), | |
| 48 chrom_col=chr_col_2, | |
| 49 start_col=start_col_2, | |
| 50 end_col=end_col_2, | |
| 51 strand_col=strand_col_2, | |
| 52 fix_strand=True ) | |
| 53 | |
| 54 out_file = open( out_fname, "w" ) | |
| 55 | |
| 56 try: | |
| 57 for line in concat( [g1, g2], sameformat=sameformat ): | |
| 58 if type( line ) is GenomicInterval: | |
| 59 out_file.write( "%s\n" % "\t".join( line.fields ) ) | |
| 60 else: | |
| 61 out_file.write( "%s\n" % line ) | |
| 62 except ParseError, exc: | |
| 63 out_file.close() | |
| 64 fail( "Invalid file format: %s" % str( exc ) ) | |
| 65 | |
| 66 out_file.close() | |
| 67 | |
| 68 if g1.skipped > 0: | |
| 69 print skipped( g1, filedesc=" of 1st dataset" ) | |
| 70 if g2.skipped > 0: | |
| 71 print skipped( g2, filedesc=" of 2nd dataset" ) | |
| 72 | |
| 73 if __name__ == "__main__": | |
| 74 main() |
