I was able to make a program that parse my samtools output file into a table that can be viewed easily in excel. While I was able to get the final result, is there a recommended improvements that be done for my code. I am trying to improve my python skills and transferring over from C and C++.
The strategy I used was to split the data I need based on the "+" sign and make it into an array. Then select the elements of the array that were the information that I needed and write it to my file.
My example input:
15051874 + 0 in total (QC-passed reads + QC-failed reads)
1998052 + 0 secondary
0 + 0 supplementary
0 + 0 duplicates
13457366 + 0 mapped (89.41% : N/A)
13053822 + 0 paired in sequencing
6526911 + 0 read1
6526911 + 0 read2
10670914 + 0 properly paired (81.75% : N/A)
10947288 + 0 with itself and mate mapped
512026 + 0 singletons (3.92% : N/A)
41524 + 0 with mate mapped to a different chr
31302 + 0 with mate mapped to a different chr (mapQ>=5)
My output:
FileName Total Secondary Supplementary duplicates mapped paired in sequencing read1 read2 properly paired with itself and mate mapped singletons with mate mapped to a different chr with mate mapped to a different chr (mapQ>=5)
10_HK_S22.merged.samtools.flag.txt 26541257 2332283 0 0 22895440 24208974 12104487 12104487 19003826 19632880 930277 69030 52261
My Program:
outFile = open("output.count.txt", "w+")
#windows platform add the r
os.chdir(r"Susceptible\featurecounts")
#open the output file to be able to write output.
outFile.write("FileName\tTotal\tSecondary\tSupplementary\tduplicates\tmapped\tpaired in sequencing\tread1\t"
"read2\tproperly paired\twith itself and mate mapped\tsingletons\twith mate mapped to a different chr\twith mate mapped to a different chr (mapQ>=5)\n")
#Iterate through files in directory with the following ending
for file in glob.glob(".flag.txt"):
#open file after retrieving the name.
with open(file, 'r') as counts_file:
#empty list/array for storing the outputs
list = []
#add the file name to array.
list.append(file)
#get values from output file.
for line in counts_file:
list.append(line.split('+')[0])
#write list to file
for item in list:
outFile.write("%s\t" % item)
#write a newline
outFile.write("\n")
#close the output file
outFile.close()