update,
This commit is contained in:
Binary file not shown.
@@ -0,0 +1,18 @@
|
||||
#!/usr/bin/env bash
|
||||
|
||||
# grep -rin TODO src
|
||||
|
||||
rm -rf delivery.zip
|
||||
rm -rf delivery
|
||||
rm -rf _temp
|
||||
|
||||
mkdir -p _temp
|
||||
cp -r src _temp
|
||||
cp -r tests _temp
|
||||
cp -r test.sh _temp
|
||||
|
||||
# set -ex
|
||||
|
||||
7za a -tzip delivery.zip _temp
|
||||
|
||||
# rm -rf _temp
|
15
task1/SCC_212_Advanced_Programming_Weeks_8_10/package.json
Normal file
15
task1/SCC_212_Advanced_Programming_Weeks_8_10/package.json
Normal file
@@ -0,0 +1,15 @@
|
||||
{
|
||||
"name": "hk1234566",
|
||||
"version": "1.0.0",
|
||||
"description": "",
|
||||
"main": "index.js",
|
||||
"directories": {
|
||||
"test": "tests"
|
||||
},
|
||||
"scripts": {
|
||||
"test": "node ./tests/test.js"
|
||||
},
|
||||
"keywords": [],
|
||||
"author": "",
|
||||
"license": "ISC"
|
||||
}
|
44
task1/SCC_212_Advanced_Programming_Weeks_8_10/quotation.md
Normal file
44
task1/SCC_212_Advanced_Programming_Weeks_8_10/quotation.md
Normal file
@@ -0,0 +1,44 @@
|
||||
# RUN
|
||||
```
|
||||
# https://filetransfer.io/data-package/98RLRG0M#link
|
||||
chmod +x ./test.sh
|
||||
./test.sh
|
||||
```
|
||||
|
||||
|
||||
# REQUIREMENTS:
|
||||
|
||||
<!-- REMEMBER - LOOP STRUCTURES ARE BANNED! -->
|
||||
|
||||
### Task 1: Frequency Counts and Sequence Matching (Week 8)
|
||||
|
||||
- Notes:
|
||||
- The returned object MUST have the mentioned structure. Otherwise, auto-testing script will fail to award marks. The returned object MUST contain two properties frequencies and offsets, DO NOT change the name of these properties.
|
||||
- The offset of a pattern is the number of characters from the start of the data string to the start of the matching sequence.
|
||||
- The patterns and data can be of arbitrary lengths.
|
||||
- A pattern could exist in overlapping characters, e.g. ‘AAA’ contains two patterns of ‘AA’ at offsets 0 and 1.
|
||||
- In the supplied .zip file, you should find a JavaScript file named ’cw.js’. This file is a template to base your implementation on. You should not modify the declared variables or the functions signatures 2/5in this file. To run the file, execute ‘node cw.js’ from the command line or terminal in the program’s folder.
|
||||
|
||||
### Task 2: Asynchronous execution using callbacks (Week9)
|
||||
In this task, you’ve been asked to read two files; ‘file.data’, which contains lines of sequences (data) and ‘file.pattern’, which contains patterns to look for. Write a function analyse1(datafile, patternfile) that takes the names of the two files and analyses them for pattern matching and offset finding using the find() function you developed in task 1.
|
||||
|
||||
The function analyse1() works as the following. It asynchronously reads the contents of the ‘file.pattern’ file. Once the reading is successful, it asynchronously reads the content of the ‘file.data’ file and calls the find() function for each data line in the data file.
|
||||
|
||||
The results of analysing the files should be stored in the results1 object. This object should contain pairs of data lines from the ‘file.data’ file as keys and the object returned by the find function as a value. For example, if the data file contains AAA and AAC (each in a separate line) and the pattern file contains AA and AC (each in a separate line), then the object result1 should be
|
||||
```
|
||||
results1 = {
|
||||
AAA: { frequencies: { AA: 2, AC: 0 }, offsets: { AA: [Array], AC: [] } },
|
||||
AAC: { frequencies: { AA: 1, AC: 1 }, offsets: { AA: [Array], AC: [Array] } }
|
||||
}
|
||||
```
|
||||
In the supplied .zip file, you should find the ‘file.pattern’ and ‘file.data’ files. These are just sample files to use. You can create your own data and pattern files (but with the same structure) to test your code.
|
||||
|
||||
### Task 3: Asynchronous execution using promises (Week9)
|
||||
- This task is similar to task 2. The difference is that you’ve been asked to use promises to process files. For this purpose, you’ve been asked to do the following:
|
||||
- Write a function, readFilePromise(), that takes a file name and returns a promise object. The promise should read the file content and returns its contents when fulfilled. Write a function analyse2(datafile, patternfile), that reads and analyses the files for pattern matching (similar to analyse1() ). The function should use the readFilePromise to read the files’ contents and find() to find matches.
|
||||
- The results of analysing the files should be stored in the results2 object, which has the same structure as
|
||||
the object results1 described in Task2 (see above).
|
||||
|
||||
### Task 4: Asynchronous execution using async/await (Week9/10)
|
||||
In this task, you’ve been asked to write analyse3(datafile, patternfile) function that is similar to analyse2(). The difference is to replace the usage of promises with the async/await. You still need to use the readFilePromise() function to return a promise when reading files.
|
||||
The results of analysing the files should be stored in the results3 object, which has the same structure as the object results1 described in Task2 (see above).
|
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
@@ -0,0 +1 @@
|
||||
1
|
15
task1/SCC_212_Advanced_Programming_Weeks_8_10/ref/test.js
Normal file
15
task1/SCC_212_Advanced_Programming_Weeks_8_10/ref/test.js
Normal file
@@ -0,0 +1,15 @@
|
||||
const fs = require("fs");
|
||||
|
||||
function readFileAsync(path) {
|
||||
fs.readFile(path, "utf8", function (err, data) {
|
||||
return data;
|
||||
});
|
||||
}
|
||||
|
||||
function processData(data) {
|
||||
console.log(data);
|
||||
}
|
||||
|
||||
let data = readFileAsync("./file.txt");
|
||||
processData(data);
|
||||
console.log("processing the rest of the program....");
|
10
task1/SCC_212_Advanced_Programming_Weeks_8_10/result.json
Normal file
10
task1/SCC_212_Advanced_Programming_Weeks_8_10/result.json
Normal file
@@ -0,0 +1,10 @@
|
||||
{
|
||||
"ACCA": {
|
||||
"frequencies": { "C": 2, "CG": 0 },
|
||||
"offsets": { "C": [1, 2], "CG": [] }
|
||||
},
|
||||
"AACGTCGACG": {
|
||||
"frequencies": { "C": 3, "CG": 3 },
|
||||
"offsets": { "C": [2, 5, 8], "CG": [2, 5, 8] }
|
||||
}
|
||||
}
|
152
task1/SCC_212_Advanced_Programming_Weeks_8_10/src/cw.js
Normal file
152
task1/SCC_212_Advanced_Programming_Weeks_8_10/src/cw.js
Normal file
@@ -0,0 +1,152 @@
|
||||
"use strict"; // do not modify or delete this line
|
||||
|
||||
const fs = require('fs')
|
||||
|
||||
//Task 1
|
||||
//data is a string, patterns is an array of patterns
|
||||
function find(data, patterns) {
|
||||
let frequencies = {};
|
||||
let offsets = {};
|
||||
|
||||
//your implementation goes here
|
||||
|
||||
// initialize variables
|
||||
patterns.map((pattern) => (frequencies[pattern] = 0));
|
||||
patterns.map((pattern) => (offsets[pattern] = []));
|
||||
|
||||
// main matching loop
|
||||
patterns.map((pattern, i) => {
|
||||
// "AAA" => ["A","A","A"]
|
||||
// => run matching starting from each characters until data.length - pattern.length + 1
|
||||
// => i.e. data = "AAAA", pattern = "AA" => (4 - 2) + 1 => 3
|
||||
data.split("").map((_, idx) => {
|
||||
if (idx < data.length - pattern.length + 1) {
|
||||
let searchResult = data.indexOf(pattern, idx);
|
||||
if (searchResult > -1 && searchResult == idx) {
|
||||
offsets[pattern].push(searchResult);
|
||||
}
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
Object.keys(offsets).map((offset) => {
|
||||
frequencies[offset] = offsets[offset].length;
|
||||
});
|
||||
|
||||
return {
|
||||
frequencies,
|
||||
offsets,
|
||||
};
|
||||
}
|
||||
|
||||
//use these for results of analyse1, analyse2 and analyse3
|
||||
let results1 = {};
|
||||
let results2 = {};
|
||||
let results3 = {};
|
||||
|
||||
// Task 2, Asynchronous execution using callbacks
|
||||
// Write a function analyse1(datafile, patternfile)
|
||||
// that takes the names of the two files and analyses them
|
||||
// for pattern matching and offset finding using the
|
||||
// find() function you developed in task 1.
|
||||
|
||||
function analyse1(dataFile, patternFile) {
|
||||
//your implementation goes here
|
||||
|
||||
fs.readFile(dataFile, { encoding: "utf-8" }, (err, dataFileContent) => {
|
||||
if (err) throw err.message;
|
||||
|
||||
// NOTE: input cleaning
|
||||
let dataList = dataFileContent.split("\n").map((data) => data.trim());
|
||||
|
||||
fs.readFile(patternFile, { encoding: "utf-8" }, (err, patternFileContent) => {
|
||||
if (err) throw err.message;
|
||||
|
||||
// NOTE: input cleaning
|
||||
let patternList = patternFileContent.split("\n").map((pattern) => pattern.trim());
|
||||
|
||||
// main matching
|
||||
dataList.map((data) => (results1[data] = find(data, patternList)));
|
||||
|
||||
// NOTE: print result1 show that working
|
||||
console.log(results1);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
//Task 3, Asynchronous execution using promises
|
||||
// Write a function, readFilePromise(), that takes a file name and returns a promise object.
|
||||
// The promise should read the file content and returns its contents when fulfilled.
|
||||
|
||||
const readFilePromise = (filePath) => {
|
||||
//your implementation goes here
|
||||
|
||||
return new Promise(function (res, rej) {
|
||||
fs.readFile(filePath, "utf-8", function (err, data) {
|
||||
if (err) {
|
||||
// reject response
|
||||
rej(err);
|
||||
} else {
|
||||
// healthy response
|
||||
res(data);
|
||||
}
|
||||
});
|
||||
});
|
||||
};
|
||||
|
||||
// Write a function analyse2(datafile, patternfile), that reads and analyses the files for
|
||||
// pattern matching (similar to analyse1() ). The function should use the readFilePromise to read
|
||||
// the files’ contents and find() to find matches.
|
||||
function analyse2(dataFile, patternFile) {
|
||||
//your implementation goes here
|
||||
|
||||
return Promise.all([readFilePromise(dataFile), readFilePromise(patternFile)]).then(
|
||||
([dataFileContent, patternFileContent]) => {
|
||||
// NOTE: input cleaning
|
||||
var dataList = dataFileContent.split("\n").map((data) => data.trim());
|
||||
var patternList = patternFileContent.split("\n").map((pattern) => pattern.trim());
|
||||
|
||||
// main matching
|
||||
dataList.map((data, i) => (results2[data] = find(data, patternList)));
|
||||
|
||||
// NOTE: print result and show that working
|
||||
console.log(results2);
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
//Task 4
|
||||
// Write a function analyse3(datafile, patternfile),
|
||||
// using async/await to re-implement the analyse2()
|
||||
|
||||
//your implementation for analyse3 goes here
|
||||
async function analyse3(dataFile, patternFile) {
|
||||
//your implementation goes here
|
||||
var dataList = await readFilePromise(dataFile);
|
||||
var patternList = await readFilePromise(patternFile);
|
||||
|
||||
// NOTE: input data cleaning
|
||||
dataList = dataList.split("\n").map((data) => data.trim());
|
||||
patternList = patternList.split("\n").map((pattern) => pattern.trim());
|
||||
|
||||
// main matching
|
||||
dataList.map((data) => (results3[data] = find(data, patternList)));
|
||||
|
||||
console.log(results3);
|
||||
}
|
||||
|
||||
//-------------------------------------------------------------------------------
|
||||
//do not modify this function
|
||||
function print() {
|
||||
console.log("Printing results...");
|
||||
Object.keys(results).forEach(function (elem) {
|
||||
console.log("sequence: ", elem);
|
||||
console.log("frequencies are: ", results[elem].frequencies);
|
||||
console.log("offsets are: ", results[elem].offsets);
|
||||
console.log("---------------------------");
|
||||
});
|
||||
}
|
||||
//--------------- export the find function (required for the marking script)
|
||||
// module.exports = { find }; //do not modify this line
|
||||
|
||||
module.exports = { find, analyse1, analyse2, analyse3 }; //do not modify this line
|
12
task1/SCC_212_Advanced_Programming_Weeks_8_10/src/file.data
Normal file
12
task1/SCC_212_Advanced_Programming_Weeks_8_10/src/file.data
Normal file
@@ -0,0 +1,12 @@
|
||||
AAA
|
||||
AAC
|
||||
ACCA
|
||||
AACGTCGACG
|
||||
TTGGTTGGTTGG
|
||||
CCCTTGCGGTT
|
||||
CCAAGGGGGTT
|
||||
CCCGGGCGGTT
|
||||
TTGGTTGGTTGG
|
||||
CCCTGGCGGTT
|
||||
CCACTGGGGGGTT
|
||||
CCCGTGGCGGTT
|
@@ -0,0 +1,9 @@
|
||||
AA
|
||||
AC
|
||||
C
|
||||
CG
|
||||
TT
|
||||
GG
|
||||
TG
|
||||
GT
|
||||
CTG
|
48
task1/SCC_212_Advanced_Programming_Weeks_8_10/template/cw.js
Normal file
48
task1/SCC_212_Advanced_Programming_Weeks_8_10/template/cw.js
Normal file
@@ -0,0 +1,48 @@
|
||||
"use strict"; // do not modify or delete this line
|
||||
|
||||
//Task 1
|
||||
//data is a string, patterns is an array of patterns
|
||||
function find(data, patterns) {
|
||||
let frequencies = {};
|
||||
let offsets = {};
|
||||
|
||||
//your implementation goes here
|
||||
}
|
||||
|
||||
//use these for results of analyse1, analyse2 and analyse3
|
||||
const results1 = {};
|
||||
const results2 = {};
|
||||
const results3 = {};
|
||||
|
||||
//Task 2
|
||||
function analyse1(dataFile, patternFile) {
|
||||
//your implementation goes here
|
||||
}
|
||||
|
||||
//Task 3
|
||||
|
||||
const readFilePromise = (filePath) => {
|
||||
//your implementation goes here
|
||||
};
|
||||
|
||||
function analyse2(dataFile, patternFile) {
|
||||
//your implementation goes here
|
||||
}
|
||||
|
||||
//Task 4
|
||||
|
||||
//your implementation for analyse3 goes here
|
||||
|
||||
//-------------------------------------------------------------------------------
|
||||
//do not modify this function
|
||||
function print() {
|
||||
console.log("Printing results...");
|
||||
Object.keys(results).forEach(function (elem) {
|
||||
console.log("sequence: ", elem);
|
||||
console.log("frequencies are: ", results[elem].frequencies);
|
||||
console.log("offsets are: ", results[elem].offsets);
|
||||
console.log("---------------------------");
|
||||
});
|
||||
}
|
||||
//--------------- export the find function (required for the marking script)
|
||||
module.exports = { find }; //do not modify this line
|
@@ -0,0 +1,12 @@
|
||||
AAA
|
||||
AAC
|
||||
ACCA
|
||||
AACGTCGACG
|
||||
TTGGTTGGTTGG
|
||||
CCCTTGCGGTT
|
||||
CCAAGGGGGTT
|
||||
CCCGGGCGGTT
|
||||
TTGGTTGGTTGG
|
||||
CCCTGGCGGTT
|
||||
CCACTGGGGGGTT
|
||||
CCCGTGGCGGTT
|
@@ -0,0 +1,9 @@
|
||||
AA
|
||||
AC
|
||||
C
|
||||
CG
|
||||
TT
|
||||
GG
|
||||
TG
|
||||
GT
|
||||
CTG
|
11
task1/SCC_212_Advanced_Programming_Weeks_8_10/test.sh
Normal file
11
task1/SCC_212_Advanced_Programming_Weeks_8_10/test.sh
Normal file
@@ -0,0 +1,11 @@
|
||||
#!/usr/bin/env bash
|
||||
|
||||
set -ex
|
||||
|
||||
|
||||
clear
|
||||
|
||||
node ./tests/test_task1.js
|
||||
node ./tests/test_task2.js
|
||||
node ./tests/test_task3.js
|
||||
node ./tests/test_task4.js
|
BIN
task1/SCC_212_Advanced_Programming_Weeks_8_10/tests/test1/Screenshot from 2022-12-10 16-29-01.png
(Stored with Git LFS)
Normal file
BIN
task1/SCC_212_Advanced_Programming_Weeks_8_10/tests/test1/Screenshot from 2022-12-10 16-29-01.png
(Stored with Git LFS)
Normal file
Binary file not shown.
@@ -0,0 +1,2 @@
|
||||
ACCA
|
||||
AACGTCGACG
|
@@ -0,0 +1,2 @@
|
||||
C
|
||||
CG
|
BIN
task1/SCC_212_Advanced_Programming_Weeks_8_10/tests/test2/Screenshot from 2022-12-10 16-28-08.png
(Stored with Git LFS)
Normal file
BIN
task1/SCC_212_Advanced_Programming_Weeks_8_10/tests/test2/Screenshot from 2022-12-10 16-28-08.png
(Stored with Git LFS)
Normal file
Binary file not shown.
@@ -0,0 +1,4 @@
|
||||
TTGGTTGGTTGG
|
||||
CCCTTGCGGTT
|
||||
CCAAGGGGGTT
|
||||
CCCGGGCGGTT
|
@@ -0,0 +1,2 @@
|
||||
TT
|
||||
GG
|
BIN
task1/SCC_212_Advanced_Programming_Weeks_8_10/tests/test3/Screenshot from 2022-12-10 16-27-02.png
(Stored with Git LFS)
Normal file
BIN
task1/SCC_212_Advanced_Programming_Weeks_8_10/tests/test3/Screenshot from 2022-12-10 16-27-02.png
(Stored with Git LFS)
Normal file
Binary file not shown.
@@ -0,0 +1,4 @@
|
||||
TTGGTTGGTTGG
|
||||
CCCTGGCGGTT
|
||||
CCACTGGGGGGTT
|
||||
CCCGTGGCGGTT
|
@@ -0,0 +1,3 @@
|
||||
TG
|
||||
GT
|
||||
CTG
|
@@ -0,0 +1,14 @@
|
||||
const cw = require("../src/cw");
|
||||
|
||||
// // task 1
|
||||
// // frequencies: { AA: 2, AC: 0 }, offsets: { AA: [ 0, 1 ], AC: [] }
|
||||
console.log(cw.find("AAA", ["AA", "AC"]));
|
||||
|
||||
// // { frequencies: { AA: 1, AC: 1 }, offsets: { AA: [ 0 ], AC: [ 1 ] } }
|
||||
console.log(cw.find("AAC", ["AA", "AC"]));
|
||||
|
||||
// // {
|
||||
// // frequencies: { AA: 2, AT: 1, CCA: 1 },
|
||||
// // offsets: { AA: [ 2, 3 ], AT: [ 4 ], CCA: [ 0 ] }
|
||||
// // }
|
||||
console.log(cw.find("CCAAATT", ["AA", "AT", "CCA"]));
|
@@ -0,0 +1,7 @@
|
||||
const cw = require("../src/cw");
|
||||
|
||||
// // task 2
|
||||
console.log("task2");
|
||||
(async () => {
|
||||
cw.analyse1("./src/file.data", "./src/file.pattern");
|
||||
})();
|
@@ -0,0 +1,41 @@
|
||||
const cw = require("../src/cw");
|
||||
|
||||
// task 3
|
||||
(async () => {
|
||||
console.log("task3");
|
||||
var test_result = await cw.analyse2("./src/file.data", "./src/file.pattern");
|
||||
console.log(test_result);
|
||||
})();
|
||||
|
||||
// task 3
|
||||
(async () => {
|
||||
var test_result = await cw.analyse2("./src/file.data", "./src/file.pattern");
|
||||
console.log(test_result);
|
||||
})();
|
||||
|
||||
// TEST CASES1
|
||||
// task 3
|
||||
(async () => {
|
||||
var test_result = await cw.analyse2("./tests/test1/file.data", "./tests/test1/file.pattern");
|
||||
console.log(test_result);
|
||||
// console.log(test_result["ACCA"].offsets["C"]);
|
||||
// console.log(test_result["AACGTCGACG"].offsets["C"]);
|
||||
// console.log(test_result["AACGTCGACG"].offsets["CG"]);
|
||||
})();
|
||||
|
||||
// TEST CASES2
|
||||
// task 3
|
||||
(async () => {
|
||||
var test_result = await cw.analyse2("./tests/test2/file.data", "./tests/test2/file.pattern");
|
||||
console.log(test_result);
|
||||
// console.log(test_result["TTGGTTGGTTGG"].offsets["TT"]);
|
||||
})();
|
||||
|
||||
// TEST CASES3
|
||||
// task 3
|
||||
(async () => {
|
||||
var test_result = await cw.analyse2("./tests/test3/file.data", "./tests/test3/file.pattern");
|
||||
console.log(test_result);
|
||||
// console.log(test_result["CCACTGGGGGGTT"].offsets["GT"]);
|
||||
// console.log(test_result["CCCGTGGCGGTT"].offsets["GT"]);
|
||||
})();
|
@@ -0,0 +1,35 @@
|
||||
const cw = require("../src/cw");
|
||||
|
||||
// task 4
|
||||
console.log("task4");
|
||||
(async () => {
|
||||
var test_result = await cw.analyse3("./src/file.data", "./src/file.pattern");
|
||||
console.log(test_result);
|
||||
})();
|
||||
|
||||
// TEST CASES1
|
||||
// task 4
|
||||
(async () => {
|
||||
var test_result = await cw.analyse3("./tests/test1/file.data", "./tests/test1/file.pattern");
|
||||
console.log(test_result);
|
||||
// console.log(test_result["ACCA"].offsets["C"]);
|
||||
// console.log(test_result["AACGTCGACG"].offsets["C"]);
|
||||
// console.log(test_result["AACGTCGACG"].offsets["CG"]);
|
||||
})();
|
||||
|
||||
// TEST CASES2
|
||||
// task 4
|
||||
(async () => {
|
||||
var test_result = await cw.analyse3("./tests/test2/file.data", "./tests/test2/file.pattern");
|
||||
console.log(test_result);
|
||||
// console.log(test_result["TTGGTTGGTTGG"].offsets["TT"]);
|
||||
})();
|
||||
|
||||
// TEST CASES3
|
||||
// task 4
|
||||
(async () => {
|
||||
var test_result = await cw.analyse3("./tests/test3/file.data", "./tests/test3/file.pattern");
|
||||
console.log(test_result);
|
||||
// console.log(test_result["CCACTGGGGGGTT"].offsets["GT"]);
|
||||
// console.log(test_result["CCCGTGGCGGTT"].offsets["GT"]);
|
||||
})();
|
Reference in New Issue
Block a user