Unconfigured Ad

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • zisis86
    Member
    • Mar 2013
    • 10

    #1

    how can i parse lines of a huge .sam file into a data frame, table, list faster in R?

    I have an R script in which i can read the lines from a .Sam file after mapping and i want to parse lines of sam file into strings in order to be easier to manipulate them and create the wig files that i want or to calculate the cov3 and cov5 that i need.
    Can you help me please to make this script work faster?how can i parse lines of a huge .sam file into a data frame faster?Or into a list? Here is my script:

    gc()
    rm(list=ls())

    exptPath <- "/home/dimitris/INDEX3PerfectUnique31cov5.sam"


    lines <- readLines(exptPath)
    pos = lines
    pos
    chrom = lines
    chrom
    pos = ""
    chrom = ""
    nn = length(lines)
    nn

    # parse lines of sam file into strings(this part is very very slow)
    rr = strsplit(lines,"\t", fixed = TRUE)
    rr
    trr = do.call(rbind.data.frame, rr)
    pos = as.numeric(as.character(trr[8:nn,4]))
    # for cov3
    #pos = pos+25
    #pos
    chrom = trr[8:nn,3]
    pos = as.numeric(pos)
    pos

    tab1 = table(chrom,pos, exclude="")
    tab1

    ftab1 = as.data.frame(tab1)
    ftab1 = subset(ftab1, ftab1[3] != 0)
    ftab1 = subset(ftab1, ftab1[1] != "<NA>")
    oftab1 = ftab1[ order(ftab1[,1]), ]
    final.ftab1 = oftab1[,2:3]
    write.table(final.ftab1, "ind3_cov5_wig.txt", row.names=FALSE, sep=" ", quote=FALSE)
  • TiborNagy
    Senior Member
    • Mar 2010
    • 329

    #2
    R stores all variables in memory, so if you would like to parse a large file, try to write a loop around readLines() and process only one line. Example.

    Comment

    • dpryan
      Devon Ryan
      • Jul 2011
      • 3478

      #3
      Are you absolutely set on using R for this? R is great for many things, but reading a whole SAM/BAM file into memory (which most of the R mechanisms for dealing with SAM/BAM files entail) isn't exactly the most efficient processing mechanism. You might find that pysam/python meets your flexibility needs while still delivering increased performance.

      Comment

      • gringer
        David Eccles (gringer)
        • May 2011
        • 845

        #4
        You're better off using a method that is essentially a map-reduce process. Use some other quick tool (i.e. doesn't take much longer than just reading out the reads using cat) to pre-process the BAM files into a format that is small and easy for another program to use. I notice that you're using strsplit and just using a subset of the columns, where subsetting the columns first using awk would be much better:

        Code:
        samtools view input.bam | awk -F '\t' '{print $3,$4}' | sort | uniq -c > ref_pos_table.txt
        [using samtools view will also skip the header lines, which will have a variable length, rather than the "always 7 lines" that your R code suggests]

        Then proceed from the ftab1 lines in your R script, loading ref_pos_table.txt:

        Code:
        ftab1 <- read.table("ref_pos_table.txt",col.names = c("Count","RNAME","POS"));
        ftab1 <- subset(ftab1, RNAME != "*");
        ... etc
        Last edited by gringer; 05-28-2014, 04:45 PM.

        Comment

        Latest Articles

        Collapse

        • SEQadmin2
          16S/ITS Profiling vs. Shotgun Metagenomics: What Changes in Your Workflow?
          by SEQadmin2




          NGS has revolutionized clinical microbiology by offering new ways to identify and surveil pathogens without prior knowledge of the target. One popular approach is 16S rRNA and ITS amplicon sequencing, which targets specific microbial markers. But studies are increasingly incorporating shotgun metagenomics, a technique that sequences all DNA from a sample without targeting specific microbial markers. This approach allows more detailed taxonomic classification, detection...
          Today, 06:46 AM
        • SEQadmin2
          Multiomic Liquid Biopsy: What DNA/RNA Panels Reveal That DNA Alone Can’t
          by SEQadmin2



          Tumors contain complex mixtures of cell populations that evolve in response to treatment and host-specific factors. Tissue biopsies provide only static snapshots of a tumor’s state. But what if we could follow that evolution over time using a minimally invasive technique?

          Liquid biopsies can detect tumor-derived material circulating in the bloodstream, providing a way to study treatment response, detect emerging resistance, and discover new biomarkers. Circulating
          ...
          10-14-2026, 09:55 AM

        ad_right_rmr

        Collapse

        News

        Collapse

        Topics Statistics Last Post
        Started by SEQadmin2, 10-28-2026, 09:57 AM
        0 responses
        22 views
        0 reactions
        Last Post SEQadmin2  
        Started by SEQadmin2, 10-28-2026, 09:51 AM
        0 responses
        14 views
        0 reactions
        Last Post SEQadmin2  
        Started by SEQadmin2, 10-22-2026, 11:13 AM
        0 responses
        67 views
        0 reactions
        Last Post SEQadmin2  
        Started by SEQadmin2, 10-22-2026, 10:48 AM
        0 responses
        47 views
        0 reactions
        Last Post SEQadmin2  
        Working...