Skip to content

Instantly share code, notes, and snippets.

@tgxn
Created July 6, 2024 16:04
Show Gist options
  • Save tgxn/b0c345dea5f3e5d3f2ca486b1d1c3d5c to your computer and use it in GitHub Desktop.
Save tgxn/b0c345dea5f3e5d3f2ca486b1d1c3d5c to your computer and use it in GitHub Desktop.
// this js file reads in data.txt
// and then for each liune it will check the contents
var file = "data.txt"
var fs = require('fs');
function binaryStringToAscii(binaryString) {
// Split the binary string into individual bits
// const bits = binaryString.split('');
const bits = str.split("").map((char, index) => char + (str[index + 1] || "")).filter((item, index, self) => index % 2 === 0);
// Convert each bit to its corresponding character code
const characterCodes = bits.map(bit => {
if (bit === '0') {
return 48;
} else if (bit === '1') {
return 49;
} else {
throw new Error(`Invalid bit: ${bit}`);
}
});
// Combine all the character codes into a single array
const asciiString = String.fromCharCode(...characterCodes);
return asciiString;
}
const dict = { 'AT': '00', 'CG': '01', 'GC': '10', 'TA': '11' };
function encodeCharsToBinary(str) {
let code = '';
for (let i = 0; i < str.length; i += 2) {
const pair = str.substring(i, i + 2);
if (dict[pair]) {
code += dict[pair];
} else {
throw new Error(`Unrecognized character pair: ${pair}`);
}
}
// return Buffer.from(code, 'binary').toString('utf-8');
return binaryStringToAscii(code);
// convert binary to ascii
return Buffer.from(code, 'binary').toString('ascii');
}
fs.readFile(file, 'utf8', function(err, data) {
if (err) {c
console.log("Error reading file: " + err);
return;
}
var cleanruns =[]
var fullLetterArray =[]
var bytes = []; // sets of 8 bits
var guesses = [];
// this should take sets of like AGGACG etc and basiclly
// sort them into sets of 8, convert to binary using the thing, and then reverse the binary, and use that as an ascii codepoint
function parseOutput(fullArrBinary) {
var fullArr = fullArrBinary.join('');
var fullArrBinary = fullArr.match(/.{1,8}/g);
console.log("FullArrBinary: " + fullArrBinary);
var conjkopined = "";
for (var i = 0; i < fullArrBinary.length; i++) {
var binary = fullArrBinary[i];
var reversed = binary.split('').reverse().join('');
var ascii = parseInt(reversed, 2);
var char = String.fromCharCode(ascii);
// console.log("Char: " + char);
conjkopined += char;
}
return conjkopined;
}
var lines = data.split('\n');
for (var i = 0; i < lines.length; i++) {
// skip if starts with #
if(lines[i].startsWith("#")) {
continue;
}
// strip all spaces, dashes etc
var clean = lines[i].replace(/\s/g, '').replace(/-/g, '');
if(clean.length != 2) {
// console.log("Line " + i + " is not 2 characters long");
var binaryy = cleanruns.join('');
var decode1 = fullLetterArray.join('');
console.log(i+ "decode1: " + decode1);
console.log(i+ "binaryy: " + binaryy);
console.log(i+ "binaryy: " + parseOutput([binaryy]));
cleanruns= []
fullLetterArray= []
// continue;
break
}
var line = lines[i];
// console.log("Line " + i + ": " + clean);
if(clean == "AT") {
// console.log("Found AT");
cleanruns.push("00");
fullLetterArray.push("AT");
}
else if(clean == "TA") {
// console.log("Found AT");
cleanruns.push("11");
fullLetterArray.push("TA");
}
else if(clean == "GC") {
// console.log("Found AT");
cleanruns.push("10");
fullLetterArray.push("GC");
}
else if(clean == "CG") {
// console.log("Found AT");
cleanruns.push("01");
fullLetterArray.push("CG");
}
}
}
);
AT
A--T
T- -A
G----C
G---- C
G --C
T---A
G--C
AT
GC
T-- A
G- - C
T----A
A--- T
T ---A
G---C
C--G
AT
CG
A-T
A---T
G--- C
A -- T
G----C
G C
G--C
GC
G C
T--A
A-- T
A-- T
G- --C
A----T
G---C
G--C
GC
CG
G--C
A--- T
G----C
T- --A
C----G
T--- A
G--C
TA
GC
T--A
A- T
A----T
C----G
A-- -T
G---C
T--A
AT
TA
A -T
# UNDERSCORE
T---A
T - -A
G----C
G----C
# M
G---C
T--A
AT
GC
# i
G- C
C --G
C-- -G
G----C
# t
A-- -T
G - C
T--A
G C
# O
AT
A--T
T--- A
A----T
# c
T-- -A
A---- T
C---G
G--C
# H
AT
C G
A - T
GC
# O
T----A
T----A
A---- T
G -C
#n
C- G
TA
CG
G- C
# d
A-- T
G---- C
C- --G
G- -C
# R
C-- G
A--T
G C
GC
# i
G--C
C---G
C-- -G
G--- C
#4
A---T
G- -C
T--A
AT
# ________
TA
T--A
GC--
G ---- C
# 1
G-- -C
A- --T
T- - A
A--T
# 5
GC
GC
T- A
A---T
# ________
T-- -A
T----A
G---- C
G-- C
# 7
T--A
GC
TA
A- T
# h
A --T
C----G
C---- G
G----C
# e
G---C
G -C
CG
GC
# ________
T--A
T--- A
G----C
G-- -C
# P
A--- T
A-- T
G-- C
GC
# 0
AT
A--T
T--- A
A -- T
# w
T----A
G --- C
T --A
G--C
# E
G C
GC
A- T
G---C
# r
C ---G
A --- T
T- - A
G---C
# ________
T --A
TA
G C
G--C
# H
A --T
C- --G
A ---T
G ----C
# o
T - A
T - A
CG
GC
# U
G -C
G---C
G - C
G --- C
# s
T--- A
A---T
T- A
G C
# E
GC
G- C
A --T
G----C
# ________
T ---A
T--- A
G --C
G- C
# ?
TA
TA
C--G
G--- C
# f
C---- G
G----C
C----G
G---C
# ________
T- A
TA
GC
G--C
# D
A---T
G-- -C
A----T
G---- C
# A
G--- C
A--T
AT
GC
# ________
T - A
T---A
G- --C
G ---C
# C
T- --A
A-- T
A--T
GC
# 3
TA
A- T
T- -A
A----T
# L
A----T
T----A
A --T
G--C
# L
A T
TA
A -T
G --- C
# }
G- --C
T----A
T ---A
G - -C
# "
C-- G
AT
CG
A- T
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment