obicount.py 1.78 KB
Newer Older
1 2
#!/usr/local/bin/python
'''
Aurélie Bonin committed
3
:py:mod:`obicount`: counts the number of sequence records 
Aurélie Bonin committed
4
=========================================================
5

Frédéric Boyer committed
6 7
.. codeauthor:: Eric Coissac <eric.coissac@metabarcoding.org>

Eric Coissac committed
8
:py:mod:`obicount` counts the number of sequence records and/or the sum of the ``count`` attributes.
Frédéric Boyer committed
9

Eric Coissac committed
10
*Example:*
Frédéric Boyer committed
11

Aurélie Bonin committed
12 13 14
    .. code-block:: bash
        
        > obicount seq.fasta  
Frédéric Boyer committed
15

Eric Coissac committed
16 17
    Prints the number of sequence records contained in the ``seq.fasta`` 
    file and the sum of their ``count`` attributes.
18 19 20 21 22 23
'''

from obitools.options import getOptionManager
from obitools.format.options import addInOutputOption

def addCountOptions(optionManager):
Aurélie Bonin committed
24 25
    group=optionManager.add_option_group('Obicount specific options')
    group.add_option('-s','--sequence',
26 27
                             action="store_true", dest="sequence",
                             default=False,
Aurélie Bonin committed
28
                             help="Prints only the number of sequence records."
29 30
                             )
 
Aurélie Bonin committed
31
    group.add_option('-a','--all',
32 33
                             action="store_true", dest="all",
                             default=False,
Aurélie Bonin committed
34
                             help="Prints only the total count of sequence records (if a sequence has no `count` attribute, its default count is 1) (default: False)."
35 36 37 38
                             )


if __name__ == '__main__':
39
    optionParser = getOptionManager([addCountOptions,addInOutputOption], progdoc=__doc__)
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
    
    (options, entries) = optionParser()
    
    count1=0
    count2=0
    
    for s in entries:
        count1+=1
        if 'count' in s:
            count2+=s['count']
        else:
            count2+=1
            
    if options.all==options.sequence:
        print count1,count2
    elif options.all:
        print count2
    else:
        print count1