obihead.py 1.58 KB
Newer Older
1 2
#!/usr/local/bin/python
'''
Aurélie Bonin committed
3
:py:mod:`obihead`: extracts the first sequence records
4
======================================================
Frédéric Boyer committed
5 6

.. codeauthor:: Eric Coissac <eric.coissac@metabarcoding.org>
Eric Coissac committed
7 8

:py:mod:`obihead` command is in some way analog to the standard Unix `head` command.
Aurélie Bonin committed
9
It selects the head of a sequence file. 
Aurélie Bonin committed
10
But instead of working text line by text line as the standard Unix tool, 
Eric Coissac committed
11 12 13 14
selection is done at the sequence record level. You can specify the number of sequence records 
to select.

  *Example:*
Eric Coissac committed
15

Aurélie Bonin committed
16
    
Eric Coissac committed
17
    .. code-block:: bash
Aurélie Bonin committed
18
    
Eric Coissac committed
19
         > obihead -n 150 seq1.fasta > seq2.fasta
Eric Coissac committed
20
    
Eric Coissac committed
21 22
    Selects the 150 first sequence records from the ``seq1.fasta`` file and stores
    them into the ``seq2.fasta`` file.
Eric Coissac committed
23

24 25 26

'''
import sys
Eric Coissac committed
27
from obitools.format.options import addInOutputOption, sequenceWriterGenerator
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
from obitools.options import getOptionManager


def addHeadOptions(optionManager):
    optionManager.add_option('-n','--sequence-count',
                             action="store", dest="count",
                             metavar="###",
                             type="int",
                             default=10,
                             help="Count of first sequences to print")
    

if __name__ == '__main__':
    optionParser = getOptionManager([addHeadOptions,addInOutputOption])
    
    (options, entries) = optionParser()
    i=0
Eric Coissac committed
45 46

    writer = sequenceWriterGenerator(options)
47 48 49
    
    for s in entries:
        if i < options.count:
Eric Coissac committed
50
            writer(s)
51 52
            i+=1
        else:
Eric Coissac committed
53
            print >>sys.stderr,""
54 55 56 57
            sys.exit(0)