int msden (struct msscene *ms, FILE *fpt, FILE *fpe, FILE *fpo, double cube_width, char *format) { int result; char message[MAXLINE]; struct surface *den; struct surface *phn; sprintf (message, "%8.3f cube width", cube_width); inform (message); if (format == NULL) informd ("msform: no output format specified"); phn = read_polyhedron (fpt); if (error ()) return (0); if (phn == NULL) { set_error2 ("msform: problem reading polyhedron"); return (0); } ms -> this_srf = phn; den = polyhedron_to_density (phn, cube_width); if (error ()) return (0); if (den == NULL) return (0); phn -> next = den; result = write_density (den, fpo); if (error ()) return (0); if (result == 0) return (0); fclose(fpo); return(1); }
void ahf_gridinfo(gridls *grid_list, int curgrid_no) { gridls *cur_grid; pqptr cur_pquad; cqptr cur_cquad, icur_cquad; nqptr cur_nquad, icur_nquad; nptr cur_node; int refinecounter; long x,y,z; /* The colouring tools */ int colCounter=0; int colREPLACE; int currentCol; nptr tsc_nodes[3][3][3]; /* nodes to assign to */ int firstCOLOUR; SPATIALREF *spatialRefHead; SPATIALREF *tmpSpatialRef; SPATIALREF *current; #ifdef AHFgridinfofile FILE *gridinfofile; char filename1[100]; #endif int i,tmp; int boundCount; int numWrongNodes, colRight; #ifdef AHFDEBUG char filename2[100]; FILE *fout; #endif intXYZ tmpPeriodic; int colour; SPATIALREF *previous; int iterate, startAHFgrid; double a, a3, omega, ovlim, rho_vir; double fl1dim, refine_len, refine_vol, refine_ovdens, refine_mass; double cur_shift; #ifdef VERBOSE /***************************************************************************/ fprintf(stderr,"################## ahf_gridinfo ###################\n"); fprintf(stderr,"Number of grids = %d\n", curgrid_no+1); #endif fprintf(io.logfile,"################## ahf_gridinfo ###################\n"); fprintf(io.logfile,"Number of grids = %d\n", curgrid_no+1); fflush(io.logfile); /* total number of grids to consider for AHF */ /* ahf.no_grids = curgrid_no - global.domgrid_no + 1;*/ ahf.no_grids = curgrid_no - global.domgrid_no; #ifdef VERBOSE fprintf(stderr,"Number of refinements = %d\n", ahf.no_grids); fprintf(stderr,"global.domgrid_no = %d\n", global.domgrid_no); #endif fprintf(io.logfile,"Number of refinements = %d\n", ahf.no_grids); fprintf(io.logfile,"global.domgrid_no = %d\n", global.domgrid_no); #ifdef DEBUG_GRIDS for(cur_grid=global.dom_grid+1; cur_grid<=global.dom_grid+ahf.no_grids; cur_grid++) { fprintf(stderr,"writing l1dim = %ld ... ",cur_grid->l1dim); //output_grid(cur_grid,0); write_density(cur_grid,"cur_grid-"); fprintf(stderr,"done\n"); } exit(0); #endif /* cosmology related stuff */ a = global.a; a3 = pow3(a); omega = calc_omega(a); ovlim = calc_virial(a); rho_vir = a3 * calc_rho_vir(a); /* comoving(!) density used to normalize densities */ /* get the number of the grid satisfying virial overdensity criterion */ refine_mass = simu.pmass*simu.med_weight; cur_grid = global.dom_grid; startAHFgrid = 1; for ( i=0; i<=ahf.no_grids; i++) { fl1dim = ((double)(cur_grid->l1dim)); cur_grid++; refine_len = (double)(simu.boxsize/((double)(fl1dim))); refine_vol = pow3(refine_len); refine_ovdens = (simu.Nth_ref*refine_mass/refine_vol) / rho_vir; #ifdef VERBOSE fprintf(stderr,"l1dim = %16.0f refine_ovdens = %16.4g ovlim = %16.4g\n", fl1dim,refine_ovdens,ovlim); #endif fprintf(io.logfile,"l1dim = %16.0f refine_ovdens = %16.4g ovlim = %16.4g\n", fl1dim,refine_ovdens,ovlim); if ( refine_ovdens < ovlim ) startAHFgrid = i; } /* store maximum overdensity possible with current refinement hierarchy (used by ahf_halos()!) */ global.max_ovdens = refine_ovdens; #ifdef VERBOSE fprintf(stderr,"max_ovdens = %g\n",global.max_ovdens); #endif fprintf(io.logfile,"max_ovdens = %g\n",global.max_ovdens); /* the first refinement grid to consider */ ahf.min_ref = startAHFgrid+AHF_MIN_REF_OFFSET; /* total number of grids to be used with AHF */ ahf.no_grids = ahf.no_grids - ahf.min_ref + 1; #ifdef VERBOSE fprintf(stderr,"min_ref = %d (ahf_nogrids = %d)\n", ahf.min_ref,ahf.no_grids); #endif fprintf(io.logfile,"min_ref = %d (ahf_nogrids = %d)\n", ahf.min_ref,ahf.no_grids); fflush(io.logfile); /* Initialising important variables */ #ifdef VERBOSE fprintf(stderr," initialising force.color on all grids...\n"); #endif spatialRefHead = NULL; /* Setting the HEAD of the link list of SPATIALREF's */ spatialRefTail = NULL; colREPLACE = FALSE; firstCOLOUR = 1; #ifdef WITH_OPENMP // // this loop over all grids and all refinements generates a linked-list starting // with spatialRefHead that contains the spatially connected refinements independent // of the actual grid (that information is contained in spatialRefHead->refLevel) // // to parallelize one could do this generation independently on each grid, // but would need to merge all linked-lists afterwards // //#pragma omp parallel private(cur_grid, ipquad, cur_pquad, cur_cquad, icur_cquad, cur_nquad, icur_nquad, firstCOLOUR, spatialRefHead, spatialRefTail, cur_node) shared(refinecounter) //#pragma omp for schedule(static) #endif for( refinecounter=0; refinecounter<ahf.no_grids; refinecounter++ ) { cur_grid = global.dom_grid+ahf.min_ref+refinecounter; #ifdef VERBOSE /* fprintf() to double-check that we set min_ref and ahf.no_grids correctly! */ fprintf(stderr,"%12d || l1dim = %12ld\n", refinecounter, cur_grid->l1dim); #endif /************************************************************ * Initialising all the colour tages in preperation for the * Spatial refinement identification. * Therefore, we are zeroing all the colour tags on the current refinement ************************************************************/ for(cur_pquad=cur_grid->pquad; cur_pquad != NULL; cur_pquad=cur_pquad->next) { for(cur_cquad = cur_pquad->loc; cur_cquad < cur_pquad->loc + cur_pquad->length; cur_cquad++) { for(icur_cquad = cur_cquad; icur_cquad != NULL; icur_cquad = icur_cquad->next) { for(cur_nquad = icur_cquad->loc; cur_nquad < icur_cquad->loc + icur_cquad->length; cur_nquad++) { for(icur_nquad = cur_nquad; icur_nquad != NULL; icur_nquad = icur_nquad->next) { for(cur_node = icur_nquad->loc; cur_node < icur_nquad->loc + icur_nquad->length; cur_node++) { /* zeroing the colour */ cur_node->force.colour = 0; } } } } } } /************************************************************ * Start looping through the refinement identifying the spatial refinements ************************************************************/ for(cur_pquad=cur_grid->pquad; cur_pquad != NULL; cur_pquad=cur_pquad->next) { z = cur_pquad->z; for(cur_cquad = cur_pquad->loc; cur_cquad < cur_pquad->loc + cur_pquad->length; cur_cquad++, z++) { for(icur_cquad = cur_cquad; icur_cquad != NULL; icur_cquad = icur_cquad->next) { y = icur_cquad->y; for(cur_nquad = icur_cquad->loc; cur_nquad < icur_cquad->loc + icur_cquad->length; cur_nquad++, y++) { for(icur_nquad = cur_nquad; icur_nquad != NULL; icur_nquad = icur_nquad->next) { x = icur_nquad->x; for(cur_node = icur_nquad->loc; cur_node < icur_nquad->loc + icur_nquad->length; cur_node++, x++) { /* The current colour of the node */ currentCol = cur_node->force.colour; /* We have finished updating the colours */ if ( (colREPLACE==TRUE) && (currentCol==0) ) colREPLACE=FALSE; /* Replacing the colours */ if ( colREPLACE ) { if ( col.numReplace == 2 ) { if (currentCol==col.holder[2]) cur_node->force.colour=col.holder[1]; } else if ( col.numReplace == 3 ) { if (currentCol==col.holder[2]) cur_node->force.colour=col.holder[0]; } else if ( col.numReplace == 4 ) { if (currentCol==col.holder[2]) cur_node->force.colour=col.holder[0]; if (currentCol==col.holder[1]) cur_node->force.colour=col.holder[0]; } else { fprintf(stderr,"Something is wrong with colREPLACE"); } } /* New Node to check */ else { /* Getting the relevant nodes and filling colHolder */ /* search for pointers if any are null return NULL */ tsc_nodes[1][1][1] = cur_node; get_TSCnodes(cur_grid, cur_pquad, icur_cquad, icur_nquad, tsc_nodes, &z, &y, &x); /* Gathering the colour information for this node */ colourInfo(tsc_nodes); /************************************************/ /* all nodes are zero, numReplace=0 */ if ( col.numReplace == 0 ) { /* Start a new colour and keep going */ if ( (tmpSpatialRef = malloc(sizeof(SPATIALREF))) == NULL ) { fprintf(stderr,"No memory for SPATIALREF\n"); exit(1); } /* Colour the node with a unique name */ colCounter++; cur_node->force.colour = colCounter; /* Point SPATIALREF to the right information */ tmpSpatialRef->name = colCounter; tmpSpatialRef->periodic.x = 0; tmpSpatialRef->periodic.y = 0; tmpSpatialRef->periodic.z = 0; tmpSpatialRef->refLevel = refinecounter; tmpSpatialRef->cur_pquad = cur_pquad; tmpSpatialRef->cur_cquad = cur_cquad; tmpSpatialRef->icur_cquad = icur_cquad; tmpSpatialRef->cur_nquad = cur_nquad; tmpSpatialRef->icur_nquad = icur_nquad; tmpSpatialRef->cur_node = cur_node; tmpSpatialRef->x = x; tmpSpatialRef->y = y; tmpSpatialRef->z = z; /* Join the new SPATIALREF to the link list */ spatialRefTail = insertColour(tmpSpatialRef, firstCOLOUR); /* Now just for the first time we need to make head also point to this newColour */ if ( firstCOLOUR==1 ) { spatialRefHead = spatialRefTail; firstCOLOUR=0; } } /************************************************/ /* One node is non-zero, numReplace=1 */ if ( col.numReplace == 1 ) { /* set using the current colour and keep going */ cur_node->force.colour = col.holder[2]; } /************************************************/ /* Two nodes are non-zero and are not equal, numReplace=2 */ if ( col.numReplace == 2 ) { /* Replace the previous colours with the lowest colour */ /* set colour using the lowest value : in this case 1 */ cur_node->force.colour = col.holder[1]; if (!colREPLACE) { /* Find and destroy the redundant SPATIALREF */ tmpSpatialRef=deleteColour(spatialRefHead, col.holder[2]); /* STU :: This is where you start looking for the deleted colours */ cur_pquad = tmpSpatialRef->cur_pquad; cur_cquad = tmpSpatialRef->cur_cquad; icur_cquad = tmpSpatialRef->icur_cquad; cur_nquad = tmpSpatialRef->cur_nquad; icur_nquad = tmpSpatialRef->icur_nquad; cur_node = tmpSpatialRef->cur_node; x = tmpSpatialRef-> x; y = tmpSpatialRef-> y; z = tmpSpatialRef-> z; free(tmpSpatialRef); tmpSpatialRef = NULL; colREPLACE=TRUE; /* When going back to replace the colour the for loop progress the node by one - which we don't want to happen Hence, we have to do an inital sweep on the first node with the colour that is going to be deleted*/ /* Getting the relevant nodes and filling colHolder */ /* search for pointers if any are null return NULL */ currentCol = cur_node->force.colour; tsc_nodes[1][1][1] = cur_node; get_TSCnodes(cur_grid, cur_pquad, icur_cquad, icur_nquad, tsc_nodes, &z, &y, &x); /* When going back to replace the colour the for loop progress the node by one - which we don't want to happen */ if ( col.numReplace == 2 ) { if (currentCol==col.holder[2]) cur_node->force.colour=col.holder[1]; } else if ( col.numReplace == 3 ) { if (currentCol==col.holder[2]) cur_node->force.colour=col.holder[0]; } else if ( col.numReplace == 4 ) { if (currentCol==col.holder[2]) cur_node->force.colour=col.holder[0]; if (currentCol==col.holder[1]) cur_node->force.colour=col.holder[0]; } else { fprintf(stderr,"Something is wrong with colREPLACE"); } /*****************************************************************/ /*****************************************************************/ } } /************************************************/ /* Two nodes are non-zero and are not equal, numReplace=2 */ /* With the last two colour elements equal */ if ( col.numReplace == 3 ) { /* Replace the previous colours with the lowest colour */ /* set colour using the lowest value : in this case 1 */ cur_node->force.colour = col.holder[0]; if (!colREPLACE) { /* Find and destroy the redendent SPATIALREF */ tmpSpatialRef=deleteColour(spatialRefHead, col.holder[2]); cur_pquad = tmpSpatialRef->cur_pquad; cur_cquad = tmpSpatialRef->cur_cquad; icur_cquad = tmpSpatialRef->icur_cquad; cur_nquad = tmpSpatialRef->cur_nquad; icur_nquad = tmpSpatialRef->icur_nquad; cur_node = tmpSpatialRef->cur_node; x = tmpSpatialRef-> x; y = tmpSpatialRef-> y; z = tmpSpatialRef-> z; free(tmpSpatialRef); tmpSpatialRef = NULL; colREPLACE=TRUE; /*****************************************************************/ /* Getting the relevant nodes and filling colHolder */ /* search for pointers if any are null return NULL */ currentCol = cur_node->force.colour; tsc_nodes[1][1][1] = cur_node; get_TSCnodes(cur_grid, cur_pquad, icur_cquad, icur_nquad, tsc_nodes, &z, &y, &x); /* When going back to replace the colour the for loop progress the node by one - which we don't want to happen */ if ( col.numReplace == 2 ) { if (currentCol==col.holder[2]) cur_node->force.colour=col.holder[1]; } else if ( col.numReplace == 3 ) { if (currentCol==col.holder[2]) cur_node->force.colour=col.holder[0]; } else if ( col.numReplace == 4 ) { if (currentCol==col.holder[2]) cur_node->force.colour=col.holder[0]; if (currentCol==col.holder[1]) cur_node->force.colour=col.holder[0]; } else { fprintf(stderr,"Something is wrong with colREPLACE"); } /*****************************************************************/ /*****************************************************************/ } } /************************************************/ /* Three node are non-zero and are not equal, numReplace=3*/ if ( col.numReplace == 4 ) { /* Loop back and replace the dupulacates */ /* set colour using the lowest value : in this case 0 */ cur_node->force.colour = col.holder[0]; if (!colREPLACE) { /* Find and destroy the redendent SPATIALREF's */ tmpSpatialRef=deleteColour(spatialRefHead, col.holder[2]); free(tmpSpatialRef); tmpSpatialRef=deleteColour(spatialRefHead, col.holder[1]); cur_pquad = tmpSpatialRef->cur_pquad; cur_cquad = tmpSpatialRef->cur_cquad; icur_cquad = tmpSpatialRef->icur_cquad; cur_nquad = tmpSpatialRef->cur_nquad; icur_nquad = tmpSpatialRef->icur_nquad; cur_node = tmpSpatialRef->cur_node; x = tmpSpatialRef-> x; y = tmpSpatialRef-> y; z = tmpSpatialRef-> z; free(tmpSpatialRef); colREPLACE=TRUE; /*****************************************************************/ /*****************************************************************/ /*****************************************************************/ /*****************************************************************/ /* Getting the relevant nodes and filling colHolder */ /* search for pointers if any are null return NULL */ currentCol = cur_node->force.colour; tsc_nodes[1][1][1] = cur_node; get_TSCnodes(cur_grid, cur_pquad, icur_cquad, icur_nquad, tsc_nodes, &z, &y, &x); /* When going back to replace the colour the for loop progress the node by one - which we don't want to happen */ if ( col.numReplace == 2 ) { if (currentCol==col.holder[2]) cur_node->force.colour=col.holder[1]; } else if ( col.numReplace == 4 ) { if (currentCol==col.holder[2]) cur_node->force.colour=col.holder[0]; if (currentCol==col.holder[1]) cur_node->force.colour=col.holder[0]; } else { fprintf(stderr,"Something is wrong with colREPLACE"); } /*****************************************************************/ /*****************************************************************/ } } /* ERROR */ if ( col.numReplace > 4 ) fprintf(stderr,"ERROR with counting col.numReplace"); } } } } } } } } /*******************************************************************************************************/ /* Checking that the grid is coloured correctly */ /*******************************************************************************************************/ #ifdef AHFDEBUG2 /* Printing the PVIEW file */ sprintf(filename2,"%shalo_pview3",global_io.params->outfile_prefix); fprintf(stderr,"%s\n",filename2); if((fout = fopen(filename2,"w")) == NULL) { fprintf(stderr,"could not open %s\n", filename2); exit(1); } #endif refinecounter=0; for( iterate=0, cur_grid=global.dom_grid+ahf.min_ref; iterate<ahf.no_grids; iterate++, cur_grid++) { /* shift of cell centre as compared to edge of box [grid units] */ cur_shift = 0.5/(double)cur_grid->l1dim; /************************************************************ * Dealing with the extents of the periodic refinements ************************************************************/ numWrongNodes = 0; boundCount = 0; for(cur_pquad=cur_grid->pquad; cur_pquad != NULL; cur_pquad=cur_pquad->next) { z = cur_pquad->z; for(cur_cquad = cur_pquad->loc; cur_cquad < cur_pquad->loc + cur_pquad->length; cur_cquad++, z++) { for(icur_cquad = cur_cquad; icur_cquad != NULL; icur_cquad = icur_cquad->next) { y = icur_cquad->y; for(cur_nquad = icur_cquad->loc; cur_nquad < icur_cquad->loc + icur_cquad->length; cur_nquad++, y++) { for(icur_nquad = cur_nquad; icur_nquad != NULL; icur_nquad = icur_nquad->next) { x = icur_nquad->x; for(cur_node = icur_nquad->loc; cur_node < icur_nquad->loc + icur_nquad->length; cur_node++, x++) { /* current node */ colour = cur_node->force.colour; tsc_nodes[1][1][1] = cur_node; get_TSCnodes(cur_grid, cur_pquad, icur_cquad, icur_nquad, tsc_nodes, &z, &y, &x); colRight = 0; colRight = checkColourInfo(tsc_nodes); numWrongNodes += colRight; #ifdef AHFDEBUG2 if ( colRight == 1 ) fprintf(fout,"p %g %g %g 0.0 1.0 0.5\n", ((float) x/(float)cur_grid->l1dim + cur_shift)*simu.boxsize, ((float) y/(float)cur_grid->l1dim + cur_shift)*simu.boxsize, ((float) z/(float)cur_grid->l1dim + cur_shift)*simu.boxsize); #endif /********************************************************************************/ /* PERIODIC BOUNDARY CONDITIONS */ /* We are on the 'bottom' boundary */ if ( (x == 0) || (y == 0) || (z == 0) ) { /* Gathering periodic information */ tmpPeriodic = testBound(tsc_nodes,x,y,z); /* This isolated refinement is also periodic */ if ( (tmpPeriodic.x == 1) || (tmpPeriodic.y == 1) || (tmpPeriodic.z == 1) ) { /* finding the corresponding isolated spatial refinement in the spatalREF linklist */ current=spatialRefHead; while ( current != NULL ) { previous = current; if ( current->name == colour ) break; current = current->next; } /* Setting the periodic elements */ if ( tmpPeriodic.x == 1 ) previous->periodic.x = 1; if ( tmpPeriodic.y == 1 ) previous->periodic.y = 1; if ( tmpPeriodic.z == 1 ) previous->periodic.z = 1; } boundCount++; } }/* current node */ } } } } } #ifdef AHFDEBUG fprintf(stderr,"Num Boundary (%d) = %d\n",refinecounter,boundCount); #endif refinecounter++; } #ifdef AHFDEBUG2 fclose(fout); #endif /**************************************************************** **************************************************************** * Creating the spatial Refinement Index * * And boundary condition information!!! * */ spatialRefIndex = NULL; if ((spatialRefIndex = calloc(colCounter+1,sizeof(SRINDEX)))==NULL) { fprintf(stderr,"Error in allocating the memory for spatialRefIndex array\n"); exit(0); } #ifdef VERBOSE fprintf(stderr,"colCounter(%d)\n",colCounter); #endif /* Creating a tempary counting array */ numIsoRef=NULL; if ((numIsoRef = calloc(ahf.no_grids,sizeof(int)))==NULL) { fprintf(stderr,"Error in allocating the memory for numIsoRef array\n"); exit(0); } for ( i=0; i<ahf.no_grids; i++) numIsoRef[i]=0; /* Setting the spatialRefIndex to -1 */ for ( i=0; i<colCounter; i++) { spatialRefIndex[i].refLevel = -1; spatialRefIndex[i].isoRefIndex = -1; } /* Loop through the spatial refinements */ /* Record their refinement level and isolated refinement index */ totnumIsoRef=0; current=spatialRefHead; while ( current!=NULL ) { spatialRefIndex[current->name].refLevel = current->refLevel; spatialRefIndex[current->name].isoRefIndex = numIsoRef[current->refLevel]; spatialRefIndex[current->name].periodic.x = current->periodic.x; spatialRefIndex[current->name].periodic.y = current->periodic.y; spatialRefIndex[current->name].periodic.z = current->periodic.z; numIsoRef[current->refLevel]++; totnumIsoRef++; current = current->next; } /**************************************************************** **************************************************************** * Looping throught the Grid structure and dumping the gridInfo file * positions - density - spatial tag */ #ifdef AHFgridinfofile fprintf(stderr,"### PRINTING GRIDINFO FILES\n"); /* Note:: do this for each refinement level starting with the first refinment */ for( iterate=0, cur_grid=global.dom_grid+ahf.min_ref; iterate<ahf.no_grids; iterate++, cur_grid++) { /* prepare filename */ sprintf(filename1,"%sz%.3f.grid.%06d",global_io.params->outfile_prefix,global.z,cur_grid->l1dim); fprintf(stderr,"%s\n",filename1); if((gridinfofile = fopen(filename1,"w")) == NULL) { fprintf(stderr,"could not open %s\n", filename1); exit(1); } /* shift of cell centre as compared to edge of box [grid units] */ cur_shift = 0.5/(double)cur_grid->l1dim; for(cur_pquad=cur_grid->pquad; cur_pquad != NULL; cur_pquad=cur_pquad->next) { z = cur_pquad->z; for(cur_cquad = cur_pquad->loc; cur_cquad < cur_pquad->loc + cur_pquad->length; cur_cquad++, z++) { for(icur_cquad = cur_cquad; icur_cquad != NULL; icur_cquad = icur_cquad->next) { y = icur_cquad->y; for(cur_nquad = icur_cquad->loc; cur_nquad < icur_cquad->loc + icur_cquad->length; cur_nquad++, y++) { for(icur_nquad = cur_nquad; icur_nquad != NULL; icur_nquad = icur_nquad->next) { x = icur_nquad->x; for(cur_node = icur_nquad->loc; cur_node < icur_nquad->loc + icur_nquad->length; cur_node++, x++) { fprintf(gridinfofile,"p %lf %lf %lf 0.0 0.0 1.0 %lf %d\n", simu.boxsize*1000*((double) x/(double)cur_grid->l1dim + cur_shift), simu.boxsize*1000*((double) y/(double)cur_grid->l1dim + cur_shift), simu.boxsize*1000*((double) z/(double)cur_grid->l1dim + cur_shift), cur_node->dens, cur_node->force.colour); } } } } } } /* Flusing and closing this grids file */ fflush(gridinfofile); fclose(gridinfofile); } #endif /* fprintf(stderr,"I'm finished here, my work is done.... time to exit :)\n"); * exit(1); */ fprintf(io.logfile,"################## ahf_gridinfo finished ##################\n\n"); fflush(io.logfile); /* free() all spatialRefHead memory */ current = spatialRefHead; while(current != NULL) { previous = current->next; free(current); current = previous; } }
/** * @brief Main entry point for R * * @param bamfilenameR Filename of read container * @param aRgvals Vector containing the user arguments * @param filterList passed from R in the form list("chr1"=c(100,200,3000,3010...start,end...)) * @return R list in the form list("chr1"=c(1,2,1,2,1,1,...),"chr1_gind"=c(1100,1200...),"chr1_lind"=c(0,112,...),"chrX"=NA,...) and a Statistics vector * @details All chromosome of the filter or all chromosomes in the file header will be scanned and passed to an R list * @note * @todo high_cov not yet implemented. */ SEXP construct_dc(SEXP bamfilenameR, SEXP aRgvals, SEXP filterList) { double *statsp;//resulting statistics in the order "total reads" "coverage" "local coverage" "max score" uint32_t upcounter=0,i=0; time_t tstart,tstop; global_densities_t gd={0}; user_arguments_t user_args; filter_t ft; SEXP histogram,stats; int *argvalsp; signal(SIGINT,SIG_DFL);//make this thing stop on CTRL+C time(&tstart); /* Set user defined values */ PROTECT(aRgvals=AS_INTEGER(aRgvals));upcounter++; if(LENGTH(aRgvals)!=10)error("Invalid amount of arguments - arguments[%d] / should be %d!\n",LENGTH(aRgvals),9); argvalsp=INTEGER_POINTER(aRgvals); user_args.bamfilename = STRING_VALUE(bamfilenameR); user_args.READTHROUGH = argvalsp[0];//bool. read from start to end and 0 take whole read whithout CIGAR splice info user_args.PAIRED = argvalsp[1]; user_args.STRANDED = argvalsp[2];//Set to 1 / -1 it will use only forward / reverse reads respectively. 0 means all reads are processed user_args.TMAPQ = argvalsp[3];//Minimum MPAQ score. Lower scored reads will be skipped user_args.COLLAPSE = argvalsp[4]; user_args.EXTEND = argvalsp[5];//extend each read in its direction by this amount of BPs user_args.HWINDOW = argvalsp[6]; user_args.COMPRESSION = argvalsp[7];//minimum BPs needed between data blocks to collapse the gap and index it user_args.VERBOSE = argvalsp[8]; user_args.UNIQUE = argvalsp[9]; /* Try to open the file */ samfile_t *bam_file; bam_file=open_samtools(user_args.bamfilename); if(!bam_file){ warning("sam/bam file not found!\n"); UNPROTECT(upcounter); return(R_NilValue); } if(user_args.HWINDOW>user_args.COMPRESSION){ warning("HWINDOW has to be smaller than COMPRESSION! HWINDOW updated to %d\n",user_args.COMPRESSION); user_args.HWINDOW=user_args.COMPRESSION; } PROTECT(histogram = NEW_INTEGER(UINT16_MAX));upcounter++;//initialize compressed scores gd.histogramp = (uint32_t*) INTEGER_POINTER(histogram); for(i = 0; i < UINT16_MAX; i++) gd.histogramp[i] = 0; gd.total_elements=bam_file->header->n_targets;//one vector per chromosome needed /* #### CHECK IF THERE IS AN ACTIVE FILTER IN PLACE */ user_args.FILTER=isNewList(filterList) ? 1 : 0; if(user_args.FILTER){ upcounter+=set_filter(filterList,&ft); gd.total_elements=ft.seqn;//overwrite total elements if filter is passed, since one density is returned per slice } // Creating a list with vector elements as many as sequences plus a character string vector: PROTECT(gd.list = allocVector(VECSXP, (gd.total_elements*3)+2));upcounter++;//3x for the two indexes and scores per chromosome PROTECT(gd.list_names = allocVector(STRSXP,(gd.total_elements*3)+2));upcounter++;//+1 for statistics vector +1 for the histogram /* PASS EVERYTHING */ write_density(&gd,&user_args,bam_file,&ft); if(!gd.total_reads)goto NO_READS_FOUND; // 1 total_reads 2 gcoverage 3 lcoverage 4 maxscore 5 lmaxscore 6 lowqual 7 filtered 8 collapsed 9 paired 10 proper_pairs 11 pos 12 neg 13 fmapmass 14 lsize 15 gsize SET_STRING_ELT(gd.list_names,gd.total_elements*3,mkChar("Statistics")); PROTECT(stats = NEW_NUMERIC(15));upcounter++; statsp = NUMERIC_POINTER(stats); *statsp++=(double)gd.total_reads; *statsp++=(double)gd.mapmass/(double)gd.gsize; *statsp++=(double)gd.lmapmass/(double)gd.lsize; *statsp++=(double)gd.maxscore; *statsp++=(double)gd.lmaxScore; *statsp++=(double)gd.lowqual; *statsp++=(double)gd.filtered_reads; *statsp++=(double)gd.collapsed; *statsp++=(double)gd.paired; *statsp++=(double)gd.ppairs/2; *statsp++=(double)gd.pos_strand; *statsp++=(double)gd.neg_strand; *statsp=(double)gd.mapmass; *statsp++=(double)gd.lsize; *statsp++=(double)gd.gsize; if(gd.lmaxScore>=umaxof(usersize)-1){ warning("\nThe maximum pile up is exceeding the maximal value of UINT16_MAX=%d. Reads have been capped to that value.\nConsider to rerun using the maxDups option!\n",UINT16_MAX); } SET_VECTOR_ELT(gd.list,gd.total_elements*3, stats); SET_STRING_ELT(gd.list_names,(gd.total_elements*3)+1,mkChar("Histogram")); SET_VECTOR_ELT(gd.list,(gd.total_elements*3)+1,histogram); setAttrib(gd.list, R_NamesSymbol, gd.list_names); NO_READS_FOUND: time(&tstop); if(user_args.VERBOSE>0)printf("About %.0f seconds passed. %llu reads processed \n", difftime(tstop, tstart),gd.total_reads); close_bamfile(bam_file); if(user_args.FILTER)destroy_filter(&ft); UNPROTECT(upcounter+gd.upcounter); if(!gd.total_reads)return(R_NilValue); else return(gd.list); }