Skip to content

Instantly share code, notes, and snippets.

@ljyloi
Last active October 9, 2023 10:05
Show Gist options
  • Save ljyloi/ae418e1c724a35faebeb5bbc96958819 to your computer and use it in GitHub Desktop.
Save ljyloi/ae418e1c724a35faebeb5bbc96958819 to your computer and use it in GitHub Desktop.
dna script
import sys
import csv
def checkIfMatch(row1, rows_2):
chr1 = 'chr' + row1[0]
bp1 = int(row1[1])
for row2 in rows_2:
chr2 = row2[0]
bp2_a = int(row2[1])
bp2_b = int(row2[2])
# chr not match
if chr1 != chr2:
continue
# bp not match
if not (bp1 >= bp2_a and bp1 <= bp2_b):
continue
return '1'
return '0'
if len(sys.argv) <= 1:
print("Usage:\n python process.py file_A file_B1 file_B2 ... file_B2")
sys.exit(1)
file1 = sys.argv[1]
file2_list = sys.argv[2:]
try:
with open(file1, 'r') as f:
csv_reader = csv.reader(f, delimiter='\t', skipinitialspace=True)
rows = list(csv_reader)
except FileNotFoundError:
print(f"文件 '{file1}' 不存在")
sys.exit(1)
for file2 in file2_list:
header = rows[0]
header.append(file2)
try:
with open(file2, 'r') as f:
csv_reader = csv.reader(f, delimiter='\t',skipinitialspace=True)
rows_2 = list(csv_reader)
except FileNotFoundError:
print(f"文件 '{file2}' 不存在")
sys.exit(1)
for i in range(1, len(rows)):
# print(rows[i][:2])
rows[i].append(checkIfMatch(rows[i][:2], rows_2))
with open(file1, 'w', newline='') as f:
csv_writer = csv.writer(f, delimiter="\t")
csv_writer.writerow(rows[0])
for row in rows[1:]:
csv_writer.writerow(row)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment