Пример #1
0
/****************************************************************************
 *
 * NAME: vHandleMcpsDataInd
 *
 * DESCRIPTION:  Handler for received data
 *
 * PARAMETERS:      Name            RW  Usage
 *					psMcpsInd		R	pointer to received data struct
 * RETURNS:
 *
 * NOTES:
 ****************************************************************************/
PRIVATE void vHandleMcpsDataInd(MAC_McpsDcfmInd_s *psMcpsInd)
{
	// Get the received data structure
	MAC_RxFrameData_s *psFrame = &psMcpsInd->uParam.sIndData.sFrame;

	// Get the mac address
	// TODO - pvAppApiGetMacAddrLocation not in docs
	// TODO - macptr not used as code is commented out
	module_MAC_ExtAddr_s* macptr = (module_MAC_ExtAddr_s*)pvAppApiGetMacAddrLocation();

	uint8 realTimeDataLen=psFrame->au8Sdu[0];
	uint8 lowPriorityDataLen=psFrame->u8SduLength-1-realTimeDataLen;


	// If not associated ??
	// TODO - should this be merged with similar code below [1]
	if (sEndDeviceData.eState != E_STATE_ASSOCIATED)
	{
		// Check for bind acceptance
		// if routed packet, handle it
		if(lowPriorityDataLen>0)
		{
			handleLowPriorityData(&psFrame->au8Sdu[realTimeDataLen+1], lowPriorityDataLen);
			return;
		}
	}

	// Only accept from bound tx
	// TODO - the TX_ADDRESS_MODE stops the following code from being reached


	if (TX_ADDRESS_MODE == 3 && (psFrame->sSrcAddr.uAddr.sExt.u32H != txMACh
			|| psFrame->sSrcAddr.uAddr.sExt.u32L != txMACl))
	{


		return;
	}
	//debugCount1++;

	// First frame, we have an association
	// TODO - Should this be merged with code above [1]
	if (sEndDeviceData.eState != E_STATE_ASSOCIATED)
	{
//		dbgPrintf("tx found \r\n");
		dbgPrintf("tx found %x %x\r\n",txMACh ,txMACl );


		// Update the association state
		sEndDeviceData.eState = E_STATE_ASSOCIATED;

		// Set the hopping mode
		setHopMode(hoppingContinuous);
	}

	//the same packet can be received many times if the ack was not received by the sender

	if(!frameReceived)
	{
		frameReceived=TRUE;
		// Record the link quality
		txLinkQuality = psFrame->u8LinkQuality;

		/*
		 * TODO - define a structure for this
		 * packet layout:
		 * [0] - 8 bit seqno
		 * [1] - 16 bit tx time
		 */
/* now done in interrupt context
		// retrieve the tx time, calculate the rx time ??
		// TODO - explain rx time calc
		int txTime = (psFrame->au8Sdu[1] + (psFrame->au8Sdu[2] << 8)) * 160;
		//allow for latencies along the way and calculate the time we think the tx has now.
		//allow for variations in packet length @ 250kbps
		//32us per byte is 512 clocks per byte
		int rxTime = (txTime + 2000* 16 +(psFrame->u8SduLength-8)*512  ) % (31* 20000* 16 ) ;

		// TODO - Explain this
		calcSyncError(rxTime);
*/
		// Update global rx data packet counter
		rxdpackets++;

		// Update latest received se. no.
		sEndDeviceData.u8RxPacketSeqNb = psFrame->au8Sdu[0];

		// Process the data packet
		vProcessReceivedDataPacket(&psFrame->au8Sdu[3], realTimeDataLen-2);

		// If there is more data, process it
		if(lowPriorityDataLen>0)
		{
		//	debugCount1++;
			handleLowPriorityData(&psFrame->au8Sdu[realTimeDataLen+1], lowPriorityDataLen);
		}

		// Send some data back
		// should check there is time to do this and limit retries

#ifdef JN5168
		//don't send if near the end of the frame as jn5168 gets upset
		//if channel changed during transmission
	//	if(getSeqClock()%(20000*16)>14000*16)return;
	//	return;
#endif

		// Declare the return packet data and default size
		uint8 au8Packet[6];
		uint8 packetLen = 4;

		// Set the return packet data indicator
		au8Packet[1] = returnPacketIdx;

		uint16 val = 0;
		switch (returnPacketIdx)
		{
		case 0:
		case 1:
		case 2:
		case 3:
		case 4:
		case 5:
		{
			// 0 - 5 ADC channels
			val = u16ReadADC(returnPacketIdx);
			au8Packet[2] = val & 0xff;
			au8Packet[3] = val >> 8;
			break;
		}
		case 6:
		{
			// 6 - TX Quality
			au8Packet[2] = getErrorRate();
			au8Packet[3] = txLinkQuality;

			// If unable to read GPS data skip the GPS data items
			if (readNmeaGps(&gpsData) == FALSE)
				returnPacketIdx = 13;
			break;
		}
		case 7:
		{
			// 7 - GPS speed
			readNmeaGps(&gpsData);
			memcpy(&au8Packet[2], &gpsData.nmeaspeed, sizeof(gpsData.nmeaspeed));
			packetLen = 6;
			break;
		}
		case 8:
		{
			// 8 - GPS height
			readNmeaGps(&gpsData);
			memcpy(&au8Packet[2], &gpsData.nmeaheight,
					sizeof(gpsData.nmeaheight));
			packetLen = 6;
			break;
		}
		case 9:
		{
			// 9 - GPS track
			readNmeaGps(&gpsData);
			memcpy(&au8Packet[2], &gpsData.nmeatrack, sizeof(gpsData.nmeatrack));
			packetLen = 6;
			break;
		}
		case 10:
		{
			// 10 - GPS time
			readNmeaGps(&gpsData);
			memcpy(&au8Packet[2], &gpsData.nmeatime, sizeof(gpsData.nmeatime));
			packetLen = 6;
			break;
		}
		case 11:
		{
			// 11 - GPS latitude
			readNmeaGps(&gpsData);
			memcpy(&au8Packet[2], &gpsData.nmealat, sizeof(gpsData.nmealat));
			packetLen = 6;
			break;
		}
		case 12:
		{
			// 12 - GPS longitude
			readNmeaGps(&gpsData);
			memcpy(&au8Packet[2], &gpsData.nmealong, sizeof(gpsData.nmealong));
			packetLen = 6;
			break;
		}
		case 13:
		{
			// 13 - satellites
			readNmeaGps(&gpsData);
			memcpy(&au8Packet[2], &gpsData.nmeasats, sizeof(gpsData.nmeasats));
			packetLen = 6;
			break;
		}
		case 14:
		{
			//corrected battery voltage in 0.01v units
			val = u16ReadADC(rxHardware.batVoltageChannel);
			int volts=val*rxHardware.batVoltageMultiplier/4096+rxHardware.batVoltageOffset;
			au8Packet[2] = volts & 0xff;
			au8Packet[3] = volts >> 8;


		}
		}

		// Set the data length
		au8Packet[0] = packetLen - 1;

		// Send the packet
		vTransmitDataPacket(au8Packet, packetLen, FALSE);

		// Update the data type for the next packet
		returnPacketIdx++;

		// If the last data type has been sent, reset to 0
		if (returnPacketIdx >= 15)
			returnPacketIdx = 0;
	}
Пример #2
0
//
// Main
//
int filterBAMMain(int argc, char** argv)
{
    parseFilterBAMOptions(argc, argv);

    // Read the graph if distance-filtering mode is enabled
    StringGraph* pGraph = NULL;
    if(!opt::asqgFile.empty())
        pGraph = SGUtil::loadASQG(opt::asqgFile, 0, false);

    // Read the BWTs if depth-filtering mode is enabled
    BWT* pBWT = NULL;
    BWT* pRBWT = NULL;
    if(!opt::fmIndexPrefix.empty())
    {
        pBWT = new BWT(opt::fmIndexPrefix + BWT_EXT, opt::sampleRate);
        pRBWT = new BWT(opt::fmIndexPrefix + RBWT_EXT, opt::sampleRate);
    }

    Timer* pTimer = new Timer(PROGRAM_IDENT);    

    // 
    int numPairsTotal = 0;
    int numPairsFilteredByDistance = 0;
    int numPairsFilteredByER = 0;
    int numPairsFilteredByQuality = 0;
    int numPairsFilteredByDepth = 0;
    int numPairsUnmapped = 0;
    int numPairsWrote = 0;

    // Open the bam files for reading/writing
    BamTools::BamReader* pBamReader = new BamTools::BamReader;
    pBamReader->Open(opt::bamFile);

    BamTools::BamWriter* pBamWriter = new BamTools::BamWriter;
    pBamWriter->Open(opt::outFile, pBamReader->GetHeaderText(), pBamReader->GetReferenceData());
    const BamTools::RefVector& referenceVector = pBamReader->GetReferenceData();


    BamTools::BamAlignment record1;
    BamTools::BamAlignment record2;
    bool done = false;

    while(!done)
    {
        if(numPairsTotal++ % 200000 == 0)
            printf("[sga filterBAM] Processed %d pairs\n", numPairsTotal);

        done = !readAlignmentPair(pBamReader, record1, record2);
        if(done)
            break;

        if(!record1.IsMapped() || !record2.IsMapped())
        {
            numPairsUnmapped += 1;
            continue;
        }

        // Ensure the pairing is correct
        if(record1.Name != record2.Name)
        {
            std::cout << "NAME FAIL: " << record1.Name << " " << record2.Name << "\n";
        }
        assert(record1.Name == record2.Name);
        bool bPassedFilters = true;

        // Check if the error rate is below the max
        double er1 = getErrorRate(record1);
        double er2 = getErrorRate(record2);

        if(er1 > opt::maxError || er2 > opt::maxError)
        {
            bPassedFilters = false;
            numPairsFilteredByER += 1;
        }

        if(record1.MapQuality < opt::minQuality || record2.MapQuality < opt::minQuality)
        {
            bPassedFilters = false;
            numPairsFilteredByQuality += 1;
        }

        // Perform depth check for pairs aligning to different contigs
        if(bPassedFilters && (pBWT != NULL && pRBWT != NULL && opt::maxKmerDepth > 0) && (record1.RefID != record2.RefID))
        {
            int maxDepth1 = getMaxKmerDepth(record1.QueryBases, pBWT, pRBWT);
            int maxDepth2 = getMaxKmerDepth(record1.QueryBases, pBWT, pRBWT);
            if(maxDepth1 > opt::maxKmerDepth || maxDepth2 > opt::maxKmerDepth)
            {
                bPassedFilters = false;
                numPairsFilteredByDepth += 1;
            }
        }

        // Perform short-insert pair check
        if(pGraph != NULL)
        {
            bPassedFilters = bPassedFilters && filterByGraph(pGraph, referenceVector, record1, record2);
            numPairsFilteredByDistance += 1;
        }
        if(bPassedFilters)
        {
            pBamWriter->SaveAlignment(record1);
            pBamWriter->SaveAlignment(record2);
            numPairsWrote += 1;
        }
    }

    std::cout << "Total pairs: " << numPairsTotal << "\n";
    std::cout << "Total pairs output: " << numPairsWrote << "\n";
    std::cout << "Total filtered because one pair is unmapped: " << numPairsUnmapped << "\n";
    std::cout << "Total filtered by distance: " << numPairsFilteredByDistance << "\n";
    std::cout << "Total filtered by error rate: " << numPairsFilteredByER << "\n";
    std::cout << "Total filtered by quality: " << numPairsFilteredByQuality << "\n";
    std::cout << "Total filtered by depth: " << numPairsFilteredByDepth << "\n";
    
    if(pGraph != NULL)
        delete pGraph;

    if(pBWT != NULL)
        delete pBWT;

    if(pRBWT != NULL)
        delete pRBWT;

    pBamWriter->Close();
    pBamReader->Close();

    delete pTimer;
    delete pBamReader;
    delete pBamWriter;
    return 0;
}