Esempio n. 1
0
bool Alignment::AddSeqs(vector< vector<string> > const & input,vector<string> const & names){
	if(input.size() != names.size()){
		cerr<<"number of input seqs are not the same as the number of seq names"<<endl;
		return 0;
	}
	for(unsigned int i=0; i < input.size();i++)
		if(AddSeq(input[i],names[i]) == 0)
			return 0;
	return 1;
}
Esempio n. 2
0
void MixSeq( MidiSeqPtr src, MidiSeqPtr dest)
{
        register MidiEvPtr firstSrc, firstDest;
        
        if( dest && src)                                        /* dest et src existent         */
        {
                if( firstSrc= src->first)                       /* src non vide                 */
                {
                        if( !(firstDest= dest->first))          /* si destination est vide      */
                        {
                                dest->first= firstSrc;                  /* recopie du premier et        */
                                dest->last= src->last;                  /* dernier evt de src           */
                        }
                        else if( Date(firstSrc) < Date(firstDest))
                                                                        /* 1ier evt source pr?c?de      */
                        {                                               /* le 1ier evt destination      */
                                dest->first= firstSrc;                  /* range dans destination       */
                                dest->last= AddSeq( firstSrc, firstDest);       /* et chainage  */
                        }
                        else dest->last= AddSeq( firstDest, firstSrc);  /* et chainage  */
                }
                MidiFreeCell((MidiEvPtr) src);
        }
}
Esempio n. 3
0
static int
alias_rtsp_out(struct libalias *la, struct ip *pip,
               struct alias_link *lnk,
               char *data,
               const char *port_str)
{
    int hlen, tlen, dlen;
    struct tcphdr *tc;
    int i, j, pos, state, port_dlen, new_dlen, delta;
    u_short p[2], new_len;
    u_short sport, eport, base_port;
    u_short salias = 0, ealias = 0, base_alias = 0;
    const char *transport_str = "transport:";
    char newdata[2048], *port_data, *port_newdata, stemp[80];
    int links_created = 0, pkt_updated = 0;
    struct alias_link *rtsp_lnk = NULL;
    struct in_addr null_addr;

    /* Calculate data length of TCP packet */
    tc = (struct tcphdr *)ip_next(pip);
    hlen = (pip->ip_hl + tc->th_off) << 2;
    tlen = ntohs(pip->ip_len);
    dlen = tlen - hlen;

    /* Find keyword, "Transport: " */
    pos = search_string(data, dlen, transport_str);
    if (pos < 0) {
        return (-1);
    }
    port_data = data + pos;
    port_dlen = dlen - pos;

    memcpy(newdata, data, pos);
    port_newdata = newdata + pos;

    while (port_dlen > (int)strlen(port_str)) {
        /* Find keyword, appropriate port string */
        pos = search_string(port_data, port_dlen, port_str);
        if (pos < 0) {
            break;
        }
        memcpy(port_newdata, port_data, pos + 1);
        port_newdata += (pos + 1);

        p[0] = p[1] = 0;
        sport = eport = 0;
        state = 0;
        for (i = pos; i < port_dlen; i++) {
            switch (state) {
            case 0:
                if (port_data[i] == '=') {
                    state++;
                }
                break;
            case 1:
                if (ISDIGIT(port_data[i])) {
                    p[0] = p[0] * 10 + port_data[i] - '0';
                } else {
                    if (port_data[i] == ';') {
                        state = 3;
                    }
                    if (port_data[i] == '-') {
                        state++;
                    }
                }
                break;
            case 2:
                if (ISDIGIT(port_data[i])) {
                    p[1] = p[1] * 10 + port_data[i] - '0';
                } else {
                    state++;
                }
                break;
            case 3:
                base_port = p[0];
                sport = htons(p[0]);
                eport = htons(p[1]);

                if (!links_created) {

                    links_created = 1;
                    /*
                     * Find an even numbered port
                     * number base that satisfies the
                     * contiguous number of ports we
                     * need
                     */
                    null_addr.s_addr = 0;
                    if (0 == (salias = FindNewPortGroup(la, null_addr,
                                                        FindAliasAddress(la, pip->ip_src),
                                                        sport, 0,
                                                        RTSP_PORT_GROUP,
                                                        IPPROTO_UDP, 1))) {
#ifdef LIBALIAS_DEBUG
                        fprintf(stderr,
                                "PacketAlias/RTSP: Cannot find contiguous RTSP data ports\n");
#endif
                    } else {

                        base_alias = ntohs(salias);
                        for (j = 0; j < RTSP_PORT_GROUP; j++) {
                            /*
                             * Establish link
                             * to port found in
                             * RTSP packet
                             */
                            rtsp_lnk = FindRtspOut(la, GetOriginalAddress(lnk), null_addr,
                                                   htons(base_port + j), htons(base_alias + j),
                                                   IPPROTO_UDP);
                            if (rtsp_lnk != NULL) {
#ifndef NO_FW_PUNCH
                                /*
                                 * Punch
                                 * hole in
                                 * firewall
                                 */
                                PunchFWHole(rtsp_lnk);
#endif
                            } else {
#ifdef LIBALIAS_DEBUG
                                fprintf(stderr,
                                        "PacketAlias/RTSP: Cannot allocate RTSP data ports\n");
#endif
                                break;
                            }
                        }
                    }
                    ealias = htons(base_alias + (RTSP_PORT_GROUP - 1));
                }
                if (salias && rtsp_lnk) {

                    pkt_updated = 1;

                    /* Copy into IP packet */
                    sprintf(stemp, "%d", ntohs(salias));
                    memcpy(port_newdata, stemp, strlen(stemp));
                    port_newdata += strlen(stemp);

                    if (eport != 0) {
                        *port_newdata = '-';
                        port_newdata++;

                        /* Copy into IP packet */
                        sprintf(stemp, "%d", ntohs(ealias));
                        memcpy(port_newdata, stemp, strlen(stemp));
                        port_newdata += strlen(stemp);
                    }
                    *port_newdata = ';';
                    port_newdata++;
                }
                state++;
                break;
            }
            if (state > 3) {
                break;
            }
        }
        port_data += i;
        port_dlen -= i;
    }

    if (!pkt_updated)
        return (-1);

    memcpy(port_newdata, port_data, port_dlen);
    port_newdata += port_dlen;
    *port_newdata = '\0';

    /* Create new packet */
    new_dlen = port_newdata - newdata;
    memcpy(data, newdata, new_dlen);

    SetAckModified(lnk);
    tc = (struct tcphdr *)ip_next(pip);
    delta = GetDeltaSeqOut(tc->th_seq, lnk);
    AddSeq(lnk, delta + new_dlen - dlen, pip->ip_hl, pip->ip_len,
           tc->th_seq, tc->th_off);

    new_len = htons(hlen + new_dlen);
    DifferentialChecksum(&pip->ip_sum,
                         &new_len,
                         &pip->ip_len,
                         1);
    pip->ip_len = new_len;

    tc->th_sum = 0;
#ifdef _KERNEL
    tc->th_x2 = 1;
#else
    tc->th_sum = TcpChecksum(pip);
#endif
    return (0);
}
Esempio n. 4
0
static void
NewFtpMessage(struct libalias *la, struct ip *pip,
    struct alias_link *lnk,
    int maxpacketsize,
    int ftp_message_type)
{
	struct alias_link *ftp_lnk;

/* Security checks. */
	if (pip->ip_src.s_addr != la->true_addr.s_addr)
		return;

	if (la->true_port < IPPORT_RESERVED)
		return;

/* Establish link to address and port found in FTP control message. */
	ftp_lnk = FindUdpTcpOut(la, la->true_addr, GetDestAddress(lnk),
	    htons(la->true_port), 0, IPPROTO_TCP, 1);

	if (ftp_lnk != NULL) {
		int slen, hlen, tlen, dlen;
		struct tcphdr *tc;

#ifndef NO_FW_PUNCH
		/* Punch hole in firewall */
		PunchFWHole(ftp_lnk);
#endif

/* Calculate data length of TCP packet */
		tc = (struct tcphdr *)ip_next(pip);
		hlen = (pip->ip_hl + tc->th_off) << 2;
		tlen = ntohs(pip->ip_len);
		dlen = tlen - hlen;

/* Create new FTP message. */
		{
			char stemp[MAX_MESSAGE_SIZE + 1];
			char *sptr;
			u_short alias_port;
			u_char *ptr;
			int a1, a2, a3, a4, p1, p2;
			struct in_addr alias_address;

/* Decompose alias address into quad format */
			alias_address = GetAliasAddress(lnk);
			ptr = (u_char *) & alias_address.s_addr;
			a1 = *ptr++;
			a2 = *ptr++;
			a3 = *ptr++;
			a4 = *ptr;

			alias_port = GetAliasPort(ftp_lnk);

/* Prepare new command */
			switch (ftp_message_type) {
			case FTP_PORT_COMMAND:
			case FTP_227_REPLY:
				/* Decompose alias port into pair format. */
				ptr = (char *)&alias_port;
				p1 = *ptr++;
				p2 = *ptr;

				if (ftp_message_type == FTP_PORT_COMMAND) {
					/* Generate PORT command string. */
					sprintf(stemp, "PORT %d,%d,%d,%d,%d,%d\r\n",
					    a1, a2, a3, a4, p1, p2);
				} else {
					/* Generate 227 reply string. */
					sprintf(stemp,
					    "227 Entering Passive Mode (%d,%d,%d,%d,%d,%d)\r\n",
					    a1, a2, a3, a4, p1, p2);
				}
				break;
			case FTP_EPRT_COMMAND:
				/* Generate EPRT command string. */
				sprintf(stemp, "EPRT |1|%d.%d.%d.%d|%d|\r\n",
				    a1, a2, a3, a4, ntohs(alias_port));
				break;
			case FTP_229_REPLY:
				/* Generate 229 reply string. */
				sprintf(stemp, "229 Entering Extended Passive Mode (|||%d|)\r\n",
				    ntohs(alias_port));
				break;
			}

/* Save string length for IP header modification */
			slen = strlen(stemp);

/* Copy modified buffer into IP packet. */
			sptr = (char *)pip;
			sptr += hlen;
			strncpy(sptr, stemp, maxpacketsize - hlen);
		}

/* Save information regarding modified seq and ack numbers */
		{
			int delta;

			SetAckModified(lnk);
			tc = (struct tcphdr *)ip_next(pip);				
			delta = GetDeltaSeqOut(tc->th_seq, lnk);
			AddSeq(lnk, delta + slen - dlen, pip->ip_hl, 
			    pip->ip_len, tc->th_seq, tc->th_off);
		}

/* Revise IP header */
		{
			u_short new_len;

			new_len = htons(hlen + slen);
			DifferentialChecksum(&pip->ip_sum,
			    &new_len,
			    &pip->ip_len,
			    1);
			pip->ip_len = new_len;
		}

/* Compute TCP checksum for revised packet */
		tc->th_sum = 0;
#ifdef _KERNEL
		tc->th_x2 = 1;
#else
		tc->th_sum = TcpChecksum(pip);
#endif
	} else {
#ifdef LIBALIAS_DEBUG
		fprintf(stderr,
		    "PacketAlias/HandleFtpOut: Cannot allocate FTP data port\n");
#endif
	}
}
Esempio n. 5
0
// LoadRawContigs
// Load reads from fasta, fastq or csfasta formated raw reads file
teBSFrsltCodes
CHomozyReduce::LoadRawContigs(int MaxNs,		// filter out input sequences having higher than this number of indeterminate bases per 100bp (default is 1, range 0..10)
					int Trim5,				// trim this number of 5' bases from input sequences (default is 0, range 0..20)
					int Trim3,				// trim this number of 3' bases from input sequences (default is 0, range 0..20)
					int MinSeqLen,		    // filter out input sequences (after any trimming) which are less than this length (default is 50bp, range 30..10000)
					int FileID,				// uniquely identifies source file
					char *pszFile)			// process from this file
{
static int FileNamesOfs = 0;
teBSFrsltCodes Rslt;
int NumDescrContigs;
int NumUnderlen;
int NumAcceptedContigs;
bool bIsFastq;
int ReadLen;

int DescrLen;
UINT8 szDescrBuff[1024];

int NumInvalValues = 0;
int NumUnsupportedBases = 0;
int NumUnderlength = 0;
int NumExcessNs = 0;

etSeqBase *pSeq;
UINT8 *pRawContigsBuff;
if((pRawContigsBuff = new UINT8 [cAllocRawSeqLen]) == NULL)
	{
	gDiagnostics.DiagOut(eDLFatal,gszProcName,"Unable to allocate memory for raw reads buffering...");
	Reset(false);
	return(eBSFerrMem);
	}

CFasta Fasta;
if((Rslt=(teBSFrsltCodes)Fasta.Open(pszFile,true))!=eBSFSuccess)
	{
	gDiagnostics.DiagOut(eDLFatal,gszProcName,"Load: Unable to open '%s' [%s] %s",pszFile,Fasta.ErrText((teBSFrsltCodes)Rslt),Fasta.GetErrMsg());
	delete pRawContigsBuff;
	Reset(false);
	return(Rslt);
	}

if(Fasta.IsSOLiD())
	{
	gDiagnostics.DiagOut(eDLFatal,gszProcName,"Sequences from '%s' are in SOLiD colorspace...",pszFile);
	delete pRawContigsBuff;
	Reset(false);
	return(eBSFerrOpnFile);
	}

bIsFastq = Fasta.IsFastq();

// get file size and alloc for expected total sequences up front to try and reduce the number of reallocs which are required
// may end up allocating more memory than actually needed but on Windows HPC seems to result in a significant throughput improvement
UINT64 ReqAllocSize = Fasta.InitialFileSize();
if(bIsFastq)
	ReqAllocSize /= 2;			// quality scores effectively double the file size relative to the actual sequences

AcquireSerialise();
if(m_pSeqs2Assemb == NULL)
	{
	m_AllocMemSeqs2Assemb = (size_t)ReqAllocSize;
#ifdef _WIN32
	m_pSeqs2Assemb = (UINT8 *) malloc((size_t)m_AllocMemSeqs2Assemb);
	if(m_pSeqs2Assemb == NULL)
		{
		gDiagnostics.DiagOut(eDLFatal,gszProcName,"LoadContigs: Concatenated sequences memory allocation of %lld bytes - %s",(INT64)m_AllocMemSeqs2Assemb,strerror(errno));
		m_AllocMemConcat = 0;
		Reset(false);
		return(eBSFerrMem);
		}
#else
	// gnu malloc is still in the 32bit world and can't handle more than 2GB allocations
	m_pSeqs2Assemb = (UINT8 *)mmap(NULL,m_AllocMemSeqs2Assemb, PROT_READ |  PROT_WRITE,MAP_PRIVATE | MAP_ANONYMOUS, -1,0);
	if(m_pSeqs2Assemb == MAP_FAILED)
		{
		gDiagnostics.DiagOut(eDLFatal,gszProcName,"LoadContigs: Concatenated sequences memory of %lld bytes through mmap()  failed - %s",(INT64)m_AllocMemSeqs2Assemb,strerror(errno));
		m_pSeqs2Assemb = NULL;
		Reset(false);
		return(eBSFerrMem);
		}
#endif
	m_Seqs2AssembLen = 0;
	m_NumSeqs2Assemb = 0;
	}
else
	{
	UINT8 *pDstSeq;
	size_t memreq;
	if((m_Seqs2AssembLen + ReqAllocSize + 100) >= m_AllocMemSeqs2Assemb)		// 100 as a small safety margin!
		{
		memreq = (size_t)(m_Seqs2AssembLen + ReqAllocSize);
#ifdef _WIN32
		pDstSeq = (UINT8 *) realloc(m_pSeqs2Assemb,memreq);
#else
		pDstSeq = (UINT8 *)mremap(m_pSeqs2Assemb,m_AllocMemSeqs2Assemb,memreq,MREMAP_MAYMOVE);
		if(pDstSeq == MAP_FAILED)
			pDstSeq = NULL;
#endif
		if(pDstSeq == NULL)
			{
			gDiagnostics.DiagOut(eDLFatal,gszProcName,"AddSeq: Memory re-allocation to %lld bytes - %s",memreq,strerror(errno));
			return(eBSFerrMem);
			}
		m_AllocMemSeqs2Assemb = memreq;
		m_pSeqs2Assemb = pDstSeq;
		}
	}
ReleaseSerialise();

if(m_AllocMemSeqs2Assemb > m_CurMaxMemWorkSetBytes)
	{
	if(!SetMaxMemWorkSetSize(m_AllocMemSeqs2Assemb * 2))
		{
		gDiagnostics.DiagOut(eDLFatal,gszProcName,"LoadContigs: Length of loaded concatenated sequences exceeds limit of %lld bytes",cMaxConcatSeqLen);
		return(eBSFerrMaxDirEls);
		}
	}

m_RdsSfxHdr.RdsSrcFiles[m_RdsSfxHdr.NumSrcFiles].SrcFileID = FileID;
strncpy((char *)m_RdsSfxHdr.RdsSrcFiles[m_RdsSfxHdr.NumSrcFiles].SrcFileName,pszFile,sizeof(m_RdsSfxHdr.RdsSrcFiles[m_RdsSfxHdr.NumSrcFiles].SrcFileName)-1);

NumUnsupportedBases = 0;
NumDescrContigs = 0;
NumUnderlen = 0;
while((Rslt = (teBSFrsltCodes)(ReadLen = Fasta.ReadSequence(pRawContigsBuff,cAllocRawSeqLen,true,false))) > eBSFSuccess)
	{
	NumDescrContigs += 1;
	if(ReadLen == eBSFFastaDescr)		// just read a descriptor line which would be as expected for multifasta or fastq
		{
		DescrLen = Fasta.ReadDescriptor((char *)szDescrBuff,sizeof(szDescrBuff)-1);
		szDescrBuff[sizeof(szDescrBuff)-1] = '\0';
		ReadLen = Fasta.ReadSequence(pRawContigsBuff,cAllocRawSeqLen);
		if(ReadLen < 0)
			{
			gDiagnostics.DiagOut(eDLFatal,gszProcName,"Problem parsing sequence after %d reads parsed",NumDescrContigs);
			gDiagnostics.DiagOut(eDLFatal,gszProcName,"Last descriptor parsed: %s",szDescrBuff);
			delete pRawContigsBuff;
			Fasta.Close();
			return(eBSFerrParse);
			}

		if(ReadLen < (Trim5 + Trim3 + MinSeqLen))
			{
			NumUnderlen += 1;
			continue;
			}

		pSeq = pRawContigsBuff;

		// trim 5' and 3' as requested
		if(Trim5 > 0)
			{
			pSeq += Trim5;
			ReadLen -= Trim5;
			}

		ReadLen -= Trim3;

		// check for excessive number of Ns
		int Idx;
		int NumNs = 0;		// number of indeterminate bases in last 100bp window
		etSeqBase *pBase = pSeq;
		for(Idx = 0; Idx < ReadLen; Idx++,pBase++)
			{
			if(Idx >= 100)
				{
				if((pBase[-100] & 0x07) == eBaseN)
					NumNs -= 1;
				}
			if((*pBase & 0x07) == eBaseN)
				NumNs += 1;
			if(NumNs > MaxNs)
				break;
			}

		if(NumNs > MaxNs ||
				(ReadLen <= 100 && NumNs > ((MaxNs * ReadLen)/100)))
			{
			NumExcessNs += 1;
			continue;
			}

		if((Rslt=AddSeq(ReadLen,pSeq)) !=eBSFSuccess)
			break;

		if(m_Seqs2AssembLen > cMaxConcatSeqLen)			// hit limit?
			{
			gDiagnostics.DiagOut(eDLFatal,gszProcName,"LoadContigs: Length of loaded concatenated sequences exceeds limit of %lld bytes",cMaxConcatSeqLen);
			Rslt = eBSFerrMaxDirEls;
			break;
			}
		continue;
		}
	else
		{
		gDiagnostics.DiagOut(eDLFatal,gszProcName,"raw sequence file '%s' processing error: %s ",pszFile,
				Rslt > 0 ? "over length read" : "not a multifasta short reads or fastq file");
		Fasta.Close();
		return(eBSFerrParse);
		}
	}
if(pRawContigsBuff != NULL)
	delete pRawContigsBuff;

if(Rslt != eBSFSuccess)
	{
	gDiagnostics.DiagOut(eDLFatal,gszProcName,"Errors processing file: %s ",pszFile);
	while(Fasta.NumErrMsgs())
		gDiagnostics.DiagOut(eDLFatal,gszProcName,Fasta.GetErrMsg());
	Fasta.Close();
	return(Rslt);
	}
Fasta.Close();

NumAcceptedContigs = NumDescrContigs - (NumUnderlen + NumExcessNs);
m_TotSeqsParsed += NumDescrContigs;
m_TotSeqsUnderLen += NumUnderlen;
m_TotSeqsExcessNs += NumExcessNs;
m_TotSeqs2Assemb += NumAcceptedContigs;
m_RdsSfxHdr.RdsSrcFiles[m_RdsSfxHdr.NumSrcFiles++].NumContigs = NumAcceptedContigs;
gDiagnostics.DiagOut(eDLInfo,gszProcName,"LoadContigs: Completed parsing %d sequences from '%s'",NumDescrContigs,pszFile);
gDiagnostics.DiagOut(eDLInfo,gszProcName,"LoadContigs: %d were under length %d, %d excessive indeterminate (%d Ns), sequences retained: %1.9d",NumUnderlen,MinSeqLen,NumExcessNs,MaxNs, NumAcceptedContigs);
m_NumRawFiles += 1;
return((teBSFrsltCodes)NumAcceptedContigs);
}
Esempio n. 6
0
// LoadContigs
// Load pre-processed reads (as generated by kangar) from '.rds' structured file
// If unable to load as .rds then will try and load with LoadRawContigs()
teBSFrsltCodes
CHomozyReduce::LoadContigs(int MaxNs,				// filter out input sequences having higher than this number of indeterminate bases per 100bp (default is 1, range 0..10)
					int Trim5,				// trim this number of 5' bases from input sequences (default is 0, range 0..20)
					int Trim3,				// trim this number of 3' bases from input sequences (default is 0, range 0..20)
					int MinSeqLen,		    // filter out input sequences (after any trimming) which are less than this length (default is 50bp, range 30..10000)
					 int FileID,			// uniquely identifies source file
					 char *pszFile)			// file containing contigs
{
teBSFrsltCodes Rslt;
int NumContigs;
int NumUnderlen;
int NumExcessNs;
int RdLen;
tsRawReadV5 *pReadV5;						// current pre-processed read being processed
tsRawReadV6 *pReadV6;						// current pre-processed read being processed
int SizeOfRawRead;
int CurReadLen;
int CurDescrLen;

UINT8 ReadBuff[0x0ffff];				// used to buffer pre-processed reads

etSeqBase *pSeq;
int ReadLen;
int NumAcceptedContigs;

int BuffLen;
int BuffOfs;
UINT8 *pSrcSeq;							// used when copying read sequence (src) into concatenated sequence (dst)
tsBSFRdsHdr RdsHdr;

gDiagnostics.DiagOut(eDLInfo,gszProcName,"Loading sequences from: %s",pszFile);

#ifdef _WIN32
m_hInFile = open(pszFile, O_READSEQ ); // file access is normally sequential..
#else
m_hInFile = open64(pszFile, O_READSEQ );
#endif

if(m_hInFile == -1)					// check if file open succeeded
	{
	gDiagnostics.DiagOut(eDLFatal,gszProcName,"Unable to open %s - %s",pszFile,strerror(errno));
	Reset(false);
	return(eBSFerrOpnFile);
	}

// expecting a pre-processed .rds file as input, header processing will confirm!
// if not a bioseq .rds then will try processing as fasta
if((Rslt=Disk2Hdr(&RdsHdr,pszFile))!=eBSFSuccess)
	{
	if(Rslt == eBSFerrNotBioseq)
		return(LoadRawContigs(MaxNs,Trim5,Trim3,MinSeqLen,FileID,pszFile));		// try as raw fasta
	Reset(true);
	return((teBSFrsltCodes)Rslt);
	}

gDiagnostics.DiagOut(eDLInfo,gszProcName,"Contigs file '%s' generator was version: %d",pszFile,RdsHdr.Version);
gDiagnostics.DiagOut(eDLInfo,gszProcName,"Contains %d sequences from %d source sequences with duplicate sequences %s, PMode was %d, %d bases 5' trimmed, %d bases 3' trimmed",
		RdsHdr.NumRds,RdsHdr.OrigNumReads,RdsHdr.FlagsK ? "retained":"removed", RdsHdr.PMode,RdsHdr.Trim5,RdsHdr.Trim3);
gDiagnostics.DiagOutMsgOnly(eDLInfo,"Sequences were originally processed from %d files",RdsHdr.NumFiles);
char *pszSrcFile = (char *)RdsHdr.FileNames;
for(BuffOfs=0;BuffOfs<RdsHdr.NumFiles;BuffOfs++)
	{
	gDiagnostics.DiagOutMsgOnly(eDLInfo,"Source file: '%s'",pszSrcFile);
	pszSrcFile += strlen(pszSrcFile) + 1;
	}

// ensure there is at least one read...
if(RdsHdr.NumRds == 0 || RdsHdr.TotReadsLen == 0)
	{
	gDiagnostics.DiagOut(eDLFatal,gszProcName,"Nothing to do, '%s' contains no reads...",pszFile);
	Reset(true);
	return(eBSFerrOpnFile);
	}

if(RdsHdr.FlagsCS)
	{
	gDiagnostics.DiagOut(eDLFatal,gszProcName,"Basespace assembly requested but sequences from '%s' are in SOLiD colorspace...",pszFile);
	Reset(false);
	return(eBSFerrOpnFile);
	}

m_RdsSfxHdr.RdsSrcFiles[m_RdsSfxHdr.NumSrcFiles].SrcFileID = FileID;
strncpy((char *)m_RdsSfxHdr.RdsSrcFiles[m_RdsSfxHdr.NumSrcFiles].SrcFileName,pszFile,sizeof(m_RdsSfxHdr.RdsSrcFiles[m_RdsSfxHdr.NumSrcFiles].SrcFileName)-1);

BuffLen = 0;
BuffOfs = 0;
NumContigs = 0;
NumUnderlen = 0;
NumExcessNs = 0;

// tsRawRead struct size is version dependent
SizeOfRawRead = RdsHdr.Version < 6 ? sizeof(tsRawReadV5) : sizeof(tsRawReadV6);

// iterate each read sequence starting from the first
lseek(m_hInFile,(long)RdsHdr.RdsOfs,SEEK_SET);
while((RdLen = read(m_hInFile,&ReadBuff[BuffLen],sizeof(ReadBuff) - BuffLen)) > 0)
	{
	BuffLen += RdLen;
	BuffOfs = 0;
	while((BuffLen - BuffOfs) >=  SizeOfRawRead)
		{
		if(RdsHdr.Version < 6)
			{
			pReadV5 = (tsRawReadV5 *)&ReadBuff[BuffOfs];
			pSrcSeq = &pReadV5->Read[pReadV5->DescrLen+1];
			CurDescrLen = pReadV5->DescrLen;
			CurReadLen = pReadV5->ReadLen;
			}
		else
			{
			pReadV6 = (tsRawReadV6 *)&ReadBuff[BuffOfs];
			pSrcSeq = &pReadV6->Read[pReadV6->DescrLen+1];
			CurDescrLen = pReadV6->DescrLen;
			CurReadLen = pReadV6->ReadLen;
			}


		if((int)(CurDescrLen + CurReadLen + SizeOfRawRead) > (BuffLen - BuffOfs))
			break;
		BuffOfs += CurDescrLen + CurReadLen + SizeOfRawRead;
		// pRead now pts at a read

		NumContigs += 1;
		pSeq = pSrcSeq;
		ReadLen = CurReadLen;

		if(ReadLen < (Trim5 + Trim3 + MinSeqLen))
			{
			NumUnderlen += 1;
			continue;
			}

		// trim 5' and 3' as requested
		if(Trim5 > 0)
			{
			pSeq += Trim5;
			ReadLen -= Trim5;
			}
		ReadLen -= Trim3;

		// check for excessive number of Ns
		int Idx;
		int NumNs = 0;		// number of indeterminate bases in last 100bp window
		etSeqBase *pBase = pSeq;
		for(Idx = 0; Idx < ReadLen; Idx++,pBase++)
			{
			if(Idx >= 100)
				{
				if((pBase[-100] & 0x07) == eBaseN)
					NumNs -= 1;
				}
			if((*pBase & 0x07) == eBaseN)
				NumNs += 1;
			if(NumNs > MaxNs)
				break;
			}

		if(NumNs > MaxNs ||
				(ReadLen <= 100 && NumNs > ((MaxNs * ReadLen)/100)))
			{
			NumExcessNs += 1;
			continue;
			}

		if(m_Seqs2AssembLen * 2 > m_CurMaxMemWorkSetBytes)
			{
			if(!SetMaxMemWorkSetSize(m_Seqs2AssembLen * 5))
				{
				gDiagnostics.DiagOut(eDLFatal,gszProcName,"LoadContigs: Length of loaded concatenated sequences exceeds limit of %lld bytes",cMaxConcatSeqLen);
				Rslt = eBSFerrMaxDirEls;
				break;
				}
			}
		if((Rslt=AddSeq(ReadLen,pSeq))!=eBSFSuccess)
				break;

		}
	if(m_RdsSfxHdr.ConcatSeqLen > cMaxConcatSeqLen)			// hit limit?
		{
		gDiagnostics.DiagOut(eDLFatal,gszProcName,"LoadContigs: Loaded length of concatenated sequences exceeds limit of %lld bytes",cMaxConcatSeqLen);
		Rslt = eBSFerrMaxDirEls;
		break;
		}

	BuffLen -= BuffOfs;
	if(BuffLen)
		memcpy(ReadBuff,&ReadBuff[BuffOfs],BuffLen);
	}
close(m_hInFile);		// reads all loaded from the .rds file so can now close
m_hInFile = -1;

NumAcceptedContigs = NumContigs - (NumUnderlen + NumExcessNs);
m_TotSeqsParsed += NumContigs;
m_TotSeqsUnderLen += NumUnderlen;
m_TotSeqsExcessNs += NumExcessNs;
m_TotSeqs2Assemb += NumAcceptedContigs;
m_RdsSfxHdr.RdsSrcFiles[m_RdsSfxHdr.NumSrcFiles++].NumContigs = NumAcceptedContigs;
if(Rslt == eBSFSuccess)
	{
	gDiagnostics.DiagOut(eDLInfo,gszProcName,"LoadContigs: Completed parsing %d sequences from '%s'",NumContigs,pszFile);
	gDiagnostics.DiagOut(eDLInfo,gszProcName,"LoadContigs: %d were under length %d, %d excessive indeterminate (%d Ns), sequences retained: %1.9d",NumUnderlen,MinSeqLen,NumExcessNs,MaxNs,NumAcceptedContigs);
	}
m_NumRawFiles += 1;
return(Rslt == eBSFSuccess ? (teBSFrsltCodes)NumAcceptedContigs : Rslt);
}
Esempio n. 7
0
void MAlignment::AddSeq(vector<vector<string> > &  a){
	for(unsigned int i=0; i < a.size();i++){
		AddSeq(a[i]);
	}
	return;
}
Esempio n. 8
0
bool Alignment::AddSeqs(vector< vector<string> > const & input){
	for(unsigned int i=0; i < input.size();i++)
		if(AddSeq(input[i]) == 0)
			return 0;
	return 1;
}
static void
AliasHandleIrcOut(struct libalias *la,
    struct ip *pip,		/* IP packet to examine */
    struct alias_link *lnk,	/* Which link are we on? */
    int maxsize			/* Maximum size of IP packet including
				 * headers */
)
{
	int hlen, tlen, dlen;
	struct in_addr true_addr;
	u_short true_port;
	char *sptr;
	struct tcphdr *tc;
	int i;			/* Iterator through the source */

/* Calculate data length of TCP packet */
	tc = (struct tcphdr *)ip_next(pip);
	hlen = (pip->ip_hl + tc->th_off) << 2;
	tlen = ntohs(pip->ip_len);
	dlen = tlen - hlen;

	/*
	 * Return if data length is too short - assume an entire PRIVMSG in
	 * each packet.
	 */
	if (dlen < (int)sizeof(":[email protected] PRIVMSG A :aDCC 1 1a") - 1)
		return;

/* Place string pointer at beginning of data */
	sptr = (char *)pip;
	sptr += hlen;
	maxsize -= hlen;	/* We're interested in maximum size of
				 * data, not packet */

	/* Search for a CTCP command [Note 1] */
	for (i = 0; i < dlen; i++) {
		if (sptr[i] == '\001')
			goto lFOUND_CTCP;
	}
	return;			/* No CTCP commands in  */
	/* Handle CTCP commands - the buffer may have to be copied */
lFOUND_CTCP:
	{
		char newpacket[65536];	/* Estimate of maximum packet size
					 * :) */
		unsigned int copyat = i;	/* Same */
		unsigned int iCopy = 0;	/* How much data have we written to
					 * copy-back string? */
		unsigned long org_addr;	/* Original IP address */
		unsigned short org_port;	/* Original source port
						 * address */

lCTCP_START:
		if (i >= dlen || iCopy >= sizeof(newpacket))
			goto lPACKET_DONE;
		newpacket[iCopy++] = sptr[i++];	/* Copy the CTCP start
						 * character */
		/* Start of a CTCP */
		if (i + 4 >= dlen)	/* Too short for DCC */
			goto lBAD_CTCP;
		if (sptr[i + 0] != 'D')
			goto lBAD_CTCP;
		if (sptr[i + 1] != 'C')
			goto lBAD_CTCP;
		if (sptr[i + 2] != 'C')
			goto lBAD_CTCP;
		if (sptr[i + 3] != ' ')
			goto lBAD_CTCP;
		/* We have a DCC command - handle it! */
		i += 4;		/* Skip "DCC " */
		if (iCopy + 4 > sizeof(newpacket))
			goto lPACKET_DONE;
		newpacket[iCopy++] = 'D';
		newpacket[iCopy++] = 'C';
		newpacket[iCopy++] = 'C';
		newpacket[iCopy++] = ' ';

		DBprintf(("Found DCC\n"));
		/*
		 * Skip any extra spaces (should not occur according to
		 * protocol, but DCC breaks CTCP protocol anyway
		 */
		while (sptr[i] == ' ') {
			if (++i >= dlen) {
				DBprintf(("DCC packet terminated in just spaces\n"));
				goto lPACKET_DONE;
			}
		}

		DBprintf(("Transferring command...\n"));
		while (sptr[i] != ' ') {
			newpacket[iCopy++] = sptr[i];
			if (++i >= dlen || iCopy >= sizeof(newpacket)) {
				DBprintf(("DCC packet terminated during command\n"));
				goto lPACKET_DONE;
			}
		}
		/* Copy _one_ space */
		if (i + 1 < dlen && iCopy < sizeof(newpacket))
			newpacket[iCopy++] = sptr[i++];

		DBprintf(("Done command - removing spaces\n"));
		/*
		 * Skip any extra spaces (should not occur according to
		 * protocol, but DCC breaks CTCP protocol anyway
		 */
		while (sptr[i] == ' ') {
			if (++i >= dlen) {
				DBprintf(("DCC packet terminated in just spaces (post-command)\n"));
				goto lPACKET_DONE;
			}
		}

		DBprintf(("Transferring filename...\n"));
		while (sptr[i] != ' ') {
			newpacket[iCopy++] = sptr[i];
			if (++i >= dlen || iCopy >= sizeof(newpacket)) {
				DBprintf(("DCC packet terminated during filename\n"));
				goto lPACKET_DONE;
			}
		}
		/* Copy _one_ space */
		if (i + 1 < dlen && iCopy < sizeof(newpacket))
			newpacket[iCopy++] = sptr[i++];

		DBprintf(("Done filename - removing spaces\n"));
		/*
		 * Skip any extra spaces (should not occur according to
		 * protocol, but DCC breaks CTCP protocol anyway
		 */
		while (sptr[i] == ' ') {
			if (++i >= dlen) {
				DBprintf(("DCC packet terminated in just spaces (post-filename)\n"));
				goto lPACKET_DONE;
			}
		}

		DBprintf(("Fetching IP address\n"));
		/* Fetch IP address */
		org_addr = 0;
		while (i < dlen && isdigit(sptr[i])) {
			if (org_addr > ULONG_MAX / 10UL) {	/* Terminate on overflow */
				DBprintf(("DCC Address overflow (org_addr == 0x%08lx, next char %c\n", org_addr, sptr[i]));
				goto lBAD_CTCP;
			}
			org_addr *= 10;
			org_addr += sptr[i++] - '0';
		}
		DBprintf(("Skipping space\n"));
		if (i + 1 >= dlen || sptr[i] != ' ') {
			DBprintf(("Overflow (%d >= %d) or bad character (%02x) terminating IP address\n", i + 1, dlen, sptr[i]));
			goto lBAD_CTCP;
		}
		/*
		 * Skip any extra spaces (should not occur according to
		 * protocol, but DCC breaks CTCP protocol anyway, so we
		 * might as well play it safe
		 */
		while (sptr[i] == ' ') {
			if (++i >= dlen) {
				DBprintf(("Packet failure - space overflow.\n"));
				goto lPACKET_DONE;
			}
		}
		DBprintf(("Fetching port number\n"));
		/* Fetch source port */
		org_port = 0;
		while (i < dlen && isdigit(sptr[i])) {
			if (org_port > 6554) {	/* Terminate on overflow
						 * (65536/10 rounded up */
				DBprintf(("DCC: port number overflow\n"));
				goto lBAD_CTCP;
			}
			org_port *= 10;
			org_port += sptr[i++] - '0';
		}
		/* Skip illegal addresses (or early termination) */
		if (i >= dlen || (sptr[i] != '\001' && sptr[i] != ' ')) {
			DBprintf(("Bad port termination\n"));
			goto lBAD_CTCP;
		}
		DBprintf(("Got IP %lu and port %u\n", org_addr, (unsigned)org_port));

		/* We've got the address and port - now alias it */
		{
			struct alias_link *dcc_lnk;
			struct in_addr destaddr;


			true_port = htons(org_port);
			true_addr.s_addr = htonl(org_addr);
			destaddr.s_addr = 0;

			/* Sanity/Security checking */
			if (!org_addr || !org_port ||
			    pip->ip_src.s_addr != true_addr.s_addr ||
			    org_port < IPPORT_RESERVED)
				goto lBAD_CTCP;

			/*
			 * Steal the FTP_DATA_PORT - it doesn't really
			 * matter, and this would probably allow it through
			 * at least _some_ firewalls.
			 */
			dcc_lnk = FindUdpTcpOut(la, true_addr, destaddr,
			    true_port, 0,
			    IPPROTO_TCP, 1);
			DBprintf(("Got a DCC link\n"));
			if (dcc_lnk) {
				struct in_addr alias_address;	/* Address from aliasing */
				u_short alias_port;	/* Port given by
							 * aliasing */
				int n;

#ifndef NO_FW_PUNCH
				/* Generate firewall hole as appropriate */
				PunchFWHole(dcc_lnk);
#endif

				alias_address = GetAliasAddress(lnk);
				n = snprintf(&newpacket[iCopy],
				    sizeof(newpacket) - iCopy,
				    "%lu ", (u_long) htonl(alias_address.s_addr));
				if (n < 0) {
					DBprintf(("DCC packet construct failure.\n"));
					goto lBAD_CTCP;
				}
				if ((iCopy += n) >= sizeof(newpacket)) {	/* Truncated/fit exactly
										 * - bad news */
					DBprintf(("DCC constructed packet overflow.\n"));
					goto lBAD_CTCP;
				}
				alias_port = GetAliasPort(dcc_lnk);
				n = snprintf(&newpacket[iCopy],
				    sizeof(newpacket) - iCopy,
				    "%u", htons(alias_port));
				if (n < 0) {
					DBprintf(("DCC packet construct failure.\n"));
					goto lBAD_CTCP;
				}
				iCopy += n;
				/*
				 * Done - truncated cases will be taken
				 * care of by lBAD_CTCP
				 */
				DBprintf(("Aliased IP %lu and port %u\n", alias_address.s_addr, (unsigned)alias_port));
			}
		}
		/*
		 * An uninteresting CTCP - state entered right after '\001'
		 * has been pushed.  Also used to copy the rest of a DCC,
		 * after IP address and port has been handled
		 */
lBAD_CTCP:
		for (; i < dlen && iCopy < sizeof(newpacket); i++, iCopy++) {
			newpacket[iCopy] = sptr[i];	/* Copy CTCP unchanged */
			if (sptr[i] == '\001') {
				goto lNORMAL_TEXT;
			}
		}
		goto lPACKET_DONE;
		/* Normal text */
lNORMAL_TEXT:
		for (; i < dlen && iCopy < sizeof(newpacket); i++, iCopy++) {
			newpacket[iCopy] = sptr[i];	/* Copy CTCP unchanged */
			if (sptr[i] == '\001') {
				goto lCTCP_START;
			}
		}
		/* Handle the end of a packet */
lPACKET_DONE:
		iCopy = iCopy > maxsize - copyat ? maxsize - copyat : iCopy;
		memcpy(sptr + copyat, newpacket, iCopy);

/* Save information regarding modified seq and ack numbers */
		{
			int delta;

			SetAckModified(lnk);
			delta = GetDeltaSeqOut(pip, lnk);
			AddSeq(pip, lnk, delta + copyat + iCopy - dlen);
		}

		/* Revise IP header */
		{
			u_short new_len;

			new_len = htons(hlen + iCopy + copyat);
			DifferentialChecksum(&pip->ip_sum,
			    &new_len,
			    &pip->ip_len,
			    1);
			pip->ip_len = new_len;
		}

		/* Compute TCP checksum for revised packet */
		tc->th_sum = 0;
#ifdef _KERNEL
		tc->th_x2 = 1;
#else
		tc->th_sum = TcpChecksum(pip);
#endif
		return;
	}
}