File: sam_filter.py

package info (click to toggle)
metaphlan2 2.6.0%2Bds-2
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 72,684 kB
  • ctags: 351
  • sloc: python: 4,352; sh: 26; makefile: 7
file content (59 lines) | stat: -rwxr-xr-x 1,476 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#!/usr/bin/env python
#Author: Duy Tin Truong (duytin.truong@unitn.it)
#        at CIBIO, University of Trento, Italy

__author__  = 'Duy Tin Truong (duytin.truong@unitn.it)'
__version__ = '0.1'
__date__    = '18 Jul 2015'

import sys
import os
import argparse 


def read_params():
    p = argparse.ArgumentParser()
    p.add_argument(
        '--input_file', 
        required=False, 
        default=None, 
        type=str,
        help='The input sam file.')
    p.add_argument(
        '--min_align_score', 
        required=True, 
        default=None, 
        type=int,
        help='The sam records with alignment score smaller than this value '
             'will be discarded.')
    p.add_argument(
        '--verbose', 
        required=False, 
        dest='quiet',
        action='store_false',
        help='Show all information. Default "not set".')
    p.set_defaults(quiet=True)

    return p.parse_args()


def main(args):
    if args.input_file == None:
        ifile = sys.stdin
    else:
        ifile = open(args.input_file, 'r')
    for line in ifile:
        if line[0] == '@':
            sys.stdout.write(line)
        else:
            spline = line.split()
            read_length = len(spline[9])
            align_score = float(spline[11].replace('AS:i:', ''))
            if align_score >= args.min_align_score * read_length / 100.0:
                sys.stdout.write(line)



if __name__ == "__main__":
    args = read_params()
    main(args)