The following python approach will work, with a few caveats:
- The use of regex to replace the number is purely because I don't know how fixed your output format is.
- You may want to run this code on one file at a time instead of all of them, so that you can output each file to a new file (or add a
write in to the code)
- I'm assuming all your values are
0.00123. If you need to account for integers etc too, then the regex needs to change.
import sys
import os
import re
# Read the readcounts file and store a dictionary of Filename: multiplier
readcounts = {}
with open(sys.argv[1], 'r') as rc:
next(rc) # skip the header
for line in rc:
(key, val) = line.split()
readcounts[key] = int(val)
# Read the file(s)
for file in sys.argv[2:]:
multiplier = readcounts[file.split(os.extsep)[0]] # look up the relevant multiplier from the dict
with open(file, 'r') as fh:
for line in fh:
print(re.sub(r'\d+\.\d*',
lambda m: str(float(m.group(0)) * multiplier),
line).strip("\n"))
Usage:
$ python scriptname.py readcounts.csv /path/to/files_to_multiply/*.ext
Inputs:
cat ERR260136.genefamilies.csv
#Gene Family ERR260136_Abundance-RPKs
UNMAPPED 0.445035
UniRef90_A0A015P9C8 0.00080211
UniRef90_A0A015P9C8|g__Bacteroides.s__Bacteroides_fragilis 0.00080211
UniRef90_A5ZYU5 0.000787149
cat ERR260140.genefamilies.csv (some sample data I made up)
#Gene Family ERR260136_Abundance-RPKs
UNMAPPED 0.1239710902
UniRef90_A0A015P9C8 0.000812739
UniRef90_A0A015P9C8|g__Bacteroides.s__Bacteroides_fragilis 0.0031
UniRef90_AYCHAYB 0.0007871123123131
Output:
python multiplying.py readcounts.csv *.genefamilies.csv
#Gene Family ERR260136_Abundance-RPKs
UNMAPPED 11409246.585900001
UniRef90_A0A015P9C8 20563.485521399998
UniRef90_A0A015P9C8|g__Bacteroides.s__Bacteroides_fragilis 20563.485521399998
UniRef90_A5ZYU5 20179.93425426
#Gene Family ERR260136_Abundance-RPKs
UNMAPPED 2376039.3365760553
UniRef90_A0A015P9C8 15577.017442164
UniRef90_A0A015P9C8|g__Bacteroides.s__Bacteroides_fragilis 59414.8356
UniRef90_AYCHAYB 15085.85439832861
Hello, your thread looks super close to what can be asked in an assignment. Could you show us what you have tried so far so we can use it as a start ? Thanks !
You can read your first file with awk, then for every line (sampleID/Read_counts) you pipe it into another awk command opening the according file (sampleID) multiplying your column with the constant (Read_counts)
It is not an assignment. I need this operation while analyzing genomic data. Being new in programing I can only find out read counts for a sample from the first file (
read_count.csv) by this:Can you please show me some way? I am stuck.
Is the indentation of your example of 1 of the 20 files correct, or is it supposed to be a fairly simple tabular format? You're extension implies csv, but it doesn't appear to be.
I am also going to strongly advise you not to do this with
bashas you requested, as it does not support floating point arithmetic without a lot of complicated fudging. See here for an example of why A: assigning the values in matrix in bashYes. It is supposed to be a .csv file only. Actually, I modified so that it can look a table here in the post. It should be like:
That is still not a csv, you need to be more clear about what these files exactly are.