obipr2.py 8.55 KB
Newer Older
1 2
#!/usr/local/bin/python
'''
3 4
:py:mod:`obipr2`: converts silva database into an ecoPCR database
=================================================================
5 6 7 8 9

:py:mod:`obipr2`: converts and optionally download the `PR2 database <http://ssu-rrna.org/pr2/>`_
into an ecoPCR database. The formated database include the taxonomy as defined by the PR2 authors.

.. warning::
10
    Take care that the numeric taxids associated to the sequences are specific 
Eric Coissac committed
11 12
    to this **PR2** database and not compatible with the NCBI taxids. 
    The taxids present in a version of the **PR2** database are are just valid for 
13 14 15 16 17 18 19 20 21 22
    this version of the database and not compatible with the taxids used in another version
    downloaded at an other time.


*Example:*

    .. code-block:: bash

           > obipr2 
           
23 24
   This command downloads and formats the latest version of the PR2 database from
   the official `PR2 web site<http://ssu-rrna.org/pr2/>`_.
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 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302
        

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

'''


from obitools.options import getOptionManager
from obitools.ecopcr.taxonomy import Taxonomy
from obitools.fasta import fastaIterator
import sys
from obitools.utils import universalOpen, ColumnFile
import re
import urllib2
from obitools.ecopcr.sequence import EcoPCRDBSequenceWriter
from obitools.utils import progressBar
from os.path import isfile, join
from os import listdir




def numberInStr(s) :
    containsNumber = False
    for c in s :
        if c.isdigit() :
            containsNumber = True
    return containsNumber

def silvaOptions(optionManager):
    optionManager.add_option('--localdb',
                             action="store", dest="local",
                             type='str',
                             default=None,
                             help="Local copy of the files located in the specified directory "
                             "will be used instead of those present on the PR2 web site")

    optionManager.add_option('-m','--min-taxid',
                             action="store", dest="taxashift",
                             type="int",
                             metavar="####",
                             default=10000000,
                             help="minimal taxid for the species taxid")

# http://5.196.17.195/pr2/download/entire_database/gb203_pr2.fasta.gz
siteurl="http://5.196.17.195/"
baseurl="%s/pr2/download/entire_database" % siteurl


def getHyperlink(url):
    furl = urllib2.urlopen(url)
    data = "".join([l.strip() for l in furl])
    
    href = re.compile('<a .*?</a>',re.IGNORECASE)
    target=re.compile('href="(.*?)"',re.IGNORECASE)
    filename=re.compile(">(.*?)</a>",re.IGNORECASE)
    
    hrefs = href.findall(data)
    
    links = {}
    
    for h in hrefs:
        t = target.search(h).group(1) 
        f = filename.search(h).group(1)
        links[f]=t 
        
    return links
    
def pr2URLS(options):
    
    global baseurl
    
    if options.local is not None:
        archive = dict((f,f) for f in listdir(options.local) if isfile(join(options.local,f)))
        baseurl=options.local
    else:
        archive=getHyperlink(baseurl)

        
    pr2file = [x.strip() for x in archive.keys() 
                 if x.strip().endswith('pr2.fasta.gz') or x.strip().endswith('pr2.fasta')
                ]
    
    version_pattern = re.compile("^gb([0-9]*)", re.IGNORECASE)
    
    versions = [int(version_pattern.search(x.strip()).group(1)) for x in pr2file]
    latest = max(versions)

    seqfile=pr2file[versions.index(latest)]
        
    pr2txfile = [x for x in archive.keys() 
                 if x.endswith('pr2.tlf.gz') or x.endswith('pr2.tlf')
                ]
            
    versions = [int(version_pattern.search(x).group(1)) for x in pr2txfile]
    print versions
    
    taxfile = pr2txfile[versions.index(latest)]
    
    try:
        sequrl = archive[seqfile]
    except KeyError:
        if seqfile[-3:]=='.gz':
            seqfile=seqfile[0:-3]
        else:
            seqfile=seqfile+'.gz'
        sequrl = archive[seqfile]
        
    try:
        taxurl = archive[taxfile]
    except KeyError:
        if taxfile[-3:]=='.gz':
            taxfile=taxfile[0:-3]
        else:
            taxfile=taxfile+'.gz'
        taxurl = archive[taxfile]

    output = "pr2_gb%d" % latest
           
    return "%s/%s" %(baseurl,sequrl),"%s/%s" %(baseurl,taxurl),output
    
    
pathElementPattern = re.compile("^ *(.*?) {(.*?)} *$", re.IGNORECASE)
def pr2PathParser(path):
    x = pathElementPattern.match(path)
    rank = x.group(1)
    if rank=='classe':
        rank='class'
    elif rank=='ordre':
        rank='order'
    elif rank=='famille':
        rank='family'
    elif rank=='genre':
        rank='genus'
    elif rank=='espece':
        rank='species'
    elif rank.strip()=="":
        rank="no rank"
    
    return rank,x.group(2)
        
class Pr2Dump(Taxonomy):  
        
    def __init__(self,taxdump=None):
        
        self._path=taxdump
        self._readNodeTable(taxdump)
        
        Taxonomy.__init__(self)
           
    def _taxonCmp(t1,t2):
        if t1[0] < t2[0]:
            return -1
        elif t1[0] > t2[0]:
            return +1
        return 0
    
    _taxonCmp=staticmethod(_taxonCmp)
        
    def _readNodeTable(self,dumpfile):

        
        nodes = ColumnFile(dumpfile, 
                           sep='\t', 
                           types=(str,pr2PathParser))
        print >>sys.stderr,"Reading taxonomy dump file..."
            # (taxid,rank,parent)

        nexttaxid = 2  
        taxidx={'root':1}         
        actaxid={} 
        taxonomy=[[1,'root',1,'root','pr2']]
        for node in nodes:
            ac = node[0]
            path = [('root','root')] + node[2:]
            allpath = [[]]
            for s in path:
                allpath.append(allpath[-1]+[s[1]])
                
            allpath.pop(0)
            allpath=[";".join(x) for x in allpath]
            i=0
            for p in allpath:
                try:
                    taxid = taxidx[p]
                except KeyError:
                    taxid=nexttaxid
                    taxidx[p]=taxid
                    nexttaxid+=1
                    parent=p.rsplit(";",1)[0]
                    ptaxid=taxidx[parent]
                    rank = path[i][0]
                    name = path[i][1]
                    taxonomy.append([taxid,rank,ptaxid,name,'pr2'])
                i+=1
            actaxid[ac]=taxid
                                
        print >>sys.stderr,"List all taxonomy rank..."    
        ranks =list(set(x[1] for x in taxonomy)) 
        ranks.sort()
        print >>sys.stderr,ranks
        rankidx = dict(map(None,ranks,xrange(len(ranks))))
        
        self._taxonomy=taxonomy
        self._localtaxon=len(taxonomy)
    
        print >>sys.stderr,"Indexing taxonomy..."
        index = {}
        for i in xrange(self._localtaxon):
            index[self._taxonomy[i][0]]=i
        
        print >>sys.stderr,"Indexing parent and rank..."
        for t in self._taxonomy:
            t[1]=rankidx[t[1]]
            t[2]=index[t[2]]
        
        self._ranks=ranks
        self._index=index 
        self._preferedName = []

        self._name=[(n[3],'scientific name',self._index[n[0]]) for n in taxonomy]    
        self.pr2ac=actaxid
        
                   
        
def pr22obi(seq,taxonomy):

    try:
        # parent = taxonomy.findTaxonByTaxid(taxonomy.silvaname[ancestor])
        oriid=seq.id 
        seq.id,seq.definition=oriid.split("|",1)
        taxid=taxonomy.pr2ac[seq.id]
        seq['taxid']=taxid
    except KeyError:
        pass
    
    return seq            
                   
                           
                 
if __name__ == '__main__':
    
    optionParser = getOptionManager([silvaOptions])

    (options, entries) = optionParser()
    
    sequrl,taxurl,options.ecopcroutput = pr2URLS(options)
    
    taxonomydata = universalOpen(taxurl)
    
    options.taxonomy = Pr2Dump(taxonomydata)

#     if options.write != '' :
#         options.write = open(options.write, 'w')
    
    entries = fastaIterator(universalOpen(sequrl))
    writer  = EcoPCRDBSequenceWriter(options)
    
    nseq = len(options.taxonomy.pr2ac)

    progressBar(1,nseq,
                head=options.ecopcroutput)

    done=0
    for e in entries:
        e = pr22obi(e, options.taxonomy)
        done+=1
        progressBar(done,nseq,
                    head=options.ecopcroutput)
        
        if 'taxid' in e:
            writer.put(e)
        else:
            print >>sys.stderr,"\nCannot find taxon for entry : %s : %s" % (e.id,e.definition)
        
    print >>sys.stderr