1 | #!/usr/bin/env python |
---|
2 | |
---|
3 | """ |
---|
4 | Merges any number of BAM files |
---|
5 | usage: %prog [options] |
---|
6 | input1 |
---|
7 | output1 |
---|
8 | input2 |
---|
9 | [input3[,input4[,input5[,...]]]] |
---|
10 | """ |
---|
11 | |
---|
12 | import os, subprocess, sys, tempfile |
---|
13 | |
---|
14 | def stop_err( msg ): |
---|
15 | sys.stderr.write( '%s\n' % msg ) |
---|
16 | sys.exit() |
---|
17 | |
---|
18 | def __main__(): |
---|
19 | infile = sys.argv[1] |
---|
20 | outfile = sys.argv[2] |
---|
21 | if len( sys.argv ) < 3: |
---|
22 | stop_err( 'There are not enough files to merge' ) |
---|
23 | filenames = sys.argv[3:] |
---|
24 | cmd = 'samtools merge %s %s %s' % ( outfile, infile, ' '.join( filenames ) ) |
---|
25 | tmp = tempfile.NamedTemporaryFile().name |
---|
26 | try: |
---|
27 | tmp_stderr = open( tmp, 'wb' ) |
---|
28 | proc = subprocess.Popen( args=cmd, shell=True, stderr=tmp_stderr.fileno() ) |
---|
29 | returncode = proc.wait() |
---|
30 | tmp_stderr.close() |
---|
31 | # get stderr, allowing for case where it's very large |
---|
32 | tmp_stderr = open( tmp, 'rb' ) |
---|
33 | stderr = '' |
---|
34 | buffsize = 1048576 |
---|
35 | try: |
---|
36 | while True: |
---|
37 | stderr += tmp_stderr.read( buffsize ) |
---|
38 | if not stderr or len( stderr ) % buffsize != 0: |
---|
39 | break |
---|
40 | except OverflowError: |
---|
41 | pass |
---|
42 | tmp_stderr.close() |
---|
43 | if returncode != 0: |
---|
44 | raise Exception, stderr |
---|
45 | if os.path.exists( tmp ): |
---|
46 | os.unlink( tmp ) |
---|
47 | except Exception, e: |
---|
48 | if os.path.exists( tmp ): |
---|
49 | os.unlink( tmp ) |
---|
50 | stop_err( 'Error running SAMtools merge tool\n' + str( e ) ) |
---|
51 | if os.path.getsize( outfile ) > 0: |
---|
52 | sys.stdout.write( '%s files merged.' % ( len( sys.argv ) - 2 ) ) |
---|
53 | else: |
---|
54 | stop_err( 'The output file is empty, there may be an error with one of your input files.' ) |
---|
55 | |
---|
56 | if __name__ == "__main__" : __main__() |
---|