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

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

:py:mod:`obitail` command is in some way analog to the standard Unix `tail` command.
9
It selects the tail of :doc:`a sequence file <../formats>`. 
Aurélie Bonin committed
10
But instead of working text line by text line as the standard Unix tool, 
Eric Coissac committed
11 12
selection is done at the sequence record level. You can specify the number of 
sequence records to select.
Frédéric Boyer committed
13

Eric Coissac committed
14
  *Example:*
Eric Coissac committed
15
    
Eric Coissac committed
16 17 18
    .. code-block:: bash
        
          > obitail -n 150 seq1.fasta > seq2.fasta
Eric Coissac committed
19
    
Eric Coissac committed
20 21
    Selects the 150 last sequence records from the ``seq1.fasta`` file and stores
    them into the ``seq2.fasta`` file.
Eric Coissac committed
22

23 24

'''
Frédéric Boyer committed
25

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

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
    
    queue = collections.deque(entries,options.count)
Eric Coissac committed
46 47 48

    writer = sequenceWriterGenerator(options)
   
49
    while queue:
Eric Coissac committed
50
        writer(queue.popleft())
51 52 53 54