ó
Y_Sc           @@  s$  d  Z  d d l m Z d d l Td d l m Z m Z m Z m Z m	 Z	 m
 Z
 m Z m Z m Z m Z m Z m Z d d d	 d
 d d d d d d d d d d d d d g Z e e e
 e e e e	 e e e e e f a d   Z d   Z e e e e e e e e	 e e f
 a d   Z d d  Z d S(   s\   Sequence file reading and writing.

Biological sequence data is stored and transmitted using a wide variety of
different file formats. This package provides convenient methods to read and
write several of these file fomats.

CoreBio is often capable of guessing the correct file type, either from the
file extension or the structure of the file:
>>> import corebio.seq_io
>>> afile = open("test_corebio/data/cap.fa")
>>> seqs = corebio.seq_io.read(afile)

Alternatively, each sequence file type has a separate module named FILETYPE_io
(e.g. fasta_io, clustal_io).
>>> import corebio.seq_io.fasta_io
>>> afile = open("test_corebio/data/cap.fa")
>>> seqs = corebio.seq_io.fasta_io.read( afile )

Sequence data can also be written back to files:
>>> fout = open("out.fa", "w")
>>> corebio.seq_io.fasta_io.write( fout, seqs )


Supported File Formats
----------------------

Module              Name            Extension  read write features   
---------------------------------------------------------------------------
array_io            array, flatfile             yes  yes    none
clustal_io          clustalw        aln         yes  yes
fasta_io            fasta, Pearson  fa          yes  yes    none
genbank_io          genbank         gb          yes         
intelligenetics_io  intelligenetics ig          yes  yes
msf_io              msf             msf         yes
nbrf_io             nbrf, pir       pir         yes
nexus_io            nexus           nexus       yes
phylip_io           phylip          phy         yes
plain_io            plain, raw                  yes  yes    none
table_io            table           tbl         yes  yes    none

Each IO module defines one or more of the following functions and variables:

read(afile, alphabet=None) 
    Read a file of sequence data and return a SeqList, a collection
    of Seq's (Alphabetic strings) and features.

read_seq(afile, alphabet=None)
    Read a single sequence from a file.

iter_seq(afile, alphabet =None) 
    Iterate over the sequences in a file. 
    
index(afile, alphabet = None)
    Instead of loading all of the sequences into memory, scan the file and
    return an index map that will load sequences on demand. Typically not
    implemented for formats with interleaved sequences.

write(afile, seqlist)
    Write a collection of sequences to the specifed file.

write_seq(afile, seq)
    Write one sequence to the file. Only implemented for non-interleaved, 
    headerless formats, such as fasta and plain.

example
    A string containing a short example of the file format

names
    A list of synonyms for the file format. E.g. for fasta_io, ( 'fasta',    
    'pearson', 'fa'). The first entry is the preferred format name.

extensions
    A list of file name extensions used for this file format. e.g. 
    fasta_io.extensions is ('fa', 'fasta', 'fast', 'seq', 'fsa', 'fst', 'nt',
    'aa','fna','mpfa').  The preferred or standard extension is first in the 
    list.


Attributes :
- formats -- Available seq_io format parsers
- format_names -- A map between format names and format parsers.
- format_extensions -- A map between filename extensions and parsers.

i    (   t   absolute_importi   (   t   *i   (   t
   clustal_iot   fasta_iot   msf_iot   nbrf_iot   nexus_iot   plain_iot	   phylip_iot   stockholm_iot   intelligenetics_iot   table_iot   array_iot
   genbank_ioR   R   R   R   R   R   R   t   null_ioR	   R
   R   R   R   t   readt   formatst   format_namest   format_extensionsc          C@  sK   i  }  x> t  D]6 } x- | j D]" } | |  k s5 t  | |  | <q Wq W|  S(   s4   Return a map between format names and format modules(   R   t   namest   AssertionError(   t   fnamest   ft   name(    (    sA   /home/psgendb/BIRCHDEV/pkg/weblogo-3.4/corebio/seq_io/__init__.pyR   ´   s    c          C@  sK   i  }  x> t  D]6 } x- | j D]" } | |  k s5 t  | |  | <q Wq W|  S(   s@   Return a map between filename extensions and sequence file types(   R   t
   extensionsR   (   t   fextR   t   ext(    (    sA   /home/psgendb/BIRCHDEV/pkg/weblogo-3.4/corebio/seq_io/__init__.pyR   ž   s    c         C@  sÁ   t    } t   } t t  } | d } t |  d  r d |  j k r |  j j d  d } | | k ru | | } q | | k r | | } q n  | | k r­ | j |  n  | j d |  | S(   Ni    R   t   .i˙˙˙˙(	   R   R   t   listt   _parserst   hasattrR   t   splitt   removet   insert(   t   finR   R   t   parserst
   best_guesst	   extension(    (    sA   /home/psgendb/BIRCHDEV/pkg/weblogo-3.4/corebio/seq_io/__init__.pyt   _get_parsersÓ   s    		
c         C@  s   t  |  } t |   } xC | D]; } |  j d  y | j |  |  SWq t k
 rY q Xq Wd j g  | D] } | j d ^ qk  } t d |   d S(   sĹ   Read a sequence file and attempt to guess its format. 
    First the filename extension (if available) is used to infer the format.
    If that fails, then we attempt to parse the file using several common   
    formats.
    
    Note, fin cannot be unseekable stream such as sys.stdin
    
    returns :
        SeqList
    raises :
        ValueError - If the file cannot be parsed.
        ValueError - Sequence do not conform to the alphabet.
    i    s   , s%   Cannot parse sequence file: Tried %s N(   t   AlphabetR&   t   seekR   t
   ValueErrort   joinR   (   R"   t   alphabetR#   t   pR   (    (    sA   /home/psgendb/BIRCHDEV/pkg/weblogo-3.4/corebio/seq_io/__init__.pyR   ë   s    )N(   t   __doc__t
   __future__R    t   seqt    R   R   R   R   R   R   R   R	   R
   R   R   R   t   __all__R   R   R   R   R&   t   NoneR   (    (    (    sA   /home/psgendb/BIRCHDEV/pkg/weblogo-3.4/corebio/seq_io/__init__.pyt   <module>|   s4   
R	*	
	$	