Ejemplo n.º 1
0
			else:
				print "ERROR: Format /CAL <calendar-name>"
#		comm /EVT <calendar-name> <event-name> <event-place> <event-start-hour> <event-end-hour> <event-description>
#		hour consists of 4 character e.g. 04pm, 05am
#		no space character for description
		elif(param[0].strip() == '/EVT'):
			if (param.__len__() == 7):
				calName = param[1].strip()

				if u.hasCalendar(calName):
					EvtName = param[2]
					EvtPlace = param[3]
					EvtStart = param[4]
					EvtEnd = param[5]
					EvtDesc = param[6]

					ev = Event(EvtName,EvtPlace,EvtStart,EvtEnd,EvtDesc)

					x = calName + 'X'
					channel.exchange_declare(exchange=x,type='fanout')

					message = ev.toString()
#					print "message:",message
					channel.basic_publish(exchange=x,routing_key='',body=message)
					print "INFO: Event sent to calendar",calName
				else:
					print "INFO: You are not in this calendar"
			else:
				print "ERROR: Parameter Error"
		elif(param[0].strip()=='/ALL'):
			u.showAllEvents()
def findOrthologsBySelfGlobalAlignment(strain, coverageTracker, sibling):
    print('Performing self-global alignment on strain: %s' % (strain.name))

    if strain.name == 'NC_015634':
        print('BREAK')

    lossEvents = []
    duplicationEvents = []
    fragments = strain.genomeFragments

    for i in range(0, len(coverageTracker)):
        if coverageTracker[i] == False:  #Operon has not been marked
            bestScore = 1000  #Make the best score some large numer
            bestEvent = None  #Initialize the event
            minDistance = 1000  #Used to track the minimum distance from singleton to operon that has an identical gene

            filteredList = iter(
                filter(
                    lambda x: x.fragmentIndex == i,
                    fragments))  #Get the fragment we need based on the index
            unmarkedFragment = next(filteredList, None)

            if len(unmarkedFragment.sequence) > 1:  #We're processing an operon
                for j in range(0, len(coverageTracker)):
                    filteredList = iter(
                        filter(lambda x: x.fragmentIndex == j, fragments)
                    )  #Get the fragment we need based on the index
                    currFragment = next(filteredList, None)

                    if i != j and coverageTracker[j] == True and len(
                            currFragment.sequence) > 1:
                        op1 = unmarkedFragment.sequence
                        op2 = currFragment.sequence

                        event = Event(0)
                        event.setFragmentDetails1(unmarkedFragment)
                        event.setFragmentDetails2(currFragment)
                        event.setGenome1Name(strain.name)
                        event.setGenome2Name(strain.name)
                        event.setTechnique('Self Global Alignment (Operon)')
                        event.setAncestralOperonGeneSequence(
                            copy.deepcopy(
                                op1))  #Set the ancestral operon sequence
                        score, event = performGlobalAlignment(
                            op1, op2, event)  #Perform the global alignment
                        event.setScore(score)

                        #Compute whether this comparison is interesting
                        threshold = max(len(op1), len(op2))
                        threshold = threshold // 3

                        numOperonDifferences = computeOperonDifferences(
                            op1, op2)

                        if numOperonDifferences <= threshold and score < bestScore:
                            bestScore = score
                            bestEvent = event
            #Make sure an origin or a terminus doesn't get mapped with a singleton gene
            elif len(
                    unmarkedFragment.sequence
            ) == 1 and unmarkedFragment.description != 'Origin' and unmarkedFragment.description != 'Terminus':
                for j in range(0, len(coverageTracker)):
                    if i != j and coverageTracker[j] == True:
                        filteredList = iter(
                            filter(lambda x: x.fragmentIndex == j, fragments)
                        )  #Get the fragment we need based on the index
                        currFragment = next(filteredList, None)

                        op1 = unmarkedFragment.sequence
                        op2 = currFragment.sequence

                        event = Event(0)
                        event.setFragmentDetails1(unmarkedFragment)
                        event.setFragmentDetails2(currFragment)
                        event.setGenome1Name(strain.name)
                        event.setGenome2Name(strain.name)
                        event.setTechnique('Self Global Alignment (Singleton)')
                        event.setAncestralOperonGeneSequence(
                            copy.deepcopy(
                                op1))  #Set the ancestral operon sequence

                        if op1[0] in op2 and abs(
                                i - j
                        ) < minDistance:  #Checks if the singleton gene is located in the operon and if the distance is smaller
                            minDistance = abs(i - j)
                            event.setScore(0)
                            bestEvent = event

            if bestEvent != None:  #A match was found meaning the operon is a duplicate therefor do not add it into the ancestor
                globals.trackingId += 1
                bestEvent.trackingEventId = globals.trackingId
                coverageTracker[i] = True
                duplicationEvents.append(bestEvent)

                duplicationDetails = ''
                position = bestEvent.fragmentDetails1.startPositionInGenome
                op = copy.deepcopy(bestEvent.fragmentDetails1.sequence)

                if bestEvent.fragmentDetails1.isNegativeOrientation == True:  #Reverses the genes if the operon was originally negative to ensure the correct position is computed
                    op.reverse()

                for gene in op:
                    duplicationDetails += gene + ' ' + str(position) + ', '
                    position += 1
                duplicationDetails = duplicationDetails[0:(
                    len(duplicationDetails) - 2)]
                duplicationDetails += ';'

                #Increment the duplicate counter with size of operon since the operon is a duplication
                strain = addDuplicationEventsToStrain(
                    strain, [len(bestEvent.fragmentDetails1.sequence)],
                    duplicationDetails)

                print('\n&&&&&& Self Global Alignment &&&&&')
                print(bestEvent.toString())
                print('&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&\n')

            else:  #No match was found therefore it must have been lost in the sibling therefore we include it in the ancestor
                coverageTracker[i] = True
                globals.trackingId += 1

                event = Event(globals.trackingId)
                event.setScore(-1)
                event.setFragmentDetails1(unmarkedFragment)
                event.setFragmentDetails2(unmarkedFragment)
                event.setGenome1Name(strain.name)
                event.setGenome2Name(strain.name)
                event.setTechnique('Self Global Alignment (No match!)')
                event.setAncestralOperonGeneSequence(
                    copy.deepcopy(unmarkedFragment.sequence))
                lossEvents.append(event)

                deletionDetails = ''
                position = event.fragmentDetails1.startPositionInGenome
                op = copy.deepcopy(event.fragmentDetails1.sequence)

                if event.fragmentDetails1.isNegativeOrientation == True:  #Reverses the genes if the operon was originally negative to ensure the correct position is computed
                    op.reverse()

                for gene in op:
                    deletionDetails += gene + ' ' + str(position) + ', '
                    position += 1
                deletionDetails = deletionDetails[0:(len(deletionDetails) - 2)]
                deletionDetails += ';'

                #Increment the loss counter with the size of the operon since the operon is a loss
                sibling = addDeletionEventsToStrain(
                    sibling, [len(event.fragmentDetails1.sequence)],
                    deletionDetails)

                print('\n&&&&&& Self Global Alignment &&&&&')
                print(event.toString())
                print('&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&\n')

    return duplicationEvents, lossEvents, coverageTracker, strain, sibling
def findOrthologsByLocalAlignment(coverageTracker1, coverageTracker2, strain1,
                                  strain2):
    events = []
    localAlignmentCounter = 0
    minValue = min(len(coverageTracker1), len(
        coverageTracker2))  #Used for computing all of the values in the matrix

    #Scan the matrix x times to find optimal local alignments everytime an entire matrix is computed
    for x in range(0, minValue):
        highestScore = -1
        distance = 50  #An arbitrary large number

        for i in range(0, len(coverageTracker1)):
            filteredList = iter(
                filter(lambda x: x.fragmentIndex == i, strain1.genomeFragments)
            )  #Get the fragment we need based on the index
            fragment1 = next(filteredList, None)

            if coverageTracker1[i] == False and len(
                    fragment1.sequence
            ) > 1:  #Make sure the operon is not marked and not a singleton

                for j in range(0, len(coverageTracker2)):
                    filteredList = iter(
                        filter(lambda x: x.fragmentIndex == j,
                               strain2.genomeFragments)
                    )  #Get the fragment we need based on the index
                    fragment2 = next(filteredList, None)

                    if coverageTracker2[j] == False and len(
                            fragment2.sequence
                    ) > 1:  #Make sure the operon is not marked and not a singleton
                        #Initialize the event
                        event = Event(0)
                        event.setFragmentDetails1(fragment1)
                        event.setFragmentDetails2(fragment2)
                        event.setGenome1Name(strain1.name)
                        event.setGenome2Name(strain2.name)
                        event.setTechnique('Local Alignment')

                        score, startPosition, endPosition, event = localAlignment(
                            fragment1, fragment2, event)

                        if (score > highestScore) or (score == highestScore and
                                                      (abs(i - j)) < distance):
                            highestScore = score
                            rowIndex = i
                            colIndex = j
                            distance = abs(i - j)
                            chosenOpEvent = event
        #After scanning the whole matrix if we found a best score, then store it
        if highestScore > -1:
            print('\n******** Local Alignment ***********')
            print('**************************************\n')
            event = chosenOpEvent
            globals.trackingId += 1
            localAlignmentCounter += 1
            coverageTracker1[rowIndex] = True
            coverageTracker2[colIndex] = True
            event.trackingEventId = globals.trackingId

            event, strain1, strain2 = reconstructOperonSequence(
                event, strain1, strain2)

            #Codon mismatches
            if event.numCodonMismatches > 0:
                codonMismatchDescription1 = constructStatement(
                    event.codonMismatchIndexesStrain1,
                    event.codonMismatchGenesStrain1, event.fragmentDetails1)
                codonMismatchDescription2 = constructStatement(
                    event.codonMismatchIndexesStrain2,
                    event.codonMismatchGenesStrain2, event.fragmentDetails2)
                strain1.addCodonMismatchDetails(codonMismatchDescription1)
                strain2.addCodonMismatchDetails(codonMismatchDescription2)

            #Substitutions
            if event.numSubstitutions > 0:
                substitutionDescription1 = constructStatement(
                    event.substitutionIndexesStrain1,
                    event.substitutionGenesStrain1, event.fragmentDetails1)
                substitutionDescription2 = constructStatement(
                    event.substitutionIndexesStrain2,
                    event.substitutionGenesStrain2, event.fragmentDetails2)
                strain1.addSubstitutionDetails(substitutionDescription1)
                strain2.addSubstitutionDetails(substitutionDescription2)

            events.append(event)  #Add the event to the tracking events list
            print(event.toString())
            print('\n**************************************')
            print('**************************************\n\n')

    return events, coverageTracker1, coverageTracker2, localAlignmentCounter, strain1, strain2
Ejemplo n.º 4
0
def findOrthologsBySelfGlobalAlignment(strain, coverageTracker, sibling):
    if globals.printToConsole:
        print('Performing self-global alignment on strain: %s' % (strain.name))

    lossEvents = []
    duplicationEvents = []
    fragments = strain.genomeFragments

    for i in range(0, len(coverageTracker)):
        if coverageTracker[i] == False:  #Operon has not been marked
            bestScore = -1000  #Make the best score some large numer
            bestEvent = None  #Initialize the event
            minDistance = 1000  #Used to track the minimum distance from singleton to operon that has an identical gene
            bestJ = -999

            filteredList = iter(
                filter(
                    lambda x: x.fragmentIndex == i,
                    fragments))  #Get the fragment we need based on the index
            unmarkedFragment = next(filteredList, None)

            if len(unmarkedFragment.sequence) > 1:  #We're processing an operon
                for j in range(0, len(coverageTracker)):
                    filteredList = iter(
                        filter(lambda x: x.fragmentIndex == j, fragments)
                    )  #Get the fragment we need based on the index
                    currFragment = next(filteredList, None)

                    if i != j and currFragment.isDuplicate == False and len(
                            currFragment.sequence) > 1:
                        op1 = unmarkedFragment.sequence
                        op2 = currFragment.sequence

                        event = Event(0)
                        event.setFragmentDetails1(unmarkedFragment)
                        event.setFragmentDetails2(currFragment)
                        event.setGenome1Name(strain.name)
                        event.setGenome2Name(strain.name)
                        event.setTechnique('Self Global Alignment (Operon)')
                        event.setAncestralOperonGeneSequence(
                            copy.deepcopy(
                                op1))  #Set the ancestral operon sequence
                        score, event = performGlobalAlignment(
                            op1, op2, event)  #Perform the global alignment
                        event.setScore(score)

                        #Compute whether this comparison is interesting
                        #threshold = max(len(op1), len(op2))
                        #threshold = threshold//3
                        #numOperonDifferences = computeOperonDifferences(op1, op2)
                        #if numOperonDifferences <= threshold and score < bestScore:
                        if (score > 0 and score > bestScore) or (
                                score == bestScore
                                and abs(i - j) < minDistance):
                            bestScore = score
                            bestEvent = event
                            minDistance = abs(i - j)
                            bestJ = j
            #Make sure an origin or a terminus doesn't get mapped with a singleton gene
            elif len(
                    unmarkedFragment.sequence
            ) == 1 and unmarkedFragment.description != 'Origin' and unmarkedFragment.description != 'Terminus':
                for j in range(0, len(coverageTracker)):

                    filteredList = iter(
                        filter(lambda x: x.fragmentIndex == j, fragments)
                    )  #Get the fragment we need based on the index
                    currFragment = next(filteredList, None)

                    if i != j and currFragment.isDuplicate == False:

                        op1 = unmarkedFragment.sequence
                        op2 = currFragment.sequence

                        event = Event(0)
                        event.setFragmentDetails1(unmarkedFragment)
                        event.setFragmentDetails2(currFragment)
                        event.setGenome1Name(strain.name)
                        event.setGenome2Name(strain.name)
                        event.setTechnique('Self Global Alignment (Singleton)')
                        event.setAncestralOperonGeneSequence(
                            copy.deepcopy(
                                op1))  #Set the ancestral operon sequence

                        if op1[0] in op2 and abs(
                                i - j
                        ) < minDistance:  #Checks if the singleton gene is located in the operon and if the distance is smaller
                            minDistance = abs(i - j)
                            event.setScore(0)
                            bestEvent = event
                            bestJ = j

            if bestEvent != None:  #A match was found meaning the operon is a duplicate therefor do not add it into the ancestor
                #Handle special case where two unmarked operons are selected as the best matches
                cycleDuplication = False
                if coverageTracker[i] == False and coverageTracker[
                        bestJ] == False:
                    bestEvent.fragmentDetails2.isDuplicate = True
                    coverageTracker[bestJ] = True
                    cycleDuplication = True
                    bestEvent.setScore(-1)
                    bestEvent.setAncestralOperonGeneSequence(
                        copy.deepcopy(bestEvent.fragmentDetails2.sequence)
                    )  #insert source as ancestral operon
                    bestEvent.setTechnique(
                        'Self Global Alignment (Cyclic Duplication!)')
                    lossEvents.append(bestEvent)

                globals.trackingId += 1
                coverageTracker[i] = True
                bestEvent.trackingEventId = globals.trackingId

                if len(bestEvent.fragmentDetails1.sequence) > 1 and len(
                        bestEvent.fragmentDetails2.sequence) > 1:
                    handleDuplicateDetails(bestEvent, strain, sibling,
                                           cycleDuplication)
                else:
                    #Singleton was mapped to an operon
                    if len(bestEvent.fragmentDetails1.sequence) == 1:
                        gene = bestEvent.fragmentDetails1.sequence[0]
                        position = bestEvent.fragmentDetails1.startPositionInGenome
                    else:
                        gene = bestEvent.fragmentDetails2.sequence[0]
                        position = bestEvent.fragmentDetails2.startPositionInGenome
                    tempString = gene + ' ' + str(position) + ';'
                    strain = addDuplicationEventsToStrain(
                        strain, [1], tempString)
                duplicationEvents.append(bestEvent)

                #This is now being handled with the function handleDuplicateDetails
                #duplicationDetails = ''
                #position = bestEvent.fragmentDetails1.startPositionInGenome
                #op = copy.deepcopy(bestEvent.fragmentDetails1.sequence)

                #if bestEvent.fragmentDetails1.isNegativeOrientation == True: #Reverses the genes if the operon was originally negative to ensure the correct position is computed
                #op.reverse()

                #for gene in op:
                #duplicationDetails += gene + ' ' + str(position) + ', '
                #position += 1
                #duplicationDetails = duplicationDetails[0:(len(duplicationDetails) - 2)]
                #duplicationDetails += ';'

                #Increment the duplicate counter with size of operon since the operon is a duplication
                #strain = addDuplicationEventsToStrain(strain, [len(bestEvent.fragmentDetails1.sequence)], duplicationDetails)
                if globals.printToConsole:
                    print('\n&&&&&& Self Global Alignment &&&&&')
                    print(bestEvent.toString())
                    print('&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&\n')

            else:  #No match was found therefore it must have been lost in the sibling therefore we include it in the ancestor
                coverageTracker[i] = True
                globals.trackingId += 1

                event = Event(globals.trackingId)
                event.setScore(-1)
                event.setFragmentDetails1(unmarkedFragment)
                event.setFragmentDetails2(unmarkedFragment)
                event.setGenome1Name(strain.name)
                event.setGenome2Name(strain.name)
                event.setTechnique('Self Global Alignment (No match!)')
                event.setAncestralOperonGeneSequence(
                    copy.deepcopy(unmarkedFragment.sequence))
                lossEvents.append(event)

                position = event.fragmentDetails1.startPositionInGenome
                op = copy.deepcopy(event.fragmentDetails1.sequence)

                tempString = ''
                for n in range(0, len(op)):
                    gene = op[n]
                    if event.fragmentDetails1.isNegativeOrientation == False:
                        tempString += gene + ' ' + str(n + position) + ', '
                    else:
                        tempString = gene + ' ' + str(position + len(op) - n -
                                                      1) + ', ' + tempString
                tempString = tempString[0:(
                    len(tempString) - 2)]  #Remove the last comma and space
                tempString += ';'

                #Increment the loss counter with the size of the operon since the operon is a loss
                sibling = addDeletionEventsToStrain(
                    sibling, [len(event.fragmentDetails1.sequence)],
                    tempString)

                if globals.printToConsole:
                    print('\n&&&&&& Self Global Alignment &&&&&')
                    print(event.toString())
                    print('&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&\n')

    return duplicationEvents, lossEvents, coverageTracker, strain, sibling