Exemplo n.º 1
0
void AsmPass1(Assembler *a, char *text) {             // 第一階段的組譯           
  int i, address = 0, number;                                                 
  Array* lines = split(text, "\r\n", REMOVE_SPLITER); // 將組合語言分割成一行一行
  ArrayEach(lines, strPrintln);                       // 印出以便觀察           
  printf("=================PASS1================\n");               
  for (i=0; i<lines->count; i++) {                    // 對於每一行                        
      strReplace(lines->item[i], ";", '\0');
      strReplace(lines->item[i], "\t", ' ');
      AsmCode *code = AsmCodeNew(lines->item[i]);     // 建立指令物件
      if (code == NULL) continue;
      code->address = address;                        // 設定該行的位址      
      Op *op = NULL;
      if (code->op != NULL) {
        op = HashTableGet(opTable, code->op);           // 查詢運算碼            
        if (op != NULL) {                               // 如果查到
          code->opCode = op->code;                      //    設定運算碼
          code->type = op->type;                        //    設定型態
        }                                                  
      }
      if (code->label != NULL)                        // 如果有標記符號
        HashTablePut(a->symTable, code->label, code); //    加入符號表中
      ArrayAdd(a->codes, code);                       //  建構指令物件陣列
      AsmCodePrintln(code);                           //    印出觀察
      code->size = AsmCodeSize(code);                 //  計算指令大小
      address += code->size;                          //  計算下一個指令位址
  }                                                                           
  ArrayFree(lines, strFree);                          // 釋放記憶體
}
Exemplo n.º 2
0
int transfer_SAM_to_position_table(char * sam_file)
{
	char linebuf[max(2*MAX_READ_LENGTH+300,3000)];
	int allreads=0,mapped=0;
	char mate_chro[MAX_CHROMOSOME_NAME_LEN+1];
	unsigned int mate_pos;
	int flags_mate;
	char cigar_mate[EXON_MAX_CIGAR_LEN+1];
	gene_input_t input_file;

	HashTable * local_reassembly_pileup_files;
	HashTable * chromosome_size_table;


	chromosome_size_table = HashTableCreate(100);
	HashTableSetDeallocationFunctions(chromosome_size_table, NULL, NULL);
	HashTableSetKeyComparisonFunction(chromosome_size_table, my_strcmp);
	HashTableSetHashFunction(chromosome_size_table, HashTableStringHashFunction);

	local_reassembly_pileup_files = HashTableCreate(100);
	HashTableSetDeallocationFunctions(local_reassembly_pileup_files, NULL, NULL);
	HashTableSetKeyComparisonFunction(local_reassembly_pileup_files, my_strcmp);
	HashTableSetHashFunction(local_reassembly_pileup_files ,HashTableStringHashFunction);

	geinput_open_sam(sam_file, &input_file,0);

	while(1)
	{
		unsigned int read_pos =0xffffffff;
		char read_name[MAX_READ_NAME_LEN+1];
		int flags;
		char chro_name[MAX_CHROMOSOME_NAME_LEN+1];
		unsigned int pos, pair_dist;
		char cigar[EXON_MAX_CIGAR_LEN+1];
		char read_text[MAX_READ_LENGTH+1];
		char qual_text[MAX_READ_LENGTH+1];
		int read_len, is_repeated, mapping_quality, is_anchor_certain=1;
		int pos_delta = 0;
		int is_paired_end_reads = 0;

		unsigned long long int file_offset = ftello(input_file.input_fp);
		if(feof(input_file.input_fp))break;

		read_text[0]=0;
		qual_text[0]=0;
		geinput_readline(&input_file, linebuf,0);
		int res = parse_SAM_line(linebuf, read_name,& flags, chro_name, & pos, cigar, & mapping_quality, & pair_dist, read_text , qual_text, & read_len, & is_repeated);
		int cigar_cursor = 0;
		int firstM = 1,xx=0;
		is_paired_end_reads = flags &1;
		if(res==0)
		{
			for(; cigar[cigar_cursor]; cigar_cursor++)
			{
				char nch = cigar[cigar_cursor]; 
				if(nch>='0'&&nch<='9')
				{
					xx=xx*10+(nch-'0');
				}else
				{
					if(nch=='M') firstM=0;
					else if(nch=='S')
					{
						if(firstM)
							pos_delta = xx;
					}
					xx=0;
				}
			}

			pos -= pos_delta;
		}

		if(res == 1) {chro_name[0]='*'; chro_name[1]=0;}
		//printf("MAPPED=%d\n", res);

		if(res == 0)	// mapped
		{

			read_pos = pos - 1;
			mapped++;
		}
		else if(res == 1 && is_paired_end_reads)	// unmapped
		{
			is_anchor_certain=0;
			if(mate_chro[0])
			{
				if(mate_chro[0]!='*')
				{
					read_pos = mate_pos ;
					strcpy(chro_name, mate_chro);
				}
				//printf("RECOVERED 1: %u - %s ; LEN=%d ; YOU_ARE_FIRST=%d\n%s\n%s\n", read_anchor_position,  read_name, read_len, flags_mate & SAM_FLAG_FIRST_READ_IN_PAIR, read_text, qual_text);
			}
			else
			{
				char read_text_null[MAX_READ_LENGTH+1];
				char qual_text_null[MAX_READ_LENGTH+1];

				geinput_readline_back(&input_file, linebuf);
				res = parse_SAM_line(linebuf, read_name,& flags_mate, mate_chro, & mate_pos, cigar_mate, & mapping_quality, & pair_dist, read_text_null , qual_text_null, & read_len, & is_repeated);
				if(res==0)
				{
					read_pos = mate_pos - 1;
					strcpy(chro_name, mate_chro);
				}
				//printf("RECOVERED 2: %u - %s ; LEN=%d ; YOU_ARE_FIRST=%d\n%s\n%s\n", read_anchor_position,  read_name, read_len, flags_mate & SAM_FLAG_FIRST_READ_IN_PAIR, read_text, qual_text);
			}
		}

		if(read_pos<0xffff0000)
		{

			unsigned int read_span = calculate_read_span(cigar);

			write_read_pos(chro_name, read_pos, read_len, flags, file_offset, cigar, read_span, local_reassembly_pileup_files);
			unsigned int old_max_pos = HashTableGet(chromosome_size_table, chro_name) - NULL;
			if(old_max_pos==0)
			{
				char * chro_name_new = malloc(strlen(chro_name)+1);
				strcpy(chro_name_new , chro_name);
				
				HashTablePut(chromosome_size_table, chro_name_new, NULL+read_pos + read_span+1);
			}
			else if(read_pos + read_span +1 > old_max_pos )
				HashTablePutReplace(chromosome_size_table, chro_name, NULL+read_pos + read_span+1, 0);
		}

		if(allreads %2==1)
			mate_chro[0] = 0;
		else
		{
			strcpy(mate_chro, chro_name);
			mate_pos = pos - 1;
			flags_mate = flags;
			
		}
		allreads++;

	}
	SUBREADprintf("Processed %d reads; %d mapped.\n", allreads, mapped);

	destroy_pileup_table(local_reassembly_pileup_files);


	finalise_sam_index(chromosome_size_table, sam_file);

	HashTableDestroy(chromosome_size_table);

	return 0;
}
int load_junc_table(char * file_name, HashTable * tab, HashTable * edge_table_l, HashTable * edge_table_r, HashTable * bin_table)
{
	FILE * fp=f_subr_open(file_name, "r");
	char new_fl[200];
	if(!fp) return -1;
	while(1)
	{
		char * ll = fgets(new_fl, 199, fp);
		char * tmp_tok=NULL;
		if(!ll || strlen(ll)<4) break;

		strtok_r(ll, "\t", &tmp_tok);    //name
		char * chrostr = strtok_r(NULL, "\t", &tmp_tok);  //chro
		char * pos1str = strtok_r(NULL, "\t", &tmp_tok);  //pos1
		char * pos2str = strtok_r(NULL, "\t", &tmp_tok);  //pos2

		unsigned int pos1 = (unsigned int)atoll(pos1str); 
		unsigned int pos2 = (unsigned int)atoll(pos2str); 

		if(pos1==0||pos2==0)continue;

		char * chro_mem = malloc(30);
		chro_mem[0]=0;
		if(strlen(chrostr)<3)strcpy(chro_mem, "chr");
		strcat(chro_mem, chrostr);

		long long int pos_key;
		if(pos2<pos1){
			HashTablePut(edge_table_l, NULL+pos2, chro_mem);
			HashTablePut(edge_table_r, NULL+pos1, chro_mem);
			pos_key = (pos2*1LLU<<32)|pos1;
		}
		else{
			HashTablePut(edge_table_l, NULL+pos1, chro_mem);
			HashTablePut(edge_table_r, NULL+pos2, chro_mem);
			pos_key = (pos1*1LLU<<32)|pos2;
		}

		HashTablePut(bin_table, NULL+pos1/50,NULL+pos1);
		HashTablePut(bin_table, NULL+pos2/50,NULL+pos2);
		HashTablePut(bin_table, NULL+pos1/50-1,NULL+pos1);
		HashTablePut(bin_table, NULL+pos2/50-1,NULL+pos2);
		HashTablePut(bin_table, NULL+pos1/50+1,NULL+pos1);
		HashTablePut(bin_table, NULL+pos2/50+1,NULL+pos2);

		HashTablePut(tab, (void *)pos_key, chro_mem);

		//printf("NEW JUNC: %s: %u - %u\n", chro_mem, pos1, pos2);
	}
	fclose(fp);
	return 0;
}