示例#1
0
def exonSkip_proc(inGsnapFileName, outGsnapFileName, outReportFileName, sampN):

    geneNameH = mygenome.geneNameH()
    geneSetH = mygenome.geneSetH()
    geneInfoH = mygenome.geneInfoH(geneNameH, geneSetH)
    refFlatH = mygenome.loadRefFlatByChr()

    result = mygsnap.gsnapFile(inGsnapFileName, False)

    juncHH = {}

    for r in result:

        match = r.matchL()[0]

        if not '(transloc)' in r.pairRel:
            raise Exception

        if len(match.segL) != 2:
            raise Exception

        splice_type = re.search('splice_type:([^,\t]*)',
                                match.segL[0][3]).group(1)
        direction = re.search('dir:([^,\t]*)', match.segL[0][3]).group(1)
        offset = int(re.search('\.\.([0-9]*)', match.segL[0][1]).group(1))

        transcript1 = re.search('label_[12]:([^,\t]*)', match.segL[0][3])
        gene1 = set()

        if transcript1:

            transcript1 = tuple(
                [x.split('.exon')[0] for x in transcript1.group(1).split('|')])

            for t in transcript1:

                g = mygenome.gene(t, geneNameH, geneSetH, geneInfoH)

                if g.geneName:
                    gene1.add(g.geneName)

        else:

            transcript1 = ()

        transcript2 = re.search('label_[12]:([^,\t]*)', match.segL[1][3])
        gene2 = set()

        if transcript2:

            transcript2 = tuple(
                [x.split('.exon')[0] for x in transcript2.group(1).split('|')])

            for t in transcript2:

                g = mygenome.gene(t, geneNameH, geneSetH, geneInfoH)

                if g.geneName:
                    gene2.add(g.geneName)

        else:

            transcript2 = ()

        s1 = match.segL[0][2]
        s2 = match.segL[1][2]

        bp1 = re.match('([+-])([^:]+):[0-9]+..([0-9]+)', s1)
        bp2 = re.match('([+-])([^:]+):([0-9]+)..[0-9]+', s2)

        if (bp1.group(1), direction) in (('+', 'sense'), ('-', 'antisense')):
            trans_strand1 = '+'
        elif (bp1.group(1), direction) in (('+', 'antisense'), ('-', 'sense')):
            trans_strand1 = '-'
        else:
            raise Exception

        if (bp2.group(1), direction) in (('+', 'sense'), ('-', 'antisense')):
            trans_strand2 = '+'
        elif (bp2.group(1), direction) in (('+', 'antisense'), ('-', 'sense')):
            trans_strand2 = '-'
        else:
            raise Exception

        bp_gene1 = mygenome.locus(
            '%s:%s-%s%s' % (bp1.group(2), int(bp1.group(3)) - 1, bp1.group(3),
                            trans_strand1)).overlappingGeneL(
                                refFlatH=refFlatH, strand_sensitive=True)
        bp_gene2 = mygenome.locus(
            '%s:%s-%s%s' % (bp2.group(2), int(bp2.group(3)) - 1, bp2.group(3),
                            trans_strand2)).overlappingGeneL(
                                refFlatH=refFlatH, strand_sensitive=True)

        if direction == 'sense':
            key = (bp1.groups()[1:], bp2.groups()[1:])
            transcript = (transcript1, transcript2)
            gene = (tuple(gene1), tuple(gene2))
            bp_gene = (bp_gene1, bp_gene2)
        elif direction == 'antisense':
            key = (bp2.groups()[1:], bp1.groups()[1:])
            transcript = (transcript2, transcript1)
            gene = (tuple(gene2), tuple(gene1))
            bp_gene = (bp_gene2, bp_gene1)
        else:
            raise Exception

        if key in juncHH:

            juncHH[key]['match'].append(r)
            juncHH[key]['seq'].append(r.seq())
            juncHH[key]['reg'].append((direction, offset))

        else:

            juncHH[key] = {
                'match': [r],
                'splice_type': splice_type,
                'seq': [r.seq()],
                'reg': [(direction, offset)],
                'transcript': transcript,
                'gene': gene,
                'bp_gene': bp_gene
            }

    juncKH = juncHH.items()
    juncKH.sort(lambda x, y: cmp(len(set(y[1]['reg'])), len(set(x[1]['reg']))))

    outGsnapFile = open(outGsnapFileName, 'w')
    outReportFile = open(outReportFileName, 'w')

    for (key, juncH) in juncKH:

        if key[0][0] == key[1][0]:
            type = 'intra'
        else:
            type = 'inter'

        geneInfo1 = []
        censusInfo1 = []

        for geneName in juncH['gene'][0]:
            gene = mygenome.gene(geneName, geneNameH, geneSetH, geneInfoH)
            geneInfo1.append(
                '%s:%s:%s' %
                (geneName, gene.getAttr('desc'), gene.getAttr('summary')))
            censusInfo1.append('%s:%s:%s:%s' %
                               (gene.getAttr('census_somatic'),
                                gene.getAttr('census_germline'),
                                gene.getAttr('census_mutType'),
                                gene.getAttr('census_translocPartners')))

        geneInfo2 = []
        censusInfo2 = []

        for geneName in juncH['gene'][1]:
            gene = mygenome.gene(geneName, geneNameH, geneSetH, geneInfoH)
            geneInfo2.append(
                '%s:%s:%s' %
                (geneName, gene.getAttr('desc'), gene.getAttr('summary')))
            censusInfo2.append('%s:%s:%s:%s' %
                               (gene.getAttr('census_somatic'),
                                gene.getAttr('census_germline'),
                                gene.getAttr('census_mutType'),
                                gene.getAttr('census_translocPartners')))

        outReportFile.write('%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\n' % \
         (type, juncH['splice_type'], sampN, ':'.join(key[0]), ':'.join(key[1]), \
         ';'.join(juncH['transcript'][0]), ';'.join(juncH['transcript'][1]), ';'.join(juncH['gene'][0]), ';'.join(juncH['gene'][1]), ';'.join(geneInfo1), ';'.join(geneInfo2), \
         ';'.join(censusInfo1), ';'.join(censusInfo2), ','.join(juncH['bp_gene'][0]), ','.join(juncH['bp_gene'][1]), \
         len(juncH['match']) ,len(set(juncH['seq'])), len(set(juncH['reg']))))

        for m in juncH['match']:
            outGsnapFile.write(m.rawText() + '\n')
示例#2
0
def genCompositeModel(outTextFileName,outFaFileName,intronSize=100): 

	geneNameH = mygenome.geneNameH()
	geneSetH = mygenome.geneSetH()
	geneInfoH = mygenome.geneInfoH(geneNameH,geneSetH)

	geneH = mygenome.loadKgByChr()
	#geneH = mygenome.loadLincByChr(h=geneH)

	outTextFile = open(outTextFileName, 'w')
	outFaFile = open(outFaFileName, 'w')

	for chrNum in range(1,23)+['X','Y','M']:
	#for chrNum in [1]:

		chrom = 'chr%s' % chrNum

		geneH_byChr = filter(lambda x: mygenome.gene(x['geneId'],geneNameH,geneSetH,geneInfoH).geneName in mygenome.TK, geneH[chrom])

		txnLocusL_combined = []

		for strand in ['+','-']:

			txnLocusL = [mygenome.locus('%s:%s-%s%s' % (chrom,h['txnSta'],h['txnEnd'],strand),h['geneId']) for h in filter(lambda x: x['strand']==strand, geneH_byChr)]
			n_before = len(txnLocusL)

			txnLocusL = mygenome.mergeLoci(txnLocusL)
			n_after = len(txnLocusL)

			#print chrom, strand, n_before, n_after

			txnLocusL_combined += txnLocusL

		txnLocusL_combined.sort(lambda x,y: cmp(x.chrEnd,y.chrEnd))
		txnLocusL_combined.sort(lambda x,y: cmp(x.chrSta,y.chrSta))

		for txnLoc in txnLocusL_combined:

			exnLocusL = []

			for h in filter(lambda x: x['geneId'] in txnLoc.id, geneH_byChr):
				for (exnSta,exnEnd) in h['exnList']:
					exnLocusL.append(mygenome.locus('%s:%s-%s%s' % (chrom, exnSta, exnEnd, h['strand'])))

			exnLocusL.sort(lambda x,y: cmp(x.chrEnd,y.chrEnd))
			exnLocusL.sort(lambda x,y: cmp(x.chrSta,y.chrSta))

			exnLocusL = mygenome.mergeLoci(exnLocusL)

			exnStaL = [str(exnLoc.chrSta) for exnLoc in exnLocusL]
			exnEndL = [str(exnLoc.chrEnd) for exnLoc in exnLocusL]

			outTextFile.write('%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\n' % (txnLoc.id,txnLoc.chrom,txnLoc.strand,txnLoc.chrSta,txnLoc.chrEnd,len(exnLocusL),','.join(exnStaL),','.join(exnEndL)))

			outFaFile.write('>%s|%s|%s|%s|%s\n' % (txnLoc.id,txnLoc.chrom,txnLoc.strand,txnLoc.chrSta,txnLoc.chrEnd))

			for i in range(len(exnLocusL)):

				exnLocCopy = copy.deepcopy(exnLocusL[i])

				exnLocCopy.strand = '+'

				if i > 0:
					exnLocCopy.chrSta -= min(intronSize, int((exnLocusL[i].chrSta - exnLocusL[i-1].chrEnd)/2))

				if i < len(exnLocusL)-1:
					exnLocCopy.chrEnd += min(intronSize, int((exnLocusL[i+1].chrSta - exnLocusL[i].chrEnd)/2))

				outFaFile.write(exnLocCopy.nibFrag())

			outFaFile.write('\n')

	outTextFile.close()
	outFaFile.close()
示例#3
0
def gsnap_process_junction(inReportFileName,outReportFileName):

	geneNameH = mygenome.geneNameH()
	geneSetH = mygenome.geneSetH()
	geneInfoH = mygenome.geneInfoH(geneNameH,geneSetH)

	outReportFile = open(outReportFileName,'w')

	for line in open(inReportFileName):

		(spliceType,sampN,bp1,bp2,t1,t2,nmatch,nseq,nreg) = line[:-1].split('\t')

		gene1 = set()

		if t1:

			transcript1 = tuple(t1.split(';'))
	
			for t in transcript1:

				g = mygenome.gene(t,geneNameH,geneSetH,geneInfoH)

				if g.geneName:
					gene1.add(g.geneName)

		else:

			gene1 = ()

		gene2 = set()

		if t2:

			transcript2 = tuple(t2.split(';'))

			for t in transcript2:

				g = mygenome.gene(t,geneNameH,geneSetH,geneInfoH)

				if g.geneName:
					gene2.add(g.geneName)

		else:

			gene2 = ()

		bp_gene1 = set()

#		transcript1 = tuple([x for x in bp1.split('|') if "uc" in x])

		for t in tuple([x for x in bp1.split('|') if "uc" in x]):

			g = mygenome.gene(t,geneNameH,geneSetH,geneInfoH)

			if g.geneName:
				bp_gene1.add(g.geneName)
		
		bp_gene2 = set()

#		transcript2 = tuple([x for x in bp2.split('|') if "uc" in x])

		for t in tuple([x for x in bp2.split('|') if "uc" in x]):

			g = mygenome.gene(t,geneNameH,geneSetH,geneInfoH)

			if g.geneName:
				bp_gene2.add(g.geneName)

#		ch1 =  tuple([x for x in id1.split('|') if not "uc" in x])
#		ch2 =  tuple([x for x in id2.split('|') if not "uc" in x])


		if tuple([x for x in bp1.split('|') if "chr" in x])[0] == tuple([x for x in bp2.split('|') if "chr" in x])[0]:
			type = 'intra'
		else:
			type = 'inter'


		geneInfo1 = []
		censusInfo1 = []

		for geneName in gene1:
			gene = mygenome.gene(geneName,geneNameH,geneSetH,geneInfoH)
			geneInfo1.append('%s:%s:%s' % (geneName,gene.getAttr('desc'),gene.getAttr('summary')))
			censusInfo1.append('%s:%s:%s:%s' % (gene.getAttr('census_somatic'),gene.getAttr('census_germline'),gene.getAttr('census_mutType'),gene.getAttr('census_translocPartners')))

		geneInfo2 = []
		censusInfo2 = []

		for geneName in gene2:
			gene = mygenome.gene(geneName,geneNameH,geneSetH,geneInfoH)
			geneInfo2.append('%s:%s:%s' % (geneName,gene.getAttr('desc'),gene.getAttr('summary')))
			censusInfo2.append('%s:%s:%s:%s' % (gene.getAttr('census_somatic'),gene.getAttr('census_germline'),gene.getAttr('census_mutType'),gene.getAttr('census_translocPartners')))


		outReportFile.write('%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\n' % \
			(type, spliceType, sampN, bp1, bp2, \
			t1, t2, ';'.join(gene1), ';'.join(gene2), ';'.join(geneInfo1), ';'.join(geneInfo2), \
			';'.join(censusInfo1), ';'.join(censusInfo2), ','.join(bp_gene1), ','.join(bp_gene2), \
			nmatch ,nseq, nreg))
示例#4
0
def gsnap_process_junction(inGsnapFileName,outGsnapFileName,outReportFileName,sampN):

	geneNameH = mygenome.geneNameH()
	geneSetH = mygenome.geneSetH()
	geneInfoH = mygenome.geneInfoH(geneNameH,geneSetH)
	refFlatH = mygenome.loadRefFlatByChr()

	result = mygsnap.gsnapFile(inGsnapFileName,False)

	juncHH = {}

	for r in result:

		match = r.matchL()[0]

		if not '(transloc)' in r.pairRel:
			raise Exception

		if len(match.segL) != 2:
			raise Exception

		splice_type = re.search('splice_type:([^,\t]*)', match.segL[0][3]).group(1)
		direction = re.search('dir:([^,\t]*)', match.segL[0][3]).group(1)
		offset = int(re.search('\.\.([0-9]*)', match.segL[0][1]).group(1))

		rm = re.search('label_[12]:([^,\t]*)', match.segL[0][3])
		gene1 = set()

		if rm:

			trans_exon1 = rm.group(1).split('|')

			for t in trans_exon1:

				g = mygenome.gene(t.split('.exon')[0],geneNameH,geneSetH,geneInfoH)

				if g.geneName:
					gene1.add(g.geneName)

		else:

			trans_exon1 = ()

		rm = re.search('label_[12]:([^,\t]*)', match.segL[0][3])
		gene2 = set()

		if rm:

			trans_exon2 = rm.group(1).split('|')

			for t in trans_exon2:

				g = mygenome.gene(t.split('.exon')[0],geneNameH,geneSetH,geneInfoH)

				if g.geneName:
					gene2.add(g.geneName)

		else:

			trans_exon2 = ()


		s1 = match.segL[0][2]
		s2 = match.segL[1][2]

		bp1 = re.match('([+-])([^:]+):[0-9]+..([0-9]+)',s1)
		bp2 = re.match('([+-])([^:]+):([0-9]+)..[0-9]+',s2)

		if (bp1.group(1),direction) in (('+','sense'),('-','antisense')):
			trans_strand1 = '+'
		elif (bp1.group(1),direction) in (('+','antisense'),('-','sense')):
			trans_strand1 = '-'
		else:
			raise Exception

		if (bp2.group(1),direction) in (('+','sense'),('-','antisense')):
			trans_strand2 = '+'
		elif (bp2.group(1),direction) in (('+','antisense'),('-','sense')):
			trans_strand2 = '-'
		else:
			raise Exception

		locus1 = mygenome.locus('%s:%s-%s%s' % (bp1.group(2),int(bp1.group(3))-1,bp1.group(3),trans_strand1))
		bp_gene1 = list(set(locus1.overlappingGeneL(refFlatH=refFlatH,strand_sensitive=True)).difference(gene1))

		locus2 = mygenome.locus('%s:%s-%s%s' % (bp2.group(2),int(bp2.group(3))-2,bp2.group(3),trans_strand2))
		bp_gene2 = list(set(locus2.overlappingGeneL(refFlatH=refFlatH,strand_sensitive=True)).difference(gene2))

		if direction=='sense':
			key = (bp1.groups()[1:],bp2.groups()[1:])
			trans_exon = (trans_exon1,trans_exon2)
			gene = (list(gene1),list(gene2))
			bp_gene = (bp_gene1,bp_gene2)
		elif direction=='antisense':
			key = (bp2.groups()[1:],bp1.groups()[1:])
			trans_exon = (trans_exon2,trans_exon1)
			gene = (list(gene2),list(gene1))
			bp_gene = (bp_gene2,bp_gene1)
		else:
			raise Exception

		if key in juncHH:

			juncHH[key]['match'].append(r)
			juncHH[key]['seq'].append(r.seq())
			juncHH[key]['reg'].append((direction,offset))

		else:

			juncHH[key] = {'match':[r], 'splice_type':splice_type, 'seq':[r.seq()], 'reg':[(direction,offset)], 'trans_exon':trans_exon, 'gene':gene, 'bp_gene':bp_gene}

	juncKH = juncHH.items()
	juncKH.sort(lambda x,y: cmp(len(set(y[1]['reg'])),len(set(x[1]['reg']))))

	outGsnapFile = open(outGsnapFileName,'w')
	outReportFile = open(outReportFileName,'w')

	for (key, juncH) in juncKH:

		if key[0][0] == key[1][0]:
			type = 'intra'
		else:
			type = 'inter'

		geneInfo1 = []
		censusInfo1 = []

		for geneName in juncH['gene'][0]+juncH['bp_gene'][0]:
			gene = mygenome.gene(geneName,geneNameH,geneSetH,geneInfoH)
			geneInfo1.append('%s:%s:%s' % (geneName,gene.getAttr('desc'),gene.getAttr('summary')))
			censusInfo1.append('%s:%s:%s:%s' % (gene.getAttr('census_somatic'),gene.getAttr('census_germline'),gene.getAttr('census_mutType'),gene.getAttr('census_translocPartners')))

		geneInfo2 = []
		censusInfo2 = []

		for geneName in juncH['gene'][1]+juncH['bp_gene'][1]:
			gene = mygenome.gene(geneName,geneNameH,geneSetH,geneInfoH)
			geneInfo2.append('%s:%s:%s' % (geneName,gene.getAttr('desc'),gene.getAttr('summary')))
			censusInfo2.append('%s:%s:%s:%s' % (gene.getAttr('census_somatic'),gene.getAttr('census_germline'),gene.getAttr('census_mutType'),gene.getAttr('census_translocPartners')))

		outReportFile.write('%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s;%s\t%s;%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\n' % \
			(type, juncH['splice_type'], sampN, ':'.join(key[0]), ':'.join(key[1]), \
			','.join(juncH['trans_exon'][0]), ','.join(juncH['trans_exon'][1]), \
			','.join(juncH['gene'][0]), ','.join(juncH['bp_gene'][0]), ','.join(juncH['gene'][1]), ','.join(juncH['bp_gene'][1]), \
			';'.join(geneInfo1), ';'.join(geneInfo2), ';'.join(censusInfo1), ';'.join(censusInfo2), \
			len(juncH['match']) ,len(set(juncH['seq'])), len(set(juncH['reg']))))

		for m in juncH['match']:
			outGsnapFile.write(m.rawText()+'\n')
示例#5
0
def gsnap_process_junction(inReportFileName, outReportFileName):

    geneNameH = mygenome.geneNameH()
    geneSetH = mygenome.geneSetH()
    geneInfoH = mygenome.geneInfoH(geneNameH, geneSetH)

    outReportFile = open(outReportFileName, 'w')

    for line in open(inReportFileName):

        (spliceType, sampN, bp1, bp2, t1, t2, nmatch, nseq,
         nreg) = line[:-1].split('\t')

        gene1 = set()

        if t1:

            transcript1 = tuple(t1.split(';'))

            for t in transcript1:

                g = mygenome.gene(t, geneNameH, geneSetH, geneInfoH)

                if g.geneName:
                    gene1.add(g.geneName)

        else:

            gene1 = ()

        gene2 = set()

        if t2:

            transcript2 = tuple(t2.split(';'))

            for t in transcript2:

                g = mygenome.gene(t, geneNameH, geneSetH, geneInfoH)

                if g.geneName:
                    gene2.add(g.geneName)

        else:

            gene2 = ()

        bp_gene1 = set()

        #		transcript1 = tuple([x for x in bp1.split('|') if "uc" in x])

        for t in tuple([x for x in bp1.split('|') if "uc" in x]):

            g = mygenome.gene(t, geneNameH, geneSetH, geneInfoH)

            if g.geneName:
                bp_gene1.add(g.geneName)

        bp_gene2 = set()

        #		transcript2 = tuple([x for x in bp2.split('|') if "uc" in x])

        for t in tuple([x for x in bp2.split('|') if "uc" in x]):

            g = mygenome.gene(t, geneNameH, geneSetH, geneInfoH)

            if g.geneName:
                bp_gene2.add(g.geneName)

#		ch1 =  tuple([x for x in id1.split('|') if not "uc" in x])
#		ch2 =  tuple([x for x in id2.split('|') if not "uc" in x])

        if tuple([x for x in bp1.split('|') if "chr" in x
                  ])[0] == tuple([x for x in bp2.split('|') if "chr" in x])[0]:
            type = 'intra'
        else:
            type = 'inter'

        geneInfo1 = []
        censusInfo1 = []

        for geneName in gene1:
            gene = mygenome.gene(geneName, geneNameH, geneSetH, geneInfoH)
            geneInfo1.append(
                '%s:%s:%s' %
                (geneName, gene.getAttr('desc'), gene.getAttr('summary')))
            censusInfo1.append('%s:%s:%s:%s' %
                               (gene.getAttr('census_somatic'),
                                gene.getAttr('census_germline'),
                                gene.getAttr('census_mutType'),
                                gene.getAttr('census_translocPartners')))

        geneInfo2 = []
        censusInfo2 = []

        for geneName in gene2:
            gene = mygenome.gene(geneName, geneNameH, geneSetH, geneInfoH)
            geneInfo2.append(
                '%s:%s:%s' %
                (geneName, gene.getAttr('desc'), gene.getAttr('summary')))
            censusInfo2.append('%s:%s:%s:%s' %
                               (gene.getAttr('census_somatic'),
                                gene.getAttr('census_germline'),
                                gene.getAttr('census_mutType'),
                                gene.getAttr('census_translocPartners')))


        outReportFile.write('%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\n' % \
         (type, spliceType, sampN, bp1, bp2, \
         t1, t2, ';'.join(gene1), ';'.join(gene2), ';'.join(geneInfo1), ';'.join(geneInfo2), \
         ';'.join(censusInfo1), ';'.join(censusInfo2), ','.join(bp_gene1), ','.join(bp_gene2), \
         nmatch ,nseq, nreg))