예제 #1
0
def hmmhit2pacbp(queryorf,
                 queryorg,
                 querycoords,
                 sbjctorf,
                 sbjctorg,
                 hmmhit,
                 verbose=False):
    """
    """
    # trim hmmhit for unmatched characters
    (sbjct_header, sbjct_start, sbjct_end, query_start, query_end, query,
     match, sbjct, score, expect) = hmmhit

    while match and match[0] == ' ':
        query = query[1:]
        match = match[1:]
        sbjct = sbjct[1:]
        sbjct_start += 1
        query_start += 1
    while match and match[-1] == ' ':
        query = query[0:-1]
        match = match[0:-1]
        sbjct = sbjct[0:-1]
        sbjct_end -= 1
        query_end -= 1

    # get orf, node and AA and DNA coordinates of this sbjct hit;
    # correct for -1 offset in start coordinate!!
    sbjct_aa_start = sbjct_start - 1 + sbjctorf.protein_startPY
    sbjct_aa_end = sbjct_end + sbjctorf.protein_startPY
    sbjctNode = (sbjctorg, sbjctorf.id)
    query = query.replace(".", "-").upper()
    sbjct = sbjct.replace(".", "-").upper()

    ############################################################################
    if verbose:
        print "hmmhit2pacbp CREATING pacbps for organism/orf: (%s,%s)" % (
            sbjctorg, sbjctorf.id)
        print "hmmhit2pacbp Q '%s'" % query
        print "hmmhit2pacbp m '%s'" % match
        print "hmmhit2pacbp S '%s'" % sbjct
        print "hmmQ:", query, query_start, query_end, "gaps:",
        print query.count('-'), len(query)
        print "hmmM:", match
        print "hmmS:", sbjct, sbjctNode, sbjct_aa_start, sbjct_aa_end,
        print "len:", sbjct_aa_end - sbjct_aa_start, len(sbjct)
    ############################################################################

    # get Node and sequence of the query
    queryNode = (queryorg, queryorf.id)
    queryseq = deepcopy(query)

    # calculate query sequence position on queryorf
    query_aa_start = querycoords[0] + query_start - 1
    query_aa_end = query_aa_start + len(queryseq) - queryseq.count('-')

    ############################################################################
    if verbose:
        print "hmmq:", queryseq, queryNode, query_aa_start, query_aa_end,
        print "len:", query_aa_end - query_aa_start, len(queryseq)
    ############################################################################

    # make a deepcopy; sbjct is needed unchanged for the next iteration
    # in the for loop, but here we want to trim of gap sequences
    sbjctseq = deepcopy(sbjct)
    sbjctaastart = deepcopy(sbjct_aa_start)
    sbjctaaend = deepcopy(sbjct_aa_end)
    while queryseq and queryseq[0] == '-':
        queryseq = queryseq[1:]
        sbjctseq = sbjctseq[1:]
        sbjctaastart += 1
    while sbjctseq and sbjctseq[0] == '-':
        queryseq = queryseq[1:]
        sbjctseq = sbjctseq[1:]
        query_aa_start += 1
    while queryseq and queryseq[-1] == '-':
        queryseq = queryseq[0:-1]
        sbjctseq = sbjctseq[0:-1]
        sbjctaaend -= 1
    while sbjctseq and sbjctseq[-1] == '-':
        queryseq = queryseq[0:-1]
        sbjctseq = sbjctseq[0:-1]
        query_aa_end -= 1

    # NEW NEW code in december 2010. Since inwpCBGs are implemented, HMM
    # profiles are build from clustalw alignments which have loosely aligned
    # tails (SPRDIF sequences). Problem with HMM is, that in the result file
    # no information is written on where in teh constructed HMM this hit
    # starts. This **sucks** because special care was taken in ABFGP code to
    # make shure the exact aa-coordinates of the applied sequences to ClustalW
    # are known. Hmmbuild here nullifies this effort by not giving start
    # coordinates. Therefore, we have to check the exact start position
    # of the HMM match on the queryorf.
    if queryseq.replace("-", "") != queryorf.getaas(query_aa_start,
                                                    query_aa_end):
        # obtain (search) query sequence, replace gaps by X symbol
        searchqueryseq = queryseq.upper().replace("-", "X")
        # count length of the query sequence; here IGNORE THE GAPS!!
        seqlen = len(queryseq.upper().replace("-", ""))

        # make fasta sequence dictionary
        seqdict = {
            'query_hmm': searchqueryseq,
            'query_orf': queryorf.protein_sequence,
        }

        # make coords dictionary for remapping
        coords = {
            'query_hmm': [0, seqlen],
            'query_orf': [queryorf.protein_startPY, queryorf.protein_endPY],
        }

        # perform clustalw multiple alignment
        (alignedseqs, alignment) = clustalw(seqs=seqdict)
        # strip exterior gaps
        alignedseqs, alignment, coords = strip_alignment_for_exterior_gaps(
            deepcopy(alignedseqs), deepcopy(alignment), deepcopy(coords))

        if alignedseqs['query_hmm'].count("-") > 0:
            # in (very) exceptional cases, gaps can be introduced in the
            # clustalw alignment in the HMM seq. This normally does not
            # occur! Fix this here by placing gaps in sbjctseq too.
            sbjctseq_as_list = list(sbjctseq)
            for pos in range(0, len(alignedseqs['query_hmm'])):
                if alignedseqs['query_hmm'][pos] == "-":
                    sbjctseq_as_list.insert(pos, "-")
                if alignedseqs['query_hmm'].find("-", pos) == -1:
                    break
            sbjctseq = "".join(sbjctseq_as_list)

        ########################################################################
        if verbose:
            print "\t", "FALSE::", sbjctseq, "[ WITH GAPS,SBJCT ]"
            print "\t", "FALSE::", queryseq, "[ WITH GAPS ]"
            for k, algseq in alignedseqs.iteritems():
                print "\t", "FALSE::", algseq, k, coords[k], len(algseq)
            print "\t", "FALSE::", sbjctseq, "SBJCT", len(sbjctseq)
            print "\t", "FALSE::", alignment, "ALMNT", len(alignment)
            print "\t", "SOLVED:", len(
                alignedseqs['query_orf']) == len(sbjctseq)
        ########################################################################

        # update query sequence & coordinates
        if len(alignedseqs['query_orf']) == len(sbjctseq):
            queryseq = alignedseqs['query_orf']
            query_aa_start = coords['query_orf'][0]
            query_aa_end = coords['query_orf'][1]
        else:
            # still not identical lengths. ClustalW recovery of HMM hit
            # failed miserably. For now: omit
            # TODO: resolve this case!!
            # example: --filewithloci examples/bilal/CFU_830450.bothss.csv
            # ## HMM clustalw input profile: False MAXSR True
            # FPKGCESGKFINWKTFKANGVNLGAWLAKEKTHDPVW foxga [561, 598]
            # FQRACR--KFID-ETLSAHAL---EWESKEIVPPEVW CFU [357, 388]
            # hmmhit2pacbp CREATING pacbps for organism/orf: (NP1064101[anid],1)
            # hmmhit2pacbp Q 'FQKACRSGKFIDWKTLKANALNLGEWLAKEKVHD'
            # hmmhit2pacbp m '+ ka +   F  W   k  + nLG Wl  E   d'
            # hmmhit2pacbp S 'YTKAFQ--PF-SWSSAKVRGANLGGWLVQEASID'
            # hmmQ: FQKACRSGKFIDWKTLKANALNLGEWLAKEKVHD 1 34 gaps: 0 34
            # hmmM: + ka +   F  W   k  + nLG Wl  E   d
            # hmmS: YTKAFQ--PF-SWSSAKVRGANLGGWLVQEASID ('NP1064101[anid]', 1) 33 64 len: 31 34
            # hmmq: FQKACRSGKFIDWKTLKANALNLGEWLAKEKVHD ('CFU', 91) 357 391 len: 34 34
            #         FALSE:: YTKAFQ---------PF-SWSS-----------------AKVR----------GANLGG--W-LVQEASID [ WITH GAPS,SBJCT ]
            #         FALSE:: FQKACRSGKFIDWKTLKANALNLGEWLAKEKVHD [ WITH GAPS ]
            #         FALSE:: FQKACR-------SGKFIDWKT-----------------LKAN----------ALNLGE--W-LAKEKVH query_hmm [0, 33] 70
            #         FALSE:: FQRACRKFIDETLSAHALEWESKEIVPPEVWQRFAEANMLIPNLAALASRMVGEIGIGNAFWRLSVQGLR query_orf [357, 427] 70
            #         FALSE:: YTKAFQ---------PF-SWSS-----------------AKVR----------GANLGG--W-LVQEASID SBJCT 71
            #         FALSE:: **:***       *.: ::*::                 * .*           :.:*:  * *: : :: ALMNT 70
            #         SOLVED: False
            # Pacbp creation failed!
            return False, None

    if queryseq and sbjctseq:
        ################################################################
        if len(queryseq) != len(sbjctseq):
            # this will result in a exception to be raised:
            # pacb.exceptions.InproperlyAppliedArgument
            # print data here about what went wrong, then
            # just let the error be raised
            print queryseq, len(queryseq), sbjctseq, len(sbjctseq)
            print hmmhit
            print "Q:", query_aa_start, query_aa_end,
            print query_aa_end - query_aa_start, "len:", len(queryseq)
            print "S:", sbjctaastart, sbjctaaend,
            print sbjctaaend - sbjctaastart, "len:", len(sbjctseq)
        ################################################################
        pacbpinput = (queryseq, sbjctseq, query_aa_start, sbjctaastart)
        pacbp = PacbP(input=pacbpinput)
        # remove consistent internal gaps caused hy HMM profile search
        pacbp.strip_consistent_internal_gaps()
        pacbp.source = 'hmmsearch'
        pacbporf = PacbPORF(pacbp, queryorf, sbjctorf)
        pacbporf.strip_unmatched_ends()
        if pacbporf.length == 0:
            # Pacbp creation failed!
            return False, None
        else:
            pacbporf.extend_pacbporf_after_stops()
            pacbpkey = pacbporf.construct_unique_key(queryNode, sbjctNode)
            # return unique key and pacbporf
            return (pacbpkey, queryNode, sbjctNode), pacbporf
    else:
        # Pacbp creation failed!
        return False, None
예제 #2
0
def _merge_pacbporfs_by_tinyexon_and_two_introns(pacbporfD,pacbporfA,
    orfSetObject,queryorsbjct,verbose = False, **kwargs):
    """
    Merge 2 PacbPORF objects by introns

    @attention: see pacb.connecting.merge_orfs_with_intron for **kwargs)

    @type  pacbporfD: PacbPORF object
    @param pacbporfD: PacbPORF object that has to deliver PSSM donor objects

    @type  pacbporfA: PacbPORF object
    @param pacbporfA: PacbPORF object that has to deliver PSSM acceptor objects

    @type  orfSetObject: object with elegiable Orfs
    @param orfSetObject: object with elegiable Orfs

    @type  queryorsbjct: string
    @param queryorsbjct: literal string 'query' or 'sbjct'

    @type  verbose: Boolean
    @param verbose: print debugging info to STDOUT when True

    @rtype:  list
    @return: list with ( intron, ExonOnOrf, intron ) on the query sequence
    """
    # input validation
    IsPacbPORF(pacbporfD)
    IsPacbPORF(pacbporfA)

    # edit **kwargs dictionary for some forced attributes
    _update_kwargs(kwargs,KWARGS_PROJECTED_TINYEXON)

    MAX_TINYEXON_NT_LENGTH = 33
    MIN_TINYEXON_NT_LENGTH = 6

    tinyexons = []
    if queryorsbjct == "query":
        donorOrf = pacbporfD.orfQ
        accepOrf = pacbporfA.orfQ
        prjctOrf = pacbporfD.orfS
        alignedDonorRange = pacbporfD.alignment_dna_range_query()
        alignedAccepRange = pacbporfA.alignment_dna_range_query()
    elif queryorsbjct == "sbjct":
        donorOrf = pacbporfD.orfS
        accepOrf = pacbporfA.orfS
        prjctOrf = pacbporfD.orfQ
        alignedDonorRange = pacbporfD.alignment_dna_range_sbjct()
        alignedAccepRange = pacbporfA.alignment_dna_range_sbjct()
    else:
        message = "'queryorsbjct' (%s), not 'query' or 'sbjct'" % queryorsbjct
        raise InproperlyAppliedArgument, message

    for dObj in donorOrf._donor_sites:
        # do not make a projection OVER the aligned area
        if dObj.pos < min(alignedDonorRange): continue
        if queryorsbjct == "query":
            (dPos,dPhase) = pacbporfD.dnaposition_query(dObj.pos,forced_return=True)
        else:
            (dPos,dPhase) = pacbporfD.dnaposition_sbjct(dObj.pos,forced_return=True)
        try:
            algDobj = pacbporfD._positions[dPos]
        except IndexError:
            # site out of range of PacbPORF -> break
            break
        for aObj in accepOrf._acceptor_sites:
            # do not make a projection OVER the aligned area
            if aObj.pos > max(alignedAccepRange): continue
            if queryorsbjct == "query":
                (aPos,aPhase) = pacbporfA.dnaposition_query(aObj.pos,forced_return=True)
            else:
                (aPos,aPhase) = pacbporfA.dnaposition_sbjct(aObj.pos,forced_return=True)
            try:
                algAobj = pacbporfA._positions[aPos]
            except IndexError:
                # site out of range of PacbPORF -> break
                break
            if queryorsbjct == "query":
                posDsbjct = algDobj.sbjct_dna_start + dPhase
                posAsbjct = algAobj.sbjct_dna_start + aPhase
            else:
                posDsbjct = algDobj.query_dna_start + dPhase
                posAsbjct = algAobj.query_dna_start + aPhase
            distance = posAsbjct - posDsbjct
            if distance >= MAX_TINYEXON_NT_LENGTH:
                break
            if distance < MIN_TINYEXON_NT_LENGTH:
                continue

            ####################################################
            # generate a ScanForMatches pattern file
            ####################################################
            # example pattern: 6...6 AG NNGNNANNANNGN[2,0,0] GT 3...3
            query = list(prjctOrf.inputgenomicsequence[posDsbjct:posAsbjct])
            # mask all non-phase0 nucleotides to N residues;
            # this represents the regularexpression for a specific
            # peptide sequence
            firstphasepositions = range( 3-dPhase % 3, len(query), 3)
            for pos in range(0,len(query)):
                if pos not in firstphasepositions:
                    query[pos] = "N"
            # calculate a ~50% mismatch number
            mismatches =  max([ 0, (len(query) - query.count("N"))/2 ])
            # write the pattern to string and subsequently to file
            # example pattern: 6...6 AG NNGNNANNANNGN[2,0,0] GT 3...3
            if kwargs['allow_non_canonical_donor']:
                sfmpat = "%s...%s AG %s[%s,0,0] G (T | C) %s...%s" % (
                    AUSO,AUSO,"".join(query),mismatches,DDSO,DDSO)
            else:
                sfmpat = "%s...%s AG %s[%s,0,0] GT %s...%s" % (
                    AUSO,AUSO,"".join(query),mismatches,DDSO,DDSO)

            ####################################################
            if verbose:
                print (pacbporfD.orfQ.id,pacbporfA.orfQ.id),
                print distance, dObj, aObj
                print sfmpat
            ####################################################

            fname = "sfmpat_tinyexon_%s_%s_%s_%s" % (
                        donorOrf.id,
                        accepOrf.id,
                        posDsbjct,
                        posAsbjct,
                        )
            fh = open(fname,'w')
            fh.write(sfmpat+"\n")
            fh.close()

            ####################################################
            # run ScanForMatches
            ####################################################
            command = """echo ">myseq\n%s" | %s %s | tr "[,]" "\t\t#" | """ +\
                      """tr -d "\n " | sed "s/>/\\n>/g" | tr "#" "\t" | """ +\
                      """awk -F'\t' '{ if (NF==4 && $2>%s && $3<%s) """ +\
                      """{ print $1"["$2","$3"]\\n"$4 } }' """
            command = command % (
                        donorOrf.inputgenomicsequence,
                        EXECUTABLE_SFM,fname,
                        dObj.pos+(kwargs['min_intron_nt_length']-3),
                        aObj.pos-(kwargs['min_intron_nt_length']-3) )
            co = osPopen(command)
            matches = parseFasta(co.readlines())
            co.close()

            # filter matches for:
            # (1) correct donor & acceptor phase
            # (2) high enough donor & acceptor site scores
            for hdr,seqmatch in matches.iteritems():
                startQ,stopQ = [ int(item) for item in hdr.split(":")[1][1:-1].split(",") ]
                exonQstart   = startQ + AUSO + 2 - 1
                exonQstop    = stopQ  - DDSO - 2

                ####################################
                # get Orf object of tinyexon
                ####################################
                tinyexonorf = None
                # select the Orf on which the tinyexon is located
                for orfObj in orfSetObject.get_elegiable_orfs(
                max_orf_start=exonQstart,min_orf_end=exonQstop):
                    orfPhase = (exonQstart - orfObj.startPY) % 3
                    if orfPhase == dPhase:               
                        tinyexonorf = orfObj
                        break
                else:
                    # No tinyexonorf assigned!! Iin case a regex matched
                    # over a STOP-codon or the regex length is smaller
                    # then the smallest Orf, no Orf can be assigned
                    continue

                # filter for donor & acceptor score            
                dScore = _score_splice_site(seqmatch[-9:],splicetype='donor')
                aScore = _score_splice_site(seqmatch[0:11],splicetype='acceptor')
                if dScore < kwargs['min_donor_pssm_score']:
                    continue
                if aScore < kwargs['min_acceptor_pssm_score']:
                    continue

                # scan Orf for splicesites
                tinyexonorf.scan_orf_for_pssm_splice_sites(
                        splicetype="donor",
                        min_pssm_score=kwargs['min_donor_pssm_score'],
                        allow_non_canonical=kwargs['allow_non_canonical_donor'],
                        non_canonical_min_pssm_score=kwargs['non_canonical_min_donor_pssm_score'])
                tinyexonorf.scan_orf_for_pssm_splice_sites(
                        splicetype="acceptor",
                        min_pssm_score=kwargs['min_acceptor_pssm_score'],
                        allow_non_canonical=kwargs['allow_non_canonical_acceptor'],
                        non_canonical_min_pssm_score=kwargs['non_canonical_min_acceptor_pssm_score'])

                # get 1th intron donor object
                intron1_aObj = None
                for a in tinyexonorf._acceptor_sites:
                    if a.pos == exonQstart:
                        intron1_aObj = a
                        break
                else:
                    # pseudo-acceptorsite as found be SFM regex
                    # is not a valid acceptor site of high enough score
                    # continue to next iteration of (hdr,seqmatch) pair
                    continue

                # get 2th intron donor object
                intron2_dObj = None
                for d in tinyexonorf._donor_sites:
                    if d.pos == exonQstop:
                        intron2_dObj = d
                        break
                else:
                    # pseudo-donorsite as found be SFM regex
                    # is not a valid acceptor site of high enough score
                    # continue to next iteration of (hdr,seqmatch) pair
                    continue


                # check if introns are of elegiable lengths
                if (intron1_aObj.pos-dObj.pos) > kwargs['max_intron_nt_length']:
                    continue
                if (aObj.pos-intron2_dObj.pos) > kwargs['max_intron_nt_length']:
                    continue

                ####################################################
                if True or verbose:
                    # if here, a candidate!!!
                    print (pacbporfD.orfQ.id,tinyexonorf.id,pacbporfA.orfQ.id),
                    print hdr, dScore, aScore
                    print seqmatch
                ####################################################

                # append to found tinyexons
                query_data      = ( tinyexonorf, exonQstart, exonQstop )
                sbjct_data      = ( prjctOrf, posDsbjct, posAsbjct )
                splicesite_data = ( dObj,intron1_aObj, intron2_dObj, aObj )
                tinyexons.append( ( query_data, sbjct_data, splicesite_data ) )


            # file cleanup
            osRemove(fname)

    # return - End Of Function - if no tinyexons are found
    if not tinyexons:
        return []

    ####################################
    # select the **best** tinyexon
    ####################################
    (query_data,sbjct_data,splicesite_data) = tinyexons[0]
    orfQ,query_dna_start,query_dna_end = query_data
    orfS,sbjct_dna_start,sbjct_dna_end = sbjct_data
    (intron1_dObj,intron1_aObj,intron2_dObj,intron2_aObj) = splicesite_data

    ####################################################
    if verbose:
        print "tinyexon orf:", orfQ
        print "tinyexon orf:", intron1_aObj
        print "tinyexon orf:", intron2_dObj
    ####################################################

    ####################################
    # make tinyexon PacbPORF
    ####################################
    startQaa = orfQ.dnapos2aapos(query_dna_start) -1
    startSaa = orfS.dnapos2aapos(sbjct_dna_start) -1
    stopQaa  = orfQ.dnapos2aapos(query_dna_end) +1
    stopSaa  = orfS.dnapos2aapos(sbjct_dna_end) +1
    # check for directly leading stop codon on tinyexon
    while startQaa <= orfQ.protein_startPY:
        startQaa+=1
        startSaa+=1
        query_dna_start+=3
        sbjct_dna_start+=3
    while startSaa <= orfS.protein_startPY:
        startQaa+=1
        startSaa+=1
        query_dna_start+=3
        sbjct_dna_start+=3
    # check for directly tailing stop codon on tinyexon
    while stopQaa > orfQ.protein_endPY:
        stopQaa-=1
        stopSaa-=1
        query_dna_end-=3
        sbjct_dna_end-=3
    while stopSaa > orfS.protein_endPY:
        stopQaa-=1
        stopSaa-=1
        query_dna_end-=3
        sbjct_dna_end-=3
    # get sequences
    qAAseq = orfQ.getaas(abs_pos_start=startQaa,abs_pos_end=stopQaa)
    sAAseq = orfS.getaas(abs_pos_start=startSaa,abs_pos_end=stopSaa)

    ####################################################
    if verbose or len(qAAseq) != len(sAAseq):
        # if unequal lengths, error will be raised upon PacbP.__init__()
        print orfQ, qAAseq, startQaa, stopQaa, (stopQaa-startQaa),
        print (query_dna_start,query_dna_end)
        print orfS, sAAseq, startSaa, stopSaa, (stopSaa-startSaa),
        print (sbjct_dna_start,sbjct_dna_end)
        print orfQ.inputgenomicsequence[query_dna_start-2:query_dna_end+2]
        print orfS.inputgenomicsequence[sbjct_dna_start-2:sbjct_dna_end+2]
    ####################################################

    # initialize extended tinyexon PacbPORF
    from pacb import PacbP
    pacbp = PacbP(input=( qAAseq, sAAseq, startQaa, startSaa ) )
    pacbp.strip_unmatched_ends()
    pacbporf = pacbp2pacbporf(pacbp,orfQ,orfS)
    pacbporf.extend_pacbporf_after_stops()
    pacbporf.source = 'ABGPprojectingTE'

    ####################################
    # make introns
    ####################################
    intron1 = IntronConnectingOrfs(
                intron1_dObj, intron1_aObj, None,
                donorOrf,pacbporf.orfQ )
    intron2 = IntronConnectingOrfs(
                intron2_dObj, intron2_aObj, None,
                pacbporf.orfQ, accepOrf )


    ################################################################
    # set some meta-data properties to the intron objects
    ################################################################
    # add distance score to intron
    intron1._distance = 0
    intron2._distance = 0

    # add Alignment Positional Periphery Score into objects
    if queryorsbjct == "query":
        succes = set_apps_intron_query(intron1,pacbporfD,pacbporf)
        succes = set_apps_intron_query(intron2,pacbporf,pacbporfA)
    else:
        succes = set_apps_intron_sbjct(intron1,pacbporfD,pacbporf)
        succes = set_apps_intron_sbjct(intron2,pacbporf,pacbporfA)

    # set GFF fsource attribute for recognition of intron sources
    intron1._gff['fsource'] = "ABGPprojectingTE"
    intron2._gff['fsource'] = "ABGPprojectingTE"

    # create _linked_to_xxx attributes
    intron1._linked_to_pacbporfs = [ pacbporf ]
    intron2._linked_to_pacbporfs = [ pacbporf ]
    intron1._linked_to_introns   = [ intron2 ]
    intron2._linked_to_introns   = [ intron1 ]

    ####################################################
    if verbose:
        print pacbporf
        pacbporf.print_protein_and_dna()
        print intron1
        print intron2
        if False:
            # printing data when this function needs to be debugged:
            print ""
            print intron1
            print intron2
            print ""
            print pacbporfD
            pacbporfD.print_protein_and_dna()
            print ""
            print pacbporf
            pacbporf.print_protein_and_dna()
            print ""
            print pacbporfA
            pacbporfA.print_protein_and_dna()
            import sys
            sys.exit()
    ####################################################

    # return introns and intermediate tinyexon PacbPORF
    return [(intron1,intron2,pacbporf)]
예제 #3
0
def _merge_pacbporfs_by_two_tinyexons(pacbporfD,pacbporfA,
    orfSetObject,queryorsbjct,verbose = False, **kwargs):
    """ """
    # edit **kwargs dictionary for some forced attributes
    _update_kwargs(kwargs,KWARGS_PROJECTED_TINYEXON)

    tinyexons = []
    sposD = pacbporfD._get_original_alignment_pos_start()
    eposD = pacbporfD._get_original_alignment_pos_end()
    sposA = pacbporfA._get_original_alignment_pos_start()
    eposA = pacbporfA._get_original_alignment_pos_end()
    if queryorsbjct == "query":
        donorOrf = pacbporfD.orfQ
        accepOrf = pacbporfA.orfQ
        prjctOrf = pacbporfD.orfS
        dStart,dEnd = sposD.query_dna_start, eposD.query_dna_end
        aStart,aEnd = sposA.query_dna_start, eposA.query_dna_end
    elif queryorsbjct == "sbjct":
        donorOrf = pacbporfD.orfS
        accepOrf = pacbporfA.orfS
        prjctOrf = pacbporfD.orfQ
        dStart,dEnd = sposD.sbjct_dna_start, eposD.sbjct_dna_end
        aStart,aEnd = sposA.sbjct_dna_start, eposA.sbjct_dna_end
    else:
        message = "'queryorsbjct' (%s), not 'query' or 'sbjct'" % queryorsbjct
        raise InproperlyAppliedArgument, message

    # get all potential combinations of two tinyexons
    tinyexoncombis = merge_orfs_with_two_tinyexons(
                donorOrf, accepOrf,
                donorOrf._donor_sites,
                accepOrf._acceptor_sites,
                orfSetObject.orfs,
                )

    results = []

    for dObj in donorOrf._donor_sites:
        if queryorsbjct == "query":
            (dPos,dPhase) = pacbporfD.dnaposition_query(dObj.pos,forced_return=True)
        else:
            (dPos,dPhase) = pacbporfD.dnaposition_sbjct(dObj.pos,forced_return=True)
        try:
            algDobj = pacbporfD._positions[dPos]
        except IndexError:
            # site out of range of PacbPORF -> break
            break

        # check if dObj is on pfD;
        # introns of tinyexons can be projected outside of pfD/pfA area
        if dObj.pos < dStart: continue

        for aObj in accepOrf._acceptor_sites:
            if queryorsbjct == "query":
                (aPos,aPhase) = pacbporfA.dnaposition_query(aObj.pos,forced_return=True)
            else:
                (aPos,aPhase) = pacbporfA.dnaposition_sbjct(aObj.pos,forced_return=True)
            try:
                algAobj = pacbporfA._positions[aPos]
            except IndexError:
                # site out of range of PacbPORF -> break
                break

            # check if aObj is on pfA;
            # introns of tinyexons can be projected outside of pfD/pfA area
            if aObj.pos > aEnd: continue

            if queryorsbjct == "query":
                posDsbjct = algDobj.sbjct_dna_start + dPhase
                posAsbjct = algAobj.sbjct_dna_start + aPhase
            else:
                posDsbjct = algDobj.query_dna_start + dPhase
                posAsbjct = algAobj.query_dna_start + aPhase
            distance = posAsbjct - posDsbjct
            if distance >= (kwargs['max_tinyexon_nt_length']*2):
                break
            if distance < (kwargs['min_tinyexon_nt_length']*2):
                continue

            filtered_tinyexoncombis = _filter_tinyexoncombis(tinyexoncombis,
                    min_length = distance,
                    max_length = distance,
                    min_first_acceptor_pos = dObj.pos + kwargs['min_tinyexon_intron_nt_length'],
                    max_final_donor_pos = aObj.pos - kwargs['min_tinyexon_intron_nt_length'],
                    phase_final_donor = aObj.phase,
                    phase_first_acceptor= dObj.phase,
                    )

            if not filtered_tinyexoncombis: continue

            ####################################################################
            if verbose:
                print distance, dObj, aObj, len(tinyexoncombis),
                print len(filtered_tinyexoncombis)
            ####################################################################

            for exon1,intron,exon2 in filtered_tinyexoncombis:
                # make preceding intron
                preceding_intron = IntronConnectingOrfs(
                    dObj,exon1.acceptor,
                    None,donorOrf,exon1.orf )

                # make subsequent intron
                subsequent_intron = IntronConnectingOrfs(
                    exon2.donor, aObj,
                    None,exon2.orf,accepOrf)

                ################################################################
                if verbose:
                    print "\t", exon1, exon1.proteinsequence(),
                    print preceding_intron.phase, exon1.donor.phase,
                    print subsequent_intron.phase, preceding_intron.shared_aa,
                    print intron.shared_aa, subsequent_intron.shared_aa 
                    print "\t", exon2, exon2.proteinsequence()
                ################################################################

                # get prjctOrf sequence for comparison
                correctionA = 0
                if aObj.phase != 0:
                    # INCLUDE the final AA which is broken by the splicesite
                    correctionA=1
                if queryorsbjct == "query":
                    startPos,_phase = pacbporfD.dnaposition_query(dObj.pos,forced_return=True)
                    stopPos,_phase  = pacbporfA.dnaposition_query(aObj.pos,forced_return=True)
                    start = pacbporfD._positions[startPos].sbjct_pos
                    stop  = pacbporfA._positions[stopPos].sbjct_pos + correctionA
                else:
                    startPos,_phase = pacbporfD.dnaposition_sbjct(dObj.pos,forced_return=True)
                    stopPos,_phase  = pacbporfA.dnaposition_sbjct(aObj.pos,forced_return=True)
                    start = pacbporfD._positions[startPos].query_pos
                    stop  = pacbporfA._positions[stopPos].query_pos + correctionA

                if stop <= start:
                    # tinyexon is so tiny that is does not have a single
                    # full aligned AA -> discard here
                    continue

                # actually get the prjctOrf sequence
                aaseq = prjctOrf.getaas(abs_pos_start=start,abs_pos_end=stop)

                # initialize a PacbP for the combination of both tinyexons
                # afterwards, check if the indentityscore is > 0.XX
                from pacb import PacbP
                seqparts = [ preceding_intron.shared_aa,
                             exon1.proteinsequence(),
                             intron.shared_aa,
                             exon2.proteinsequence(),
                             subsequent_intron.shared_aa ]

                ################################################################
                if verbose or len("".join(seqparts)) != len(aaseq):
                    print pacbporfD
                    print exon1.orf, exon2.orf, prjctOrf
                    print pacbporfA
                    print seqparts
                    print aaseq, len(aaseq), len("".join(seqparts)), (start,stop)
                    print "'%s'" % queryorsbjct,
                    print "Q", (algDobj.query_pos, algAobj.query_pos),
                    print "S", (algDobj.sbjct_pos, algAobj.sbjct_pos)
                    print "distance:", distance, kwargs['max_tinyexon_nt_length'],
                    print (posDsbjct, posAsbjct),
                    print "Q-dna:", ( algDobj.query_dna_start, dPhase, algAobj.query_dna_start, aPhase ),
                    print "S-dna:", ( algDobj.sbjct_dna_start, dPhase, algAobj.sbjct_dna_start, aPhase )
                ################################################################

                # ignore by continue when sequences not identical in length
                if len("".join(seqparts)) != len(aaseq): continue

                testpacbp = PacbP(input=( "".join(seqparts), aaseq, 0, 0) )
                testpacbp.strip_unmatched_ends()

                if not ( testpacbp.identityscore > 0.60 and\
                (float(testpacbp.length) / len(aaseq)) > 0.70 ):
                    # not a very convincing alignment
                    continue

                ################################################################
                if verbose:
                    print testpacbp
                    testpacbp.print_protein()
                ################################################################

                # if here, succesfully mapped 2 tiny exons!!
                # get all sequences/coordinates in place for
                # pacbporf formation
                orfQ1   = exon1.orf
                orfS1   = prjctOrf
                orfQ2   = exon2.orf
                orfS2   = prjctOrf
                seqQ1   = exon1.proteinsequence()
                seqQ2   = exon2.proteinsequence()
                coordQ1 = exon1.acceptor.pos / 3
                coordS1 = start
                coordQ2 = exon2.acceptor.pos / 3
                coordS2 = start + len(seqparts[0]) + len(seqparts[1]) + len(seqparts[2])
                seqS1   = aaseq[0:(len(seqparts[0])+len(seqparts[1]))]
                seqS2   = aaseq[-(len(seqparts[3])+len(seqparts[4])):]
                if len(seqparts[0]):
                    seqS1 = seqS1[1:]
                    coordS1 += 1
                if len(seqparts[4]):
                    seqS2 = seqS2[:-1]

                if queryorsbjct == "sbjct": 
                    # swap query <-> sbjct
                    orfQ1,orfS1 = orfS1,orfQ1 
                    orfQ2,orfS2 = orfS2,orfQ2
                    seqQ1,seqS1 = seqS1,seqQ1
                    seqQ2,seqS2 = seqS2,seqQ2
                    coordQ1,coordS1 = coordS1,coordQ1
                    coordQ2,coordS2 = coordS2,coordQ2

                ################################################################
                if verbose:
                    print "tinypacbporf1:", seqQ1, seqQ2, coordQ1, coordQ2
                    print "tinypacbporf2:", seqS1, seqS2, coordS1, coordS2
                ################################################################


                # make pacbporfs
                pacbp1 = PacbP(input=( seqQ1, seqS1, coordQ1, coordS1) )
                pacbp1.strip_unmatched_ends()
                tinypacbporf1 = pacbp2pacbporf(pacbp1,orfQ1,orfS1)
                tinypacbporf1.extend_pacbporf_after_stops()
                pacbp2 = PacbP(input=( seqQ2, seqS2, coordQ2, coordS2) )
                pacbp2.strip_unmatched_ends()
                tinypacbporf2 = pacbp2pacbporf(pacbp2,orfQ2,orfS2)
                tinypacbporf2.extend_pacbporf_after_stops()

                ################################################################
                if verbose:
                    print tinypacbporf1
                    tinypacbporf1.print_protein_and_dna()
                    print tinypacbporf2
                    tinypacbporf2.print_protein_and_dna()
                ################################################################


                ################################################################
                # set some meta-data properties to the intron objects
                ################################################################
                # add distance score to intron
                preceding_intron._distance  = 0
                intron._distance            = 0
                subsequent_intron._distance = 0
            
                # add Alignment Positional Periphery Score into objects
                if queryorsbjct == "query":
                    succes = set_apps_intron_query(preceding_intron,pacbporfD,tinypacbporf1)
                    succes = set_apps_intron_query(intron,tinypacbporf1,tinypacbporf2)
                    succes = set_apps_intron_query(subsequent_intron,tinypacbporf2,pacbporfA)
                else:
                    succes = set_apps_intron_sbjct(preceding_intron,pacbporfD,tinypacbporf1)
                    succes = set_apps_intron_sbjct(intron,tinypacbporf1,tinypacbporf2)
                    succes = set_apps_intron_sbjct(subsequent_intron,tinypacbporf2,pacbporfA)
            
                # set GFF fsource attribute for recognition of intron sources
                preceding_intron._gff['fsource']  = "ABGPprojectingTE"
                intron._gff['fsource']            = "ABGPprojectingTE"
                subsequent_intron._gff['fsource'] = "ABGPprojectingTE"


                # create _linked_to_xxx attributes
                preceding_intron._linked_to_pacbporfs = [ tinypacbporf1, tinypacbporf2 ]
                intron._linked_to_pacbporfs = [ tinypacbporf1, tinypacbporf2 ]
                subsequent_intron._linked_to_pacbporfs = [ tinypacbporf1, tinypacbporf2 ]
                preceding_intron._linked_to_introns   = [ intron,subsequent_intron ]
                intron._linked_to_introns             = [ preceding_intron,subsequent_intron ]
                subsequent_intron._linked_to_introns  = [ intron,preceding_intron ]

                ################################################################
                # append to results
                ################################################################
                results.append( (
                    preceding_intron,
                    intron,
                    subsequent_intron,
                    tinypacbporf1,
                    tinypacbporf2,
                    ) )


    # return 3 introns and 2 intermediate tinyexon PacbPORFs (per row)
    return results
예제 #4
0
파일: mapping.py 프로젝트: IanReid/ABFGP
def merge_pacbporfs_by_tinyexons(pacbporfD,pacbporfA,
    orfSetObjQ,orfSetObjS,verbose=False,**kwargs):
    """ """
    # input validation
    IsPacbPORF(pacbporfD)
    IsPacbPORF(pacbporfA)

    # edit **kwargs dictionary for some forced attributes
    _update_kwargs(kwargs,KWARGS_MAPPED_INTRON)
    if not kwargs.has_key('aligned_site_max_triplet_distance'):
        kwargs['aligned_site_max_triplet_distance'] = kwargs['max_aa_offset']

    # settings for minimal alignment entropy score
    min_donor_site_alignment_entropy = 0.0
    min_acceptor_site_alignment_entropy = 0.0

    resultlistQ = merge_orfs_with_tinyexon(
            pacbporfD.orfQ,pacbporfA.orfQ,
            preceding_donor_sites=pacbporfD.orfQ._donor_sites,
            subsequent_acceptor_sites=pacbporfA.orfQ._acceptor_sites,
            orflist=orfSetObjQ.orfs,**kwargs)
    resultlistS = merge_orfs_with_tinyexon(
            pacbporfD.orfS,pacbporfA.orfS,
            preceding_donor_sites=pacbporfD.orfS._donor_sites,
            subsequent_acceptor_sites=pacbporfA.orfS._acceptor_sites,
            orflist=orfSetObjS.orfs,**kwargs)

    # translate resultlists to dict: key == exon, value = [ {intronsD},{intronsS} ]
    resultdictQ,key2exonQ = _tinyexon_list_2_dict(resultlistQ)
    resultdictS,key2exonS = _tinyexon_list_2_dict(resultlistS)

    # get unique list of donors & acceptors
    donorQ = olba( list(Set([inD.donor for inD,te,inA in resultlistQ ])), order_by='pos')
    donorS = olba( list(Set([inD.donor for inD,te,inA in resultlistS ])), order_by='pos')
    accepQ = olba( list(Set([inA.acceptor for inD,te,inA in resultlistQ ])), order_by='pos')
    accepS = olba( list(Set([inA.acceptor for inD,te,inA in resultlistS ])), order_by='pos')

    ## filter for alignable donor & acceptor sites
    kwargs['allow_non_canonical']               = True # True
    kwargs['aligned_site_max_triplet_distance'] = 0     # 2
    algdonors = _filter_for_alignable_splice_sites(donorQ,donorS,pacbporfD,**kwargs)
    algacceps = _filter_for_alignable_splice_sites(accepQ,accepS,pacbporfA,**kwargs)

    # settings for minimal alignment entropy score
    # TODO TODO -> THIS MUST BE FIXED TO A NICE THRESHOLD VALUE!!!
    min_donor_site_alignment_entropy = 0.1
    min_acceptor_site_alignment_entropy = 0.1


    # remove sites with to low alignment entropy
    algdonors = _filter_for_entropy(algdonors,pacbporfD,'donor',
                min_alignment_entropy=min_donor_site_alignment_entropy)
    algacceps = _filter_for_entropy(algacceps,pacbporfA,'acceptor',
                min_alignment_entropy=min_acceptor_site_alignment_entropy)

    # return list: intronQD,intronSD,tinyexon,intronAQ,intronAS
    return_list = []

    ############################################################################
    if verbose:
        print "bridges constructed: ORFS:",
        print (pacbporfD.orfQ.id,pacbporfA.orfQ.id),
        print (pacbporfD.orfS.id,pacbporfA.orfS.id),
        print len(resultdictQ), len(resultdictS),
        print ( len(resultlistQ), len(donorQ), len(accepQ) ),
        print ( len(resultlistS), len(donorS), len(accepS) ),
        print ( len(algdonors), len(algacceps) )
    ############################################################################

    for keyQ,tinyexonQ in key2exonQ.iteritems():
        for keyS,tinyexonS in key2exonS.iteritems():
            if tinyexonQ.donor.phase != tinyexonS.donor.phase:
                continue
            if tinyexonQ.acceptor.phase != tinyexonS.acceptor.phase:
                continue
            if tinyexonQ.length != tinyexonS.length:
                continue
            # if here, then tinyexons of identical structure


            ####################################################################
            if verbose:
                print tinyexonQ.length, tinyexonQ.donor.phase,
                print ( len(resultdictQ[keyQ][0]), len(resultdictQ[keyQ][1]) ),
                print ( len(resultdictS[keyS][0]), len(resultdictS[keyS][1]) ),
                print tinyexonQ,
                print tinyexonQ.proteinsequence(), tinyexonS.proteinsequence(),
                print tinyexonS.acceptor.pssm_score + tinyexonS.donor.pssm_score
            ####################################################################

            donor_introns = []
            acceptor_introns = []
            for intronDQkey, intronDQ in resultdictQ[keyQ][0].iteritems():
                if intronDQ.donor.pos not in [ dQ.pos for dQ,dS in algdonors ]:
                    continue
                for intronDSkey, intronDS in resultdictS[keyS][0].iteritems():
                    if intronDS.donor.pos not in [ dS.pos for dQ,dS in algdonors ]:
                        continue
                    # check if they exists as aligned sites
                    alignedkey = ( intronDQ.donor.pos, intronDS.donor.pos )
                    if alignedkey not in [ (dQ.pos, dS.pos) for dQ,dS in algdonors ]:
                        continue
                    # if here, we have a set of introns 5' of the tinyexon
                    # which are perfectly alignable!
                    donor_introns.append((intronDQ,intronDS))

            for intronAQkey, intronAQ in resultdictQ[keyQ][1].iteritems():
                if intronAQ.acceptor.pos not in [ aQ.pos for aQ,aS in algacceps ]:
                    continue
                for intronASkey, intronAS in resultdictS[keyS][1].iteritems():
                    if intronAS.acceptor.pos not in [ aS.pos for aQ,aS in algacceps ]:
                        continue
                    # check if they exists as aligned sites
                    alignedkey = ( intronAQ.acceptor.pos, intronAS.acceptor.pos )
                    if alignedkey not in [ (aQ.pos, aS.pos) for aQ,aS in algacceps ]:
                        continue
                    # if here, we have a set of introns 3' of the tinyexon
                    # which are perfectly alignable!
                    acceptor_introns.append((intronAQ,intronAS))

            if not len(donor_introns) or not len(acceptor_introns):
                # no aligned 5' && aligned 3' introns
                continue

            # initialize extended tinyexon PacbPORF
            from pacb import PacbP
            pacbp = PacbP(input=( 
                    tinyexonQ.proteinsequence(),
                    tinyexonS.proteinsequence(),
                    tinyexonQ.protein_start(),
                    tinyexonS.protein_start(),
                    ) )
            pacbp.strip_unmatched_ends()
            # continue if no fraction could be aligned
            if len(pacbp) == 0: continue
            tinypacbporf = pacbp2pacbporf(pacbp,tinyexonQ.orf,tinyexonS.orf)
            tinypacbporf.extend_pacbporf_after_stops()

            ####################################################################
            if verbose:
                print tinypacbporf
                tinypacbporf.print_protein_and_dna()
                print len(donor_introns), len(acceptor_introns),
                print max([ dQ.donor.pssm_score+dS.donor.pssm_score for dQ,dS in donor_introns]),
                print max([ aQ.acceptor.pssm_score+aS.acceptor.pssm_score for aQ,aS in acceptor_introns])
            ####################################################################


            # if here, we have accepted tinyexon bridges!
            # gather them and store to return_list
            for intronDQkey, intronDQ in resultdictQ[keyQ][0].iteritems():
                if intronDQ.donor.pos not in [ dQ.pos for dQ,dS in algdonors ]:
                    continue
                for intronDSkey, intronDS in resultdictS[keyS][0].iteritems():
                    if intronDS.donor.pos not in [ dS.pos for dQ,dS in algdonors ]:
                        continue
                    for intronAQkey, intronAQ in resultdictQ[keyQ][1].iteritems():
                        if intronAQ.acceptor.pos not in [ aQ.pos for aQ,aS in algacceps ]:
                            continue
                        for intronASkey, intronAS in resultdictS[keyS][1].iteritems():
                            if intronAS.acceptor.pos not in [ aS.pos for aQ,aS in algacceps ]:
                                continue
                            ####################################################
                            # set some meta-data properties to the intron objects
                            ####################################################
                            _score_introns_obtained_by_mapping(
                                    intronDQ,intronDS,pacbporfD,
                                    tinypacbporf,source='ABGPmappingTE')
                            _score_introns_obtained_by_mapping(
                                    intronAQ,intronAS,tinypacbporf,
                                    pacbporfA,source='ABGPmappingTE')
                            # create _linked_to_xxx attributes
                            intronDQ._linked_to_pacbporfs = [ tinypacbporf ]
                            intronAQ._linked_to_pacbporfs = [ tinypacbporf ]
                            intronDS._linked_to_pacbporfs = [ tinypacbporf ]
                            intronAS._linked_to_pacbporfs = [ tinypacbporf ]
                            intronDQ._linked_to_introns   = [ intronAQ ]
                            intronAQ._linked_to_introns   = [ intronDQ ]
                            intronDS._linked_to_introns   = [ intronAS ]
                            intronAS._linked_to_introns   = [ intronDS ]
                            # append to tmp result list
                            return_list.append(
                                (intronDQ,intronDS,tinypacbporf,intronAQ,intronAS)
                                )

    # check if there are >1 candidate tiny exons
    # currently, we choose only to return the **best** mapped tinyexon 
    if len(return_list) == 0:
        pass
    elif len(return_list) == 1:
        pass
    else:
        # only take the highest scoring candidate here 
        min_distance = min([ (a._distance+d._distance) for a,b,c,d,e in return_list ])
        pos2score = []
        for (intronDQ,intronDS,tinypacbporf,intronAQ,intronAS) in return_list:
            if (intronDQ._distance + intronAQ._distance) > min_distance:
                pos2score.append( 0.0 )
            else:
                # calculate overall pssm score
                total_pssm = 0.0
                total_pssm += intronDQ.donor.pssm_score
                total_pssm += intronDQ.acceptor.pssm_score
                total_pssm += intronDS.donor.pssm_score
                total_pssm += intronDS.acceptor.pssm_score
                total_pssm += intronAQ.donor.pssm_score
                total_pssm += intronAQ.acceptor.pssm_score
                total_pssm += intronAS.donor.pssm_score
                total_pssm += intronAS.acceptor.pssm_score
                pos2score.append( total_pssm )
        # get highest score and linked tinyexon
        max_score = max(pos2score)
        return_list = [ return_list[pos2score.index(max_score)] ]

    ############################################################################
    # some printing in verbose mode
    if verbose and return_list:
        (intronDQ,intronDS,tinypacbporf,intronAQ,intronAS) = return_list[0]
        print "BEST MAPPED TINYEXON:"
        print tinypacbporf
        print tinypacbporf.query, intronDQ._distance, intronAQ._distance,
        print ( intronDQ.donor.pos, intronDQ.acceptor.pos ),
        print ( intronDS.donor.pos, intronDS.acceptor.pos ),
        print ( intronAQ.donor.pos, intronAQ.acceptor.pos ),
        print ( intronAS.donor.pos, intronAS.acceptor.pos )
    ############################################################################

    # return the result list
    return return_list
예제 #5
0
def _merge_pacbporfs_by_two_tinyexons(pacbporfD,
                                      pacbporfA,
                                      orfSetObject,
                                      queryorsbjct,
                                      verbose=False,
                                      **kwargs):
    """ """
    # edit **kwargs dictionary for some forced attributes
    _update_kwargs(kwargs, KWARGS_PROJECTED_TINYEXON)

    tinyexons = []
    sposD = pacbporfD._get_original_alignment_pos_start()
    eposD = pacbporfD._get_original_alignment_pos_end()
    sposA = pacbporfA._get_original_alignment_pos_start()
    eposA = pacbporfA._get_original_alignment_pos_end()
    if queryorsbjct == "query":
        donorOrf = pacbporfD.orfQ
        accepOrf = pacbporfA.orfQ
        prjctOrf = pacbporfD.orfS
        dStart, dEnd = sposD.query_dna_start, eposD.query_dna_end
        aStart, aEnd = sposA.query_dna_start, eposA.query_dna_end
    elif queryorsbjct == "sbjct":
        donorOrf = pacbporfD.orfS
        accepOrf = pacbporfA.orfS
        prjctOrf = pacbporfD.orfQ
        dStart, dEnd = sposD.sbjct_dna_start, eposD.sbjct_dna_end
        aStart, aEnd = sposA.sbjct_dna_start, eposA.sbjct_dna_end
    else:
        message = "'queryorsbjct' (%s), not 'query' or 'sbjct'" % queryorsbjct
        raise InproperlyAppliedArgument, message

    # get all potential combinations of two tinyexons
    tinyexoncombis = merge_orfs_with_two_tinyexons(
        donorOrf,
        accepOrf,
        donorOrf._donor_sites,
        accepOrf._acceptor_sites,
        orfSetObject.orfs,
    )

    results = []

    for dObj in donorOrf._donor_sites:
        if queryorsbjct == "query":
            (dPos, dPhase) = pacbporfD.dnaposition_query(dObj.pos,
                                                         forced_return=True)
        else:
            (dPos, dPhase) = pacbporfD.dnaposition_sbjct(dObj.pos,
                                                         forced_return=True)
        try:
            algDobj = pacbporfD._positions[dPos]
        except IndexError:
            # site out of range of PacbPORF -> break
            break

        # check if dObj is on pfD;
        # introns of tinyexons can be projected outside of pfD/pfA area
        if dObj.pos < dStart: continue

        for aObj in accepOrf._acceptor_sites:
            if queryorsbjct == "query":
                (aPos,
                 aPhase) = pacbporfA.dnaposition_query(aObj.pos,
                                                       forced_return=True)
            else:
                (aPos,
                 aPhase) = pacbporfA.dnaposition_sbjct(aObj.pos,
                                                       forced_return=True)
            try:
                algAobj = pacbporfA._positions[aPos]
            except IndexError:
                # site out of range of PacbPORF -> break
                break

            # check if aObj is on pfA;
            # introns of tinyexons can be projected outside of pfD/pfA area
            if aObj.pos > aEnd: continue

            if queryorsbjct == "query":
                posDsbjct = algDobj.sbjct_dna_start + dPhase
                posAsbjct = algAobj.sbjct_dna_start + aPhase
            else:
                posDsbjct = algDobj.query_dna_start + dPhase
                posAsbjct = algAobj.query_dna_start + aPhase
            distance = posAsbjct - posDsbjct
            if distance >= (kwargs['max_tinyexon_nt_length'] * 2):
                break
            if distance < (kwargs['min_tinyexon_nt_length'] * 2):
                continue

            filtered_tinyexoncombis = _filter_tinyexoncombis(
                tinyexoncombis,
                min_length=distance,
                max_length=distance,
                min_first_acceptor_pos=dObj.pos +
                kwargs['min_tinyexon_intron_nt_length'],
                max_final_donor_pos=aObj.pos -
                kwargs['min_tinyexon_intron_nt_length'],
                phase_final_donor=aObj.phase,
                phase_first_acceptor=dObj.phase,
            )

            if not filtered_tinyexoncombis: continue

            ####################################################################
            if verbose:
                print distance, dObj, aObj, len(tinyexoncombis),
                print len(filtered_tinyexoncombis)
            ####################################################################

            for exon1, intron, exon2 in filtered_tinyexoncombis:
                # make preceding intron
                preceding_intron = IntronConnectingOrfs(
                    dObj, exon1.acceptor, None, donorOrf, exon1.orf)

                # make subsequent intron
                subsequent_intron = IntronConnectingOrfs(
                    exon2.donor, aObj, None, exon2.orf, accepOrf)

                ################################################################
                if verbose:
                    print "\t", exon1, exon1.proteinsequence(),
                    print preceding_intron.phase, exon1.donor.phase,
                    print subsequent_intron.phase, preceding_intron.shared_aa,
                    print intron.shared_aa, subsequent_intron.shared_aa
                    print "\t", exon2, exon2.proteinsequence()
                ################################################################

                # get prjctOrf sequence for comparison
                correctionA = 0
                if aObj.phase != 0:
                    # INCLUDE the final AA which is broken by the splicesite
                    correctionA = 1
                if queryorsbjct == "query":
                    startPos, _phase = pacbporfD.dnaposition_query(
                        dObj.pos, forced_return=True)
                    stopPos, _phase = pacbporfA.dnaposition_query(
                        aObj.pos, forced_return=True)
                    start = pacbporfD._positions[startPos].sbjct_pos
                    stop = pacbporfA._positions[stopPos].sbjct_pos + correctionA
                else:
                    startPos, _phase = pacbporfD.dnaposition_sbjct(
                        dObj.pos, forced_return=True)
                    stopPos, _phase = pacbporfA.dnaposition_sbjct(
                        aObj.pos, forced_return=True)
                    start = pacbporfD._positions[startPos].query_pos
                    stop = pacbporfA._positions[stopPos].query_pos + correctionA

                if stop <= start:
                    # tinyexon is so tiny that is does not have a single
                    # full aligned AA -> discard here
                    continue

                # actually get the prjctOrf sequence
                aaseq = prjctOrf.getaas(abs_pos_start=start, abs_pos_end=stop)

                # initialize a PacbP for the combination of both tinyexons
                # afterwards, check if the indentityscore is > 0.XX
                from pacb import PacbP
                seqparts = [
                    preceding_intron.shared_aa,
                    exon1.proteinsequence(), intron.shared_aa,
                    exon2.proteinsequence(), subsequent_intron.shared_aa
                ]

                ################################################################
                if verbose or len("".join(seqparts)) != len(aaseq):
                    print pacbporfD
                    print exon1.orf, exon2.orf, prjctOrf
                    print pacbporfA
                    print seqparts
                    print aaseq, len(aaseq), len("".join(seqparts)), (start,
                                                                      stop)
                    print "'%s'" % queryorsbjct,
                    print "Q", (algDobj.query_pos, algAobj.query_pos),
                    print "S", (algDobj.sbjct_pos, algAobj.sbjct_pos)
                    print "distance:", distance, kwargs[
                        'max_tinyexon_nt_length'],
                    print(posDsbjct, posAsbjct),
                    print "Q-dna:", (algDobj.query_dna_start, dPhase,
                                     algAobj.query_dna_start, aPhase),
                    print "S-dna:", (algDobj.sbjct_dna_start, dPhase,
                                     algAobj.sbjct_dna_start, aPhase)
                ################################################################

                # ignore by continue when sequences not identical in length
                if len("".join(seqparts)) != len(aaseq): continue

                testpacbp = PacbP(input=("".join(seqparts), aaseq, 0, 0))
                testpacbp.strip_unmatched_ends()

                if not ( testpacbp.identityscore > 0.60 and\
                (float(testpacbp.length) / len(aaseq)) > 0.70 ):
                    # not a very convincing alignment
                    continue

                ################################################################
                if verbose:
                    print testpacbp
                    testpacbp.print_protein()
                ################################################################

                # if here, succesfully mapped 2 tiny exons!!
                # get all sequences/coordinates in place for
                # pacbporf formation
                orfQ1 = exon1.orf
                orfS1 = prjctOrf
                orfQ2 = exon2.orf
                orfS2 = prjctOrf
                seqQ1 = exon1.proteinsequence()
                seqQ2 = exon2.proteinsequence()
                coordQ1 = exon1.acceptor.pos / 3
                coordS1 = start
                coordQ2 = exon2.acceptor.pos / 3
                coordS2 = start + len(seqparts[0]) + len(seqparts[1]) + len(
                    seqparts[2])
                seqS1 = aaseq[0:(len(seqparts[0]) + len(seqparts[1]))]
                seqS2 = aaseq[-(len(seqparts[3]) + len(seqparts[4])):]
                if len(seqparts[0]):
                    seqS1 = seqS1[1:]
                    coordS1 += 1
                if len(seqparts[4]):
                    seqS2 = seqS2[:-1]

                if queryorsbjct == "sbjct":
                    # swap query <-> sbjct
                    orfQ1, orfS1 = orfS1, orfQ1
                    orfQ2, orfS2 = orfS2, orfQ2
                    seqQ1, seqS1 = seqS1, seqQ1
                    seqQ2, seqS2 = seqS2, seqQ2
                    coordQ1, coordS1 = coordS1, coordQ1
                    coordQ2, coordS2 = coordS2, coordQ2

                ################################################################
                if verbose:
                    print "tinypacbporf1:", seqQ1, seqQ2, coordQ1, coordQ2
                    print "tinypacbporf2:", seqS1, seqS2, coordS1, coordS2
                ################################################################

                # make pacbporfs
                pacbp1 = PacbP(input=(seqQ1, seqS1, coordQ1, coordS1))
                pacbp1.strip_unmatched_ends()
                tinypacbporf1 = pacbp2pacbporf(pacbp1, orfQ1, orfS1)
                tinypacbporf1.extend_pacbporf_after_stops()
                pacbp2 = PacbP(input=(seqQ2, seqS2, coordQ2, coordS2))
                pacbp2.strip_unmatched_ends()
                tinypacbporf2 = pacbp2pacbporf(pacbp2, orfQ2, orfS2)
                tinypacbporf2.extend_pacbporf_after_stops()

                ################################################################
                if verbose:
                    print tinypacbporf1
                    tinypacbporf1.print_protein_and_dna()
                    print tinypacbporf2
                    tinypacbporf2.print_protein_and_dna()
                ################################################################

                ################################################################
                # set some meta-data properties to the intron objects
                ################################################################
                # add distance score to intron
                preceding_intron._distance = 0
                intron._distance = 0
                subsequent_intron._distance = 0

                # add Alignment Positional Periphery Score into objects
                if queryorsbjct == "query":
                    succes = set_apps_intron_query(preceding_intron, pacbporfD,
                                                   tinypacbporf1)
                    succes = set_apps_intron_query(intron, tinypacbporf1,
                                                   tinypacbporf2)
                    succes = set_apps_intron_query(subsequent_intron,
                                                   tinypacbporf2, pacbporfA)
                else:
                    succes = set_apps_intron_sbjct(preceding_intron, pacbporfD,
                                                   tinypacbporf1)
                    succes = set_apps_intron_sbjct(intron, tinypacbporf1,
                                                   tinypacbporf2)
                    succes = set_apps_intron_sbjct(subsequent_intron,
                                                   tinypacbporf2, pacbporfA)

                # set GFF fsource attribute for recognition of intron sources
                preceding_intron._gff['fsource'] = "ABGPprojectingTE"
                intron._gff['fsource'] = "ABGPprojectingTE"
                subsequent_intron._gff['fsource'] = "ABGPprojectingTE"

                # create _linked_to_xxx attributes
                preceding_intron._linked_to_pacbporfs = [
                    tinypacbporf1, tinypacbporf2
                ]
                intron._linked_to_pacbporfs = [tinypacbporf1, tinypacbporf2]
                subsequent_intron._linked_to_pacbporfs = [
                    tinypacbporf1, tinypacbporf2
                ]
                preceding_intron._linked_to_introns = [
                    intron, subsequent_intron
                ]
                intron._linked_to_introns = [
                    preceding_intron, subsequent_intron
                ]
                subsequent_intron._linked_to_introns = [
                    intron, preceding_intron
                ]

                ################################################################
                # append to results
                ################################################################
                results.append((
                    preceding_intron,
                    intron,
                    subsequent_intron,
                    tinypacbporf1,
                    tinypacbporf2,
                ))

    # return 3 introns and 2 intermediate tinyexon PacbPORFs (per row)
    return results
예제 #6
0
def _merge_pacbporfs_by_tinyexon_and_two_introns(pacbporfD,
                                                 pacbporfA,
                                                 orfSetObject,
                                                 queryorsbjct,
                                                 verbose=False,
                                                 **kwargs):
    """
    Merge 2 PacbPORF objects by introns

    @attention: see pacb.connecting.merge_orfs_with_intron for **kwargs)

    @type  pacbporfD: PacbPORF object
    @param pacbporfD: PacbPORF object that has to deliver PSSM donor objects

    @type  pacbporfA: PacbPORF object
    @param pacbporfA: PacbPORF object that has to deliver PSSM acceptor objects

    @type  orfSetObject: object with elegiable Orfs
    @param orfSetObject: object with elegiable Orfs

    @type  queryorsbjct: string
    @param queryorsbjct: literal string 'query' or 'sbjct'

    @type  verbose: Boolean
    @param verbose: print debugging info to STDOUT when True

    @rtype:  list
    @return: list with ( intron, ExonOnOrf, intron ) on the query sequence
    """
    # input validation
    IsPacbPORF(pacbporfD)
    IsPacbPORF(pacbporfA)

    # edit **kwargs dictionary for some forced attributes
    _update_kwargs(kwargs, KWARGS_PROJECTED_TINYEXON)

    MAX_TINYEXON_NT_LENGTH = 33
    MIN_TINYEXON_NT_LENGTH = 6

    tinyexons = []
    if queryorsbjct == "query":
        donorOrf = pacbporfD.orfQ
        accepOrf = pacbporfA.orfQ
        prjctOrf = pacbporfD.orfS
        alignedDonorRange = pacbporfD.alignment_dna_range_query()
        alignedAccepRange = pacbporfA.alignment_dna_range_query()
    elif queryorsbjct == "sbjct":
        donorOrf = pacbporfD.orfS
        accepOrf = pacbporfA.orfS
        prjctOrf = pacbporfD.orfQ
        alignedDonorRange = pacbporfD.alignment_dna_range_sbjct()
        alignedAccepRange = pacbporfA.alignment_dna_range_sbjct()
    else:
        message = "'queryorsbjct' (%s), not 'query' or 'sbjct'" % queryorsbjct
        raise InproperlyAppliedArgument, message

    for dObj in donorOrf._donor_sites:
        # do not make a projection OVER the aligned area
        if dObj.pos < min(alignedDonorRange): continue
        if queryorsbjct == "query":
            (dPos, dPhase) = pacbporfD.dnaposition_query(dObj.pos,
                                                         forced_return=True)
        else:
            (dPos, dPhase) = pacbporfD.dnaposition_sbjct(dObj.pos,
                                                         forced_return=True)
        try:
            algDobj = pacbporfD._positions[dPos]
        except IndexError:
            # site out of range of PacbPORF -> break
            break
        for aObj in accepOrf._acceptor_sites:
            # do not make a projection OVER the aligned area
            if aObj.pos > max(alignedAccepRange): continue
            if queryorsbjct == "query":
                (aPos,
                 aPhase) = pacbporfA.dnaposition_query(aObj.pos,
                                                       forced_return=True)
            else:
                (aPos,
                 aPhase) = pacbporfA.dnaposition_sbjct(aObj.pos,
                                                       forced_return=True)
            try:
                algAobj = pacbporfA._positions[aPos]
            except IndexError:
                # site out of range of PacbPORF -> break
                break
            if queryorsbjct == "query":
                posDsbjct = algDobj.sbjct_dna_start + dPhase
                posAsbjct = algAobj.sbjct_dna_start + aPhase
            else:
                posDsbjct = algDobj.query_dna_start + dPhase
                posAsbjct = algAobj.query_dna_start + aPhase
            distance = posAsbjct - posDsbjct
            if distance >= MAX_TINYEXON_NT_LENGTH:
                break
            if distance < MIN_TINYEXON_NT_LENGTH:
                continue

            ####################################################
            # generate a ScanForMatches pattern file
            ####################################################
            # example pattern: 6...6 AG NNGNNANNANNGN[2,0,0] GT 3...3
            query = list(prjctOrf.inputgenomicsequence[posDsbjct:posAsbjct])
            # mask all non-phase0 nucleotides to N residues;
            # this represents the regularexpression for a specific
            # peptide sequence
            firstphasepositions = range(3 - dPhase % 3, len(query), 3)
            for pos in range(0, len(query)):
                if pos not in firstphasepositions:
                    query[pos] = "N"
            # calculate a ~50% mismatch number
            mismatches = max([0, (len(query) - query.count("N")) / 2])
            # write the pattern to string and subsequently to file
            # example pattern: 6...6 AG NNGNNANNANNGN[2,0,0] GT 3...3
            if kwargs['allow_non_canonical_donor']:
                sfmpat = "%s...%s AG %s[%s,0,0] G (T | C) %s...%s" % (
                    AUSO, AUSO, "".join(query), mismatches, DDSO, DDSO)
            else:
                sfmpat = "%s...%s AG %s[%s,0,0] GT %s...%s" % (
                    AUSO, AUSO, "".join(query), mismatches, DDSO, DDSO)

            ####################################################
            if verbose:
                print(pacbporfD.orfQ.id, pacbporfA.orfQ.id),
                print distance, dObj, aObj
                print sfmpat
            ####################################################

            fname = "sfmpat_tinyexon_%s_%s_%s_%s" % (
                donorOrf.id,
                accepOrf.id,
                posDsbjct,
                posAsbjct,
            )
            fh = open(fname, 'w')
            fh.write(sfmpat + "\n")
            fh.close()

            ####################################################
            # run ScanForMatches
            ####################################################
            command = """echo ">myseq\n%s" | %s %s | tr "[,]" "\t\t#" | """ +\
                      """tr -d "\n " | sed "s/>/\\n>/g" | tr "#" "\t" | """ +\
                      """awk -F'\t' '{ if (NF==4 && $2>%s && $3<%s) """ +\
                      """{ print $1"["$2","$3"]\\n"$4 } }' """
            command = command % (donorOrf.inputgenomicsequence, EXECUTABLE_SFM,
                                 fname, dObj.pos +
                                 (kwargs['min_intron_nt_length'] - 3),
                                 aObj.pos -
                                 (kwargs['min_intron_nt_length'] - 3))
            co = osPopen(command)
            matches = parseFasta(co.readlines())
            co.close()

            # filter matches for:
            # (1) correct donor & acceptor phase
            # (2) high enough donor & acceptor site scores
            for hdr, seqmatch in matches.iteritems():
                startQ, stopQ = [
                    int(item) for item in hdr.split(":")[1][1:-1].split(",")
                ]
                exonQstart = startQ + AUSO + 2 - 1
                exonQstop = stopQ - DDSO - 2

                ####################################
                # get Orf object of tinyexon
                ####################################
                tinyexonorf = None
                # select the Orf on which the tinyexon is located
                for orfObj in orfSetObject.get_eligible_orfs(
                        max_orf_start=exonQstart, min_orf_end=exonQstop):
                    orfPhase = (exonQstart - orfObj.startPY) % 3
                    if orfPhase == dPhase:
                        tinyexonorf = orfObj
                        break
                else:
                    # No tinyexonorf assigned!! Iin case a regex matched
                    # over a STOP-codon or the regex length is smaller
                    # then the smallest Orf, no Orf can be assigned
                    continue

                # filter for donor & acceptor score
                dScore = _score_splice_site(seqmatch[-9:], splicetype='donor')
                aScore = _score_splice_site(seqmatch[0:11],
                                            splicetype='acceptor')
                if dScore < kwargs['min_donor_pssm_score']:
                    continue
                if aScore < kwargs['min_acceptor_pssm_score']:
                    continue

                # scan Orf for splicesites
                tinyexonorf.scan_orf_for_pssm_splice_sites(
                    splicetype="donor",
                    min_pssm_score=kwargs['min_donor_pssm_score'],
                    allow_non_canonical=kwargs['allow_non_canonical_donor'],
                    non_canonical_min_pssm_score=kwargs[
                        'non_canonical_min_donor_pssm_score'])
                tinyexonorf.scan_orf_for_pssm_splice_sites(
                    splicetype="acceptor",
                    min_pssm_score=kwargs['min_acceptor_pssm_score'],
                    allow_non_canonical=kwargs['allow_non_canonical_acceptor'],
                    non_canonical_min_pssm_score=kwargs[
                        'non_canonical_min_acceptor_pssm_score'])

                # get 1th intron donor object
                intron1_aObj = None
                for a in tinyexonorf._acceptor_sites:
                    if a.pos == exonQstart:
                        intron1_aObj = a
                        break
                else:
                    # pseudo-acceptorsite as found be SFM regex
                    # is not a valid acceptor site of high enough score
                    # continue to next iteration of (hdr,seqmatch) pair
                    continue

                # get 2th intron donor object
                intron2_dObj = None
                for d in tinyexonorf._donor_sites:
                    if d.pos == exonQstop:
                        intron2_dObj = d
                        break
                else:
                    # pseudo-donorsite as found be SFM regex
                    # is not a valid acceptor site of high enough score
                    # continue to next iteration of (hdr,seqmatch) pair
                    continue

                # check if introns are of elegiable lengths
                if (intron1_aObj.pos -
                        dObj.pos) > kwargs['max_intron_nt_length']:
                    continue
                if (aObj.pos -
                        intron2_dObj.pos) > kwargs['max_intron_nt_length']:
                    continue

                ####################################################
                if True or verbose:
                    # if here, a candidate!!!
                    print(pacbporfD.orfQ.id, tinyexonorf.id,
                          pacbporfA.orfQ.id),
                    print hdr, dScore, aScore
                    print seqmatch
                ####################################################

                # append to found tinyexons
                query_data = (tinyexonorf, exonQstart, exonQstop)
                sbjct_data = (prjctOrf, posDsbjct, posAsbjct)
                splicesite_data = (dObj, intron1_aObj, intron2_dObj, aObj)
                tinyexons.append((query_data, sbjct_data, splicesite_data))

            # file cleanup
            osRemove(fname)

    # return - End Of Function - if no tinyexons are found
    if not tinyexons:
        return []

    ####################################
    # select the **best** tinyexon
    ####################################
    (query_data, sbjct_data, splicesite_data) = tinyexons[0]
    orfQ, query_dna_start, query_dna_end = query_data
    orfS, sbjct_dna_start, sbjct_dna_end = sbjct_data
    (intron1_dObj, intron1_aObj, intron2_dObj, intron2_aObj) = splicesite_data

    ####################################################
    if verbose:
        print "tinyexon orf:", orfQ
        print "tinyexon orf:", intron1_aObj
        print "tinyexon orf:", intron2_dObj
    ####################################################

    ####################################
    # make tinyexon PacbPORF
    ####################################
    startQaa = orfQ.dnapos2aapos(query_dna_start) - 1
    startSaa = orfS.dnapos2aapos(sbjct_dna_start) - 1
    stopQaa = orfQ.dnapos2aapos(query_dna_end) + 1
    stopSaa = orfS.dnapos2aapos(sbjct_dna_end) + 1
    # check for directly leading stop codon on tinyexon
    while startQaa <= orfQ.protein_startPY:
        startQaa += 1
        startSaa += 1
        query_dna_start += 3
        sbjct_dna_start += 3
    while startSaa <= orfS.protein_startPY:
        startQaa += 1
        startSaa += 1
        query_dna_start += 3
        sbjct_dna_start += 3
    # check for directly tailing stop codon on tinyexon
    while stopQaa > orfQ.protein_endPY:
        stopQaa -= 1
        stopSaa -= 1
        query_dna_end -= 3
        sbjct_dna_end -= 3
    while stopSaa > orfS.protein_endPY:
        stopQaa -= 1
        stopSaa -= 1
        query_dna_end -= 3
        sbjct_dna_end -= 3
    # get sequences
    qAAseq = orfQ.getaas(abs_pos_start=startQaa, abs_pos_end=stopQaa)
    sAAseq = orfS.getaas(abs_pos_start=startSaa, abs_pos_end=stopSaa)

    ####################################################
    if verbose or len(qAAseq) != len(sAAseq):
        # if unequal lengths, error will be raised upon PacbP.__init__()
        print orfQ, qAAseq, startQaa, stopQaa, (stopQaa - startQaa),
        print(query_dna_start, query_dna_end)
        print orfS, sAAseq, startSaa, stopSaa, (stopSaa - startSaa),
        print(sbjct_dna_start, sbjct_dna_end)
        print orfQ.inputgenomicsequence[query_dna_start - 2:query_dna_end + 2]
        print orfS.inputgenomicsequence[sbjct_dna_start - 2:sbjct_dna_end + 2]
    ####################################################

    # initialize extended tinyexon PacbPORF
    from pacb import PacbP
    pacbp = PacbP(input=(qAAseq, sAAseq, startQaa, startSaa))
    pacbp.strip_unmatched_ends()
    pacbporf = pacbp2pacbporf(pacbp, orfQ, orfS)
    pacbporf.extend_pacbporf_after_stops()
    pacbporf.source = 'ABGPprojectingTE'

    ####################################
    # make introns
    ####################################
    intron1 = IntronConnectingOrfs(intron1_dObj, intron1_aObj, None, donorOrf,
                                   pacbporf.orfQ)
    intron2 = IntronConnectingOrfs(intron2_dObj, intron2_aObj, None,
                                   pacbporf.orfQ, accepOrf)

    ################################################################
    # set some meta-data properties to the intron objects
    ################################################################
    # add distance score to intron
    intron1._distance = 0
    intron2._distance = 0

    # add Alignment Positional Periphery Score into objects
    if queryorsbjct == "query":
        succes = set_apps_intron_query(intron1, pacbporfD, pacbporf)
        succes = set_apps_intron_query(intron2, pacbporf, pacbporfA)
    else:
        succes = set_apps_intron_sbjct(intron1, pacbporfD, pacbporf)
        succes = set_apps_intron_sbjct(intron2, pacbporf, pacbporfA)

    # set GFF fsource attribute for recognition of intron sources
    intron1._gff['fsource'] = "ABGPprojectingTE"
    intron2._gff['fsource'] = "ABGPprojectingTE"

    # create _linked_to_xxx attributes
    intron1._linked_to_pacbporfs = [pacbporf]
    intron2._linked_to_pacbporfs = [pacbporf]
    intron1._linked_to_introns = [intron2]
    intron2._linked_to_introns = [intron1]

    ####################################################
    if verbose:
        print pacbporf
        pacbporf.print_protein_and_dna()
        print intron1
        print intron2
        if False:
            # printing data when this function needs to be debugged:
            print ""
            print intron1
            print intron2
            print ""
            print pacbporfD
            pacbporfD.print_protein_and_dna()
            print ""
            print pacbporf
            pacbporf.print_protein_and_dna()
            print ""
            print pacbporfA
            pacbporfA.print_protein_and_dna()
            import sys
            sys.exit()
    ####################################################

    # return introns and intermediate tinyexon PacbPORF
    return [(intron1, intron2, pacbporf)]
예제 #7
0
def hmmhit2pacbp(queryorf,queryorg,querycoords,sbjctorf,sbjctorg,hmmhit,verbose=False):
    """
    """
    # trim hmmhit for unmatched characters
    ( sbjct_header, sbjct_start, sbjct_end,
      query_start, query_end,
      query, match, sbjct, score, expect ) = hmmhit

    while match and match[0] == ' ':
        query = query[1:]
        match = match[1:]
        sbjct = sbjct[1:]
        sbjct_start+=1
        query_start+=1
    while match and match[-1] == ' ':
        query = query[0:-1]
        match = match[0:-1]
        sbjct = sbjct[0:-1]
        sbjct_end-=1
        query_end-=1

    # get orf, node and AA and DNA coordinates of this sbjct hit;
    # correct for -1 offset in start coordinate!!
    sbjct_aa_start  = sbjct_start - 1 + sbjctorf.protein_startPY
    sbjct_aa_end    = sbjct_end + sbjctorf.protein_startPY
    sbjctNode       = (sbjctorg,sbjctorf.id)
    query           = query.replace(".","-").upper()
    sbjct           = sbjct.replace(".","-").upper()

    ############################################################################
    if verbose:
        print "hmmhit2pacbp CREATING pacbps for organism/orf: (%s,%s)" % (
                sbjctorg,sbjctorf.id)
        print "hmmhit2pacbp Q '%s'" % query
        print "hmmhit2pacbp m '%s'" % match
        print "hmmhit2pacbp S '%s'" % sbjct
        print "hmmQ:", query, query_start, query_end, "gaps:",
        print query.count('-'), len(query)
        print "hmmM:", match
        print "hmmS:", sbjct, sbjctNode, sbjct_aa_start, sbjct_aa_end,
        print "len:", sbjct_aa_end-sbjct_aa_start , len(sbjct)
    ############################################################################

    # get Node and sequence of the query
    queryNode = (queryorg,queryorf.id)
    queryseq  = deepcopy(query)

    # calculate query sequence position on queryorf
    query_aa_start = querycoords[0] + query_start - 1
    query_aa_end   = query_aa_start + len(queryseq) - queryseq.count('-')

    ############################################################################
    if verbose:
        print "hmmq:", queryseq, queryNode, query_aa_start, query_aa_end,
        print "len:", query_aa_end-query_aa_start, len(queryseq)
    ############################################################################

    # make a deepcopy; sbjct is needed unchanged for the next iteration
    # in the for loop, but here we want to trim of gap sequences
    sbjctseq = deepcopy(sbjct)
    sbjctaastart = deepcopy(sbjct_aa_start)
    sbjctaaend   = deepcopy(sbjct_aa_end)
    while queryseq and queryseq[0] == '-':
        queryseq = queryseq[1:]
        sbjctseq = sbjctseq[1:]
        sbjctaastart+=1
    while sbjctseq and sbjctseq[0] == '-':
        queryseq = queryseq[1:]
        sbjctseq = sbjctseq[1:]
        query_aa_start+=1
    while queryseq and queryseq[-1] == '-':
        queryseq = queryseq[0:-1]
        sbjctseq = sbjctseq[0:-1]
        sbjctaaend-=1
    while sbjctseq and sbjctseq[-1] == '-':
        queryseq = queryseq[0:-1]
        sbjctseq = sbjctseq[0:-1]
        query_aa_end-=1

    # NEW NEW code in december 2010. Since inwpCBGs are implemented, HMM
    # profiles are build from clustalw alignments which have loosely aligned
    # tails (SPRDIF sequences). Problem with HMM is, that in the result file
    # no information is written on where in teh constructed HMM this hit
    # starts. This **sucks** because special care was taken in ABFGP code to
    # make shure the exact aa-coordinates of the applied sequences to ClustalW
    # are known. Hmmbuild here nullifies this effort by not giving start
    # coordinates. Therefore, we have to check the exact start position
    # of the HMM match on the queryorf.
    if queryseq.replace("-","") != queryorf.getaas(query_aa_start,query_aa_end):
        # obtain (search) query sequence, replace gaps by X symbol
        searchqueryseq = queryseq.upper().replace("-","X")
        # count length of the query sequence; here IGNORE THE GAPS!!
        seqlen = len(queryseq.upper().replace("-",""))

        # make fasta sequence dictionary
        seqdict = {
            'query_hmm': searchqueryseq,
            'query_orf': queryorf.protein_sequence,
            }

        # make coords dictionary for remapping
        coords = {
            'query_hmm':[0,seqlen],
            'query_orf':[queryorf.protein_startPY,queryorf.protein_endPY],
            }

        # perform clustalw multiple alignment
        (alignedseqs,alignment) = clustalw( seqs= seqdict )
        # strip exterior gaps
        alignedseqs,alignment,coords = strip_alignment_for_exterior_gaps(
            deepcopy(alignedseqs),deepcopy(alignment),deepcopy(coords) )

        if alignedseqs['query_hmm'].count("-") > 0:
            # in (very) exceptional cases, gaps can be introduced in the
            # clustalw alignment in the HMM seq. This normally does not
            # occur! Fix this here by placing gaps in sbjctseq too.
            sbjctseq_as_list = list(sbjctseq)
            for pos in range(0,len(alignedseqs['query_hmm'])):
                if alignedseqs['query_hmm'][pos] == "-":
                    sbjctseq_as_list.insert(pos,"-")
                if alignedseqs['query_hmm'].find("-",pos) == -1:
                    break
            sbjctseq = "".join(sbjctseq_as_list)

        ########################################################################
        if verbose:
            print "\t", "FALSE::", sbjctseq, "[ WITH GAPS,SBJCT ]" 
            print "\t", "FALSE::", queryseq, "[ WITH GAPS ]" 
            for k,algseq in alignedseqs.iteritems():
                print "\t", "FALSE::", algseq, k, coords[k], len(algseq)
            print "\t", "FALSE::", sbjctseq, "SBJCT", len(sbjctseq)
            print "\t", "FALSE::", alignment, "ALMNT", len(alignment)
            print "\t", "SOLVED:", len(alignedseqs['query_orf']) == len(sbjctseq)
        ########################################################################
    
        # update query sequence & coordinates
        if len(alignedseqs['query_orf']) == len(sbjctseq):
            queryseq       = alignedseqs['query_orf']
            query_aa_start = coords['query_orf'][0]
            query_aa_end   = coords['query_orf'][1]
        else:
            # still not identical lengths. ClustalW recovery of HMM hit
            # failed miserably. For now: omit
            # TODO: resolve this case!!
            # example: --filewithloci examples/bilal/CFU_830450.bothss.csv
            # ## HMM clustalw input profile: False MAXSR True
            # FPKGCESGKFINWKTFKANGVNLGAWLAKEKTHDPVW foxga [561, 598]
            # FQRACR--KFID-ETLSAHAL---EWESKEIVPPEVW CFU [357, 388]
            # hmmhit2pacbp CREATING pacbps for organism/orf: (NP1064101[anid],1)
            # hmmhit2pacbp Q 'FQKACRSGKFIDWKTLKANALNLGEWLAKEKVHD'
            # hmmhit2pacbp m '+ ka +   F  W   k  + nLG Wl  E   d'
            # hmmhit2pacbp S 'YTKAFQ--PF-SWSSAKVRGANLGGWLVQEASID'
            # hmmQ: FQKACRSGKFIDWKTLKANALNLGEWLAKEKVHD 1 34 gaps: 0 34
            # hmmM: + ka +   F  W   k  + nLG Wl  E   d
            # hmmS: YTKAFQ--PF-SWSSAKVRGANLGGWLVQEASID ('NP1064101[anid]', 1) 33 64 len: 31 34
            # hmmq: FQKACRSGKFIDWKTLKANALNLGEWLAKEKVHD ('CFU', 91) 357 391 len: 34 34
            #         FALSE:: YTKAFQ---------PF-SWSS-----------------AKVR----------GANLGG--W-LVQEASID [ WITH GAPS,SBJCT ]
            #         FALSE:: FQKACRSGKFIDWKTLKANALNLGEWLAKEKVHD [ WITH GAPS ]
            #         FALSE:: FQKACR-------SGKFIDWKT-----------------LKAN----------ALNLGE--W-LAKEKVH query_hmm [0, 33] 70
            #         FALSE:: FQRACRKFIDETLSAHALEWESKEIVPPEVWQRFAEANMLIPNLAALASRMVGEIGIGNAFWRLSVQGLR query_orf [357, 427] 70
            #         FALSE:: YTKAFQ---------PF-SWSS-----------------AKVR----------GANLGG--W-LVQEASID SBJCT 71
            #         FALSE:: **:***       *.: ::*::                 * .*           :.:*:  * *: : :: ALMNT 70
            #         SOLVED: False
            # Pacbp creation failed!
            return False, None

    if queryseq and sbjctseq:
        ################################################################
        if len(queryseq) != len(sbjctseq):
            # this will result in a exception to be raised:
            # pacb.exceptions.InproperlyAppliedArgument
            # print data here about what went wrong, then
            # just let the error be raised
            print queryseq, len(queryseq), sbjctseq, len(sbjctseq)
            print hmmhit
            print "Q:", query_aa_start, query_aa_end,
            print query_aa_end - query_aa_start, "len:", len(queryseq)
            print "S:", sbjctaastart, sbjctaaend,
            print sbjctaaend - sbjctaastart, "len:",len(sbjctseq)
        ################################################################
        pacbpinput = (queryseq,sbjctseq,query_aa_start,sbjctaastart)
        pacbp      = PacbP(input=pacbpinput)
        # remove consistent internal gaps caused hy HMM profile search
        pacbp.strip_consistent_internal_gaps()
        pacbp.source = 'hmmsearch'
        pacbporf   = PacbPORF(pacbp,queryorf,sbjctorf)
        pacbporf.strip_unmatched_ends()
        if pacbporf.length==0:
            # Pacbp creation failed!
            return False, None
        else:
            pacbporf.extend_pacbporf_after_stops()
            pacbpkey = pacbporf.construct_unique_key(queryNode,sbjctNode)
            # return unique key and pacbporf
            return (pacbpkey,queryNode,sbjctNode), pacbporf
    else:
        # Pacbp creation failed!
        return False, None
예제 #8
0
파일: mapping.py 프로젝트: IanReid/ABFGP
def merge_pacbporfs_by_tinyexons(pacbporfD,
                                 pacbporfA,
                                 orfSetObjQ,
                                 orfSetObjS,
                                 verbose=False,
                                 **kwargs):
    """ """
    # input validation
    IsPacbPORF(pacbporfD)
    IsPacbPORF(pacbporfA)

    # edit **kwargs dictionary for some forced attributes
    _update_kwargs(kwargs, KWARGS_MAPPED_INTRON)
    if not kwargs.has_key('aligned_site_max_triplet_distance'):
        kwargs['aligned_site_max_triplet_distance'] = kwargs['max_aa_offset']

    # settings for minimal alignment entropy score
    min_donor_site_alignment_entropy = 0.0
    min_acceptor_site_alignment_entropy = 0.0

    resultlistQ = merge_orfs_with_tinyexon(
        pacbporfD.orfQ,
        pacbporfA.orfQ,
        preceding_donor_sites=pacbporfD.orfQ._donor_sites,
        subsequent_acceptor_sites=pacbporfA.orfQ._acceptor_sites,
        orflist=orfSetObjQ.orfs,
        **kwargs)
    resultlistS = merge_orfs_with_tinyexon(
        pacbporfD.orfS,
        pacbporfA.orfS,
        preceding_donor_sites=pacbporfD.orfS._donor_sites,
        subsequent_acceptor_sites=pacbporfA.orfS._acceptor_sites,
        orflist=orfSetObjS.orfs,
        **kwargs)

    # translate resultlists to dict: key == exon, value = [ {intronsD},{intronsS} ]
    resultdictQ, key2exonQ = _tinyexon_list_2_dict(resultlistQ)
    resultdictS, key2exonS = _tinyexon_list_2_dict(resultlistS)

    # get unique list of donors & acceptors
    donorQ = olba(list(Set([inD.donor for inD, te, inA in resultlistQ])),
                  order_by='pos')
    donorS = olba(list(Set([inD.donor for inD, te, inA in resultlistS])),
                  order_by='pos')
    accepQ = olba(list(Set([inA.acceptor for inD, te, inA in resultlistQ])),
                  order_by='pos')
    accepS = olba(list(Set([inA.acceptor for inD, te, inA in resultlistS])),
                  order_by='pos')

    ## filter for alignable donor & acceptor sites
    kwargs['allow_non_canonical'] = True  # True
    kwargs['aligned_site_max_triplet_distance'] = 0  # 2
    algdonors = _filter_for_alignable_splice_sites(donorQ, donorS, pacbporfD,
                                                   **kwargs)
    algacceps = _filter_for_alignable_splice_sites(accepQ, accepS, pacbporfA,
                                                   **kwargs)

    # settings for minimal alignment entropy score
    # TODO TODO -> THIS MUST BE FIXED TO A NICE THRESHOLD VALUE!!!
    min_donor_site_alignment_entropy = 0.1
    min_acceptor_site_alignment_entropy = 0.1

    # remove sites with to low alignment entropy
    algdonors = _filter_for_entropy(
        algdonors,
        pacbporfD,
        'donor',
        min_alignment_entropy=min_donor_site_alignment_entropy)
    algacceps = _filter_for_entropy(
        algacceps,
        pacbporfA,
        'acceptor',
        min_alignment_entropy=min_acceptor_site_alignment_entropy)

    # return list: intronQD,intronSD,tinyexon,intronAQ,intronAS
    return_list = []

    ############################################################################
    if verbose:
        print "bridges constructed: ORFS:",
        print(pacbporfD.orfQ.id, pacbporfA.orfQ.id),
        print(pacbporfD.orfS.id, pacbporfA.orfS.id),
        print len(resultdictQ), len(resultdictS),
        print(len(resultlistQ), len(donorQ), len(accepQ)),
        print(len(resultlistS), len(donorS), len(accepS)),
        print(len(algdonors), len(algacceps))
    ############################################################################

    for keyQ, tinyexonQ in key2exonQ.iteritems():
        for keyS, tinyexonS in key2exonS.iteritems():
            if tinyexonQ.donor.phase != tinyexonS.donor.phase:
                continue
            if tinyexonQ.acceptor.phase != tinyexonS.acceptor.phase:
                continue
            if tinyexonQ.length != tinyexonS.length:
                continue
            # if here, then tinyexons of identical structure

            ####################################################################
            if verbose:
                print tinyexonQ.length, tinyexonQ.donor.phase,
                print(len(resultdictQ[keyQ][0]), len(resultdictQ[keyQ][1])),
                print(len(resultdictS[keyS][0]), len(resultdictS[keyS][1])),
                print tinyexonQ,
                print tinyexonQ.proteinsequence(), tinyexonS.proteinsequence(),
                print tinyexonS.acceptor.pssm_score + tinyexonS.donor.pssm_score
            ####################################################################

            donor_introns = []
            acceptor_introns = []
            for intronDQkey, intronDQ in resultdictQ[keyQ][0].iteritems():
                if intronDQ.donor.pos not in [dQ.pos for dQ, dS in algdonors]:
                    continue
                for intronDSkey, intronDS in resultdictS[keyS][0].iteritems():
                    if intronDS.donor.pos not in [
                            dS.pos for dQ, dS in algdonors
                    ]:
                        continue
                    # check if they exists as aligned sites
                    alignedkey = (intronDQ.donor.pos, intronDS.donor.pos)
                    if alignedkey not in [(dQ.pos, dS.pos)
                                          for dQ, dS in algdonors]:
                        continue
                    # if here, we have a set of introns 5' of the tinyexon
                    # which are perfectly alignable!
                    donor_introns.append((intronDQ, intronDS))

            for intronAQkey, intronAQ in resultdictQ[keyQ][1].iteritems():
                if intronAQ.acceptor.pos not in [
                        aQ.pos for aQ, aS in algacceps
                ]:
                    continue
                for intronASkey, intronAS in resultdictS[keyS][1].iteritems():
                    if intronAS.acceptor.pos not in [
                            aS.pos for aQ, aS in algacceps
                    ]:
                        continue
                    # check if they exists as aligned sites
                    alignedkey = (intronAQ.acceptor.pos, intronAS.acceptor.pos)
                    if alignedkey not in [(aQ.pos, aS.pos)
                                          for aQ, aS in algacceps]:
                        continue
                    # if here, we have a set of introns 3' of the tinyexon
                    # which are perfectly alignable!
                    acceptor_introns.append((intronAQ, intronAS))

            if not len(donor_introns) or not len(acceptor_introns):
                # no aligned 5' && aligned 3' introns
                continue

            # initialize extended tinyexon PacbPORF
            from pacb import PacbP
            pacbp = PacbP(input=(
                tinyexonQ.proteinsequence(),
                tinyexonS.proteinsequence(),
                tinyexonQ.protein_start(),
                tinyexonS.protein_start(),
            ))
            pacbp.strip_unmatched_ends()
            # continue if no fraction could be aligned
            if len(pacbp) == 0: continue
            tinypacbporf = pacbp2pacbporf(pacbp, tinyexonQ.orf, tinyexonS.orf)
            tinypacbporf.extend_pacbporf_after_stops()

            ####################################################################
            if verbose:
                print tinypacbporf
                tinypacbporf.print_protein_and_dna()
                print len(donor_introns), len(acceptor_introns),
                print max([
                    dQ.donor.pssm_score + dS.donor.pssm_score
                    for dQ, dS in donor_introns
                ]),
                print max([
                    aQ.acceptor.pssm_score + aS.acceptor.pssm_score
                    for aQ, aS in acceptor_introns
                ])
            ####################################################################

            # if here, we have accepted tinyexon bridges!
            # gather them and store to return_list
            for intronDQkey, intronDQ in resultdictQ[keyQ][0].iteritems():
                if intronDQ.donor.pos not in [dQ.pos for dQ, dS in algdonors]:
                    continue
                for intronDSkey, intronDS in resultdictS[keyS][0].iteritems():
                    if intronDS.donor.pos not in [
                            dS.pos for dQ, dS in algdonors
                    ]:
                        continue
                    for intronAQkey, intronAQ in resultdictQ[keyQ][
                            1].iteritems():
                        if intronAQ.acceptor.pos not in [
                                aQ.pos for aQ, aS in algacceps
                        ]:
                            continue
                        for intronASkey, intronAS in resultdictS[keyS][
                                1].iteritems():
                            if intronAS.acceptor.pos not in [
                                    aS.pos for aQ, aS in algacceps
                            ]:
                                continue
                            ####################################################
                            # set some meta-data properties to the intron objects
                            ####################################################
                            _score_introns_obtained_by_mapping(
                                intronDQ,
                                intronDS,
                                pacbporfD,
                                tinypacbporf,
                                source='ABGPmappingTE')
                            _score_introns_obtained_by_mapping(
                                intronAQ,
                                intronAS,
                                tinypacbporf,
                                pacbporfA,
                                source='ABGPmappingTE')
                            # create _linked_to_xxx attributes
                            intronDQ._linked_to_pacbporfs = [tinypacbporf]
                            intronAQ._linked_to_pacbporfs = [tinypacbporf]
                            intronDS._linked_to_pacbporfs = [tinypacbporf]
                            intronAS._linked_to_pacbporfs = [tinypacbporf]
                            intronDQ._linked_to_introns = [intronAQ]
                            intronAQ._linked_to_introns = [intronDQ]
                            intronDS._linked_to_introns = [intronAS]
                            intronAS._linked_to_introns = [intronDS]
                            # append to tmp result list
                            return_list.append(
                                (intronDQ, intronDS, tinypacbporf, intronAQ,
                                 intronAS))

    # check if there are >1 candidate tiny exons
    # currently, we choose only to return the **best** mapped tinyexon
    if len(return_list) == 0:
        pass
    elif len(return_list) == 1:
        pass
    else:
        # only take the highest scoring candidate here
        min_distance = min([(a._distance + d._distance)
                            for a, b, c, d, e in return_list])
        pos2score = []
        for (intronDQ, intronDS, tinypacbporf, intronAQ,
             intronAS) in return_list:
            if (intronDQ._distance + intronAQ._distance) > min_distance:
                pos2score.append(0.0)
            else:
                # calculate overall pssm score
                total_pssm = 0.0
                total_pssm += intronDQ.donor.pssm_score
                total_pssm += intronDQ.acceptor.pssm_score
                total_pssm += intronDS.donor.pssm_score
                total_pssm += intronDS.acceptor.pssm_score
                total_pssm += intronAQ.donor.pssm_score
                total_pssm += intronAQ.acceptor.pssm_score
                total_pssm += intronAS.donor.pssm_score
                total_pssm += intronAS.acceptor.pssm_score
                pos2score.append(total_pssm)
        # get highest score and linked tinyexon
        max_score = max(pos2score)
        return_list = [return_list[pos2score.index(max_score)]]

    ############################################################################
    # some printing in verbose mode
    if verbose and return_list:
        (intronDQ, intronDS, tinypacbporf, intronAQ, intronAS) = return_list[0]
        print "BEST MAPPED TINYEXON:"
        print tinypacbporf
        print tinypacbporf.query, intronDQ._distance, intronAQ._distance,
        print(intronDQ.donor.pos, intronDQ.acceptor.pos),
        print(intronDS.donor.pos, intronDS.acceptor.pos),
        print(intronAQ.donor.pos, intronAQ.acceptor.pos),
        print(intronAS.donor.pos, intronAS.acceptor.pos)
    ############################################################################

    # return the result list
    return return_list